Feed.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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$
  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-2010 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. } else {
  195. $generator = html_entity_decode($generator, ENT_QUOTES, $this->getEncoding());
  196. }
  197. $this->_data['generator'] = $generator;
  198. return $this->_data['generator'];
  199. }
  200. /**
  201. * Get the feed ID
  202. *
  203. * @return string|null
  204. */
  205. public function getId()
  206. {
  207. if (array_key_exists('id', $this->_data)) {
  208. return $this->_data['id'];
  209. }
  210. $id = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
  211. if (!$id) {
  212. if ($this->getLink()) {
  213. $id = $this->getLink();
  214. } elseif ($this->getTitle()) {
  215. $id = $this->getTitle();
  216. } else {
  217. $id = null;
  218. }
  219. }
  220. $this->_data['id'] = $id;
  221. return $this->_data['id'];
  222. }
  223. /**
  224. * Get the feed language
  225. *
  226. * @return string|null
  227. */
  228. public function getLanguage()
  229. {
  230. if (array_key_exists('language', $this->_data)) {
  231. return $this->_data['language'];
  232. }
  233. $language = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:lang)');
  234. if (!$language) {
  235. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  236. }
  237. if (!$language) {
  238. $language = null;
  239. }
  240. $this->_data['language'] = $language;
  241. return $this->_data['language'];
  242. }
  243. /**
  244. * Get the feed image
  245. *
  246. * @return array|null
  247. */
  248. public function getImage()
  249. {
  250. if (array_key_exists('image', $this->_data)) {
  251. return $this->_data['image'];
  252. }
  253. $imageUrl = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:logo)');
  254. if (!$imageUrl) {
  255. $image = null;
  256. } else {
  257. $image = array('uri'=>$imageUrl);
  258. }
  259. $this->_data['image'] = $image;
  260. return $this->_data['image'];
  261. }
  262. /**
  263. * Get the base URI of the feed (if set).
  264. *
  265. * @return string|null
  266. */
  267. public function getBaseUrl()
  268. {
  269. if (array_key_exists('baseUrl', $this->_data)) {
  270. return $this->_data['baseUrl'];
  271. }
  272. $baseUrl = $this->_xpath->evaluate('string(//@xml:base[1])');
  273. if (!$baseUrl) {
  274. $baseUrl = null;
  275. }
  276. $this->_data['baseUrl'] = $baseUrl;
  277. return $this->_data['baseUrl'];
  278. }
  279. /**
  280. * Get a link to the source website
  281. *
  282. * @return string|null
  283. */
  284. public function getLink()
  285. {
  286. if (array_key_exists('link', $this->_data)) {
  287. return $this->_data['link'];
  288. }
  289. $link = null;
  290. $list = $this->_xpath->query(
  291. $this->getXpathPrefix() . '/atom:link[@rel="alternate"]/@href' . '|' .
  292. $this->getXpathPrefix() . '/atom:link[not(@rel)]/@href'
  293. );
  294. if ($list->length) {
  295. $link = $list->item(0)->nodeValue;
  296. $link = $this->_absolutiseUri($link);
  297. }
  298. $this->_data['link'] = $link;
  299. return $this->_data['link'];
  300. }
  301. /**
  302. * Get a link to the feed's XML Url
  303. *
  304. * @return string|null
  305. */
  306. public function getFeedLink()
  307. {
  308. if (array_key_exists('feedlink', $this->_data)) {
  309. return $this->_data['feedlink'];
  310. }
  311. $link = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:link[@rel="self"]/@href)');
  312. $link = $this->_absolutiseUri($link);
  313. $this->_data['feedlink'] = $link;
  314. return $this->_data['feedlink'];
  315. }
  316. /**
  317. * Get an array of any supported Pusubhubbub endpoints
  318. *
  319. * @return array|null
  320. */
  321. public function getHubs()
  322. {
  323. if (array_key_exists('hubs', $this->_data)) {
  324. return $this->_data['hubs'];
  325. }
  326. $hubs = array();
  327. $list = $this->_xpath->query($this->getXpathPrefix()
  328. . '//atom:link[@rel="hub"]/@href');
  329. if ($list->length) {
  330. foreach ($list as $uri) {
  331. $hubs[] = $this->_absolutiseUri($uri->nodeValue);
  332. }
  333. } else {
  334. $hubs = null;
  335. }
  336. $this->_data['hubs'] = $hubs;
  337. return $this->_data['hubs'];
  338. }
  339. /**
  340. * Get the feed title
  341. *
  342. * @return string|null
  343. */
  344. public function getTitle()
  345. {
  346. if (array_key_exists('title', $this->_data)) {
  347. return $this->_data['title'];
  348. }
  349. $title = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
  350. if (!$title) {
  351. $title = null;
  352. }
  353. $this->_data['title'] = $title;
  354. return $this->_data['title'];
  355. }
  356. /**
  357. * Get all categories
  358. *
  359. * @return Zend_Feed_Reader_Collection_Category
  360. */
  361. public function getCategories()
  362. {
  363. if (array_key_exists('categories', $this->_data)) {
  364. return $this->_data['categories'];
  365. }
  366. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10) {
  367. $list = $this->_xpath->query($this->getXpathPrefix() . '/atom:category');
  368. } else {
  369. /**
  370. * Since Atom 0.3 did not support categories, it would have used the
  371. * Dublin Core extension. However there is a small possibility Atom 0.3
  372. * may have been retrofittied to use Atom 1.0 instead.
  373. */
  374. $this->_xpath->registerNamespace('atom10', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  375. $list = $this->_xpath->query($this->getXpathPrefix() . '/atom10:category');
  376. }
  377. if ($list->length) {
  378. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  379. foreach ($list as $category) {
  380. $categoryCollection[] = array(
  381. 'term' => $category->getAttribute('term'),
  382. 'scheme' => $category->getAttribute('scheme'),
  383. 'label' => html_entity_decode($category->getAttribute('label'))
  384. );
  385. }
  386. } else {
  387. return new Zend_Feed_Reader_Collection_Category;
  388. }
  389. $this->_data['categories'] = $categoryCollection;
  390. return $this->_data['categories'];
  391. }
  392. /**
  393. * Get an author entry in RSS format
  394. *
  395. * @param DOMElement $element
  396. * @return string
  397. */
  398. protected function _getAuthor(DOMElement $element)
  399. {
  400. $author = array();
  401. $emailNode = $element->getElementsByTagName('email');
  402. $nameNode = $element->getElementsByTagName('name');
  403. $uriNode = $element->getElementsByTagName('uri');
  404. if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
  405. $author['email'] = $emailNode->item(0)->nodeValue;
  406. }
  407. if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
  408. $author['name'] = $nameNode->item(0)->nodeValue;
  409. }
  410. if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
  411. $author['uri'] = $uriNode->item(0)->nodeValue;
  412. }
  413. if (empty($author)) {
  414. return null;
  415. }
  416. return $author;
  417. }
  418. /**
  419. * Attempt to absolutise the URI, i.e. if a relative URI apply the
  420. * xml:base value as a prefix to turn into an absolute URI.
  421. */
  422. protected function _absolutiseUri($link)
  423. {
  424. if (!Zend_Uri::check($link)) {
  425. if (!is_null($this->getBaseUrl())) {
  426. $link = $this->getBaseUrl() . $link;
  427. if (!Zend_Uri::check($link)) {
  428. $link = null;
  429. }
  430. }
  431. }
  432. return $link;
  433. }
  434. /**
  435. * Register the default namespaces for the current feed format
  436. */
  437. protected function _registerNamespaces()
  438. {
  439. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10
  440. || $this->getType() == Zend_Feed_Reader::TYPE_ATOM_03
  441. ) {
  442. return; // pre-registered at Feed level
  443. }
  444. $atomDetected = $this->_getAtomType();
  445. switch ($atomDetected) {
  446. case Zend_Feed_Reader::TYPE_ATOM_03:
  447. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
  448. break;
  449. default:
  450. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  451. break;
  452. }
  453. }
  454. /**
  455. * Detect the presence of any Atom namespaces in use
  456. */
  457. protected function _getAtomType()
  458. {
  459. $dom = $this->getDomDocument();
  460. $prefixAtom03 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_03);
  461. $prefixAtom10 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_10);
  462. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_10)
  463. || !empty($prefixAtom10)) {
  464. return Zend_Feed_Reader::TYPE_ATOM_10;
  465. }
  466. if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_03)
  467. || !empty($prefixAtom03)) {
  468. return Zend_Feed_Reader::TYPE_ATOM_03;
  469. }
  470. }
  471. }