PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Feed/Reader/Feed/Rss.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 533 lines | 324 code | 96 blank | 113 comment | 74 complexity | e3e35b64bef3bcf460659aa15807a32b 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 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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 Zend_Feed_Abstract $feed
  49. * @param string $type
  50. * @param string $xpath
  51. */
  52. public function __construct(DomDocument $dom, $type = null)
  53. {
  54. parent::__construct($dom, $type);
  55. $dublinCoreClass = Zend_Feed_Reader::getPluginLoader()->getClassName('DublinCore_Feed');
  56. $this->_extensions['DublinCore_Feed'] = new $dublinCoreClass($dom, $this->_data['type'], $this->_xpath);
  57. $atomClass = Zend_Feed_Reader::getPluginLoader()->getClassName('Atom_Feed');
  58. $this->_extensions['Atom_Feed'] = new $atomClass($dom, $this->_data['type'], $this->_xpath);
  59. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  60. $xpathPrefix = '/rss/channel';
  61. } else {
  62. $xpathPrefix = '/rdf:RDF/rss:channel';
  63. }
  64. foreach ($this->_extensions as $extension) {
  65. $extension->setXpathPrefix($xpathPrefix);
  66. }
  67. }
  68. /**
  69. * Get a single author
  70. *
  71. * @param int $index
  72. * @return string|null
  73. */
  74. public function getAuthor($index = 0)
  75. {
  76. $authors = $this->getAuthors();
  77. if (isset($authors[$index])) {
  78. return $authors[$index];
  79. }
  80. return null;
  81. }
  82. /**
  83. * Get an array with feed authors
  84. *
  85. * @return array
  86. */
  87. public function getAuthors()
  88. {
  89. if (array_key_exists('authors', $this->_data)) {
  90. return $this->_data['authors'];
  91. }
  92. $authors = array();
  93. if (empty($authors)) {
  94. $authors = $this->getExtension('DublinCore')->getAuthors();
  95. }
  96. if (empty($authors)) {
  97. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  98. $list = $this->_xpath->query('//author');
  99. } else {
  100. $list = $this->_xpath->query('//rss:author');
  101. }
  102. foreach ($list as $authorObj) {
  103. $authors[] = $authorObj->nodeValue;
  104. }
  105. }
  106. if (empty($authors)) {
  107. $authors = $this->getExtension('Atom')->getAuthors();
  108. }
  109. if (empty($authors)) {
  110. $authors = null;
  111. } else {
  112. $authors = array_unique($authors);
  113. }
  114. $this->_data['authors'] = $authors;
  115. return $this->_data['authors'];
  116. }
  117. /**
  118. * Get the copyright entry
  119. *
  120. * @return string|null
  121. */
  122. public function getCopyright()
  123. {
  124. if (array_key_exists('copyright', $this->_data)) {
  125. return $this->_data['copyright'];
  126. }
  127. $copyright = null;
  128. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  129. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  130. $copyright = $this->_xpath->evaluate('string(/rss/channel/copyright)');
  131. }
  132. if (!$copyright && !is_null($this->getExtension('DublinCore'))) {
  133. $copyright = $this->getExtension('DublinCore')->getCopyright();
  134. }
  135. if (empty($copyright)) {
  136. $copyright = $this->getExtension('Atom')->getCopyright();
  137. }
  138. if (!$copyright) {
  139. $copyright = null;
  140. }
  141. $this->_data['copyright'] = $copyright;
  142. return $this->_data['copyright'];
  143. }
  144. /**
  145. * Get the feed creation date
  146. *
  147. * @return string|null
  148. */
  149. public function getDateCreated()
  150. {
  151. return $this->getDateModified();
  152. }
  153. /**
  154. * Get the feed modification date
  155. *
  156. * @return Zend_Date
  157. */
  158. public function getDateModified()
  159. {
  160. if (array_key_exists('datemodified', $this->_data)) {
  161. return $this->_data['datemodified'];
  162. }
  163. $dateModified = null;
  164. $date = null;
  165. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  166. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  167. $dateModified = $this->_xpath->evaluate('string(/rss/channel/pubDate)');
  168. if (!$dateModified) {
  169. $dateModified = $this->_xpath->evaluate('string(/rss/channel/lastBuildDate)');
  170. }
  171. if ($dateModified) {
  172. $date = new Zend_Date();
  173. try {
  174. $date->set($dateModified, Zend_Date::RFC_822);
  175. } catch (Zend_Date_Exception $e) {
  176. try {
  177. $date->set($dateModified, Zend_Date::RFC_2822);
  178. } catch (Zend_Date_Exception $e) {
  179. try {
  180. $date->set($dateModified, Zend_Date::DATES);
  181. } catch (Zend_Date_Exception $e) {
  182. require_once 'Zend/Feed/Exception.php';
  183. throw new Zend_Feed_Exception(
  184. 'Could not load date due to unrecognised format (should follow RFC 822 or 2822): '
  185. . $e->getMessage()
  186. );
  187. }
  188. }
  189. }
  190. }
  191. }
  192. if (!$date) {
  193. $date = $this->getExtension('DublinCore')->getDate();
  194. }
  195. if (!$date) {
  196. $date = $this->getExtension('Atom')->getDateModified();
  197. }
  198. if (!$date) {
  199. $date = null;
  200. }
  201. $this->_data['datemodified'] = $date;
  202. return $this->_data['datemodified'];
  203. }
  204. /**
  205. * Get the feed description
  206. *
  207. * @return string|null
  208. */
  209. public function getDescription()
  210. {
  211. if (array_key_exists('description', $this->_data)) {
  212. return $this->_data['description'];
  213. }
  214. $description = null;
  215. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  216. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  217. $description = $this->_xpath->evaluate('string(/rss/channel/description)');
  218. } else {
  219. $description = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/rss:description)');
  220. }
  221. if (!$description && !is_null($this->getExtension('DublinCore'))) {
  222. $description = $this->getExtension('DublinCore')->getDescription();
  223. }
  224. if (empty($description)) {
  225. $description = $this->getExtension('Atom')->getDescription();
  226. }
  227. if (!$description) {
  228. $description = null;
  229. }
  230. $this->_data['description'] = $description;
  231. return $this->_data['description'];
  232. }
  233. /**
  234. * Get the feed ID
  235. *
  236. * @return string|null
  237. */
  238. public function getId()
  239. {
  240. if (array_key_exists('id', $this->_data)) {
  241. return $this->_data['id'];
  242. }
  243. $id = null;
  244. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  245. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  246. $id = $this->_xpath->evaluate('string(/rss/channel/guid)');
  247. }
  248. if (!$id && !is_null($this->getExtension('DublinCore'))) {
  249. $id = $this->getExtension('DublinCore')->getId();
  250. }
  251. if (empty($id)) {
  252. $id = $this->getExtension('Atom')->getId();
  253. }
  254. if (!$id) {
  255. if ($this->getLink()) {
  256. $id = $this->getLink();
  257. } elseif ($this->getTitle()) {
  258. $id = $this->getTitle();
  259. } else {
  260. $id = null;
  261. }
  262. }
  263. $this->_data['id'] = $id;
  264. return $this->_data['id'];
  265. }
  266. /**
  267. * Get the feed language
  268. *
  269. * @return string|null
  270. */
  271. public function getLanguage()
  272. {
  273. if (array_key_exists('language', $this->_data)) {
  274. return $this->_data['language'];
  275. }
  276. $language = null;
  277. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  278. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  279. $language = $this->_xpath->evaluate('string(/rss/channel/language)');
  280. }
  281. if (!$language && !is_null($this->getExtension('DublinCore'))) {
  282. $language = $this->getExtension('DublinCore')->getLanguage();
  283. }
  284. if (empty($language)) {
  285. $language = $this->getExtension('Atom')->getLanguage();
  286. }
  287. if (!$language) {
  288. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  289. }
  290. if (!$language) {
  291. $language = null;
  292. }
  293. $this->_data['language'] = $language;
  294. return $this->_data['language'];
  295. }
  296. /**
  297. * Get a link to the feed
  298. *
  299. * @return string|null
  300. */
  301. public function getLink()
  302. {
  303. if (array_key_exists('link', $this->_data)) {
  304. return $this->_data['link'];
  305. }
  306. $link = null;
  307. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  308. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  309. $link = $this->_xpath->evaluate('string(/rss/channel/link)');
  310. } else {
  311. $link = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/rss:link)');
  312. }
  313. if (empty($link)) {
  314. $link = $this->getExtension('Atom')->getLink();
  315. }
  316. if (!$link) {
  317. $link = null;
  318. }
  319. $this->_data['link'] = $link;
  320. return $this->_data['link'];
  321. }
  322. /**
  323. * Get a link to the feed XML
  324. *
  325. * @return string|null
  326. */
  327. public function getFeedLink()
  328. {
  329. if (array_key_exists('feedlink', $this->_data)) {
  330. return $this->_data['feedlink'];
  331. }
  332. $link = null;
  333. $link = $this->getExtension('Atom')->getFeedLink();
  334. if (!$link) {
  335. $link = null;
  336. }
  337. $this->_data['feedlink'] = $link;
  338. return $this->_data['feedlink'];
  339. }
  340. /**
  341. * Get the feed generator entry
  342. *
  343. * @return string|null
  344. */
  345. public function getGenerator()
  346. {
  347. if (array_key_exists('generator', $this->_data)) {
  348. return $this->_data['generator'];
  349. }
  350. $generator = null;
  351. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  352. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  353. $generator = $this->_xpath->evaluate('string(/rss/channel/generator)');
  354. }
  355. if (!$generator) {
  356. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  357. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  358. $generator = $this->_xpath->evaluate('string(/rss/channel/atom:generator)');
  359. } else {
  360. $generator = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/atom:generator)');
  361. }
  362. if ($generator) {
  363. $generator = html_entity_decode($generator, ENT_QUOTES, $this->getEncoding());
  364. }
  365. }
  366. if (empty($generator)) {
  367. $generator = $this->getExtension('Atom')->getGenerator();
  368. }
  369. if (!$generator) {
  370. $generator = null;
  371. }
  372. $this->_data['generator'] = $generator;
  373. return $this->_data['generator'];
  374. }
  375. /**
  376. * Get the feed title
  377. *
  378. * @return string|null
  379. */
  380. public function getTitle()
  381. {
  382. if (array_key_exists('title', $this->_data)) {
  383. return $this->_data['title'];
  384. }
  385. $title = null;
  386. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  387. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  388. $title = $this->_xpath->evaluate('string(/rss/channel/title)');
  389. } else {
  390. $title = $this->_xpath->evaluate('string(/rdf:RDF/rss:channel/rss:title)');
  391. }
  392. if (!$title && !is_null($this->getExtension('DublinCore'))) {
  393. $title = $this->getExtension('DublinCore')->getTitle();
  394. }
  395. if (!$title) {
  396. $title = $this->getExtension('Atom')->getTitle();
  397. }
  398. if (!$title) {
  399. $title = null;
  400. }
  401. $this->_data['title'] = $title;
  402. return $this->_data['title'];
  403. }
  404. /**
  405. * Read all entries to the internal entries array
  406. *
  407. */
  408. protected function _indexEntries()
  409. {
  410. $entries = array();
  411. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  412. $entries = $this->_xpath->evaluate('//item');
  413. } else {
  414. $entries = $this->_xpath->evaluate('//rss:item');
  415. }
  416. foreach($entries as $index=>$entry) {
  417. $this->_entries[$index] = $entry;
  418. }
  419. }
  420. /**
  421. * Register the default namespaces for the current feed format
  422. *
  423. */
  424. protected function _registerNamespaces()
  425. {
  426. switch ($this->_data['type']) {
  427. case Zend_Feed_Reader::TYPE_RSS_10:
  428. $this->_xpath->registerNamespace('rdf', Zend_Feed_Reader::NAMESPACE_RDF);
  429. $this->_xpath->registerNamespace('rss', Zend_Feed_Reader::NAMESPACE_RSS_10);
  430. break;
  431. case Zend_Feed_Reader::TYPE_RSS_090:
  432. $this->_xpath->registerNamespace('rdf', Zend_Feed_Reader::NAMESPACE_RDF);
  433. $this->_xpath->registerNamespace('rss', Zend_Feed_Reader::NAMESPACE_RSS_090);
  434. break;
  435. }
  436. }
  437. }