Entry.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-2014 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_Extension_EntryAbstract
  27. */
  28. require_once 'Zend/Feed/Reader/Extension/EntryAbstract.php';
  29. /**
  30. * @see Zend_Date
  31. */
  32. require_once 'Zend/Date.php';
  33. /**
  34. * @see Zend_Uri
  35. */
  36. require_once 'Zend/Uri.php';
  37. /**
  38. * @see Zend_Feed_Reader_Collection_Category
  39. */
  40. require_once 'Zend/Feed/Reader/Collection/Category.php';
  41. /**
  42. * @see Zend_Feed_Reader_Feed_Atom_Source
  43. */
  44. require_once 'Zend/Feed/Reader/Feed/Atom/Source.php';
  45. /**
  46. * @category Zend
  47. * @package Zend_Feed_Reader
  48. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  49. * @license http://framework.zend.com/license/new-bsd New BSD License
  50. */
  51. class Zend_Feed_Reader_Extension_Atom_Entry
  52. extends Zend_Feed_Reader_Extension_EntryAbstract
  53. {
  54. /**
  55. * Get the specified author
  56. *
  57. * @param int $index
  58. * @return string|null
  59. */
  60. public function getAuthor($index = 0)
  61. {
  62. $authors = $this->getAuthors();
  63. if (isset($authors[$index])) {
  64. return $authors[$index];
  65. }
  66. return null;
  67. }
  68. /**
  69. * Get an array with feed authors
  70. *
  71. * @return array
  72. */
  73. public function getAuthors()
  74. {
  75. if (array_key_exists('authors', $this->_data)) {
  76. return $this->_data['authors'];
  77. }
  78. $authors = array();
  79. $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:author');
  80. if (!$list->length) {
  81. /**
  82. * TODO: Limit query to feed level els only!
  83. */
  84. $list = $this->getXpath()->query('//atom:author');
  85. }
  86. if ($list->length) {
  87. foreach ($list as $author) {
  88. $author = $this->_getAuthor($author);
  89. if (!empty($author)) {
  90. $authors[] = $author;
  91. }
  92. }
  93. }
  94. if (count($authors) == 0) {
  95. $authors = null;
  96. } else {
  97. $authors = new Zend_Feed_Reader_Collection_Author(
  98. Zend_Feed_Reader::arrayUnique($authors)
  99. );
  100. }
  101. $this->_data['authors'] = $authors;
  102. return $this->_data['authors'];
  103. }
  104. /**
  105. * Get the entry content
  106. *
  107. * @return string
  108. */
  109. public function getContent()
  110. {
  111. if (array_key_exists('content', $this->_data)) {
  112. return $this->_data['content'];
  113. }
  114. $content = null;
  115. $el = $this->getXpath()->query($this->getXpathPrefix() . '/atom:content');
  116. if($el->length > 0) {
  117. $el = $el->item(0);
  118. $type = $el->getAttribute('type');
  119. switch ($type) {
  120. case '':
  121. case 'text':
  122. case 'text/plain':
  123. case 'html':
  124. case 'text/html':
  125. $content = $el->nodeValue;
  126. break;
  127. case 'xhtml':
  128. $this->getXpath()->registerNamespace('xhtml', 'http://www.w3.org/1999/xhtml');
  129. $xhtml = $this->getXpath()->query(
  130. $this->getXpathPrefix() . '/atom:content/xhtml:div'
  131. )->item(0);
  132. //$xhtml->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
  133. $d = new DOMDocument('1.0', $this->getEncoding());
  134. $xhtmls = $d->importNode($xhtml, true);
  135. $d->appendChild($xhtmls);
  136. $content = $this->_collectXhtml(
  137. $d->saveXML(),
  138. $d->lookupPrefix('http://www.w3.org/1999/xhtml')
  139. );
  140. break;
  141. }
  142. }
  143. if (!$content) {
  144. $content = $this->getDescription();
  145. }
  146. $this->_data['content'] = trim($content);
  147. return $this->_data['content'];
  148. }
  149. /**
  150. * Parse out XHTML to remove the namespacing
  151. */
  152. protected function _collectXhtml($xhtml, $prefix)
  153. {
  154. if (!empty($prefix)) $prefix = $prefix . ':';
  155. $matches = array(
  156. "/<\?xml[^<]*>[^<]*<" . $prefix . "div[^<]*/",
  157. "/<\/" . $prefix . "div>\s*$/"
  158. );
  159. $xhtml = preg_replace($matches, '', $xhtml);
  160. if (!empty($prefix)) {
  161. $xhtml = preg_replace("/(<[\/]?)" . $prefix . "([a-zA-Z]+)/", '$1$2', $xhtml);
  162. }
  163. return $xhtml;
  164. }
  165. /**
  166. * Get the entry creation date
  167. *
  168. * @return string
  169. */
  170. public function getDateCreated()
  171. {
  172. if (array_key_exists('datecreated', $this->_data)) {
  173. return $this->_data['datecreated'];
  174. }
  175. $date = null;
  176. if ($this->_getAtomType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  177. $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
  178. } else {
  179. $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
  180. }
  181. if ($dateCreated) {
  182. $date = new Zend_Date;
  183. $date->set($dateCreated, Zend_Date::ISO_8601);
  184. }
  185. $this->_data['datecreated'] = $date;
  186. return $this->_data['datecreated'];
  187. }
  188. /**
  189. * Get the entry modification date
  190. *
  191. * @return string
  192. */
  193. public function getDateModified()
  194. {
  195. if (array_key_exists('datemodified', $this->_data)) {
  196. return $this->_data['datemodified'];
  197. }
  198. $date = null;
  199. if ($this->_getAtomType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  200. $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
  201. } else {
  202. $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
  203. }
  204. if ($dateModified) {
  205. $date = new Zend_Date;
  206. $date->set($dateModified, Zend_Date::ISO_8601);
  207. }
  208. $this->_data['datemodified'] = $date;
  209. return $this->_data['datemodified'];
  210. }
  211. /**
  212. * Get the entry description
  213. *
  214. * @return string
  215. */
  216. public function getDescription()
  217. {
  218. if (array_key_exists('description', $this->_data)) {
  219. return $this->_data['description'];
  220. }
  221. $description = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:summary)');
  222. if (!$description) {
  223. $description = null;
  224. }
  225. $this->_data['description'] = $description;
  226. return $this->_data['description'];
  227. }
  228. /**
  229. * Get the entry enclosure
  230. *
  231. * @return string
  232. */
  233. public function getEnclosure()
  234. {
  235. if (array_key_exists('enclosure', $this->_data)) {
  236. return $this->_data['enclosure'];
  237. }
  238. $enclosure = null;
  239. $nodeList = $this->getXpath()->query($this->getXpathPrefix() . '/atom:link[@rel="enclosure"]');
  240. if ($nodeList->length > 0) {
  241. $enclosure = new stdClass();
  242. $enclosure->url = $nodeList->item(0)->getAttribute('href');
  243. $enclosure->length = $nodeList->item(0)->getAttribute('length');
  244. $enclosure->type = $nodeList->item(0)->getAttribute('type');
  245. }
  246. $this->_data['enclosure'] = $enclosure;
  247. return $this->_data['enclosure'];
  248. }
  249. /**
  250. * Get the entry ID
  251. *
  252. * @return string
  253. */
  254. public function getId()
  255. {
  256. if (array_key_exists('id', $this->_data)) {
  257. return $this->_data['id'];
  258. }
  259. $id = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
  260. if (!$id) {
  261. if ($this->getPermalink()) {
  262. $id = $this->getPermalink();
  263. } elseif ($this->getTitle()) {
  264. $id = $this->getTitle();
  265. } else {
  266. $id = null;
  267. }
  268. }
  269. $this->_data['id'] = $id;
  270. return $this->_data['id'];
  271. }
  272. /**
  273. * Get the base URI of the feed (if set).
  274. *
  275. * @return string|null
  276. */
  277. public function getBaseUrl()
  278. {
  279. if (array_key_exists('baseUrl', $this->_data)) {
  280. return $this->_data['baseUrl'];
  281. }
  282. $baseUrl = $this->getXpath()->evaluate('string('
  283. . $this->getXpathPrefix() . '/@xml:base[1]'
  284. . ')');
  285. if (!$baseUrl) {
  286. $baseUrl = $this->getXpath()->evaluate('string(//@xml:base[1])');
  287. }
  288. if (!$baseUrl) {
  289. $baseUrl = null;
  290. }
  291. $this->_data['baseUrl'] = $baseUrl;
  292. return $this->_data['baseUrl'];
  293. }
  294. /**
  295. * Get a specific link
  296. *
  297. * @param int $index
  298. * @return string
  299. */
  300. public function getLink($index = 0)
  301. {
  302. if (!array_key_exists('links', $this->_data)) {
  303. $this->getLinks();
  304. }
  305. if (isset($this->_data['links'][$index])) {
  306. return $this->_data['links'][$index];
  307. }
  308. return null;
  309. }
  310. /**
  311. * Get all links
  312. *
  313. * @return array
  314. */
  315. public function getLinks()
  316. {
  317. if (array_key_exists('links', $this->_data)) {
  318. return $this->_data['links'];
  319. }
  320. $links = array();
  321. $list = $this->getXpath()->query(
  322. $this->getXpathPrefix() . '//atom:link[@rel="alternate"]/@href' . '|' .
  323. $this->getXpathPrefix() . '//atom:link[not(@rel)]/@href'
  324. );
  325. if ($list->length) {
  326. foreach ($list as $link) {
  327. $links[] = $this->_absolutiseUri($link->value);
  328. }
  329. }
  330. $this->_data['links'] = $links;
  331. return $this->_data['links'];
  332. }
  333. /**
  334. * Get a permalink to the entry
  335. *
  336. * @return string
  337. */
  338. public function getPermalink()
  339. {
  340. return $this->getLink(0);
  341. }
  342. /**
  343. * Get the entry title
  344. *
  345. * @return string
  346. */
  347. public function getTitle()
  348. {
  349. if (array_key_exists('title', $this->_data)) {
  350. return $this->_data['title'];
  351. }
  352. $title = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
  353. if (!$title) {
  354. $title = null;
  355. }
  356. $this->_data['title'] = $title;
  357. return $this->_data['title'];
  358. }
  359. /**
  360. * Get the number of comments/replies for current entry
  361. *
  362. * @return integer
  363. */
  364. public function getCommentCount()
  365. {
  366. if (array_key_exists('commentcount', $this->_data)) {
  367. return $this->_data['commentcount'];
  368. }
  369. $count = null;
  370. $this->getXpath()->registerNamespace('thread10', 'http://purl.org/syndication/thread/1.0');
  371. $list = $this->getXpath()->query(
  372. $this->getXpathPrefix() . '//atom:link[@rel="replies"]/@thread10:count'
  373. );
  374. if ($list->length) {
  375. $count = $list->item(0)->value;
  376. }
  377. $this->_data['commentcount'] = $count;
  378. return $this->_data['commentcount'];
  379. }
  380. /**
  381. * Returns a URI pointing to the HTML page where comments can be made on this entry
  382. *
  383. * @return string
  384. */
  385. public function getCommentLink()
  386. {
  387. if (array_key_exists('commentlink', $this->_data)) {
  388. return $this->_data['commentlink'];
  389. }
  390. $link = null;
  391. $list = $this->getXpath()->query(
  392. $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="text/html"]/@href'
  393. );
  394. if ($list->length) {
  395. $link = $list->item(0)->value;
  396. $link = $this->_absolutiseUri($link);
  397. }
  398. $this->_data['commentlink'] = $link;
  399. return $this->_data['commentlink'];
  400. }
  401. /**
  402. * Returns a URI pointing to a feed of all comments for this entry
  403. *
  404. * @return string
  405. */
  406. public function getCommentFeedLink($type = 'atom')
  407. {
  408. if (array_key_exists('commentfeedlink', $this->_data)) {
  409. return $this->_data['commentfeedlink'];
  410. }
  411. $link = null;
  412. $list = $this->getXpath()->query(
  413. $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="application/'.$type.'+xml"]/@href'
  414. );
  415. if ($list->length) {
  416. $link = $list->item(0)->value;
  417. $link = $this->_absolutiseUri($link);
  418. }
  419. $this->_data['commentfeedlink'] = $link;
  420. return $this->_data['commentfeedlink'];
  421. }
  422. /**
  423. * Get all categories
  424. *
  425. * @return Zend_Feed_Reader_Collection_Category
  426. */
  427. public function getCategories()
  428. {
  429. if (array_key_exists('categories', $this->_data)) {
  430. return $this->_data['categories'];
  431. }
  432. if ($this->_getAtomType() == Zend_Feed_Reader::TYPE_ATOM_10) {
  433. $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:category');
  434. } else {
  435. /**
  436. * Since Atom 0.3 did not support categories, it would have used the
  437. * Dublin Core extension. However there is a small possibility Atom 0.3
  438. * may have been retrofittied to use Atom 1.0 instead.
  439. */
  440. $this->getXpath()->registerNamespace('atom10', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  441. $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom10:category');
  442. }
  443. if ($list->length) {
  444. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  445. foreach ($list as $category) {
  446. $categoryCollection[] = array(
  447. 'term' => $category->getAttribute('term'),
  448. 'scheme' => $category->getAttribute('scheme'),
  449. 'label' => $category->getAttribute('label')
  450. );
  451. }
  452. } else {
  453. return new Zend_Feed_Reader_Collection_Category;
  454. }
  455. $this->_data['categories'] = $categoryCollection;
  456. return $this->_data['categories'];
  457. }
  458. /**
  459. * Get source feed metadata from the entry
  460. *
  461. * @return Zend_Feed_Reader_Feed_Atom_Source|null
  462. */
  463. public function getSource()
  464. {
  465. if (array_key_exists('source', $this->_data)) {
  466. return $this->_data['source'];
  467. }
  468. $source = null;
  469. // TODO: Investigate why _getAtomType() fails here. Is it even needed?
  470. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10) {
  471. $list = $this->getXpath()->query($this->getXpathPrefix() . '/atom:source[1]');
  472. if ($list->length) {
  473. $element = $list->item(0);
  474. $source = new Zend_Feed_Reader_Feed_Atom_Source($element, $this->getXpathPrefix());
  475. }
  476. }
  477. $this->_data['source'] = $source;
  478. return $this->_data['source'];
  479. }
  480. /**
  481. * Attempt to absolutise the URI, i.e. if a relative URI apply the
  482. * xml:base value as a prefix to turn into an absolute URI.
  483. */
  484. protected function _absolutiseUri($link)
  485. {
  486. if (!Zend_Uri::check($link)) {
  487. if ($this->getBaseUrl() !== null) {
  488. $link = $this->getBaseUrl() . $link;
  489. if (!Zend_Uri::check($link)) {
  490. $link = null;
  491. }
  492. }
  493. }
  494. return $link;
  495. }
  496. /**
  497. * Get an author entry
  498. *
  499. * @param DOMElement $element
  500. * @return string
  501. */
  502. protected function _getAuthor(DOMElement $element)
  503. {
  504. $author = array();
  505. $emailNode = $element->getElementsByTagName('email');
  506. $nameNode = $element->getElementsByTagName('name');
  507. $uriNode = $element->getElementsByTagName('uri');
  508. if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
  509. $author['email'] = $emailNode->item(0)->nodeValue;
  510. }
  511. if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
  512. $author['name'] = $nameNode->item(0)->nodeValue;
  513. }
  514. if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
  515. $author['uri'] = $uriNode->item(0)->nodeValue;
  516. }
  517. if (empty($author)) {
  518. return null;
  519. }
  520. return $author;
  521. }
  522. /**
  523. * Register the default namespaces for the current feed format
  524. */
  525. protected function _registerNamespaces()
  526. {
  527. switch ($this->_getAtomType()) {
  528. case Zend_Feed_Reader::TYPE_ATOM_03:
  529. $this->getXpath()->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
  530. break;
  531. default:
  532. $this->getXpath()->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  533. break;
  534. }
  535. }
  536. /**
  537. * Detect the presence of any Atom namespaces in use
  538. */
  539. protected function _getAtomType()
  540. {
  541. $dom = $this->getDomDocument();
  542. $prefixAtom03 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_03);
  543. $prefixAtom10 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_10);
  544. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_03)
  545. || !empty($prefixAtom03)) {
  546. return Zend_Feed_Reader::TYPE_ATOM_03;
  547. }
  548. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_10)
  549. || !empty($prefixAtom10)) {
  550. return Zend_Feed_Reader::TYPE_ATOM_10;
  551. }
  552. }
  553. }