PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/zend/1.10.2-minapp/library/Zend/Feed/Writer/Renderer/Feed/Rss.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 374 lines | 232 code | 24 blank | 118 comment | 29 complexity | 0b62b0a907e571b1ed2ed85839130a33 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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_Writer
  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: Rss.php 20519 2010-01-22 14:06:24Z padraic $
  20. */
  21. /** @see Zend_Feed_Writer_Feed */
  22. require_once 'Zend/Feed/Writer/Feed.php';
  23. /** @see Zend_Version */
  24. require_once 'Zend/Version.php';
  25. /** @see Zend_Feed_Writer_Renderer_RendererInterface */
  26. require_once 'Zend/Feed/Writer/Renderer/RendererInterface.php';
  27. /** @see Zend_Feed_Writer_Renderer_Entry_Rss */
  28. require_once 'Zend/Feed/Writer/Renderer/Entry/Rss.php';
  29. /** @see Zend_Feed_Writer_Renderer_RendererAbstract */
  30. require_once 'Zend/Feed/Writer/Renderer/RendererAbstract.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Feed_Writer
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Feed_Writer_Renderer_Feed_Rss
  38. extends Zend_Feed_Writer_Renderer_RendererAbstract
  39. implements Zend_Feed_Writer_Renderer_RendererInterface
  40. {
  41. /**
  42. * Constructor
  43. *
  44. * @param Zend_Feed_Writer_Feed $container
  45. * @return void
  46. */
  47. public function __construct (Zend_Feed_Writer_Feed $container)
  48. {
  49. parent::__construct($container);
  50. }
  51. /**
  52. * Render RSS feed
  53. *
  54. * @return Zend_Feed_Writer_Renderer_Feed_Rss
  55. */
  56. public function render()
  57. {
  58. if (!$this->_container->getEncoding()) {
  59. $this->_container->setEncoding('UTF-8');
  60. }
  61. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  62. $this->_dom->formatOutput = true;
  63. $this->_dom->substituteEntities = false;
  64. $rss = $this->_dom->createElement('rss');
  65. $this->setRootElement($rss);
  66. $rss->setAttribute('version', '2.0');
  67. $channel = $this->_dom->createElement('channel');
  68. $rss->appendChild($channel);
  69. $this->_dom->appendChild($rss);
  70. $this->_setLanguage($this->_dom, $channel);
  71. $this->_setBaseUrl($this->_dom, $channel);
  72. $this->_setTitle($this->_dom, $channel);
  73. $this->_setDescription($this->_dom, $channel);
  74. $this->_setDateCreated($this->_dom, $channel);
  75. $this->_setDateModified($this->_dom, $channel);
  76. $this->_setGenerator($this->_dom, $channel);
  77. $this->_setLink($this->_dom, $channel);
  78. $this->_setAuthors($this->_dom, $channel);
  79. $this->_setCopyright($this->_dom, $channel);
  80. $this->_setCategories($this->_dom, $channel);
  81. foreach ($this->_extensions as $ext) {
  82. $ext->setType($this->getType());
  83. $ext->setRootElement($this->getRootElement());
  84. $ext->setDomDocument($this->getDomDocument(), $channel);
  85. $ext->render();
  86. }
  87. foreach ($this->_container as $entry) {
  88. if ($this->getDataContainer()->getEncoding()) {
  89. $entry->setEncoding($this->getDataContainer()->getEncoding());
  90. }
  91. if ($entry instanceof Zend_Feed_Writer_Entry) {
  92. $renderer = new Zend_Feed_Writer_Renderer_Entry_Rss($entry);
  93. } else {
  94. continue;
  95. }
  96. if ($this->_ignoreExceptions === true) {
  97. $renderer->ignoreExceptions();
  98. }
  99. $renderer->setType($this->getType());
  100. $renderer->setRootElement($this->_dom->documentElement);
  101. $renderer->render();
  102. $element = $renderer->getElement();
  103. $imported = $this->_dom->importNode($element, true);
  104. $channel->appendChild($imported);
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Set feed language
  110. *
  111. * @param DOMDocument $dom
  112. * @param DOMElement $root
  113. * @return void
  114. */
  115. protected function _setLanguage(DOMDocument $dom, DOMElement $root)
  116. {
  117. $lang = $this->getDataContainer()->getLanguage();
  118. if (!$lang) {
  119. return;
  120. }
  121. $language = $dom->createElement('language');
  122. $root->appendChild($language);
  123. $language->nodeValue = $lang;
  124. }
  125. /**
  126. * Set feed title
  127. *
  128. * @param DOMDocument $dom
  129. * @param DOMElement $root
  130. * @return void
  131. */
  132. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  133. {
  134. if(!$this->getDataContainer()->getTitle()) {
  135. require_once 'Zend/Feed/Exception.php';
  136. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  137. . ' title element but a title has not been set';
  138. $exception = new Zend_Feed_Exception($message);
  139. if (!$this->_ignoreExceptions) {
  140. throw $exception;
  141. } else {
  142. $this->_exceptions[] = $exception;
  143. return;
  144. }
  145. }
  146. $title = $dom->createElement('title');
  147. $root->appendChild($title);
  148. $text = $dom->createTextNode($this->getDataContainer()->getTitle());
  149. $title->appendChild($text);
  150. }
  151. /**
  152. * Set feed description
  153. *
  154. * @param DOMDocument $dom
  155. * @param DOMElement $root
  156. * @return void
  157. */
  158. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  159. {
  160. if(!$this->getDataContainer()->getDescription()) {
  161. require_once 'Zend/Feed/Exception.php';
  162. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  163. . ' description element but one has not been set';
  164. $exception = new Zend_Feed_Exception($message);
  165. if (!$this->_ignoreExceptions) {
  166. throw $exception;
  167. } else {
  168. $this->_exceptions[] = $exception;
  169. return;
  170. }
  171. }
  172. $subtitle = $dom->createElement('description');
  173. $root->appendChild($subtitle);
  174. $text = $dom->createTextNode($this->getDataContainer()->getDescription());
  175. $subtitle->appendChild($text);
  176. }
  177. /**
  178. * Set date feed was last modified
  179. *
  180. * @param DOMDocument $dom
  181. * @param DOMElement $root
  182. * @return void
  183. */
  184. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  185. {
  186. if(!$this->getDataContainer()->getDateModified()) {
  187. return;
  188. }
  189. $updated = $dom->createElement('pubDate');
  190. $root->appendChild($updated);
  191. $text = $dom->createTextNode(
  192. $this->getDataContainer()->getDateModified()->get(Zend_Date::RSS)
  193. );
  194. $updated->appendChild($text);
  195. }
  196. /**
  197. * Set feed generator string
  198. *
  199. * @param DOMDocument $dom
  200. * @param DOMElement $root
  201. * @return void
  202. */
  203. protected function _setGenerator(DOMDocument $dom, DOMElement $root)
  204. {
  205. if(!$this->getDataContainer()->getGenerator()) {
  206. $this->getDataContainer()->setGenerator('Zend_Feed_Writer',
  207. Zend_Version::VERSION, 'http://framework.zend.com');
  208. }
  209. $gdata = $this->getDataContainer()->getGenerator();
  210. $generator = $dom->createElement('generator');
  211. $root->appendChild($generator);
  212. $name = $gdata['name'];
  213. if (array_key_exists('version', $gdata)) {
  214. $name .= ' ' . $gdata['version'];
  215. }
  216. if (array_key_exists('uri', $gdata)) {
  217. $name .= ' (' . $gdata['uri'] . ')';
  218. }
  219. $text = $dom->createTextNode($name);
  220. $generator->appendChild($text);
  221. }
  222. /**
  223. * Set link to feed
  224. *
  225. * @param DOMDocument $dom
  226. * @param DOMElement $root
  227. * @return void
  228. */
  229. protected function _setLink(DOMDocument $dom, DOMElement $root)
  230. {
  231. $value = $this->getDataContainer()->getLink();
  232. if(!$value) {
  233. require_once 'Zend/Feed/Exception.php';
  234. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  235. . ' link element but one has not been set';
  236. $exception = new Zend_Feed_Exception($message);
  237. if (!$this->_ignoreExceptions) {
  238. throw $exception;
  239. } else {
  240. $this->_exceptions[] = $exception;
  241. return;
  242. }
  243. }
  244. $link = $dom->createElement('link');
  245. $root->appendChild($link);
  246. $text = $dom->createTextNode($value);
  247. $link->appendChild($text);
  248. if (!Zend_Uri::check($value)) {
  249. $link->setAttribute('isPermaLink', 'false');
  250. }
  251. }
  252. /**
  253. * Set feed authors
  254. *
  255. * @param DOMDocument $dom
  256. * @param DOMElement $root
  257. * @return void
  258. */
  259. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  260. {
  261. $authors = $this->getDataContainer()->getAuthors();
  262. if (!$authors || empty($authors)) {
  263. return;
  264. }
  265. foreach ($authors as $data) {
  266. $author = $this->_dom->createElement('author');
  267. $name = $data['name'];
  268. if (array_key_exists('email', $data)) {
  269. $name = $data['email'] . ' (' . $data['name'] . ')';
  270. }
  271. $text = $dom->createTextNode($name);
  272. $author->appendChild($text);
  273. $root->appendChild($author);
  274. }
  275. }
  276. /**
  277. * Set feed copyright
  278. *
  279. * @param DOMDocument $dom
  280. * @param DOMElement $root
  281. * @return void
  282. */
  283. protected function _setCopyright(DOMDocument $dom, DOMElement $root)
  284. {
  285. $copyright = $this->getDataContainer()->getCopyright();
  286. if (!$copyright) {
  287. return;
  288. }
  289. $copy = $dom->createElement('copyright');
  290. $root->appendChild($copy);
  291. $text = $dom->createTextNode($copyright);
  292. $copy->appendChild($text);
  293. }
  294. /**
  295. * Set date feed was created
  296. *
  297. * @param DOMDocument $dom
  298. * @param DOMElement $root
  299. * @return void
  300. */
  301. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  302. {
  303. if(!$this->getDataContainer()->getDateCreated()) {
  304. return;
  305. }
  306. if(!$this->getDataContainer()->getDateModified()) {
  307. $this->getDataContainer()->setDateModified(
  308. $this->getDataContainer()->getDateCreated()
  309. );
  310. }
  311. }
  312. /**
  313. * Set base URL to feed links
  314. *
  315. * @param DOMDocument $dom
  316. * @param DOMElement $root
  317. * @return void
  318. */
  319. protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
  320. {
  321. $baseUrl = $this->getDataContainer()->getBaseUrl();
  322. if (!$baseUrl) {
  323. return;
  324. }
  325. $root->setAttribute('xml:base', $baseUrl);
  326. }
  327. /**
  328. * Set feed categories
  329. *
  330. * @param DOMDocument $dom
  331. * @param DOMElement $root
  332. * @return void
  333. */
  334. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  335. {
  336. $categories = $this->getDataContainer()->getCategories();
  337. if (!$categories) {
  338. return;
  339. }
  340. foreach ($categories as $cat) {
  341. $category = $dom->createElement('category');
  342. if (isset($cat['scheme'])) {
  343. $category->setAttribute('domain', $cat['scheme']);
  344. }
  345. $text = $dom->createTextNode($cat['term']);
  346. $category->appendChild($text);
  347. $root->appendChild($category);
  348. }
  349. }
  350. }