Entry.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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_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-2010 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($d->saveXML());
  137. break;
  138. }
  139. }
  140. if (!$content) {
  141. $content = $this->getDescription();
  142. }
  143. $this->_data['content'] = trim($content);
  144. return $this->_data['content'];
  145. }
  146. /**
  147. * Parse out XHTML to remove the namespacing
  148. */
  149. protected function _collectXhtml($xhtml)
  150. {
  151. $matches = array(
  152. "/<\?xml[^<]+/",
  153. "/<div.*xmlns=[^<]+/",
  154. "/<\/div>\s*$/"
  155. );
  156. $cleaned = preg_replace($matches, '', $xhtml);
  157. return $cleaned;
  158. }
  159. /**
  160. * Get the entry creation date
  161. *
  162. * @return string
  163. */
  164. public function getDateCreated()
  165. {
  166. if (array_key_exists('datecreated', $this->_data)) {
  167. return $this->_data['datecreated'];
  168. }
  169. $date = null;
  170. if ($this->_getAtomType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  171. $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
  172. } else {
  173. $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
  174. }
  175. if ($dateCreated) {
  176. $date = new Zend_Date;
  177. $date->set($dateCreated, Zend_Date::ISO_8601);
  178. }
  179. $this->_data['datecreated'] = $date;
  180. return $this->_data['datecreated'];
  181. }
  182. /**
  183. * Get the entry modification date
  184. *
  185. * @return string
  186. */
  187. public function getDateModified()
  188. {
  189. if (array_key_exists('datemodified', $this->_data)) {
  190. return $this->_data['datemodified'];
  191. }
  192. $date = null;
  193. if ($this->_getAtomType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  194. $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
  195. } else {
  196. $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
  197. }
  198. if ($dateModified) {
  199. $date = new Zend_Date;
  200. $date->set($dateModified, Zend_Date::ISO_8601);
  201. }
  202. $this->_data['datemodified'] = $date;
  203. return $this->_data['datemodified'];
  204. }
  205. /**
  206. * Get the entry description
  207. *
  208. * @return string
  209. */
  210. public function getDescription()
  211. {
  212. if (array_key_exists('description', $this->_data)) {
  213. return $this->_data['description'];
  214. }
  215. $description = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:summary)');
  216. if (!$description) {
  217. $description = null;
  218. } else {
  219. $description = html_entity_decode($description, ENT_QUOTES, $this->getEncoding());
  220. }
  221. $this->_data['description'] = $description;
  222. return $this->_data['description'];
  223. }
  224. /**
  225. * Get the entry enclosure
  226. *
  227. * @return string
  228. */
  229. public function getEnclosure()
  230. {
  231. if (array_key_exists('enclosure', $this->_data)) {
  232. return $this->_data['enclosure'];
  233. }
  234. $enclosure = null;
  235. $nodeList = $this->getXpath()->query($this->getXpathPrefix() . '/atom:link[@rel="enclosure"]');
  236. if ($nodeList->length > 0) {
  237. $enclosure = new stdClass();
  238. $enclosure->url = $nodeList->item(0)->getAttribute('href');
  239. $enclosure->length = $nodeList->item(0)->getAttribute('length');
  240. $enclosure->type = $nodeList->item(0)->getAttribute('type');
  241. }
  242. $this->_data['enclosure'] = $enclosure;
  243. return $this->_data['enclosure'];
  244. }
  245. /**
  246. * Get the entry ID
  247. *
  248. * @return string
  249. */
  250. public function getId()
  251. {
  252. if (array_key_exists('id', $this->_data)) {
  253. return $this->_data['id'];
  254. }
  255. $id = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
  256. if (!$id) {
  257. if ($this->getPermalink()) {
  258. $id = $this->getPermalink();
  259. } elseif ($this->getTitle()) {
  260. $id = $this->getTitle();
  261. } else {
  262. $id = null;
  263. }
  264. }
  265. $this->_data['id'] = $id;
  266. return $this->_data['id'];
  267. }
  268. /**
  269. * Get the base URI of the feed (if set).
  270. *
  271. * @return string|null
  272. */
  273. public function getBaseUrl()
  274. {
  275. if (array_key_exists('baseUrl', $this->_data)) {
  276. return $this->_data['baseUrl'];
  277. }
  278. $baseUrl = $this->getXpath()->evaluate('string('
  279. . $this->getXpathPrefix() . '/@xml:base[1]'
  280. . ')');
  281. if (!$baseUrl) {
  282. $baseUrl = $this->getXpath()->evaluate('string(//@xml:base[1])');
  283. }
  284. if (!$baseUrl) {
  285. $baseUrl = null;
  286. }
  287. $this->_data['baseUrl'] = $baseUrl;
  288. return $this->_data['baseUrl'];
  289. }
  290. /**
  291. * Get a specific link
  292. *
  293. * @param int $index
  294. * @return string
  295. */
  296. public function getLink($index = 0)
  297. {
  298. if (!array_key_exists('links', $this->_data)) {
  299. $this->getLinks();
  300. }
  301. if (isset($this->_data['links'][$index])) {
  302. return $this->_data['links'][$index];
  303. }
  304. return null;
  305. }
  306. /**
  307. * Get all links
  308. *
  309. * @return array
  310. */
  311. public function getLinks()
  312. {
  313. if (array_key_exists('links', $this->_data)) {
  314. return $this->_data['links'];
  315. }
  316. $links = array();
  317. $list = $this->getXpath()->query(
  318. $this->getXpathPrefix() . '//atom:link[@rel="alternate"]/@href' . '|' .
  319. $this->getXpathPrefix() . '//atom:link[not(@rel)]/@href'
  320. );
  321. if ($list->length) {
  322. foreach ($list as $link) {
  323. $links[] = $this->_absolutiseUri($link->value);
  324. }
  325. }
  326. $this->_data['links'] = $links;
  327. return $this->_data['links'];
  328. }
  329. /**
  330. * Get a permalink to the entry
  331. *
  332. * @return string
  333. */
  334. public function getPermalink()
  335. {
  336. return $this->getLink(0);
  337. }
  338. /**
  339. * Get the entry title
  340. *
  341. * @return string
  342. */
  343. public function getTitle()
  344. {
  345. if (array_key_exists('title', $this->_data)) {
  346. return $this->_data['title'];
  347. }
  348. $title = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
  349. if (!$title) {
  350. $title = null;
  351. } else {
  352. $title = html_entity_decode($title, ENT_QUOTES, $this->getEncoding());
  353. }
  354. $this->_data['title'] = $title;
  355. return $this->_data['title'];
  356. }
  357. /**
  358. * Get the number of comments/replies for current entry
  359. *
  360. * @return integer
  361. */
  362. public function getCommentCount()
  363. {
  364. if (array_key_exists('commentcount', $this->_data)) {
  365. return $this->_data['commentcount'];
  366. }
  367. $count = null;
  368. $this->getXpath()->registerNamespace('thread10', 'http://purl.org/syndication/thread/1.0');
  369. $list = $this->getXpath()->query(
  370. $this->getXpathPrefix() . '//atom:link[@rel="replies"]/@thread10:count'
  371. );
  372. if ($list->length) {
  373. $count = $list->item(0)->value;
  374. }
  375. $this->_data['commentcount'] = $count;
  376. return $this->_data['commentcount'];
  377. }
  378. /**
  379. * Returns a URI pointing to the HTML page where comments can be made on this entry
  380. *
  381. * @return string
  382. */
  383. public function getCommentLink()
  384. {
  385. if (array_key_exists('commentlink', $this->_data)) {
  386. return $this->_data['commentlink'];
  387. }
  388. $link = null;
  389. $list = $this->getXpath()->query(
  390. $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="text/html"]/@href'
  391. );
  392. if ($list->length) {
  393. $link = $list->item(0)->value;
  394. $link = $this->_absolutiseUri($link);
  395. }
  396. $this->_data['commentlink'] = $link;
  397. return $this->_data['commentlink'];
  398. }
  399. /**
  400. * Returns a URI pointing to a feed of all comments for this entry
  401. *
  402. * @return string
  403. */
  404. public function getCommentFeedLink($type = 'atom')
  405. {
  406. if (array_key_exists('commentfeedlink', $this->_data)) {
  407. return $this->_data['commentfeedlink'];
  408. }
  409. $link = null;
  410. $list = $this->getXpath()->query(
  411. $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="application/'.$type.'+xml"]/@href'
  412. );
  413. if ($list->length) {
  414. $link = $list->item(0)->value;
  415. $link = $this->_absolutiseUri($link);
  416. }
  417. $this->_data['commentfeedlink'] = $link;
  418. return $this->_data['commentfeedlink'];
  419. }
  420. /**
  421. * Get all categories
  422. *
  423. * @return Zend_Feed_Reader_Collection_Category
  424. */
  425. public function getCategories()
  426. {
  427. if (array_key_exists('categories', $this->_data)) {
  428. return $this->_data['categories'];
  429. }
  430. if ($this->_getAtomType() == Zend_Feed_Reader::TYPE_ATOM_10) {
  431. $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:category');
  432. } else {
  433. /**
  434. * Since Atom 0.3 did not support categories, it would have used the
  435. * Dublin Core extension. However there is a small possibility Atom 0.3
  436. * may have been retrofittied to use Atom 1.0 instead.
  437. */
  438. $this->getXpath()->registerNamespace('atom10', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  439. $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom10:category');
  440. }
  441. if ($list->length) {
  442. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  443. foreach ($list as $category) {
  444. $categoryCollection[] = array(
  445. 'term' => $category->getAttribute('term'),
  446. 'scheme' => $category->getAttribute('scheme'),
  447. 'label' => html_entity_decode($category->getAttribute('label'))
  448. );
  449. }
  450. } else {
  451. return new Zend_Feed_Reader_Collection_Category;
  452. }
  453. $this->_data['categories'] = $categoryCollection;
  454. return $this->_data['categories'];
  455. }
  456. /**
  457. * Get source feed metadata from the entry
  458. *
  459. * @return Zend_Feed_Reader_Feed_Atom_Source|null
  460. */
  461. public function getSource()
  462. {
  463. if (array_key_exists('source', $this->_data)) {
  464. return $this->_data['source'];
  465. }
  466. $source = null;
  467. // TODO: Investigate why _getAtomType() fails here. Is it even needed?
  468. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10) {
  469. $list = $this->getXpath()->query($this->getXpathPrefix() . '/atom:source[1]');
  470. if ($list->length) {
  471. $element = $list->item(0);
  472. $source = new Zend_Feed_Reader_Feed_Atom_Source($element, $this->getXpathPrefix());
  473. }
  474. }
  475. $this->_data['source'] = $source;
  476. return $this->_data['source'];
  477. }
  478. /**
  479. * Attempt to absolutise the URI, i.e. if a relative URI apply the
  480. * xml:base value as a prefix to turn into an absolute URI.
  481. */
  482. protected function _absolutiseUri($link)
  483. {
  484. if (!Zend_Uri::check($link)) {
  485. if (!is_null($this->getBaseUrl())) {
  486. $link = $this->getBaseUrl() . $link;
  487. if (!Zend_Uri::check($link)) {
  488. $link = null;
  489. }
  490. }
  491. }
  492. return $link;
  493. }
  494. /**
  495. * Get an author entry
  496. *
  497. * @param DOMElement $element
  498. * @return string
  499. */
  500. protected function _getAuthor(DOMElement $element)
  501. {
  502. $author = array();
  503. $emailNode = $element->getElementsByTagName('email');
  504. $nameNode = $element->getElementsByTagName('name');
  505. $uriNode = $element->getElementsByTagName('uri');
  506. if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
  507. $author['email'] = $emailNode->item(0)->nodeValue;
  508. }
  509. if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
  510. $author['name'] = $nameNode->item(0)->nodeValue;
  511. }
  512. if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
  513. $author['uri'] = $uriNode->item(0)->nodeValue;
  514. }
  515. if (empty($author)) {
  516. return null;
  517. }
  518. return $author;
  519. }
  520. /**
  521. * Register the default namespaces for the current feed format
  522. */
  523. protected function _registerNamespaces()
  524. {
  525. switch ($this->_getAtomType()) {
  526. case Zend_Feed_Reader::TYPE_ATOM_03:
  527. $this->getXpath()->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
  528. break;
  529. default:
  530. $this->getXpath()->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  531. break;
  532. }
  533. }
  534. /**
  535. * Detect the presence of any Atom namespaces in use
  536. */
  537. protected function _getAtomType()
  538. {
  539. $dom = $this->getDomDocument();
  540. $prefixAtom03 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_03);
  541. $prefixAtom10 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_10);
  542. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_03)
  543. || !empty($prefixAtom03)) {
  544. return Zend_Feed_Reader::TYPE_ATOM_03;
  545. }
  546. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_10)
  547. || !empty($prefixAtom10)) {
  548. return Zend_Feed_Reader::TYPE_ATOM_10;
  549. }
  550. }
  551. }