Atom.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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-2008 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_FeedAbstract
  23. */
  24. require_once 'Zend/Feed/Reader/FeedAbstract.php';
  25. /**
  26. * @see Zend_Feed_Reader_Extension_Atom_Feed
  27. */
  28. require_once 'Zend/Feed/Reader/Extension/Atom/Feed.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Feed_Reader
  32. * @copyright Copyright (c) 2005-2008 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_Feed_Atom extends Zend_Feed_Reader_FeedAbstract
  36. {
  37. /**
  38. * Constructor
  39. *
  40. * @param DOMDocument $dom
  41. * @param string $type
  42. */
  43. public function __construct(DomDocument $dom, $type = null)
  44. {
  45. parent::__construct($dom, $type);
  46. $atomClass = Zend_Feed_Reader::getPluginLoader()->getClassName('Atom_Feed');
  47. $this->_extensions['Atom_Feed'] = new $atomClass($dom, $this->_data['type'], $this->_xpath);
  48. foreach ($this->_extensions as $extension) {
  49. $extension->setXpathPrefix('/atom:feed');
  50. }
  51. }
  52. /**
  53. * Get a single author
  54. *
  55. * @param int $index
  56. * @return string|null
  57. */
  58. public function getAuthor($index = 0)
  59. {
  60. $authors = $this->getAuthors();
  61. if (isset($authors[$index])) {
  62. return $authors[$index];
  63. }
  64. return null;
  65. }
  66. /**
  67. * Get an array with feed authors
  68. *
  69. * @return array
  70. */
  71. public function getAuthors()
  72. {
  73. if (array_key_exists('authors', $this->_data)) {
  74. return $this->_data['authors'];
  75. }
  76. $people = $this->getExtension('Atom')->getAuthors();
  77. $this->_data['authors'] = $people;
  78. return $this->_data['authors'];
  79. }
  80. /**
  81. * Get the copyright entry
  82. *
  83. * @return string|null
  84. */
  85. public function getCopyright()
  86. {
  87. if (array_key_exists('copyright', $this->_data)) {
  88. return $this->_data['copyright'];
  89. }
  90. $copyright = $this->getExtension('Atom')->getCopyright();
  91. if (!$copyright) {
  92. $copyright = null;
  93. }
  94. $this->_data['copyright'] = $copyright;
  95. return $this->_data['copyright'];
  96. }
  97. /**
  98. * Get the feed creation date
  99. *
  100. * @return string|null
  101. */
  102. public function getDateCreated()
  103. {
  104. if (array_key_exists('datecreated', $this->_data)) {
  105. return $this->_data['datecreated'];
  106. }
  107. $dateCreated = $this->getExtension('Atom')->getDateCreated();
  108. if (!$dateCreated) {
  109. $dateCreated = null;
  110. }
  111. $this->_data['datecreated'] = $dateCreated;
  112. return $this->_data['datecreated'];
  113. }
  114. /**
  115. * Get the feed modification date
  116. *
  117. * @return string|null
  118. */
  119. public function getDateModified()
  120. {
  121. if (array_key_exists('datemodified', $this->_data)) {
  122. return $this->_data['datemodified'];
  123. }
  124. $dateModified = $this->getExtension('Atom')->getDateModified();
  125. if (!$dateModified) {
  126. $dateModified = null;
  127. }
  128. $this->_data['datemodified'] = $dateModified;
  129. return $this->_data['datemodified'];
  130. }
  131. /**
  132. * Get the feed description
  133. *
  134. * @return string|null
  135. */
  136. public function getDescription()
  137. {
  138. if (array_key_exists('description', $this->_data)) {
  139. return $this->_data['description'];
  140. }
  141. $description = $this->getExtension('Atom')->getDescription();
  142. if (!$description) {
  143. $description = null;
  144. }
  145. $this->_data['description'] = $description;
  146. return $this->_data['description'];
  147. }
  148. /**
  149. * Get the feed generator entry
  150. *
  151. * @return string|null
  152. */
  153. public function getGenerator()
  154. {
  155. if (array_key_exists('generator', $this->_data)) {
  156. return $this->_data['generator'];
  157. }
  158. $generator = $this->getExtension('Atom')->getGenerator();
  159. $this->_data['generator'] = $generator;
  160. return $this->_data['generator'];
  161. }
  162. /**
  163. * Get the feed ID
  164. *
  165. * @return string|null
  166. */
  167. public function getId()
  168. {
  169. if (array_key_exists('id', $this->_data)) {
  170. return $this->_data['id'];
  171. }
  172. $id = $this->getExtension('Atom')->getId();
  173. $this->_data['id'] = $id;
  174. return $this->_data['id'];
  175. }
  176. /**
  177. * Get the feed language
  178. *
  179. * @return string|null
  180. */
  181. public function getLanguage()
  182. {
  183. if (array_key_exists('language', $this->_data)) {
  184. return $this->_data['language'];
  185. }
  186. $language = $this->getExtension('Atom')->getLanguage();
  187. if (!$language) {
  188. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  189. }
  190. if (!$language) {
  191. $language = null;
  192. }
  193. $this->_data['language'] = $language;
  194. return $this->_data['language'];
  195. }
  196. /**
  197. * Get a link to the source website
  198. *
  199. * @return string|null
  200. */
  201. public function getBaseUrl()
  202. {
  203. if (array_key_exists('baseUrl', $this->_data)) {
  204. return $this->_data['baseUrl'];
  205. }
  206. $baseUrl = $this->getExtension('Atom')->getBaseUrl();
  207. $this->_data['baseUrl'] = $baseUrl;
  208. return $this->_data['baseUrl'];
  209. }
  210. /**
  211. * Get a link to the source website
  212. *
  213. * @return string|null
  214. */
  215. public function getLink()
  216. {
  217. if (array_key_exists('link', $this->_data)) {
  218. return $this->_data['link'];
  219. }
  220. $link = $this->getExtension('Atom')->getLink();
  221. $this->_data['link'] = $link;
  222. return $this->_data['link'];
  223. }
  224. /**
  225. * Get a link to the feed's XML Url
  226. *
  227. * @return string|null
  228. */
  229. public function getFeedLink()
  230. {
  231. if (array_key_exists('feedlink', $this->_data)) {
  232. return $this->_data['feedlink'];
  233. }
  234. $link = $this->getExtension('Atom')->getFeedLink();
  235. $this->_data['feedlink'] = $link;
  236. return $this->_data['feedlink'];
  237. }
  238. /**
  239. * Get the feed title
  240. *
  241. * @return string|null
  242. */
  243. public function getTitle()
  244. {
  245. if (array_key_exists('title', $this->_data)) {
  246. return $this->_data['title'];
  247. }
  248. $title = $this->getExtension('Atom')->getTitle();
  249. $this->_data['title'] = $title;
  250. return $this->_data['title'];
  251. }
  252. /**
  253. * Get an array of any supported Pusubhubbub endpoints
  254. *
  255. * @return array|null
  256. */
  257. public function getHubs()
  258. {
  259. if (array_key_exists('hubs', $this->_data)) {
  260. return $this->_data['hubs'];
  261. }
  262. $hubs = $this->getExtension('Atom')->getHubs();
  263. $this->_data['hubs'] = $hubs;
  264. return $this->_data['hubs'];
  265. }
  266. /**
  267. * Read all entries to the internal entries array
  268. *
  269. */
  270. protected function _indexEntries()
  271. {
  272. if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10 ||
  273. $this->getType() == Zend_Feed_Reader::TYPE_ATOM_03) {
  274. $entries = array();
  275. $entries = $this->_xpath->evaluate('//atom:entry');
  276. foreach($entries as $index=>$entry) {
  277. $this->_entries[$index] = $entry;
  278. }
  279. }
  280. }
  281. /**
  282. * Register the default namespaces for the current feed format
  283. *
  284. */
  285. protected function _registerNamespaces()
  286. {
  287. switch ($this->_data['type']) {
  288. case Zend_Feed_Reader::TYPE_ATOM_03:
  289. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
  290. break;
  291. case Zend_Feed_Reader::TYPE_ATOM_10:
  292. default:
  293. $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
  294. }
  295. }
  296. }