Rss.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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$
  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. $renderer = new Zend_Feed_Writer_Renderer_Entry_Rss($entry);
  92. if ($this->_ignoreExceptions === true) {
  93. $renderer->ignoreExceptions();
  94. }
  95. $renderer->setType($this->getType());
  96. $renderer->setRootElement($this->_dom->documentElement);
  97. $renderer->render();
  98. $element = $renderer->getElement();
  99. $imported = $this->_dom->importNode($element, true);
  100. $channel->appendChild($imported);
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Set feed language
  106. *
  107. * @param DOMDocument $dom
  108. * @param DOMElement $root
  109. * @return void
  110. */
  111. protected function _setLanguage(DOMDocument $dom, DOMElement $root)
  112. {
  113. $lang = $this->getDataContainer()->getLanguage();
  114. if (!$lang) {
  115. return;
  116. }
  117. $language = $dom->createElement('language');
  118. $root->appendChild($language);
  119. $language->nodeValue = $lang;
  120. }
  121. /**
  122. * Set feed title
  123. *
  124. * @param DOMDocument $dom
  125. * @param DOMElement $root
  126. * @return void
  127. */
  128. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  129. {
  130. if(!$this->getDataContainer()->getTitle()) {
  131. require_once 'Zend/Feed/Exception.php';
  132. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  133. . ' title element but a title has not been set';
  134. $exception = new Zend_Feed_Exception($message);
  135. if (!$this->_ignoreExceptions) {
  136. throw $exception;
  137. } else {
  138. $this->_exceptions[] = $exception;
  139. return;
  140. }
  141. }
  142. $title = $dom->createElement('title');
  143. $root->appendChild($title);
  144. $text = $dom->createTextNode($this->getDataContainer()->getTitle());
  145. $title->appendChild($text);
  146. }
  147. /**
  148. * Set feed description
  149. *
  150. * @param DOMDocument $dom
  151. * @param DOMElement $root
  152. * @return void
  153. */
  154. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  155. {
  156. if(!$this->getDataContainer()->getDescription()) {
  157. require_once 'Zend/Feed/Exception.php';
  158. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  159. . ' description element but one has not been set';
  160. $exception = new Zend_Feed_Exception($message);
  161. if (!$this->_ignoreExceptions) {
  162. throw $exception;
  163. } else {
  164. $this->_exceptions[] = $exception;
  165. return;
  166. }
  167. }
  168. $subtitle = $dom->createElement('description');
  169. $root->appendChild($subtitle);
  170. $text = $dom->createTextNode($this->getDataContainer()->getDescription());
  171. $subtitle->appendChild($text);
  172. }
  173. /**
  174. * Set date feed was last modified
  175. *
  176. * @param DOMDocument $dom
  177. * @param DOMElement $root
  178. * @return void
  179. */
  180. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  181. {
  182. if(!$this->getDataContainer()->getDateModified()) {
  183. return;
  184. }
  185. $updated = $dom->createElement('pubDate');
  186. $root->appendChild($updated);
  187. $text = $dom->createTextNode(
  188. $this->getDataContainer()->getDateModified()->get(Zend_Date::RSS)
  189. );
  190. $updated->appendChild($text);
  191. }
  192. /**
  193. * Set feed generator string
  194. *
  195. * @param DOMDocument $dom
  196. * @param DOMElement $root
  197. * @return void
  198. */
  199. protected function _setGenerator(DOMDocument $dom, DOMElement $root)
  200. {
  201. if(!$this->getDataContainer()->getGenerator()) {
  202. $this->getDataContainer()->setGenerator('Zend_Feed_Writer',
  203. Zend_Version::VERSION, 'http://framework.zend.com');
  204. }
  205. $gdata = $this->getDataContainer()->getGenerator();
  206. $generator = $dom->createElement('generator');
  207. $root->appendChild($generator);
  208. $name = $gdata['name'];
  209. if (array_key_exists('version', $gdata)) {
  210. $name .= ' ' . $gdata['version'];
  211. }
  212. if (array_key_exists('uri', $gdata)) {
  213. $name .= ' (' . $gdata['uri'] . ')';
  214. }
  215. $text = $dom->createTextNode($name);
  216. $generator->appendChild($text);
  217. }
  218. /**
  219. * Set link to feed
  220. *
  221. * @param DOMDocument $dom
  222. * @param DOMElement $root
  223. * @return void
  224. */
  225. protected function _setLink(DOMDocument $dom, DOMElement $root)
  226. {
  227. $value = $this->getDataContainer()->getLink();
  228. if(!$value) {
  229. require_once 'Zend/Feed/Exception.php';
  230. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  231. . ' link element but one has not been set';
  232. $exception = new Zend_Feed_Exception($message);
  233. if (!$this->_ignoreExceptions) {
  234. throw $exception;
  235. } else {
  236. $this->_exceptions[] = $exception;
  237. return;
  238. }
  239. }
  240. $link = $dom->createElement('link');
  241. $root->appendChild($link);
  242. $text = $dom->createTextNode($value);
  243. $link->appendChild($text);
  244. if (!Zend_Uri::check($value)) {
  245. $link->setAttribute('isPermaLink', 'false');
  246. }
  247. }
  248. /**
  249. * Set feed authors
  250. *
  251. * @param DOMDocument $dom
  252. * @param DOMElement $root
  253. * @return void
  254. */
  255. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  256. {
  257. $authors = $this->getDataContainer()->getAuthors();
  258. if (!$authors || empty($authors)) {
  259. return;
  260. }
  261. foreach ($authors as $data) {
  262. $author = $this->_dom->createElement('author');
  263. $name = $data['name'];
  264. if (array_key_exists('email', $data)) {
  265. $name = $data['email'] . ' (' . $data['name'] . ')';
  266. }
  267. $text = $dom->createTextNode($name);
  268. $author->appendChild($text);
  269. $root->appendChild($author);
  270. }
  271. }
  272. /**
  273. * Set feed copyright
  274. *
  275. * @param DOMDocument $dom
  276. * @param DOMElement $root
  277. * @return void
  278. */
  279. protected function _setCopyright(DOMDocument $dom, DOMElement $root)
  280. {
  281. $copyright = $this->getDataContainer()->getCopyright();
  282. if (!$copyright) {
  283. return;
  284. }
  285. $copy = $dom->createElement('copyright');
  286. $root->appendChild($copy);
  287. $text = $dom->createTextNode($copyright);
  288. $copy->appendChild($text);
  289. }
  290. /**
  291. * Set date feed was created
  292. *
  293. * @param DOMDocument $dom
  294. * @param DOMElement $root
  295. * @return void
  296. */
  297. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  298. {
  299. if(!$this->getDataContainer()->getDateCreated()) {
  300. return;
  301. }
  302. if(!$this->getDataContainer()->getDateModified()) {
  303. $this->getDataContainer()->setDateModified(
  304. $this->getDataContainer()->getDateCreated()
  305. );
  306. }
  307. }
  308. /**
  309. * Set base URL to feed links
  310. *
  311. * @param DOMDocument $dom
  312. * @param DOMElement $root
  313. * @return void
  314. */
  315. protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
  316. {
  317. $baseUrl = $this->getDataContainer()->getBaseUrl();
  318. if (!$baseUrl) {
  319. return;
  320. }
  321. $root->setAttribute('xml:base', $baseUrl);
  322. }
  323. /**
  324. * Set feed categories
  325. *
  326. * @param DOMDocument $dom
  327. * @param DOMElement $root
  328. * @return void
  329. */
  330. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  331. {
  332. $categories = $this->getDataContainer()->getCategories();
  333. if (!$categories) {
  334. return;
  335. }
  336. foreach ($categories as $cat) {
  337. $category = $dom->createElement('category');
  338. if (isset($cat['scheme'])) {
  339. $category->setAttribute('domain', $cat['scheme']);
  340. }
  341. $text = $dom->createTextNode($cat['term']);
  342. $category->appendChild($text);
  343. $root->appendChild($category);
  344. }
  345. }
  346. }