Rss.php 16 KB

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