Feed.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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_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. * @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_Feed
  40. extends Zend_Feed_Reader_Extension_FeedAbstract
  41. {
  42. /**
  43. * Get a single 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('//atom:author');
  67. $contributors = $this->_xpath->query('//atom:contributor');
  68. $people = array();
  69. if ($authors->length) {
  70. foreach ($authors as $author) {
  71. $author = $this->_getAuthor($author);
  72. if (!empty($author)) {
  73. $people[] = $author;
  74. }
  75. }
  76. }
  77. if ($contributors->length) {
  78. foreach ($contributors as $contributor) {
  79. $contributor = $this->_getAuthor($contributor);
  80. if (!empty($contributor)) {
  81. $people[] = $contributor;
  82. }
  83. }
  84. }
  85. if (empty($people)) {
  86. $people = null;
  87. } else {
  88. $people = array_unique($people);
  89. }
  90. $this->_data['authors'] = $people;
  91. return $this->_data['authors'];
  92. }
  93. /**
  94. * Get the copyright entry
  95. *
  96. * @return string|null
  97. */
  98. public function getCopyright()
  99. {
  100. if (array_key_exists('copyright', $this->_data)) {
  101. return $this->_data['copyright'];
  102. }
  103. $copyright = null;
  104. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  105. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:copyright)');
  106. } else {
  107. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:rights)');
  108. }
  109. if (!$copyright) {
  110. $copyright = null;
  111. }
  112. $this->_data['copyright'] = $copyright;
  113. return $this->_data['copyright'];
  114. }
  115. /**
  116. * Get the feed creation date
  117. *
  118. * @return Zend_Date|null
  119. */
  120. public function getDateCreated()
  121. {
  122. if (array_key_exists('datecreated', $this->_data)) {
  123. return $this->_data['datecreated'];
  124. }
  125. $date = null;
  126. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  127. $dateCreated = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
  128. } else {
  129. $dateCreated = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
  130. }
  131. if ($dateCreated) {
  132. $date = new Zend_Date;
  133. $date->set($dateCreated, Zend_Date::ISO_8601);
  134. }
  135. $this->_data['datecreated'] = $date;
  136. return $this->_data['datecreated'];
  137. }
  138. /**
  139. * Get the feed modification date
  140. *
  141. * @return Zend_Date|null
  142. */
  143. public function getDateModified()
  144. {
  145. if (array_key_exists('datemodified', $this->_data)) {
  146. return $this->_data['datemodified'];
  147. }
  148. $date = null;
  149. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  150. $dateModified = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
  151. } else {
  152. $dateModified = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
  153. }
  154. if ($dateModified) {
  155. $date = new Zend_Date;
  156. $date->set($dateModified, Zend_Date::ISO_8601);
  157. }
  158. $this->_data['datemodified'] = $date;
  159. return $this->_data['datemodified'];
  160. }
  161. /**
  162. * Get the feed description
  163. *
  164. * @return string|null
  165. */
  166. public function getDescription()
  167. {
  168. if (array_key_exists('description', $this->_data)) {
  169. return $this->_data['description'];
  170. }
  171. $description = null;
  172. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  173. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:tagline)'); // TODO: Is this the same as subtitle?
  174. } else {
  175. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:subtitle)');
  176. }
  177. if (!$description) {
  178. $description = null;
  179. }
  180. $this->_data['description'] = $description;
  181. return $this->_data['description'];
  182. }
  183. /**
  184. * Get the feed generator entry
  185. *
  186. * @return string|null
  187. */
  188. public function getGenerator()
  189. {
  190. if (array_key_exists('generator', $this->_data)) {
  191. return $this->_data['generator'];
  192. }
  193. // TODO: Add uri support
  194. $generator = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:generator)');
  195. if (!$generator) {
  196. $generator = null;
  197. } else {
  198. $generator = html_entity_decode($generator, ENT_QUOTES, $this->getEncoding());
  199. }
  200. $this->_data['generator'] = $generator;
  201. return $this->_data['generator'];
  202. }
  203. /**
  204. * Get the feed ID
  205. *
  206. * @return string|null
  207. */
  208. public function getId()
  209. {
  210. if (array_key_exists('id', $this->_data)) {
  211. return $this->_data['id'];
  212. }
  213. $id = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
  214. if (!$id) {
  215. if ($this->getLink()) {
  216. $id = $this->getLink();
  217. } elseif ($this->getTitle()) {
  218. $id = $this->getTitle();
  219. } else {
  220. $id = null;
  221. }
  222. }
  223. $this->_data['id'] = $id;
  224. return $this->_data['id'];
  225. }
  226. /**
  227. * Get the feed language
  228. *
  229. * @return string|null
  230. */
  231. public function getLanguage()
  232. {
  233. if (array_key_exists('language', $this->_data)) {
  234. return $this->_data['language'];
  235. }
  236. $language = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:lang)');
  237. if (!$language) {
  238. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  239. }
  240. if (!$language) {
  241. $language = null;
  242. }
  243. $this->_data['language'] = $language;
  244. return $this->_data['language'];
  245. }
  246. /**
  247. * Get the base URI of the feed (if set).
  248. *
  249. * @return string|null
  250. */
  251. public function getBaseUrl()
  252. {
  253. if (array_key_exists('baseUrl', $this->_data)) {
  254. return $this->_data['baseUrl'];
  255. }
  256. $baseUrl = $this->_xpath->evaluate('string(//@xml:base[1])');
  257. if (!$baseUrl) {
  258. $baseUrl = null;
  259. }
  260. $this->_data['baseUrl'] = $baseUrl;
  261. return $this->_data['baseUrl'];
  262. }
  263. /**
  264. * Get a link to the source website
  265. *
  266. * @return string|null
  267. */
  268. public function getLink()
  269. {
  270. if (array_key_exists('link', $this->_data)) {
  271. return $this->_data['link'];
  272. }
  273. $link = null;
  274. $list = $this->_xpath->query(
  275. $this->getXpathPrefix() . '/atom:link[@rel="alternate"]/@href' . '|' .
  276. $this->getXpathPrefix() . '/atom:link[not(@rel)]/@href'
  277. );
  278. if ($list->length) {
  279. $link = $list->item(0)->nodeValue;
  280. $link = $this->_absolutiseUri($link);
  281. }
  282. $this->_data['link'] = $link;
  283. return $this->_data['link'];
  284. }
  285. /**
  286. * Get a link to the feed's XML Url
  287. *
  288. * @return string|null
  289. */
  290. public function getFeedLink()
  291. {
  292. if (array_key_exists('feedlink', $this->_data)) {
  293. return $this->_data['feedlink'];
  294. }
  295. $link = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:link[@rel="self"]/@href)');
  296. $link = $this->_absolutiseUri($link);
  297. $this->_data['feedlink'] = $link;
  298. return $this->_data['feedlink'];
  299. }
  300. /**
  301. * Get an array of any supported Pusubhubbub endpoints
  302. *
  303. * @return array|null
  304. */
  305. public function getHubs()
  306. {
  307. if (array_key_exists('hubs', $this->_data)) {
  308. return $this->_data['hubs'];
  309. }
  310. $hubs = array();
  311. $list = $this->_xpath->query($this->getXpathPrefix()
  312. . '//atom:link[@rel="hub"]/@href');
  313. if ($list->length) {
  314. foreach ($list as $uri) {
  315. $hubs[] = $this->_absolutiseUri($uri->nodeValue);
  316. }
  317. } else {
  318. $hubs = null;
  319. }
  320. $this->_data['hubs'] = $hubs;
  321. return $this->_data['hubs'];
  322. }
  323. /**
  324. * Get the feed title
  325. *
  326. * @return string|null
  327. */
  328. public function getTitle()
  329. {
  330. if (array_key_exists('title', $this->_data)) {
  331. return $this->_data['title'];
  332. }
  333. $title = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
  334. if (!$title) {
  335. $title = null;
  336. }
  337. $this->_data['title'] = $title;
  338. return $this->_data['title'];
  339. }
  340. /**
  341. * Get an author entry in RSS format
  342. *
  343. * @param DOMElement $element
  344. * @return string
  345. */
  346. protected function _getAuthor(DOMElement $element)
  347. {
  348. $email = null;
  349. $name = null;
  350. $uri = null;
  351. $emailNode = $element->getElementsByTagName('email');
  352. $nameNode = $element->getElementsByTagName('name');
  353. $uriNode = $element->getElementsByTagName('uri');
  354. if ($emailNode->length) {
  355. $email = $emailNode->item(0)->nodeValue;
  356. }
  357. if ($nameNode->length) {
  358. $name = $nameNode->item(0)->nodeValue;
  359. }
  360. if ($uriNode->length) {
  361. $uri = $uriNode->item(0)->nodeValue;
  362. }
  363. if (!empty($email)) {
  364. return $email . (empty($name) ? '' : ' (' . $name . ')');
  365. } else if (!empty($name)) {
  366. return $name;
  367. } else if (!empty($uri)) {
  368. return $uri;
  369. }
  370. return null;
  371. }
  372. /**
  373. * Attempt to absolutise the URI, i.e. if a relative URI apply the
  374. * xml:base value as a prefix to turn into an absolute URI.
  375. */
  376. protected function _absolutiseUri($link)
  377. {
  378. if (!Zend_Uri::check($link)) {
  379. if (!is_null($this->getBaseUrl())) {
  380. $link = $this->getBaseUrl() . $link;
  381. if (!Zend_Uri::check($link)) {
  382. $link = null;
  383. }
  384. }
  385. }
  386. return $link;
  387. }
  388. /**
  389. * Register the default namespaces for the current feed format
  390. */
  391. protected function _registerNamespaces()
  392. {
  393. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10
  394. || $this->getType() == Zend_Feed_Reader::TYPE_ATOM_03
  395. ) {
  396. return; // pre-registered at Feed level
  397. }
  398. $atomDetected = $this->_getAtomType();
  399. switch ($atomDetected) {
  400. case Zend_Feed_Reader::TYPE_ATOM_03:
  401. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
  402. break;
  403. default:
  404. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  405. break;
  406. }
  407. }
  408. /**
  409. * Detect the presence of any Atom namespaces in use
  410. */
  411. protected function _getAtomType()
  412. {
  413. $nslist = $this->getDomDocument()->documentElement->attributes;
  414. if (!$nslist->length) {
  415. return null;
  416. }
  417. foreach ($nslist as $ns) {
  418. if ($ns->value == Zend_Feed_Reader::NAMESPACE_ATOM_10) {
  419. return Zend_Feed_Reader::TYPE_ATOM_10;
  420. }
  421. if ($ns->value == Zend_Feed_Reader::NAMESPACE_ATOM_03) {
  422. return Zend_Feed_Reader::TYPE_ATOM_03;
  423. }
  424. }
  425. }
  426. }