Entry.php 14 KB

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