Entry.php 16 KB

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