Rss.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /**
  22. * @see Zend_Feed_Writer_Renderer_RendererAbstract
  23. */
  24. require_once 'Zend/Feed/Writer/Renderer/RendererAbstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Feed_Writer
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Feed_Writer_Renderer_Entry_Rss
  32. extends Zend_Feed_Writer_Renderer_RendererAbstract
  33. implements Zend_Feed_Writer_Renderer_RendererInterface
  34. {
  35. /**
  36. * Constructor
  37. *
  38. * @param Zend_Feed_Writer_Entry $container
  39. * @return void
  40. */
  41. public function __construct (Zend_Feed_Writer_Entry $container)
  42. {
  43. parent::__construct($container);
  44. }
  45. /**
  46. * Render RSS entry
  47. *
  48. * @return Zend_Feed_Writer_Renderer_Entry_Rss
  49. */
  50. public function render()
  51. {
  52. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  53. $this->_dom->formatOutput = true;
  54. $this->_dom->substituteEntities = false;
  55. $entry = $this->_dom->createElement('item');
  56. $this->_dom->appendChild($entry);
  57. $this->_setTitle($this->_dom, $entry);
  58. $this->_setDescription($this->_dom, $entry);
  59. $this->_setDateCreated($this->_dom, $entry);
  60. $this->_setDateModified($this->_dom, $entry);
  61. $this->_setLink($this->_dom, $entry);
  62. $this->_setId($this->_dom, $entry);
  63. $this->_setAuthors($this->_dom, $entry);
  64. $this->_setEnclosure($this->_dom, $entry);
  65. $this->_setCommentLink($this->_dom, $entry);
  66. $this->_setCategories($this->_dom, $entry);
  67. foreach ($this->_extensions as $ext) {
  68. $ext->setType($this->getType());
  69. $ext->setRootElement($this->getRootElement());
  70. $ext->setDomDocument($this->getDomDocument(), $entry);
  71. $ext->render();
  72. }
  73. return $this;
  74. }
  75. /**
  76. * Set entry title
  77. *
  78. * @param DOMDocument $dom
  79. * @param DOMElement $root
  80. * @return void
  81. */
  82. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  83. {
  84. if(!$this->getDataContainer()->getDescription()
  85. && !$this->getDataContainer()->getTitle()) {
  86. require_once 'Zend/Feed/Exception.php';
  87. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  88. . ' title element but a title has not been set. In addition, there'
  89. . ' is no description as required in the absence of a title.';
  90. $exception = new Zend_Feed_Exception($message);
  91. if (!$this->_ignoreExceptions) {
  92. throw $exception;
  93. } else {
  94. $this->_exceptions[] = $exception;
  95. return;
  96. }
  97. }
  98. $title = $dom->createElement('title');
  99. $root->appendChild($title);
  100. $text = $dom->createTextNode($this->getDataContainer()->getTitle());
  101. $title->appendChild($text);
  102. }
  103. /**
  104. * Set entry description
  105. *
  106. * @param DOMDocument $dom
  107. * @param DOMElement $root
  108. * @return void
  109. */
  110. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  111. {
  112. if(!$this->getDataContainer()->getDescription()
  113. && !$this->getDataContainer()->getTitle()) {
  114. require_once 'Zend/Feed/Exception.php';
  115. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  116. . ' description element but a description has not been set. In'
  117. . ' addition, there is no title element as required in the absence'
  118. . ' of a description.';
  119. $exception = new Zend_Feed_Exception($message);
  120. if (!$this->_ignoreExceptions) {
  121. throw $exception;
  122. } else {
  123. $this->_exceptions[] = $exception;
  124. return;
  125. }
  126. }
  127. $subtitle = $dom->createElement('description');
  128. $root->appendChild($subtitle);
  129. $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
  130. $subtitle->appendChild($text);
  131. }
  132. /**
  133. * Set date entry was last modified
  134. *
  135. * @param DOMDocument $dom
  136. * @param DOMElement $root
  137. * @return void
  138. */
  139. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  140. {
  141. if(!$this->getDataContainer()->getDateModified()) {
  142. return;
  143. }
  144. $updated = $dom->createElement('pubDate');
  145. $root->appendChild($updated);
  146. $text = $dom->createTextNode(
  147. $this->getDataContainer()->getDateModified()->get(Zend_Date::RSS)
  148. );
  149. $updated->appendChild($text);
  150. }
  151. /**
  152. * Set date entry was created
  153. *
  154. * @param DOMDocument $dom
  155. * @param DOMElement $root
  156. * @return void
  157. */
  158. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  159. {
  160. if (!$this->getDataContainer()->getDateCreated()) {
  161. return;
  162. }
  163. if (!$this->getDataContainer()->getDateModified()) {
  164. $this->getDataContainer()->setDateModified(
  165. $this->getDataContainer()->getDateCreated()
  166. );
  167. }
  168. }
  169. /**
  170. * Set entry authors
  171. *
  172. * @param DOMDocument $dom
  173. * @param DOMElement $root
  174. * @return void
  175. */
  176. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  177. {
  178. $authors = $this->_container->getAuthors();
  179. if ((!$authors || empty($authors))) {
  180. return;
  181. }
  182. foreach ($authors as $data) {
  183. $author = $this->_dom->createElement('author');
  184. $name = $data['name'];
  185. if (array_key_exists('email', $data)) {
  186. $name = $data['email'] . ' (' . $data['name'] . ')';
  187. }
  188. $text = $dom->createTextNode($name);
  189. $author->appendChild($text);
  190. $root->appendChild($author);
  191. }
  192. }
  193. /**
  194. * Set entry enclosure
  195. *
  196. * @param DOMDocument $dom
  197. * @param DOMElement $root
  198. * @return void
  199. */
  200. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  201. {
  202. $data = $this->_container->getEnclosure();
  203. if ((!$data || empty($data))) {
  204. return;
  205. }
  206. $enclosure = $this->_dom->createElement('enclosure');
  207. $enclosure->setAttribute('type', $data['type']);
  208. $enclosure->setAttribute('length', $data['length']);
  209. $enclosure->setAttribute('url', $data['uri']);
  210. $root->appendChild($enclosure);
  211. }
  212. /**
  213. * Set link to entry
  214. *
  215. * @param DOMDocument $dom
  216. * @param DOMElement $root
  217. * @return void
  218. */
  219. protected function _setLink(DOMDocument $dom, DOMElement $root)
  220. {
  221. if(!$this->getDataContainer()->getLink()) {
  222. return;
  223. }
  224. $link = $dom->createElement('link');
  225. $root->appendChild($link);
  226. $text = $dom->createTextNode($this->getDataContainer()->getLink());
  227. $link->appendChild($text);
  228. }
  229. /**
  230. * Set entry identifier
  231. *
  232. * @param DOMDocument $dom
  233. * @param DOMElement $root
  234. * @return void
  235. */
  236. protected function _setId(DOMDocument $dom, DOMElement $root)
  237. {
  238. if(!$this->getDataContainer()->getId()
  239. && !$this->getDataContainer()->getLink()) {
  240. return;
  241. }
  242. $id = $dom->createElement('guid');
  243. $root->appendChild($id);
  244. if (!$this->getDataContainer()->getId()) {
  245. $this->getDataContainer()->setId(
  246. $this->getDataContainer()->getLink());
  247. }
  248. $text = $dom->createTextNode($this->getDataContainer()->getId());
  249. $id->appendChild($text);
  250. if (!Zend_Uri::check($this->getDataContainer()->getId())) {
  251. $id->setAttribute('isPermaLink', 'false');
  252. }
  253. }
  254. /**
  255. * Set link to entry comments
  256. *
  257. * @param DOMDocument $dom
  258. * @param DOMElement $root
  259. * @return void
  260. */
  261. protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
  262. {
  263. $link = $this->getDataContainer()->getCommentLink();
  264. if (!$link) {
  265. return;
  266. }
  267. $clink = $this->_dom->createElement('comments');
  268. $text = $dom->createTextNode($link);
  269. $clink->appendChild($text);
  270. $root->appendChild($clink);
  271. }
  272. /**
  273. * Set entry categories
  274. *
  275. * @param DOMDocument $dom
  276. * @param DOMElement $root
  277. * @return void
  278. */
  279. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  280. {
  281. $categories = $this->getDataContainer()->getCategories();
  282. if (!$categories) {
  283. return;
  284. }
  285. foreach ($categories as $cat) {
  286. $category = $dom->createElement('category');
  287. if (isset($cat['scheme'])) {
  288. $category->setAttribute('domain', $cat['scheme']);
  289. }
  290. $text = $dom->createCDATASection($cat['term']);
  291. $category->appendChild($text);
  292. $root->appendChild($category);
  293. }
  294. }
  295. }