PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Zend/Feed/Reader/Feed/Rss.php

https://github.com/wangzifeng/magento-mirror
PHP | 532 lines | 319 code | 96 blank | 117 comment | 76 complexity | b93904731af1caa92051f6ceddd19afc MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Reader
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Rss.php 19044 2009-11-19 16:44:24Z padraic $
  20. */
  21. /**
  22. * @see Zend_Feed_Reader_FeedAbstract
  23. */
  24. #require_once 'Zend/Feed/Reader/FeedAbstract.php';
  25. /**
  26. * @see Zend_feed_Reader_Extension_Atom_Feed
  27. */
  28. #require_once 'Zend/Feed/Reader/Extension/Atom/Feed.php';
  29. /**
  30. * @see Zend_Feed_Reader_Extension_DublinCore_Feed
  31. */
  32. #require_once 'Zend/Feed/Reader/Extension/DublinCore/Feed.php';
  33. /**
  34. * @see Zend_Date
  35. */
  36. #require_once 'Zend/Date.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Feed_Reader
  40. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Feed_Reader_Feed_Rss extends Zend_Feed_Reader_FeedAbstract
  44. {
  45. /**
  46. * Constructor
  47. *
  48. * @param DOMDocument $dom
  49. * @param string $type
  50. */
  51. public function __construct(DomDocument $dom, $type = null)
  52. {
  53. parent::__construct($dom, $type);
  54. $dublinCoreClass = Zend_Feed_Reader::getPluginLoader()->getClassName('DublinCore_Feed');
  55. $this->_extensions['DublinCore_Feed'] = new $dublinCoreClass($dom, $this->_data['type'], $this->_xpath);
  56. $atomClass = Zend_Feed_Reader::getPluginLoader()->getClassName('Atom_Feed');
  57. $this->_extensions['Atom_Feed'] = new $atomClass($dom, $this->_data['type'], $this->_xpath);
  58. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  59. $xpathPrefix = '/rss/channel';
  60. } else {
  61. $xpathPrefix = '/rdf:RDF/rss:channel';
  62. }
  63. foreach ($this->_extensions as $extension) {
  64. $extension->setXpathPrefix($xpathPrefix);
  65. }
  66. }
  67. /**
  68. * Get a single author
  69. *
  70. * @param int $index
  71. * @return string|null
  72. */
  73. public function getAuthor($index = 0)
  74. {
  75. $authors = $this->getAuthors();
  76. if (isset($authors[$index])) {
  77. return $authors[$index];
  78. }
  79. return null;
  80. }
  81. /**
  82. * Get an array with feed authors
  83. *
  84. * @return array
  85. */
  86. public function getAuthors()
  87. {
  88. if (array_key_exists('authors', $this->_data)) {
  89. return $this->_data['authors'];
  90. }
  91. $authors = array();
  92. if (empty($authors)) {
  93. $authors = $this->getExtension('DublinCore')->getAuthors();
  94. }
  95. if (empty($authors)) {
  96. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  97. $list = $this->_xpath->query('//author');
  98. } else {
  99. $list = $this->_xpath->query('//rss:author');
  100. }
  101. foreach ($list as $authorObj) {
  102. $authors[] = $authorObj->nodeValue;
  103. }
  104. }
  105. if (empty($authors)) {
  106. $authors = $this->getExtension('Atom')->getAuthors();
  107. }
  108. if (empty($authors)) {
  109. $authors = null;
  110. } else {
  111. $authors = array_unique($authors);
  112. }
  113. $this->_data['authors'] = $authors;
  114. return $this->_data['authors'];
  115. }
  116. /**
  117. * Get the copyright entry
  118. *
  119. * @return string|null
  120. */
  121. public function getCopyright()
  122. {
  123. if (array_key_exists('copyright', $this->_data)) {
  124. return $this->_data['copyright'];
  125. }
  126. $copyright = null;
  127. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  128. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  129. $copyright = $this->_xpath->evaluate('string(/rss/channel/copyright)');
  130. }
  131. if (!$copyright && !is_null($this->getExtension('DublinCore'))) {
  132. $copyright = $this->getExtension('DublinCore')->getCopyright();
  133. }
  134. if (empty($copyright)) {
  135. $copyright = $this->getExtension('Atom')->getCopyright();
  136. }
  137. if (!$copyright) {
  138. $copyright = null;
  139. }
  140. $this->_data['copyright'] = $copyright;
  141. return $this->_data['copyright'];
  142. }
  143. /**
  144. * Get the feed creation date
  145. *
  146. * @return string|null
  147. */
  148. public function getDateCreated()
  149. {
  150. return $this->getDateModified();
  151. }
  152. /**
  153. * Get the feed modification date
  154. *
  155. * @return Zend_Date
  156. */
  157. public function getDateModified()
  158. {
  159. if (array_key_exists('datemodified', $this->_data)) {
  160. return $this->_data['datemodified'];
  161. }
  162. $dateModified = null;
  163. $date = null;
  164. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  165. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  166. $dateModified = $this->_xpath->evaluate('string(/rss/channel/pubDate)');
  167. if (!$dateModified) {
  168. $dateModified = $this->_xpath->evaluate('string(/rss/channel/lastBuildDate)');
  169. }
  170. if ($dateModified) {
  171. $dateStandards = array(Zend_Date::RSS, Zend_Date::RFC_822,
  172. Zend_Date::RFC_2822, Zend_Date::DATES);
  173. $date = new Zend_Date;
  174. foreach ($dateStandards as $standard) {
  175. try {
  176. $date->set($dateModified, $standard);
  177. break;
  178. } catch (Zend_Date_Exception $e) {
  179. if ($standard == Zend_Date::DATES) {
  180. #require_once 'Zend/Feed/Exception.php';
  181. throw new Zend_Feed_Exception(
  182. 'Could not load date due to unrecognised'
  183. .' format (should follow RFC 822 or 2822):'
  184. . $e->getMessage()
  185. );
  186. }
  187. }
  188. }
  189. }
  190. }
  191. if (!$date) {
  192. $date = $this->getExtension('DublinCore')->getDate();
  193. }
  194. if (!$date) {
  195. $date = $this->getExtension('Atom')->getDateModified();
  196. }
  197. if (!$date) {
  198. $date = null;
  199. }
  200. $this->_data['datemodified'] = $date;
  201. return $this->_data['datemodified'];
  202. }
  203. /**
  204. * Get the feed description
  205. *
  206. * @return string|null
  207. */
  208. public function getDescription()
  209. {
  210. if (array_key_exists('description', $this->_data)) {
  211. return $this->_data['description'];
  212. }
  213. $description = null;
  214. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  215. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  216. $description = $this->_xpath->evaluate('string(/rss/channel/description)');
  217. } else {
  218. $description = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/rss:description)');
  219. }
  220. if (!$description && !is_null($this->getExtension('DublinCore'))) {
  221. $description = $this->getExtension('DublinCore')->getDescription();
  222. }
  223. if (empty($description)) {
  224. $description = $this->getExtension('Atom')->getDescription();
  225. }
  226. if (!$description) {
  227. $description = null;
  228. }
  229. $this->_data['description'] = $description;
  230. return $this->_data['description'];
  231. }
  232. /**
  233. * Get the feed ID
  234. *
  235. * @return string|null
  236. */
  237. public function getId()
  238. {
  239. if (array_key_exists('id', $this->_data)) {
  240. return $this->_data['id'];
  241. }
  242. $id = null;
  243. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  244. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  245. $id = $this->_xpath->evaluate('string(/rss/channel/guid)');
  246. }
  247. if (!$id && !is_null($this->getExtension('DublinCore'))) {
  248. $id = $this->getExtension('DublinCore')->getId();
  249. }
  250. if (empty($id)) {
  251. $id = $this->getExtension('Atom')->getId();
  252. }
  253. if (!$id) {
  254. if ($this->getLink()) {
  255. $id = $this->getLink();
  256. } elseif ($this->getTitle()) {
  257. $id = $this->getTitle();
  258. } else {
  259. $id = null;
  260. }
  261. }
  262. $this->_data['id'] = $id;
  263. return $this->_data['id'];
  264. }
  265. /**
  266. * Get the feed language
  267. *
  268. * @return string|null
  269. */
  270. public function getLanguage()
  271. {
  272. if (array_key_exists('language', $this->_data)) {
  273. return $this->_data['language'];
  274. }
  275. $language = null;
  276. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  277. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  278. $language = $this->_xpath->evaluate('string(/rss/channel/language)');
  279. }
  280. if (!$language && !is_null($this->getExtension('DublinCore'))) {
  281. $language = $this->getExtension('DublinCore')->getLanguage();
  282. }
  283. if (empty($language)) {
  284. $language = $this->getExtension('Atom')->getLanguage();
  285. }
  286. if (!$language) {
  287. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  288. }
  289. if (!$language) {
  290. $language = null;
  291. }
  292. $this->_data['language'] = $language;
  293. return $this->_data['language'];
  294. }
  295. /**
  296. * Get a link to the feed
  297. *
  298. * @return string|null
  299. */
  300. public function getLink()
  301. {
  302. if (array_key_exists('link', $this->_data)) {
  303. return $this->_data['link'];
  304. }
  305. $link = null;
  306. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  307. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  308. $link = $this->_xpath->evaluate('string(/rss/channel/link)');
  309. } else {
  310. $link = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/rss:link)');
  311. }
  312. if (empty($link)) {
  313. $link = $this->getExtension('Atom')->getLink();
  314. }
  315. if (!$link) {
  316. $link = null;
  317. }
  318. $this->_data['link'] = $link;
  319. return $this->_data['link'];
  320. }
  321. /**
  322. * Get a link to the feed XML
  323. *
  324. * @return string|null
  325. */
  326. public function getFeedLink()
  327. {
  328. if (array_key_exists('feedlink', $this->_data)) {
  329. return $this->_data['feedlink'];
  330. }
  331. $link = null;
  332. $link = $this->getExtension('Atom')->getFeedLink();
  333. if (!$link) {
  334. $link = null;
  335. }
  336. $this->_data['feedlink'] = $link;
  337. return $this->_data['feedlink'];
  338. }
  339. /**
  340. * Get the feed generator entry
  341. *
  342. * @return string|null
  343. */
  344. public function getGenerator()
  345. {
  346. if (array_key_exists('generator', $this->_data)) {
  347. return $this->_data['generator'];
  348. }
  349. $generator = null;
  350. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  351. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  352. $generator = $this->_xpath->evaluate('string(/rss/channel/generator)');
  353. }
  354. if (!$generator) {
  355. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  356. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  357. $generator = $this->_xpath->evaluate('string(/rss/channel/atom:generator)');
  358. } else {
  359. $generator = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/atom:generator)');
  360. }
  361. if ($generator) {
  362. $generator = html_entity_decode($generator, ENT_QUOTES, $this->getEncoding());
  363. }
  364. }
  365. if (empty($generator)) {
  366. $generator = $this->getExtension('Atom')->getGenerator();
  367. }
  368. if (!$generator) {
  369. $generator = null;
  370. }
  371. $this->_data['generator'] = $generator;
  372. return $this->_data['generator'];
  373. }
  374. /**
  375. * Get the feed title
  376. *
  377. * @return string|null
  378. */
  379. public function getTitle()
  380. {
  381. if (array_key_exists('title', $this->_data)) {
  382. return $this->_data['title'];
  383. }
  384. $title = null;
  385. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  386. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  387. $title = $this->_xpath->evaluate('string(/rss/channel/title)');
  388. } else {
  389. $title = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/rss:title)');
  390. }
  391. if (!$title && !is_null($this->getExtension('DublinCore'))) {
  392. $title = $this->getExtension('DublinCore')->getTitle();
  393. }
  394. if (!$title) {
  395. $title = $this->getExtension('Atom')->getTitle();
  396. }
  397. if (!$title) {
  398. $title = null;
  399. }
  400. $this->_data['title'] = $title;
  401. return $this->_data['title'];
  402. }
  403. /**
  404. * Read all entries to the internal entries array
  405. *
  406. */
  407. protected function _indexEntries()
  408. {
  409. $entries = array();
  410. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  411. $entries = $this->_xpath->evaluate('//item');
  412. } else {
  413. $entries = $this->_xpath->evaluate('//rss:item');
  414. }
  415. foreach($entries as $index=>$entry) {
  416. $this->_entries[$index] = $entry;
  417. }
  418. }
  419. /**
  420. * Register the default namespaces for the current feed format
  421. *
  422. */
  423. protected function _registerNamespaces()
  424. {
  425. switch ($this->_data['type']) {
  426. case Zend_Feed_Reader::TYPE_RSS_10:
  427. $this->_xpath->registerNamespace('rdf', Zend_Feed_Reader::NAMESPACE_RDF);
  428. $this->_xpath->registerNamespace('rss', Zend_Feed_Reader::NAMESPACE_RSS_10);
  429. break;
  430. case Zend_Feed_Reader::TYPE_RSS_090:
  431. $this->_xpath->registerNamespace('rdf', Zend_Feed_Reader::NAMESPACE_RDF);
  432. $this->_xpath->registerNamespace('rss', Zend_Feed_Reader::NAMESPACE_RSS_090);
  433. break;
  434. }
  435. }
  436. }