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

/src/application/libraries/Zend/Feed/Reader/Extension/Atom/Feed.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 590 lines | 343 code | 102 blank | 145 comment | 73 complexity | 98401411a2bd92362a4f9e1ecb135038 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Feed.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Feed_Reader_Extension_FeedAbstract
  23. */
  24. require_once 'Zend/Feed/Reader/Extension/FeedAbstract.php';
  25. /**
  26. * @see Zend_Date
  27. */
  28. require_once 'Zend/Date.php';
  29. /**
  30. * @see Zend_Uri
  31. */
  32. require_once 'Zend/Uri.php';
  33. /**
  34. * @see Zend_Feed_Reader_Collection_Author
  35. */
  36. require_once 'Zend/Feed/Reader/Collection/Author.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Feed_Reader
  40. * @copyright Copyright (c) 2005-2011 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_Feed
  44. extends Zend_Feed_Reader_Extension_FeedAbstract
  45. {
  46. /**
  47. * Get a single 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. $list = $this->_xpath->query('//atom:author');
  71. $authors = array();
  72. if ($list->length) {
  73. foreach ($list as $author) {
  74. $author = $this->_getAuthor($author);
  75. if (!empty($author)) {
  76. $authors[] = $author;
  77. }
  78. }
  79. }
  80. if (count($authors) == 0) {
  81. $authors = null;
  82. } else {
  83. $authors = new Zend_Feed_Reader_Collection_Author(
  84. Zend_Feed_Reader::arrayUnique($authors)
  85. );
  86. }
  87. $this->_data['authors'] = $authors;
  88. return $this->_data['authors'];
  89. }
  90. /**
  91. * Get the copyright entry
  92. *
  93. * @return string|null
  94. */
  95. public function getCopyright()
  96. {
  97. if (array_key_exists('copyright', $this->_data)) {
  98. return $this->_data['copyright'];
  99. }
  100. $copyright = null;
  101. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  102. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:copyright)');
  103. } else {
  104. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:rights)');
  105. }
  106. if (!$copyright) {
  107. $copyright = null;
  108. }
  109. $this->_data['copyright'] = $copyright;
  110. return $this->_data['copyright'];
  111. }
  112. /**
  113. * Get the feed creation date
  114. *
  115. * @return Zend_Date|null
  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 feed modification date
  137. *
  138. * @return Zend_Date|null
  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 feed description
  160. *
  161. * @return string|null
  162. */
  163. public function getDescription()
  164. {
  165. if (array_key_exists('description', $this->_data)) {
  166. return $this->_data['description'];
  167. }
  168. $description = null;
  169. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  170. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:tagline)'); // TODO: Is this the same as subtitle?
  171. } else {
  172. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:subtitle)');
  173. }
  174. if (!$description) {
  175. $description = null;
  176. }
  177. $this->_data['description'] = $description;
  178. return $this->_data['description'];
  179. }
  180. /**
  181. * Get the feed generator entry
  182. *
  183. * @return string|null
  184. */
  185. public function getGenerator()
  186. {
  187. if (array_key_exists('generator', $this->_data)) {
  188. return $this->_data['generator'];
  189. }
  190. // TODO: Add uri support
  191. $generator = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:generator)');
  192. if (!$generator) {
  193. $generator = null;
  194. }
  195. $this->_data['generator'] = $generator;
  196. return $this->_data['generator'];
  197. }
  198. /**
  199. * Get the feed ID
  200. *
  201. * @return string|null
  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->getLink()) {
  211. $id = $this->getLink();
  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 feed language
  223. *
  224. * @return string|null
  225. */
  226. public function getLanguage()
  227. {
  228. if (array_key_exists('language', $this->_data)) {
  229. return $this->_data['language'];
  230. }
  231. $language = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:lang)');
  232. if (!$language) {
  233. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  234. }
  235. if (!$language) {
  236. $language = null;
  237. }
  238. $this->_data['language'] = $language;
  239. return $this->_data['language'];
  240. }
  241. /**
  242. * Get the feed image
  243. *
  244. * @return array|null
  245. */
  246. public function getImage()
  247. {
  248. if (array_key_exists('image', $this->_data)) {
  249. return $this->_data['image'];
  250. }
  251. $imageUrl = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:logo)');
  252. if (!$imageUrl) {
  253. $image = null;
  254. } else {
  255. $image = array('uri'=>$imageUrl);
  256. }
  257. $this->_data['image'] = $image;
  258. return $this->_data['image'];
  259. }
  260. /**
  261. * Get the feed image
  262. *
  263. * @return array|null
  264. */
  265. public function getIcon()
  266. {
  267. if (array_key_exists('icon', $this->_data)) {
  268. return $this->_data['icon'];
  269. }
  270. $imageUrl = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:icon)');
  271. if (!$imageUrl) {
  272. $image = null;
  273. } else {
  274. $image = array('uri'=>$imageUrl);
  275. }
  276. $this->_data['icon'] = $image;
  277. return $this->_data['icon'];
  278. }
  279. /**
  280. * Get the base URI of the feed (if set).
  281. *
  282. * @return string|null
  283. */
  284. public function getBaseUrl()
  285. {
  286. if (array_key_exists('baseUrl', $this->_data)) {
  287. return $this->_data['baseUrl'];
  288. }
  289. $baseUrl = $this->_xpath->evaluate('string(//@xml:base[1])');
  290. if (!$baseUrl) {
  291. $baseUrl = null;
  292. }
  293. $this->_data['baseUrl'] = $baseUrl;
  294. return $this->_data['baseUrl'];
  295. }
  296. /**
  297. * Get a link to the source website
  298. *
  299. * @return string|null
  300. */
  301. public function getLink()
  302. {
  303. if (array_key_exists('link', $this->_data)) {
  304. return $this->_data['link'];
  305. }
  306. $link = null;
  307. $list = $this->_xpath->query(
  308. $this->getXpathPrefix() . '/atom:link[@rel="alternate"]/@href' . '|' .
  309. $this->getXpathPrefix() . '/atom:link[not(@rel)]/@href'
  310. );
  311. if ($list->length) {
  312. $link = $list->item(0)->nodeValue;
  313. $link = $this->_absolutiseUri($link);
  314. }
  315. $this->_data['link'] = $link;
  316. return $this->_data['link'];
  317. }
  318. /**
  319. * Get a link to the feed's XML Url
  320. *
  321. * @return string|null
  322. */
  323. public function getFeedLink()
  324. {
  325. if (array_key_exists('feedlink', $this->_data)) {
  326. return $this->_data['feedlink'];
  327. }
  328. $link = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:link[@rel="self"]/@href)');
  329. $link = $this->_absolutiseUri($link);
  330. $this->_data['feedlink'] = $link;
  331. return $this->_data['feedlink'];
  332. }
  333. /**
  334. * Get an array of any supported Pusubhubbub endpoints
  335. *
  336. * @return array|null
  337. */
  338. public function getHubs()
  339. {
  340. if (array_key_exists('hubs', $this->_data)) {
  341. return $this->_data['hubs'];
  342. }
  343. $hubs = array();
  344. $list = $this->_xpath->query($this->getXpathPrefix()
  345. . '//atom:link[@rel="hub"]/@href');
  346. if ($list->length) {
  347. foreach ($list as $uri) {
  348. $hubs[] = $this->_absolutiseUri($uri->nodeValue);
  349. }
  350. } else {
  351. $hubs = null;
  352. }
  353. $this->_data['hubs'] = $hubs;
  354. return $this->_data['hubs'];
  355. }
  356. /**
  357. * Get the feed title
  358. *
  359. * @return string|null
  360. */
  361. public function getTitle()
  362. {
  363. if (array_key_exists('title', $this->_data)) {
  364. return $this->_data['title'];
  365. }
  366. $title = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
  367. if (!$title) {
  368. $title = null;
  369. }
  370. $this->_data['title'] = $title;
  371. return $this->_data['title'];
  372. }
  373. /**
  374. * Get all categories
  375. *
  376. * @return Zend_Feed_Reader_Collection_Category
  377. */
  378. public function getCategories()
  379. {
  380. if (array_key_exists('categories', $this->_data)) {
  381. return $this->_data['categories'];
  382. }
  383. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10) {
  384. $list = $this->_xpath->query($this->getXpathPrefix() . '/atom:category');
  385. } else {
  386. /**
  387. * Since Atom 0.3 did not support categories, it would have used the
  388. * Dublin Core extension. However there is a small possibility Atom 0.3
  389. * may have been retrofittied to use Atom 1.0 instead.
  390. */
  391. $this->_xpath->registerNamespace('atom10', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  392. $list = $this->_xpath->query($this->getXpathPrefix() . '/atom10:category');
  393. }
  394. if ($list->length) {
  395. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  396. foreach ($list as $category) {
  397. $categoryCollection[] = array(
  398. 'term' => $category->getAttribute('term'),
  399. 'scheme' => $category->getAttribute('scheme'),
  400. 'label' => $category->getAttribute('label')
  401. );
  402. }
  403. } else {
  404. return new Zend_Feed_Reader_Collection_Category;
  405. }
  406. $this->_data['categories'] = $categoryCollection;
  407. return $this->_data['categories'];
  408. }
  409. /**
  410. * Get an author entry in RSS format
  411. *
  412. * @param DOMElement $element
  413. * @return string
  414. */
  415. protected function _getAuthor(DOMElement $element)
  416. {
  417. $author = array();
  418. $emailNode = $element->getElementsByTagName('email');
  419. $nameNode = $element->getElementsByTagName('name');
  420. $uriNode = $element->getElementsByTagName('uri');
  421. if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
  422. $author['email'] = $emailNode->item(0)->nodeValue;
  423. }
  424. if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
  425. $author['name'] = $nameNode->item(0)->nodeValue;
  426. }
  427. if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
  428. $author['uri'] = $uriNode->item(0)->nodeValue;
  429. }
  430. if (empty($author)) {
  431. return null;
  432. }
  433. return $author;
  434. }
  435. /**
  436. * Attempt to absolutise the URI, i.e. if a relative URI apply the
  437. * xml:base value as a prefix to turn into an absolute URI.
  438. */
  439. protected function _absolutiseUri($link)
  440. {
  441. if (!Zend_Uri::check($link)) {
  442. if ($this->getBaseUrl() !== null) {
  443. $link = $this->getBaseUrl() . $link;
  444. if (!Zend_Uri::check($link)) {
  445. $link = null;
  446. }
  447. }
  448. }
  449. return $link;
  450. }
  451. /**
  452. * Register the default namespaces for the current feed format
  453. */
  454. protected function _registerNamespaces()
  455. {
  456. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10
  457. || $this->getType() == Zend_Feed_Reader::TYPE_ATOM_03
  458. ) {
  459. return; // pre-registered at Feed level
  460. }
  461. $atomDetected = $this->_getAtomType();
  462. switch ($atomDetected) {
  463. case Zend_Feed_Reader::TYPE_ATOM_03:
  464. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
  465. break;
  466. default:
  467. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  468. break;
  469. }
  470. }
  471. /**
  472. * Detect the presence of any Atom namespaces in use
  473. */
  474. protected function _getAtomType()
  475. {
  476. $dom = $this->getDomDocument();
  477. $prefixAtom03 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_03);
  478. $prefixAtom10 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_10);
  479. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_10)
  480. || !empty($prefixAtom10)) {
  481. return Zend_Feed_Reader::TYPE_ATOM_10;
  482. }
  483. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_03)
  484. || !empty($prefixAtom03)) {
  485. return Zend_Feed_Reader::TYPE_ATOM_03;
  486. }
  487. }
  488. }