Entry.php 13 KB

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