PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Feed/Reader/Entry/Rss.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 668 lines | 389 code | 116 blank | 163 comment | 82 complexity | 21d6854cc043244cd52cb6f3d23f1f96 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Rss.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. }
  291. $this->_data['description'] = $description;
  292. return $this->_data['description'];
  293. }
  294. /**
  295. * Get the entry enclosure
  296. * @return string
  297. */
  298. public function getEnclosure()
  299. {
  300. if (array_key_exists('enclosure', $this->_data)) {
  301. return $this->_data['enclosure'];
  302. }
  303. $enclosure = null;
  304. if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_20) {
  305. $nodeList = $this->_xpath->query($this->_xpathQueryRss . '/enclosure');
  306. if ($nodeList->length > 0) {
  307. $enclosure = new stdClass();
  308. $enclosure->url = $nodeList->item(0)->getAttribute('url');
  309. $enclosure->length = $nodeList->item(0)->getAttribute('length');
  310. $enclosure->type = $nodeList->item(0)->getAttribute('type');
  311. }
  312. }
  313. if (!$enclosure) {
  314. $enclosure = $this->getExtension('Atom')->getEnclosure();
  315. }
  316. $this->_data['enclosure'] = $enclosure;
  317. return $this->_data['enclosure'];
  318. }
  319. /**
  320. * Get the entry ID
  321. *
  322. * @return string
  323. */
  324. public function getId()
  325. {
  326. if (array_key_exists('id', $this->_data)) {
  327. return $this->_data['id'];
  328. }
  329. $id = null;
  330. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  331. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  332. ) {
  333. $id = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/guid)');
  334. }
  335. if (!$id) {
  336. $id = $this->getExtension('DublinCore')->getId();
  337. }
  338. if (empty($id)) {
  339. $id = $this->getExtension('Atom')->getId();
  340. }
  341. if (!$id) {
  342. if ($this->getPermalink()) {
  343. $id = $this->getPermalink();
  344. } elseif ($this->getTitle()) {
  345. $id = $this->getTitle();
  346. } else {
  347. $id = null;
  348. }
  349. }
  350. $this->_data['id'] = $id;
  351. return $this->_data['id'];
  352. }
  353. /**
  354. * Get a specific link
  355. *
  356. * @param int $index
  357. * @return string
  358. */
  359. public function getLink($index = 0)
  360. {
  361. if (!array_key_exists('links', $this->_data)) {
  362. $this->getLinks();
  363. }
  364. if (isset($this->_data['links'][$index])) {
  365. return $this->_data['links'][$index];
  366. }
  367. return null;
  368. }
  369. /**
  370. * Get all links
  371. *
  372. * @return array
  373. */
  374. public function getLinks()
  375. {
  376. if (array_key_exists('links', $this->_data)) {
  377. return $this->_data['links'];
  378. }
  379. $links = array();
  380. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  381. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  382. $list = $this->_xpath->query($this->_xpathQueryRss.'//link');
  383. } else {
  384. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:link');
  385. }
  386. if (!$list->length) {
  387. $links = $this->getExtension('Atom')->getLinks();
  388. } else {
  389. foreach ($list as $link) {
  390. $links[] = $link->nodeValue;
  391. }
  392. }
  393. $this->_data['links'] = $links;
  394. return $this->_data['links'];
  395. }
  396. /**
  397. * Get all categories
  398. *
  399. * @return Zend_Feed_Reader_Collection_Category
  400. */
  401. public function getCategories()
  402. {
  403. if (array_key_exists('categories', $this->_data)) {
  404. return $this->_data['categories'];
  405. }
  406. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
  407. $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
  408. $list = $this->_xpath->query($this->_xpathQueryRss.'//category');
  409. } else {
  410. $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:category');
  411. }
  412. if ($list->length) {
  413. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  414. foreach ($list as $category) {
  415. $categoryCollection[] = array(
  416. 'term' => $category->nodeValue,
  417. 'scheme' => $category->getAttribute('domain'),
  418. 'label' => $category->nodeValue,
  419. );
  420. }
  421. } else {
  422. $categoryCollection = $this->getExtension('DublinCore')->getCategories();
  423. }
  424. if (count($categoryCollection) == 0) {
  425. $categoryCollection = $this->getExtension('Atom')->getCategories();
  426. }
  427. $this->_data['categories'] = $categoryCollection;
  428. return $this->_data['categories'];
  429. }
  430. /**
  431. * Get a permalink to the entry
  432. *
  433. * @return string
  434. */
  435. public function getPermalink()
  436. {
  437. return $this->getLink(0);
  438. }
  439. /**
  440. * Get the entry title
  441. *
  442. * @return string
  443. */
  444. public function getTitle()
  445. {
  446. if (array_key_exists('title', $this->_data)) {
  447. return $this->_data['title'];
  448. }
  449. $title = null;
  450. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  451. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  452. ) {
  453. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/title)');
  454. } else {
  455. $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:title)');
  456. }
  457. if (!$title) {
  458. $title = $this->getExtension('DublinCore')->getTitle();
  459. }
  460. if (!$title) {
  461. $title = $this->getExtension('Atom')->getTitle();
  462. }
  463. if (!$title) {
  464. $title = null;
  465. }
  466. $this->_data['title'] = $title;
  467. return $this->_data['title'];
  468. }
  469. /**
  470. * Get the number of comments/replies for current entry
  471. *
  472. * @return string|null
  473. */
  474. public function getCommentCount()
  475. {
  476. if (array_key_exists('commentcount', $this->_data)) {
  477. return $this->_data['commentcount'];
  478. }
  479. $commentcount = $this->getExtension('Slash')->getCommentCount();
  480. if (!$commentcount) {
  481. $commentcount = $this->getExtension('Thread')->getCommentCount();
  482. }
  483. if (!$commentcount) {
  484. $commentcount = $this->getExtension('Atom')->getCommentCount();
  485. }
  486. if (!$commentcount) {
  487. $commentcount = null;
  488. }
  489. $this->_data['commentcount'] = $commentcount;
  490. return $this->_data['commentcount'];
  491. }
  492. /**
  493. * Returns a URI pointing to the HTML page where comments can be made on this entry
  494. *
  495. * @return string
  496. */
  497. public function getCommentLink()
  498. {
  499. if (array_key_exists('commentlink', $this->_data)) {
  500. return $this->_data['commentlink'];
  501. }
  502. $commentlink = null;
  503. if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10
  504. && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090
  505. ) {
  506. $commentlink = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/comments)');
  507. }
  508. if (!$commentlink) {
  509. $commentlink = $this->getExtension('Atom')->getCommentLink();
  510. }
  511. if (!$commentlink) {
  512. $commentlink = null;
  513. }
  514. $this->_data['commentlink'] = $commentlink;
  515. return $this->_data['commentlink'];
  516. }
  517. /**
  518. * Returns a URI pointing to a feed of all comments for this entry
  519. *
  520. * @return string
  521. */
  522. public function getCommentFeedLink()
  523. {
  524. if (array_key_exists('commentfeedlink', $this->_data)) {
  525. return $this->_data['commentfeedlink'];
  526. }
  527. $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink();
  528. if (!$commentfeedlink) {
  529. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss');
  530. }
  531. if (!$commentfeedlink) {
  532. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf');
  533. }
  534. if (!$commentfeedlink) {
  535. $commentfeedlink = null;
  536. }
  537. $this->_data['commentfeedlink'] = $commentfeedlink;
  538. return $this->_data['commentfeedlink'];
  539. }
  540. /**
  541. * Set the XPath query (incl. on all Extensions)
  542. *
  543. * @param DOMXPath $xpath
  544. */
  545. public function setXpath(DOMXPath $xpath)
  546. {
  547. parent::setXpath($xpath);
  548. foreach ($this->_extensions as $extension) {
  549. $extension->setXpath($this->_xpath);
  550. }
  551. }
  552. }