PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Feed/Reader/Extension/Atom/Entry.php

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