Rss.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Feed_Reader
  23. */
  24. require_once 'Zend/Feed/Reader.php';
  25. /**
  26. * @see Zend_Feed_Reader_EntryInterface
  27. */
  28. require_once 'Zend/Feed/Reader/EntryInterface.php';
  29. /**
  30. * @see Zend_Feed_Reader_EntryAbstract
  31. */
  32. require_once 'Zend/Feed/Reader/EntryAbstract.php';
  33. /**
  34. * @see Zend_Feed_Reader_Extension_DublinCore_Entry
  35. */
  36. require_once 'Zend/Feed/Reader/Extension/DublinCore/Entry.php';
  37. /**
  38. * @see Zend_Feed_Reader_Extension_Content_Entry
  39. */
  40. require_once 'Zend/Feed/Reader/Extension/Content/Entry.php';
  41. /**
  42. * @see Zend_Feed_Reader_Extension_Atom_Entry
  43. */
  44. require_once 'Zend/Feed/Reader/Extension/Atom/Entry.php';
  45. /**
  46. * @see Zend_Feed_Reader_Extension_WellformedWeb_Entry
  47. */
  48. require_once 'Zend/Feed/Reader/Extension/WellFormedWeb/Entry.php';
  49. /**
  50. * @see Zend_Feed_Reader_Extension_Slash_Entry
  51. */
  52. require_once 'Zend/Feed/Reader/Extension/Slash/Entry.php';
  53. /**
  54. * @see Zend_Feed_Reader_Extension_Thread_Entry
  55. */
  56. require_once 'Zend/Feed/Reader/Extension/Thread/Entry.php';
  57. /**
  58. * @see Zend_Date
  59. */
  60. require_once 'Zend/Date.php';
  61. /**
  62. * @see Zend_Feed_Reader_Collection_Category
  63. */
  64. require_once 'Zend/Feed/Reader/Collection/Category.php';
  65. /**
  66. * @category Zend
  67. * @package Zend_Feed_Reader
  68. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  69. * @license http://framework.zend.com/license/new-bsd New BSD License
  70. */
  71. class Zend_Feed_Reader_Entry_Rss extends Zend_Feed_Reader_EntryAbstract implements Zend_Feed_Reader_EntryInterface
  72. {
  73. /**
  74. * XPath query for RDF
  75. *
  76. * @var string
  77. */
  78. protected $_xpathQueryRdf = '';
  79. /**
  80. * XPath query for RSS
  81. *
  82. * @var string
  83. */
  84. protected $_xpathQueryRss = '';
  85. /**
  86. * Constructor
  87. *
  88. * @param Zend_Feed_Entry_Abstract $entry
  89. * @param string $entryKey
  90. * @param string $type
  91. * @return void
  92. */
  93. public function __construct(DOMElement $entry, $entryKey, $type = null)
  94. {
  95. parent::__construct($entry, $entryKey, $type);
  96. $this->_xpathQueryRss = '//item[' . ($this->_entryKey+1) . ']';
  97. $this->_xpathQueryRdf = '//rss:item[' . ($this->_entryKey+1) . ']';
  98. $pluginLoader = Zend_Feed_Reader::getPluginLoader();
  99. $dublinCoreClass = $pluginLoader->getClassName('DublinCore_Entry');
  100. $this->_extensions['DublinCore_Entry'] = new $dublinCoreClass($entry, $entryKey, $type);
  101. $contentClass = $pluginLoader->getClassName('Content_Entry');
  102. $this->_extensions['Content_Entry'] = new $contentClass($entry, $entryKey, $type);
  103. $atomClass = $pluginLoader->getClassName('Atom_Entry');
  104. $this->_extensions['Atom_Entry'] = new $atomClass($entry, $entryKey, $type);
  105. $wfwClass = $pluginLoader->getClassName('WellFormedWeb_Entry');
  106. $this->_extensions['WellFormedWeb_Entry'] = new $wfwClass($entry, $entryKey, $type);
  107. $slashClass = $pluginLoader->getClassName('Slash_Entry');
  108. $this->_extensions['Slash_Entry'] = new $slashClass($entry, $entryKey, $type);
  109. $threadClass = $pluginLoader->getClassName('Thread_Entry');
  110. $this->_extensions['Thread_Entry'] = new $threadClass($entry, $entryKey, $type);
  111. }
  112. /**
  113. * Get an author entry
  114. *
  115. * @param DOMElement $element
  116. * @return string
  117. */
  118. public function getAuthor($index = 0)
  119. {
  120. $authors = $this->getAuthors();
  121. if (isset($authors[$index])) {
  122. return $authors[$index];
  123. }
  124. return null;
  125. }
  126. /**
  127. * Get an array with feed authors
  128. *
  129. * @return array
  130. */
  131. public function getAuthors()
  132. {
  133. if (array_key_exists('authors', $this->_data)) {
  134. return $this->_data['authors'];
  135. }
  136. $authors = array();
  137. // @todo: create a list from all potential sources rather than from alternatives
  138. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  139. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  140. $list = $this->_xpath->evaluate($this->_xpathQueryRss.'//author');
  141. } else {
  142. $list = $this->_xpath->evaluate($this->_xpathQueryRdf.'//rss:author');
  143. }
  144. if (!$list->length) {
  145. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  146. $list = $this->_xpath->query('//author');
  147. } else {
  148. $list = $this->_xpath->query('//rss:author');
  149. }
  150. }
  151. if ($list->length) {
  152. foreach ($list as $author) {
  153. if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_20
  154. && preg_match("/\(([^\)]+)\)/", $author->nodeValue, $matches, PREG_OFFSET_CAPTURE)
  155. ) {
  156. // source name from RSS 2.0 <author>
  157. // format "joe@example.com (Joe Bloggs)"
  158. $authors[] = $matches[1][0];
  159. } else {
  160. $authors[] = $author->nodeValue;
  161. }
  162. }
  163. $authors = array_unique($authors);
  164. }
  165. if (empty($authors)) {
  166. $authors = $this->getExtension('DublinCore')->getAuthors();
  167. }
  168. if (empty($authors)) {
  169. $authors = $this->getExtension('Atom')->getAuthors();
  170. }
  171. $this->_data['authors'] = $authors;
  172. return $this->_data['authors'];
  173. }
  174. /**
  175. * Get the entry content
  176. *
  177. * @return string
  178. */
  179. public function getContent()
  180. {
  181. if (array_key_exists('content', $this->_data)) {
  182. return $this->_data['content'];
  183. }
  184. $content = $this->getExtension('Content')->getContent();
  185. if (!$content) {
  186. $content = $this->getDescription();
  187. }
  188. if (empty($content)) {
  189. $content = $this->getExtension('Atom')->getContent();
  190. }
  191. $this->_data['content'] = $content;
  192. return $this->_data['content'];
  193. }
  194. /**
  195. * Get the entry's date of creation
  196. *
  197. * @return string
  198. */
  199. public function getDateCreated()
  200. {
  201. return $this->getDateModified();
  202. }
  203. /**
  204. * Get the entry's date of modification
  205. *
  206. * @return string
  207. */
  208. public function getDateModified()
  209. {
  210. if (array_key_exists('datemodified', $this->_data)) {
  211. return $this->_data['datemodified'];
  212. }
  213. $dateModified = null;
  214. $date = null;
  215. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  216. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  217. ) {
  218. $dateModified = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/pubDate)');
  219. if ($dateModified) {
  220. $dateStandards = array(Zend_Date::RSS, Zend_Date::RFC_822,
  221. Zend_Date::RFC_2822, Zend_Date::DATES);
  222. $date = new Zend_Date;
  223. foreach ($dateStandards as $standard) {
  224. try {
  225. $date->set($dateModified, $standard);
  226. break;
  227. } catch (Zend_Date_Exception $e) {
  228. if ($standard == Zend_Date::DATES) {
  229. require_once 'Zend/Feed/Exception.php';
  230. throw new Zend_Feed_Exception(
  231. 'Could not load date due to unrecognised'
  232. .' format (should follow RFC 822 or 2822):'
  233. . $e->getMessage(),
  234. 0, $e
  235. );
  236. }
  237. }
  238. }
  239. }
  240. }
  241. if (!$date) {
  242. $date = $this->getExtension('DublinCore')->getDate();
  243. }
  244. if (!$date) {
  245. $date = $this->getExtension('Atom')->getDateModified();
  246. }
  247. if (!$date) {
  248. $date = null;
  249. }
  250. $this->_data['datemodified'] = $date;
  251. return $this->_data['datemodified'];
  252. }
  253. /**
  254. * Get the entry description
  255. *
  256. * @return string
  257. */
  258. public function getDescription()
  259. {
  260. if (array_key_exists('description', $this->_data)) {
  261. return $this->_data['description'];
  262. }
  263. $description = null;
  264. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  265. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  266. ) {
  267. $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/description)');
  268. } else {
  269. $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:description)');
  270. }
  271. if (!$description) {
  272. $description = $this->getExtension('DublinCore')->getDescription();
  273. }
  274. if (empty($description)) {
  275. $description = $this->getExtension('Atom')->getDescription();
  276. }
  277. if (!$description) {
  278. $description = null;
  279. } else {
  280. $description = html_entity_decode($description, ENT_QUOTES, $this->getEncoding());
  281. }
  282. $this->_data['description'] = $description;
  283. return $this->_data['description'];
  284. }
  285. /**
  286. * Get the entry enclosure
  287. * @return string
  288. */
  289. public function getEnclosure()
  290. {
  291. if (array_key_exists('enclosure', $this->_data)) {
  292. return $this->_data['enclosure'];
  293. }
  294. $enclosure = null;
  295. if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_20) {
  296. $nodeList = $this->_xpath->query($this->_xpathQueryRss . '/enclosure');
  297. if ($nodeList->length > 0) {
  298. $enclosure = new stdClass();
  299. $enclosure->url = $nodeList->item(0)->getAttribute('url');
  300. $enclosure->length = $nodeList->item(0)->getAttribute('length');
  301. $enclosure->type = $nodeList->item(0)->getAttribute('type');
  302. }
  303. }
  304. if (!$enclosure) {
  305. $enclosure = $this->getExtension('Atom')->getEnclosure();
  306. }
  307. $this->_data['enclosure'] = $enclosure;
  308. return $this->_data['enclosure'];
  309. }
  310. /**
  311. * Get the entry ID
  312. *
  313. * @return string
  314. */
  315. public function getId()
  316. {
  317. if (array_key_exists('id', $this->_data)) {
  318. return $this->_data['id'];
  319. }
  320. $id = null;
  321. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  322. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  323. ) {
  324. $id = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/guid)');
  325. }
  326. if (!$id) {
  327. $id = $this->getExtension('DublinCore')->getId();
  328. }
  329. if (empty($id)) {
  330. $id = $this->getExtension('Atom')->getId();
  331. }
  332. if (!$id) {
  333. if ($this->getPermalink()) {
  334. $id = $this->getPermalink();
  335. } elseif ($this->getTitle()) {
  336. $id = $this->getTitle();
  337. } else {
  338. $id = null;
  339. }
  340. }
  341. $this->_data['id'] = $id;
  342. return $this->_data['id'];
  343. }
  344. /**
  345. * Get a specific link
  346. *
  347. * @param int $index
  348. * @return string
  349. */
  350. public function getLink($index = 0)
  351. {
  352. if (!array_key_exists('links', $this->_data)) {
  353. $this->getLinks();
  354. }
  355. if (isset($this->_data['links'][$index])) {
  356. return $this->_data['links'][$index];
  357. }
  358. return null;
  359. }
  360. /**
  361. * Get all links
  362. *
  363. * @return array
  364. */
  365. public function getLinks()
  366. {
  367. if (array_key_exists('links', $this->_data)) {
  368. return $this->_data['links'];
  369. }
  370. $links = array();
  371. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  372. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  373. $list = $this->_xpath->query($this->_xpathQueryRss.'//link');
  374. } else {
  375. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:link');
  376. }
  377. if (!$list->length) {
  378. $links = $this->getExtension('Atom')->getLinks();
  379. } else {
  380. foreach ($list as $link) {
  381. $links[] = $link->nodeValue;
  382. }
  383. }
  384. $this->_data['links'] = $links;
  385. return $this->_data['links'];
  386. }
  387. /**
  388. * Get all categories
  389. *
  390. * @return Zend_Feed_Reader_Collection_Category
  391. */
  392. public function getCategories()
  393. {
  394. if (array_key_exists('categories', $this->_data)) {
  395. return $this->_data['categories'];
  396. }
  397. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  398. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  399. $list = $this->_xpath->query($this->_xpathQueryRss.'//category');
  400. } else {
  401. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:category');
  402. }
  403. if ($list->length) {
  404. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  405. foreach ($list as $category) {
  406. $categoryCollection[] = array(
  407. 'term' => $category->nodeValue,
  408. 'scheme' => $category->getAttribute('domain'),
  409. 'label' => $category->nodeValue,
  410. );
  411. }
  412. } else {
  413. $categoryCollection = $this->getExtension('DublinCore')->getCategories();
  414. }
  415. if (count($categoryCollection) == 0) {
  416. $categoryCollection = $this->getExtension('Atom')->getCategories();
  417. }
  418. $this->_data['categories'] = $categoryCollection;
  419. return $this->_data['categories'];
  420. }
  421. /**
  422. * Get a permalink to the entry
  423. *
  424. * @return string
  425. */
  426. public function getPermalink()
  427. {
  428. return $this->getLink(0);
  429. }
  430. /**
  431. * Get the entry title
  432. *
  433. * @return string
  434. */
  435. public function getTitle()
  436. {
  437. if (array_key_exists('title', $this->_data)) {
  438. return $this->_data['title'];
  439. }
  440. $title = null;
  441. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  442. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  443. ) {
  444. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/title)');
  445. } else {
  446. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:title)');
  447. }
  448. if (!$title) {
  449. $title = $this->getExtension('DublinCore')->getTitle();
  450. }
  451. if (!$title) {
  452. $title = $this->getExtension('Atom')->getTitle();
  453. }
  454. if (!$title) {
  455. $title = null;
  456. }
  457. $this->_data['title'] = $title;
  458. return $this->_data['title'];
  459. }
  460. /**
  461. * Get the number of comments/replies for current entry
  462. *
  463. * @return string|null
  464. */
  465. public function getCommentCount()
  466. {
  467. if (array_key_exists('commentcount', $this->_data)) {
  468. return $this->_data['commentcount'];
  469. }
  470. $commentcount = $this->getExtension('Slash')->getCommentCount();
  471. if (!$commentcount) {
  472. $commentcount = $this->getExtension('Thread')->getCommentCount();
  473. }
  474. if (!$commentcount) {
  475. $commentcount = $this->getExtension('Atom')->getCommentCount();
  476. }
  477. if (!$commentcount) {
  478. $commentcount = null;
  479. }
  480. $this->_data['commentcount'] = $commentcount;
  481. return $this->_data['commentcount'];
  482. }
  483. /**
  484. * Returns a URI pointing to the HTML page where comments can be made on this entry
  485. *
  486. * @return string
  487. */
  488. public function getCommentLink()
  489. {
  490. if (array_key_exists('commentlink', $this->_data)) {
  491. return $this->_data['commentlink'];
  492. }
  493. $commentlink = null;
  494. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  495. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  496. ) {
  497. $commentlink = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/comments)');
  498. }
  499. if (!$commentlink) {
  500. $commentlink = $this->getExtension('Atom')->getCommentLink();
  501. }
  502. if (!$commentlink) {
  503. $commentlink = null;
  504. }
  505. $this->_data['commentlink'] = $commentlink;
  506. return $this->_data['commentlink'];
  507. }
  508. /**
  509. * Returns a URI pointing to a feed of all comments for this entry
  510. *
  511. * @return string
  512. */
  513. public function getCommentFeedLink()
  514. {
  515. if (array_key_exists('commentfeedlink', $this->_data)) {
  516. return $this->_data['commentfeedlink'];
  517. }
  518. $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink();
  519. if (!$commentfeedlink) {
  520. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss');
  521. }
  522. if (!$commentfeedlink) {
  523. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf');
  524. }
  525. if (!$commentfeedlink) {
  526. $commentfeedlink = null;
  527. }
  528. $this->_data['commentfeedlink'] = $commentfeedlink;
  529. return $this->_data['commentfeedlink'];
  530. }
  531. /**
  532. * Set the XPath query (incl. on all Extensions)
  533. *
  534. * @param DOMXPath $xpath
  535. */
  536. public function setXpath(DOMXPath $xpath)
  537. {
  538. parent::setXpath($xpath);
  539. foreach ($this->_extensions as $extension) {
  540. $extension->setXpath($this->_xpath);
  541. }
  542. }
  543. }