Entry.php 16 KB

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