Feed.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. * @category Zend
  31. * @package Zend_Feed_Reader
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Feed_Reader_Extension_Atom_Feed
  36. extends Zend_Feed_Reader_Extension_FeedAbstract
  37. {
  38. /**
  39. * Get a single author
  40. *
  41. * @param int $index
  42. * @return string|null
  43. */
  44. public function getAuthor($index = 0)
  45. {
  46. $authors = $this->getAuthors();
  47. if (isset($authors[$index])) {
  48. return $authors[$index];
  49. }
  50. return null;
  51. }
  52. /**
  53. * Get an array with feed authors
  54. *
  55. * @return array
  56. */
  57. public function getAuthors()
  58. {
  59. if (array_key_exists('authors', $this->_data)) {
  60. return $this->_data['authors'];
  61. }
  62. $authors = $this->_xpath->query('//atom:author');
  63. $contributors = $this->_xpath->query('//atom:contributor');
  64. $people = array();
  65. if ($authors->length) {
  66. foreach ($authors as $author) {
  67. $author = $this->_getAuthor($author);
  68. if (!empty($author)) {
  69. $people[] = $author;
  70. }
  71. }
  72. }
  73. if ($contributors->length) {
  74. foreach ($contributors as $contributor) {
  75. $contributor = $this->_getAuthor($contributor);
  76. if (!empty($contributor)) {
  77. $people[] = $contributor;
  78. }
  79. }
  80. }
  81. if (empty($people)) {
  82. $people = null;
  83. } else {
  84. $people = array_unique($people);
  85. }
  86. $this->_data['authors'] = $people;
  87. return $this->_data['authors'];
  88. }
  89. /**
  90. * Get the copyright entry
  91. *
  92. * @return string|null
  93. */
  94. public function getCopyright()
  95. {
  96. if (array_key_exists('copyright', $this->_data)) {
  97. return $this->_data['copyright'];
  98. }
  99. $copyright = null;
  100. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  101. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:copyright)');
  102. } else {
  103. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:rights)');
  104. }
  105. if (!$copyright) {
  106. $copyright = null;
  107. }
  108. $this->_data['copyright'] = $copyright;
  109. return $this->_data['copyright'];
  110. }
  111. /**
  112. * Get the feed creation date
  113. *
  114. * @return Zend_Date|null
  115. */
  116. public function getDateCreated()
  117. {
  118. if (array_key_exists('datecreated', $this->_data)) {
  119. return $this->_data['datecreated'];
  120. }
  121. $date = null;
  122. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  123. $dateCreated = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
  124. } else {
  125. $dateCreated = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
  126. }
  127. if ($dateCreated) {
  128. $date = new Zend_Date;
  129. $date->set($dateCreated, Zend_Date::ISO_8601);
  130. }
  131. $this->_data['datecreated'] = $date;
  132. return $this->_data['datecreated'];
  133. }
  134. /**
  135. * Get the feed modification date
  136. *
  137. * @return Zend_Date|null
  138. */
  139. public function getDateModified()
  140. {
  141. if (array_key_exists('datemodified', $this->_data)) {
  142. return $this->_data['datemodified'];
  143. }
  144. $date = null;
  145. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  146. $dateModified = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
  147. } else {
  148. $dateModified = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
  149. }
  150. if ($dateModified) {
  151. $date = new Zend_Date;
  152. $date->set($dateModified, Zend_Date::ISO_8601);
  153. }
  154. $this->_data['datemodified'] = $date;
  155. return $this->_data['datemodified'];
  156. }
  157. /**
  158. * Get the feed description
  159. *
  160. * @return string|null
  161. */
  162. public function getDescription()
  163. {
  164. if (array_key_exists('description', $this->_data)) {
  165. return $this->_data['description'];
  166. }
  167. $description = null;
  168. if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
  169. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:tagline)'); // TODO: Is this the same as subtitle?
  170. } else {
  171. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:subtitle)');
  172. }
  173. if (!$description) {
  174. $description = null;
  175. }
  176. $this->_data['description'] = $description;
  177. return $this->_data['description'];
  178. }
  179. /**
  180. * Get the feed generator entry
  181. *
  182. * @return string|null
  183. */
  184. public function getGenerator()
  185. {
  186. if (array_key_exists('generator', $this->_data)) {
  187. return $this->_data['generator'];
  188. }
  189. // TODO: Add uri support
  190. $generator = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:generator)');
  191. if (!$generator) {
  192. $generator = null;
  193. } else {
  194. $generator = html_entity_decode($generator, ENT_QUOTES, $this->getEncoding());
  195. }
  196. $this->_data['generator'] = $generator;
  197. return $this->_data['generator'];
  198. }
  199. /**
  200. * Get the feed ID
  201. *
  202. * @return string|null
  203. */
  204. public function getId()
  205. {
  206. if (array_key_exists('id', $this->_data)) {
  207. return $this->_data['id'];
  208. }
  209. $id = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
  210. if (!$id) {
  211. if ($this->getLink()) {
  212. $id = $this->getLink();
  213. } elseif ($this->getTitle()) {
  214. $id = $this->getTitle();
  215. } else {
  216. $id = null;
  217. }
  218. }
  219. $this->_data['id'] = $id;
  220. return $this->_data['id'];
  221. }
  222. /**
  223. * Get the feed language
  224. *
  225. * @return string|null
  226. */
  227. public function getLanguage()
  228. {
  229. if (array_key_exists('language', $this->_data)) {
  230. return $this->_data['language'];
  231. }
  232. $language = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:lang)');
  233. if (!$language) {
  234. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  235. }
  236. if (!$language) {
  237. $language = null;
  238. }
  239. $this->_data['language'] = $language;
  240. return $this->_data['language'];
  241. }
  242. /**
  243. * Get a link to the source website
  244. *
  245. * @return string|null
  246. */
  247. public function getLink()
  248. {
  249. if (array_key_exists('link', $this->_data)) {
  250. return $this->_data['link'];
  251. }
  252. $link = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:link/@href)');
  253. if (!$link) {
  254. $link = null;
  255. }
  256. $this->_data['link'] = $link;
  257. return $this->_data['link'];
  258. }
  259. /**
  260. * Get a link to the feed's XML Url
  261. *
  262. * @return string|null
  263. */
  264. public function getFeedLink()
  265. {
  266. if (array_key_exists('feedlink', $this->_data)) {
  267. return $this->_data['feedlink'];
  268. }
  269. $link = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:link[@rel="self"]/@href)');
  270. if (!$link) {
  271. $link = null;
  272. }
  273. $this->_data['feedlink'] = $link;
  274. return $this->_data['feedlink'];
  275. }
  276. /**
  277. * Get the feed title
  278. *
  279. * @return string|null
  280. */
  281. public function getTitle()
  282. {
  283. if (array_key_exists('title', $this->_data)) {
  284. return $this->_data['title'];
  285. }
  286. $title = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
  287. if (!$title) {
  288. $title = null;
  289. }
  290. $this->_data['title'] = $title;
  291. return $this->_data['title'];
  292. }
  293. /**
  294. * Get an author entry in RSS format
  295. *
  296. * @param DOMElement $element
  297. * @return string
  298. */
  299. protected function _getAuthor(DOMElement $element)
  300. {
  301. $email = null;
  302. $name = null;
  303. $uri = null;
  304. $emailNode = $element->getElementsByTagName('email');
  305. $nameNode = $element->getElementsByTagName('name');
  306. $uriNode = $element->getElementsByTagName('uri');
  307. if ($emailNode->length) {
  308. $email = $emailNode->item(0)->nodeValue;
  309. }
  310. if ($nameNode->length) {
  311. $name = $nameNode->item(0)->nodeValue;
  312. }
  313. if ($uriNode->length) {
  314. $uri = $uriNode->item(0)->nodeValue;
  315. }
  316. if (!empty($email)) {
  317. return $email . (empty($name) ? '' : ' (' . $name . ')');
  318. } else if (!empty($name)) {
  319. return $name;
  320. } else if (!empty($uri)) {
  321. return $uri;
  322. }
  323. return null;
  324. }
  325. /**
  326. * Register the default namespaces for the current feed format
  327. */
  328. protected function _registerNamespaces()
  329. {
  330. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10
  331. || $this->getType() == Zend_Feed_Reader::TYPE_ATOM_03
  332. ) {
  333. return; // pre-registered at Feed level
  334. }
  335. $atomDetected = $this->_getAtomType();
  336. switch ($atomDetected) {
  337. case Zend_Feed_Reader::TYPE_ATOM_03:
  338. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
  339. break;
  340. default:
  341. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  342. break;
  343. }
  344. }
  345. /**
  346. * Detect the presence of any Atom namespaces in use
  347. */
  348. protected function _getAtomType()
  349. {
  350. $nslist = $this->getDomDocument()->documentElement->attributes;
  351. if (!$nslist->length) {
  352. return null;
  353. }
  354. foreach ($nslist as $ns) {
  355. if ($ns->value == Zend_Feed_Reader::NAMESPACE_ATOM_10) {
  356. return Zend_Feed_Reader::TYPE_ATOM_10;
  357. }
  358. if ($ns->value == Zend_Feed_Reader::NAMESPACE_ATOM_03) {
  359. return Zend_Feed_Reader::TYPE_ATOM_03;
  360. }
  361. }
  362. }
  363. }