Entry.php 17 KB

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