Feed.php 14 KB

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