Entry.php 18 KB

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