PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/website/library/Zend/Feed/Atom.php

https://bitbucket.org/efdac/e-forest_platform
PHP | 390 lines | 203 code | 52 blank | 135 comment | 27 complexity | ba66134577f7dba467836a0076238db4 MD5 | raw file
  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
  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: Atom.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * @see Zend_Feed_Abstract
  23. */
  24. require_once 'Zend/Feed/Abstract.php';
  25. /**
  26. * @see Zend_Feed_Entry_Atom
  27. */
  28. require_once 'Zend/Feed/Entry/Atom.php';
  29. /**
  30. * Atom feed class
  31. *
  32. * The Zend_Feed_Atom class is a concrete subclass of the general
  33. * Zend_Feed_Abstract class, tailored for representing an Atom
  34. * feed. It shares all of the same methods with its abstract
  35. * parent. The distinction is made in the format of data that
  36. * Zend_Feed_Atom expects, and as a further pointer for users as to
  37. * what kind of feed object they have been passed.
  38. *
  39. * @category Zend
  40. * @package Zend_Feed
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Feed_Atom extends Zend_Feed_Abstract
  45. {
  46. /**
  47. * The classname for individual feed elements.
  48. *
  49. * @var string
  50. */
  51. protected $_entryClassName = 'Zend_Feed_Entry_Atom';
  52. /**
  53. * The element name for individual feed elements (Atom <entry>
  54. * elements).
  55. *
  56. * @var string
  57. */
  58. protected $_entryElementName = 'entry';
  59. /**
  60. * The default namespace for Atom feeds.
  61. *
  62. * @var string
  63. */
  64. protected $_defaultNamespace = 'atom';
  65. /**
  66. * Override Zend_Feed_Abstract to set up the $_element and $_entries aliases.
  67. *
  68. * @return void
  69. * @throws Zend_Feed_Exception
  70. */
  71. public function __wakeup()
  72. {
  73. parent::__wakeup();
  74. // Find the base feed element and create an alias to it.
  75. $element = $this->_element->getElementsByTagName('feed')->item(0);
  76. if (!$element) {
  77. // Try to find a single <entry> instead.
  78. $element = $this->_element->getElementsByTagName($this->_entryElementName)->item(0);
  79. if (!$element) {
  80. /**
  81. * @see Zend_Feed_Exception
  82. */
  83. require_once 'Zend/Feed/Exception.php';
  84. throw new Zend_Feed_Exception('No root <feed> or <' . $this->_entryElementName
  85. . '> element found, cannot parse feed.');
  86. }
  87. $doc = new DOMDocument($this->_element->version,
  88. $this->_element->actualEncoding);
  89. $feed = $doc->appendChild($doc->createElement('feed'));
  90. $feed->appendChild($doc->importNode($element, true));
  91. $element = $feed;
  92. }
  93. $this->_element = $element;
  94. // Find the entries and save a pointer to them for speed and
  95. // simplicity.
  96. $this->_buildEntryCache();
  97. }
  98. /**
  99. * Easy access to <link> tags keyed by "rel" attributes.
  100. *
  101. * If $elt->link() is called with no arguments, we will attempt to
  102. * return the value of the <link> tag(s) like all other
  103. * method-syntax attribute access. If an argument is passed to
  104. * link(), however, then we will return the "href" value of the
  105. * first <link> tag that has a "rel" attribute matching $rel:
  106. *
  107. * $elt->link(): returns the value of the link tag.
  108. * $elt->link('self'): returns the href from the first <link rel="self"> in the entry.
  109. *
  110. * @param string $rel The "rel" attribute to look for.
  111. * @return mixed
  112. */
  113. public function link($rel = null)
  114. {
  115. if ($rel === null) {
  116. return parent::__call('link', null);
  117. }
  118. // index link tags by their "rel" attribute.
  119. $links = parent::__get('link');
  120. if (!is_array($links)) {
  121. if ($links instanceof Zend_Feed_Element) {
  122. $links = array($links);
  123. } else {
  124. return $links;
  125. }
  126. }
  127. foreach ($links as $link) {
  128. if (empty($link['rel'])) {
  129. continue;
  130. }
  131. if ($rel == $link['rel']) {
  132. return $link['href'];
  133. }
  134. }
  135. return null;
  136. }
  137. /**
  138. * Make accessing some individual elements of the feed easier.
  139. *
  140. * Special accessors 'entry' and 'entries' are provided so that if
  141. * you wish to iterate over an Atom feed's entries, you can do so
  142. * using foreach ($feed->entries as $entry) or foreach
  143. * ($feed->entry as $entry).
  144. *
  145. * @param string $var The property to access.
  146. * @return mixed
  147. */
  148. public function __get($var)
  149. {
  150. switch ($var) {
  151. case 'entry':
  152. // fall through to the next case
  153. case 'entries':
  154. return $this;
  155. default:
  156. return parent::__get($var);
  157. }
  158. }
  159. /**
  160. * Generate the header of the feed when working in write mode
  161. *
  162. * @param array $array the data to use
  163. * @return DOMElement root node
  164. */
  165. protected function _mapFeedHeaders($array)
  166. {
  167. $feed = $this->_element->createElement('feed');
  168. $feed->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
  169. $id = $this->_element->createElement('id', $array->link);
  170. $feed->appendChild($id);
  171. $title = $this->_element->createElement('title');
  172. $title->appendChild($this->_element->createCDATASection($array->title));
  173. $feed->appendChild($title);
  174. if (isset($array->author)) {
  175. $author = $this->_element->createElement('author');
  176. $name = $this->_element->createElement('name', $array->author);
  177. $author->appendChild($name);
  178. if (isset($array->email)) {
  179. $email = $this->_element->createElement('email', $array->email);
  180. $author->appendChild($email);
  181. }
  182. $feed->appendChild($author);
  183. }
  184. $updated = isset($array->lastUpdate) ? $array->lastUpdate : time();
  185. $updated = $this->_element->createElement('updated', date(DATE_ATOM, $updated));
  186. $feed->appendChild($updated);
  187. if (isset($array->published)) {
  188. $published = $this->_element->createElement('published', date(DATE_ATOM, $array->published));
  189. $feed->appendChild($published);
  190. }
  191. $link = $this->_element->createElement('link');
  192. $link->setAttribute('rel', 'self');
  193. $link->setAttribute('href', $array->link);
  194. if (isset($array->language)) {
  195. $link->setAttribute('hreflang', $array->language);
  196. }
  197. $feed->appendChild($link);
  198. if (isset($array->description)) {
  199. $subtitle = $this->_element->createElement('subtitle');
  200. $subtitle->appendChild($this->_element->createCDATASection($array->description));
  201. $feed->appendChild($subtitle);
  202. }
  203. if (isset($array->copyright)) {
  204. $copyright = $this->_element->createElement('rights', $array->copyright);
  205. $feed->appendChild($copyright);
  206. }
  207. if (isset($array->image)) {
  208. $image = $this->_element->createElement('logo', $array->image);
  209. $feed->appendChild($image);
  210. }
  211. $generator = !empty($array->generator) ? $array->generator : 'Zend_Feed';
  212. $generator = $this->_element->createElement('generator', $generator);
  213. $feed->appendChild($generator);
  214. return $feed;
  215. }
  216. /**
  217. * Generate the entries of the feed when working in write mode
  218. *
  219. * The following nodes are constructed for each feed entry
  220. * <entry>
  221. * <id>url to feed entry</id>
  222. * <title>entry title</title>
  223. * <updated>last update</updated>
  224. * <link rel="alternate" href="url to feed entry" />
  225. * <summary>short text</summary>
  226. * <content>long version, can contain html</content>
  227. * </entry>
  228. *
  229. * @param array $array the data to use
  230. * @param DOMElement $root the root node to use
  231. * @return void
  232. */
  233. protected function _mapFeedEntries(DOMElement $root, $array)
  234. {
  235. foreach ($array as $dataentry) {
  236. $entry = $this->_element->createElement('entry');
  237. $id = $this->_element->createElement('id', isset($dataentry->guid) ? $dataentry->guid : $dataentry->link);
  238. $entry->appendChild($id);
  239. $title = $this->_element->createElement('title');
  240. $title->appendChild($this->_element->createCDATASection($dataentry->title));
  241. $entry->appendChild($title);
  242. $updated = isset($dataentry->lastUpdate) ? $dataentry->lastUpdate : time();
  243. $updated = $this->_element->createElement('updated', date(DATE_ATOM, $updated));
  244. $entry->appendChild($updated);
  245. $link = $this->_element->createElement('link');
  246. $link->setAttribute('rel', 'alternate');
  247. $link->setAttribute('href', $dataentry->link);
  248. $entry->appendChild($link);
  249. $summary = $this->_element->createElement('summary');
  250. $summary->appendChild($this->_element->createCDATASection($dataentry->description));
  251. $entry->appendChild($summary);
  252. if (isset($dataentry->content)) {
  253. $content = $this->_element->createElement('content');
  254. $content->setAttribute('type', 'html');
  255. $content->appendChild($this->_element->createCDATASection($dataentry->content));
  256. $entry->appendChild($content);
  257. }
  258. if (isset($dataentry->category)) {
  259. foreach ($dataentry->category as $category) {
  260. $node = $this->_element->createElement('category');
  261. $node->setAttribute('term', $category['term']);
  262. if (isset($category['scheme'])) {
  263. $node->setAttribute('scheme', $category['scheme']);
  264. }
  265. $entry->appendChild($node);
  266. }
  267. }
  268. if (isset($dataentry->source)) {
  269. $source = $this->_element->createElement('source');
  270. $title = $this->_element->createElement('title', $dataentry->source['title']);
  271. $source->appendChild($title);
  272. $link = $this->_element->createElement('link', $dataentry->source['title']);
  273. $link->setAttribute('rel', 'alternate');
  274. $link->setAttribute('href', $dataentry->source['url']);
  275. $source->appendChild($link);
  276. }
  277. if (isset($dataentry->enclosure)) {
  278. foreach ($dataentry->enclosure as $enclosure) {
  279. $node = $this->_element->createElement('link');
  280. $node->setAttribute('rel', 'enclosure');
  281. $node->setAttribute('href', $enclosure['url']);
  282. if (isset($enclosure['type'])) {
  283. $node->setAttribute('type', $enclosure['type']);
  284. }
  285. if (isset($enclosure['length'])) {
  286. $node->setAttribute('length', $enclosure['length']);
  287. }
  288. $entry->appendChild($node);
  289. }
  290. }
  291. if (isset($dataentry->comments)) {
  292. $comments = $this->_element->createElementNS('http://wellformedweb.org/CommentAPI/',
  293. 'wfw:comment',
  294. $dataentry->comments);
  295. $entry->appendChild($comments);
  296. }
  297. if (isset($dataentry->commentRss)) {
  298. $comments = $this->_element->createElementNS('http://wellformedweb.org/CommentAPI/',
  299. 'wfw:commentRss',
  300. $dataentry->commentRss);
  301. $entry->appendChild($comments);
  302. }
  303. $root->appendChild($entry);
  304. }
  305. }
  306. /**
  307. * Override Zend_Feed_Element to allow formated feeds
  308. *
  309. * @return string
  310. */
  311. public function saveXml()
  312. {
  313. // Return a complete document including XML prologue.
  314. $doc = new DOMDocument($this->_element->ownerDocument->version,
  315. $this->_element->ownerDocument->actualEncoding);
  316. $doc->appendChild($doc->importNode($this->_element, true));
  317. $doc->formatOutput = true;
  318. return $doc->saveXML();
  319. }
  320. /**
  321. * Send feed to a http client with the correct header
  322. *
  323. * @return void
  324. * @throws Zend_Feed_Exception if headers have already been sent
  325. */
  326. public function send()
  327. {
  328. if (headers_sent()) {
  329. /**
  330. * @see Zend_Feed_Exception
  331. */
  332. require_once 'Zend/Feed/Exception.php';
  333. throw new Zend_Feed_Exception('Cannot send ATOM because headers have already been sent.');
  334. }
  335. header('Content-Type: application/atom+xml; charset=' . $this->_element->ownerDocument->actualEncoding);
  336. echo $this->saveXML();
  337. }
  338. }