Rss.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. require_once 'Zend/Feed/Exception.php';
  260. throw new Zend_Feed_Exception(
  261. 'Could not load date due to unrecognised format (should follow RFC 822 or 2822): '
  262. . $e->getMessage()
  263. );
  264. }
  265. }
  266. }
  267. }
  268. if (!$date) {
  269. $date = $this->_dc->getDate();
  270. }
  271. if (!$date) {
  272. $date = $this->_atom->getDateModified();
  273. }
  274. if (!$date) {
  275. $date = null;
  276. }
  277. $this->_data['datemodified'] = $date;
  278. return $this->_data['datemodified'];
  279. }
  280. /**
  281. * Get the entry description
  282. *
  283. * @return string
  284. */
  285. public function getDescription()
  286. {
  287. if (array_key_exists('description', $this->_data)) {
  288. return $this->_data['description'];
  289. }
  290. $description = null;
  291. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  292. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  293. ) {
  294. $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/description)');
  295. } else {
  296. $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:description)');
  297. }
  298. if (!$description) {
  299. $description = $this->_dc->getDescription();
  300. }
  301. if (empty($description)) {
  302. $description = $this->_atom->getDescription();
  303. }
  304. if (!$description) {
  305. $description = null;
  306. } else {
  307. $description = html_entity_decode($description, ENT_QUOTES, $this->getEncoding());
  308. }
  309. $this->_data['description'] = $description;
  310. return $this->_data['description'];
  311. }
  312. /**
  313. * Get the entry enclosure
  314. *
  315. * @return string
  316. */
  317. public function getEnclosure()
  318. {
  319. if (array_key_exists('enclosure', $this->_data)) {
  320. return $this->_data['enclosure'];
  321. }
  322. $enclosure = null;
  323. if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_20) {
  324. $nodeList = $this->_xpath->query($this->_xpathQueryRss . '/enclosure');
  325. if ($nodeList->length > 0) {
  326. $enclosure = new stdClass();
  327. $enclosure->url = $nodeList->item(0)->getAttribute('url');
  328. $enclosure->length = $nodeList->item(0)->getAttribute('length');
  329. $enclosure->type = $nodeList->item(0)->getAttribute('type');
  330. }
  331. }
  332. $this->_data['enclosure'] = $enclosure;
  333. return $this->_data['enclosure'];
  334. }
  335. /**
  336. * Get the entry ID
  337. *
  338. * @return string
  339. */
  340. public function getId()
  341. {
  342. if (array_key_exists('id', $this->_data)) {
  343. return $this->_data['id'];
  344. }
  345. $id = null;
  346. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  347. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  348. ) {
  349. $id = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/guid)');
  350. }
  351. if (!$id) {
  352. $id = $this->_dc->getId();
  353. }
  354. if (empty($id)) {
  355. $id = $this->_atom->getId();
  356. }
  357. if (!$id) {
  358. if ($this->getPermalink()) {
  359. $id = $this->getPermalink();
  360. } elseif ($this->getTitle()) {
  361. $id = $this->getTitle();
  362. } else {
  363. $id = null;
  364. }
  365. }
  366. $this->_data['id'] = $id;
  367. return $this->_data['id'];
  368. }
  369. /**
  370. * Get a specific link
  371. *
  372. * @param int $index
  373. * @return string
  374. */
  375. public function getLink($index = 0)
  376. {
  377. if (!array_key_exists('links', $this->_data)) {
  378. $this->getLinks();
  379. }
  380. if (isset($this->_data['links'][$index])) {
  381. return $this->_data['links'][$index];
  382. }
  383. return null;
  384. }
  385. /**
  386. * Get all links
  387. *
  388. * @return array
  389. */
  390. public function getLinks()
  391. {
  392. if (array_key_exists('links', $this->_data)) {
  393. return $this->_data['links'];
  394. }
  395. $links = array();
  396. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  397. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  398. $list = $this->_xpath->query($this->_xpathQueryRss.'//link');
  399. } else {
  400. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:link');
  401. }
  402. if (!$list->length) {
  403. $links = $this->_atom->getLinks();
  404. } else {
  405. foreach ($list as $link) {
  406. $links[] = $link->nodeValue;
  407. }
  408. }
  409. $this->_data['links'] = $links;
  410. return $this->_data['links'];
  411. }
  412. /**
  413. * Get a permalink to the entry
  414. *
  415. * @return string
  416. */
  417. public function getPermalink()
  418. {
  419. return $this->getLink(0);
  420. }
  421. /**
  422. * Get the entry title
  423. *
  424. * @return string
  425. */
  426. public function getTitle()
  427. {
  428. if (array_key_exists('title', $this->_data)) {
  429. return $this->_data['title'];
  430. }
  431. $title = null;
  432. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  433. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  434. ) {
  435. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/title)');
  436. } else {
  437. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:title)');
  438. }
  439. if (!$title) {
  440. $title = $this->_dc->getTitle();
  441. }
  442. if (!$title) {
  443. $title = $this->_atom->getTitle();
  444. }
  445. if (!$title) {
  446. $title = null;
  447. }
  448. $this->_data['title'] = $title;
  449. return $this->_data['title'];
  450. }
  451. /**
  452. * Get the number of comments/replies for current entry
  453. *
  454. * @return string|null
  455. */
  456. public function getCommentCount()
  457. {
  458. if (array_key_exists('commentcount', $this->_data)) {
  459. return $this->_data['commentcount'];
  460. }
  461. $commentcount = $this->_slash->getCommentCount();
  462. if (!$commentcount) {
  463. $commentcount = $this->_thread->getCommentCount();
  464. }
  465. if (!$commentcount) {
  466. $commentcount = $this->_atom->getCommentCount();
  467. }
  468. if (!$commentcount) {
  469. $commentcount = null;
  470. }
  471. $this->_data['commentcount'] = $commentcount;
  472. return $this->_data['commentcount'];
  473. }
  474. /**
  475. * Returns a URI pointing to the HTML page where comments can be made on this entry
  476. *
  477. * @return string
  478. */
  479. public function getCommentLink()
  480. {
  481. if (array_key_exists('commentlink', $this->_data)) {
  482. return $this->_data['commentlink'];
  483. }
  484. $commentlink = null;
  485. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  486. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  487. ) {
  488. $commentlink = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/comments)');
  489. }
  490. if (!$commentlink) {
  491. $commentlink = $this->_atom->getCommentLink();
  492. }
  493. if (!$commentlink) {
  494. $commentlink = null;
  495. }
  496. $this->_data['commentlink'] = $commentlink;
  497. return $this->_data['commentlink'];
  498. }
  499. /**
  500. * Returns a URI pointing to a feed of all comments for this entry
  501. *
  502. * @return string
  503. */
  504. public function getCommentFeedLink()
  505. {
  506. if (array_key_exists('commentfeedlink', $this->_data)) {
  507. return $this->_data['commentfeedlink'];
  508. }
  509. $commentfeedlink = $this->_wfw->getCommentFeedLink();
  510. if (!$commentfeedlink) {
  511. $commentfeedlink = $this->_atom->getCommentFeedLink('rss');
  512. }
  513. if (!$commentfeedlink) {
  514. $commentfeedlink = $this->_atom->getCommentFeedLink('rdf');
  515. }
  516. if (!$commentfeedlink) {
  517. $commentfeedlink = null;
  518. }
  519. $this->_data['commentfeedlink'] = $commentfeedlink;
  520. return $this->_data['commentfeedlink'];
  521. }
  522. /**
  523. * Set the XPath query (incl. on all Extensions)
  524. *
  525. * @param DOMXPath $xpath
  526. */
  527. public function setXpath(DOMXPath $xpath)
  528. {
  529. parent::setXpath($xpath);
  530. $this->_dc->setXpath($this->_xpath);
  531. $this->_content->setXpath($this->_xpath);
  532. $this->_atom->setXpath($this->_xpath);
  533. $this->_slash->setXpath($this->_xpath);
  534. $this->_wfw->setXpath($this->_xpath);
  535. $this->_thread->setXpath($this->_xpath);
  536. foreach ($this->_extensions as $extension) {
  537. $extension->setXpath($this->_xpath);
  538. }
  539. }
  540. }