Rss.php 17 KB

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