Rss.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. $dateStandards = array(Zend_Date::RSS, Zend_Date::RFC_822,
  217. Zend_Date::RFC_2822, Zend_Date::DATES);
  218. $date = new Zend_Date;
  219. foreach ($dateStandards as $standard) {
  220. try {
  221. $date->set($dateModified, $standard);
  222. break;
  223. } catch (Zend_Date_Exception $e) {
  224. if ($standard == Zend_Date::DATES) {
  225. require_once 'Zend/Feed/Exception.php';
  226. throw new Zend_Feed_Exception(
  227. 'Could not load date due to unrecognised'
  228. .' 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. * @return string
  283. */
  284. public function getEnclosure()
  285. {
  286. if (array_key_exists('enclosure', $this->_data)) {
  287. return $this->_data['enclosure'];
  288. }
  289. $enclosure = null;
  290. if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_20) {
  291. $nodeList = $this->_xpath->query($this->_xpathQueryRss . '/enclosure');
  292. if ($nodeList->length > 0) {
  293. $enclosure = new stdClass();
  294. $enclosure->url = $nodeList->item(0)->getAttribute('url');
  295. $enclosure->length = $nodeList->item(0)->getAttribute('length');
  296. $enclosure->type = $nodeList->item(0)->getAttribute('type');
  297. }
  298. }
  299. if (!$enclosure) {
  300. $enclosure = $this->getExtension('Atom')->getEnclosure();
  301. }
  302. $this->_data['enclosure'] = $enclosure;
  303. return $this->_data['enclosure'];
  304. }
  305. /**
  306. * Get the entry ID
  307. *
  308. * @return string
  309. */
  310. public function getId()
  311. {
  312. if (array_key_exists('id', $this->_data)) {
  313. return $this->_data['id'];
  314. }
  315. $id = null;
  316. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  317. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  318. ) {
  319. $id = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/guid)');
  320. }
  321. if (!$id) {
  322. $id = $this->getExtension('DublinCore')->getId();
  323. }
  324. if (empty($id)) {
  325. $id = $this->getExtension('Atom')->getId();
  326. }
  327. if (!$id) {
  328. if ($this->getPermalink()) {
  329. $id = $this->getPermalink();
  330. } elseif ($this->getTitle()) {
  331. $id = $this->getTitle();
  332. } else {
  333. $id = null;
  334. }
  335. }
  336. $this->_data['id'] = $id;
  337. return $this->_data['id'];
  338. }
  339. /**
  340. * Get a specific link
  341. *
  342. * @param int $index
  343. * @return string
  344. */
  345. public function getLink($index = 0)
  346. {
  347. if (!array_key_exists('links', $this->_data)) {
  348. $this->getLinks();
  349. }
  350. if (isset($this->_data['links'][$index])) {
  351. return $this->_data['links'][$index];
  352. }
  353. return null;
  354. }
  355. /**
  356. * Get all links
  357. *
  358. * @return array
  359. */
  360. public function getLinks()
  361. {
  362. if (array_key_exists('links', $this->_data)) {
  363. return $this->_data['links'];
  364. }
  365. $links = array();
  366. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  367. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  368. $list = $this->_xpath->query($this->_xpathQueryRss.'//link');
  369. } else {
  370. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:link');
  371. }
  372. if (!$list->length) {
  373. $links = $this->getExtension('Atom')->getLinks();
  374. } else {
  375. foreach ($list as $link) {
  376. $links[] = $link->nodeValue;
  377. }
  378. }
  379. $this->_data['links'] = $links;
  380. return $this->_data['links'];
  381. }
  382. /**
  383. * Get a permalink to the entry
  384. *
  385. * @return string
  386. */
  387. public function getPermalink()
  388. {
  389. return $this->getLink(0);
  390. }
  391. /**
  392. * Get the entry title
  393. *
  394. * @return string
  395. */
  396. public function getTitle()
  397. {
  398. if (array_key_exists('title', $this->_data)) {
  399. return $this->_data['title'];
  400. }
  401. $title = null;
  402. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  403. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  404. ) {
  405. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/title)');
  406. } else {
  407. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:title)');
  408. }
  409. if (!$title) {
  410. $title = $this->getExtension('DublinCore')->getTitle();
  411. }
  412. if (!$title) {
  413. $title = $this->getExtension('Atom')->getTitle();
  414. }
  415. if (!$title) {
  416. $title = null;
  417. }
  418. $this->_data['title'] = $title;
  419. return $this->_data['title'];
  420. }
  421. /**
  422. * Get the number of comments/replies for current entry
  423. *
  424. * @return string|null
  425. */
  426. public function getCommentCount()
  427. {
  428. if (array_key_exists('commentcount', $this->_data)) {
  429. return $this->_data['commentcount'];
  430. }
  431. $commentcount = $this->getExtension('Slash')->getCommentCount();
  432. if (!$commentcount) {
  433. $commentcount = $this->getExtension('Thread')->getCommentCount();
  434. }
  435. if (!$commentcount) {
  436. $commentcount = $this->getExtension('Atom')->getCommentCount();
  437. }
  438. if (!$commentcount) {
  439. $commentcount = null;
  440. }
  441. $this->_data['commentcount'] = $commentcount;
  442. return $this->_data['commentcount'];
  443. }
  444. /**
  445. * Returns a URI pointing to the HTML page where comments can be made on this entry
  446. *
  447. * @return string
  448. */
  449. public function getCommentLink()
  450. {
  451. if (array_key_exists('commentlink', $this->_data)) {
  452. return $this->_data['commentlink'];
  453. }
  454. $commentlink = null;
  455. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  456. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  457. ) {
  458. $commentlink = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/comments)');
  459. }
  460. if (!$commentlink) {
  461. $commentlink = $this->getExtension('Atom')->getCommentLink();
  462. }
  463. if (!$commentlink) {
  464. $commentlink = null;
  465. }
  466. $this->_data['commentlink'] = $commentlink;
  467. return $this->_data['commentlink'];
  468. }
  469. /**
  470. * Returns a URI pointing to a feed of all comments for this entry
  471. *
  472. * @return string
  473. */
  474. public function getCommentFeedLink()
  475. {
  476. if (array_key_exists('commentfeedlink', $this->_data)) {
  477. return $this->_data['commentfeedlink'];
  478. }
  479. $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink();
  480. if (!$commentfeedlink) {
  481. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss');
  482. }
  483. if (!$commentfeedlink) {
  484. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf');
  485. }
  486. if (!$commentfeedlink) {
  487. $commentfeedlink = null;
  488. }
  489. $this->_data['commentfeedlink'] = $commentfeedlink;
  490. return $this->_data['commentfeedlink'];
  491. }
  492. /**
  493. * Set the XPath query (incl. on all Extensions)
  494. *
  495. * @param DOMXPath $xpath
  496. */
  497. public function setXpath(DOMXPath $xpath)
  498. {
  499. parent::setXpath($xpath);
  500. foreach ($this->_extensions as $extension) {
  501. $extension->setXpath($this->_xpath);
  502. }
  503. }
  504. }