Rss.php 18 KB

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