Rss.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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-2010 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-2010 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. $authors_dc = $this->getExtension('DublinCore')->getAuthors();
  138. if (!empty($authors_dc)) {
  139. foreach ($authors_dc as $author) {
  140. $authors[] = array(
  141. 'name' => $author['name']
  142. );
  143. }
  144. }
  145. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  146. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  147. $list = $this->_xpath->query($this->_xpathQueryRss . '//author');
  148. } else {
  149. $list = $this->_xpath->query($this->_xpathQueryRdf . '//rss:author');
  150. }
  151. if ($list->length) {
  152. foreach ($list as $author) {
  153. $string = trim($author->nodeValue);
  154. $email = null;
  155. $name = null;
  156. $data = array();
  157. // Pretty rough parsing - but it's a catchall
  158. if (preg_match("/^.*@[^ ]*/", $string, $matches)) {
  159. $data['email'] = trim($matches[0]);
  160. if (preg_match("/\((.*)\)$/", $string, $matches)) {
  161. $data['name'] = $matches[1];
  162. }
  163. $authors[] = $data;
  164. }
  165. }
  166. }
  167. if (count($authors) == 0) {
  168. $authors = $this->getExtension('Atom')->getAuthors();
  169. } else {
  170. $authors = new Zend_Feed_Reader_Collection_Author(
  171. Zend_Feed_Reader::arrayUnique($authors)
  172. );
  173. }
  174. if (count($authors) == 0) {
  175. $authors = null;
  176. }
  177. $this->_data['authors'] = $authors;
  178. return $this->_data['authors'];
  179. }
  180. /**
  181. * Get the entry content
  182. *
  183. * @return string
  184. */
  185. public function getContent()
  186. {
  187. if (array_key_exists('content', $this->_data)) {
  188. return $this->_data['content'];
  189. }
  190. $content = $this->getExtension('Content')->getContent();
  191. if (!$content) {
  192. $content = $this->getDescription();
  193. }
  194. if (empty($content)) {
  195. $content = $this->getExtension('Atom')->getContent();
  196. }
  197. $this->_data['content'] = $content;
  198. return $this->_data['content'];
  199. }
  200. /**
  201. * Get the entry's date of creation
  202. *
  203. * @return string
  204. */
  205. public function getDateCreated()
  206. {
  207. return $this->getDateModified();
  208. }
  209. /**
  210. * Get the entry's date of modification
  211. *
  212. * @return string
  213. */
  214. public function getDateModified()
  215. {
  216. if (array_key_exists('datemodified', $this->_data)) {
  217. return $this->_data['datemodified'];
  218. }
  219. $dateModified = null;
  220. $date = null;
  221. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  222. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  223. ) {
  224. $dateModified = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/pubDate)');
  225. if ($dateModified) {
  226. $dateModifiedParsed = strtotime($dateModified);
  227. if ($dateModifiedParsed) {
  228. $date = new Zend_Date($dateModifiedParsed);
  229. } else {
  230. $dateStandards = array(Zend_Date::RSS, Zend_Date::RFC_822,
  231. Zend_Date::RFC_2822, Zend_Date::DATES);
  232. $date = new Zend_Date;
  233. foreach ($dateStandards as $standard) {
  234. try {
  235. $date->set($dateModified, $standard);
  236. break;
  237. } catch (Zend_Date_Exception $e) {
  238. if ($standard == Zend_Date::DATES) {
  239. require_once 'Zend/Feed/Exception.php';
  240. throw new Zend_Feed_Exception(
  241. 'Could not load date due to unrecognised'
  242. .' format (should follow RFC 822 or 2822):'
  243. . $e->getMessage(),
  244. 0, $e
  245. );
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. if (!$date) {
  253. $date = $this->getExtension('DublinCore')->getDate();
  254. }
  255. if (!$date) {
  256. $date = $this->getExtension('Atom')->getDateModified();
  257. }
  258. if (!$date) {
  259. $date = null;
  260. }
  261. $this->_data['datemodified'] = $date;
  262. return $this->_data['datemodified'];
  263. }
  264. /**
  265. * Get the entry description
  266. *
  267. * @return string
  268. */
  269. public function getDescription()
  270. {
  271. if (array_key_exists('description', $this->_data)) {
  272. return $this->_data['description'];
  273. }
  274. $description = null;
  275. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  276. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  277. ) {
  278. $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/description)');
  279. } else {
  280. $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:description)');
  281. }
  282. if (!$description) {
  283. $description = $this->getExtension('DublinCore')->getDescription();
  284. }
  285. if (empty($description)) {
  286. $description = $this->getExtension('Atom')->getDescription();
  287. }
  288. if (!$description) {
  289. $description = null;
  290. } else {
  291. $description = html_entity_decode($description, ENT_QUOTES, $this->getEncoding());
  292. }
  293. $this->_data['description'] = $description;
  294. return $this->_data['description'];
  295. }
  296. /**
  297. * Get the entry enclosure
  298. * @return string
  299. */
  300. public function getEnclosure()
  301. {
  302. if (array_key_exists('enclosure', $this->_data)) {
  303. return $this->_data['enclosure'];
  304. }
  305. $enclosure = null;
  306. if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_20) {
  307. $nodeList = $this->_xpath->query($this->_xpathQueryRss . '/enclosure');
  308. if ($nodeList->length > 0) {
  309. $enclosure = new stdClass();
  310. $enclosure->url = $nodeList->item(0)->getAttribute('url');
  311. $enclosure->length = $nodeList->item(0)->getAttribute('length');
  312. $enclosure->type = $nodeList->item(0)->getAttribute('type');
  313. }
  314. }
  315. if (!$enclosure) {
  316. $enclosure = $this->getExtension('Atom')->getEnclosure();
  317. }
  318. $this->_data['enclosure'] = $enclosure;
  319. return $this->_data['enclosure'];
  320. }
  321. /**
  322. * Get the entry ID
  323. *
  324. * @return string
  325. */
  326. public function getId()
  327. {
  328. if (array_key_exists('id', $this->_data)) {
  329. return $this->_data['id'];
  330. }
  331. $id = null;
  332. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  333. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  334. ) {
  335. $id = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/guid)');
  336. }
  337. if (!$id) {
  338. $id = $this->getExtension('DublinCore')->getId();
  339. }
  340. if (empty($id)) {
  341. $id = $this->getExtension('Atom')->getId();
  342. }
  343. if (!$id) {
  344. if ($this->getPermalink()) {
  345. $id = $this->getPermalink();
  346. } elseif ($this->getTitle()) {
  347. $id = $this->getTitle();
  348. } else {
  349. $id = null;
  350. }
  351. }
  352. $this->_data['id'] = $id;
  353. return $this->_data['id'];
  354. }
  355. /**
  356. * Get a specific link
  357. *
  358. * @param int $index
  359. * @return string
  360. */
  361. public function getLink($index = 0)
  362. {
  363. if (!array_key_exists('links', $this->_data)) {
  364. $this->getLinks();
  365. }
  366. if (isset($this->_data['links'][$index])) {
  367. return $this->_data['links'][$index];
  368. }
  369. return null;
  370. }
  371. /**
  372. * Get all links
  373. *
  374. * @return array
  375. */
  376. public function getLinks()
  377. {
  378. if (array_key_exists('links', $this->_data)) {
  379. return $this->_data['links'];
  380. }
  381. $links = array();
  382. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  383. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  384. $list = $this->_xpath->query($this->_xpathQueryRss.'//link');
  385. } else {
  386. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:link');
  387. }
  388. if (!$list->length) {
  389. $links = $this->getExtension('Atom')->getLinks();
  390. } else {
  391. foreach ($list as $link) {
  392. $links[] = $link->nodeValue;
  393. }
  394. }
  395. $this->_data['links'] = $links;
  396. return $this->_data['links'];
  397. }
  398. /**
  399. * Get all categories
  400. *
  401. * @return Zend_Feed_Reader_Collection_Category
  402. */
  403. public function getCategories()
  404. {
  405. if (array_key_exists('categories', $this->_data)) {
  406. return $this->_data['categories'];
  407. }
  408. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  409. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  410. $list = $this->_xpath->query($this->_xpathQueryRss.'//category');
  411. } else {
  412. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:category');
  413. }
  414. if ($list->length) {
  415. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  416. foreach ($list as $category) {
  417. $categoryCollection[] = array(
  418. 'term' => $category->nodeValue,
  419. 'scheme' => $category->getAttribute('domain'),
  420. 'label' => $category->nodeValue,
  421. );
  422. }
  423. } else {
  424. $categoryCollection = $this->getExtension('DublinCore')->getCategories();
  425. }
  426. if (count($categoryCollection) == 0) {
  427. $categoryCollection = $this->getExtension('Atom')->getCategories();
  428. }
  429. $this->_data['categories'] = $categoryCollection;
  430. return $this->_data['categories'];
  431. }
  432. /**
  433. * Get a permalink to the entry
  434. *
  435. * @return string
  436. */
  437. public function getPermalink()
  438. {
  439. return $this->getLink(0);
  440. }
  441. /**
  442. * Get the entry title
  443. *
  444. * @return string
  445. */
  446. public function getTitle()
  447. {
  448. if (array_key_exists('title', $this->_data)) {
  449. return $this->_data['title'];
  450. }
  451. $title = null;
  452. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  453. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  454. ) {
  455. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/title)');
  456. } else {
  457. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:title)');
  458. }
  459. if (!$title) {
  460. $title = $this->getExtension('DublinCore')->getTitle();
  461. }
  462. if (!$title) {
  463. $title = $this->getExtension('Atom')->getTitle();
  464. }
  465. if (!$title) {
  466. $title = null;
  467. }
  468. $this->_data['title'] = $title;
  469. return $this->_data['title'];
  470. }
  471. /**
  472. * Get the number of comments/replies for current entry
  473. *
  474. * @return string|null
  475. */
  476. public function getCommentCount()
  477. {
  478. if (array_key_exists('commentcount', $this->_data)) {
  479. return $this->_data['commentcount'];
  480. }
  481. $commentcount = $this->getExtension('Slash')->getCommentCount();
  482. if (!$commentcount) {
  483. $commentcount = $this->getExtension('Thread')->getCommentCount();
  484. }
  485. if (!$commentcount) {
  486. $commentcount = $this->getExtension('Atom')->getCommentCount();
  487. }
  488. if (!$commentcount) {
  489. $commentcount = null;
  490. }
  491. $this->_data['commentcount'] = $commentcount;
  492. return $this->_data['commentcount'];
  493. }
  494. /**
  495. * Returns a URI pointing to the HTML page where comments can be made on this entry
  496. *
  497. * @return string
  498. */
  499. public function getCommentLink()
  500. {
  501. if (array_key_exists('commentlink', $this->_data)) {
  502. return $this->_data['commentlink'];
  503. }
  504. $commentlink = null;
  505. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  506. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  507. ) {
  508. $commentlink = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/comments)');
  509. }
  510. if (!$commentlink) {
  511. $commentlink = $this->getExtension('Atom')->getCommentLink();
  512. }
  513. if (!$commentlink) {
  514. $commentlink = null;
  515. }
  516. $this->_data['commentlink'] = $commentlink;
  517. return $this->_data['commentlink'];
  518. }
  519. /**
  520. * Returns a URI pointing to a feed of all comments for this entry
  521. *
  522. * @return string
  523. */
  524. public function getCommentFeedLink()
  525. {
  526. if (array_key_exists('commentfeedlink', $this->_data)) {
  527. return $this->_data['commentfeedlink'];
  528. }
  529. $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink();
  530. if (!$commentfeedlink) {
  531. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss');
  532. }
  533. if (!$commentfeedlink) {
  534. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf');
  535. }
  536. if (!$commentfeedlink) {
  537. $commentfeedlink = null;
  538. }
  539. $this->_data['commentfeedlink'] = $commentfeedlink;
  540. return $this->_data['commentfeedlink'];
  541. }
  542. /**
  543. * Set the XPath query (incl. on all Extensions)
  544. *
  545. * @param DOMXPath $xpath
  546. */
  547. public function setXpath(DOMXPath $xpath)
  548. {
  549. parent::setXpath($xpath);
  550. foreach ($this->_extensions as $extension) {
  551. $extension->setXpath($this->_xpath);
  552. }
  553. }
  554. }