Rss.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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-2009 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-2009 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. $entry = $this->_dom->createElement('item');
  55. $this->_dom->appendChild($entry);
  56. $this->_setTitle($this->_dom, $entry);
  57. $this->_setDescription($this->_dom, $entry);
  58. $this->_setDateCreated($this->_dom, $entry);
  59. $this->_setDateModified($this->_dom, $entry);
  60. $this->_setLink($this->_dom, $entry);
  61. $this->_setId($this->_dom, $entry);
  62. $this->_setAuthors($this->_dom, $entry);
  63. $this->_setEnclosure($this->_dom, $entry);
  64. $this->_setCommentLink($this->_dom, $entry);
  65. foreach ($this->_extensions as $ext) {
  66. $ext->setType($this->getType());
  67. $ext->setRootElement($this->getRootElement());
  68. $ext->setDomDocument($this->getDomDocument(), $entry);
  69. $ext->render();
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Set entry title
  75. *
  76. * @param DOMDocument $dom
  77. * @param DOMElement $root
  78. * @return void
  79. */
  80. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  81. {
  82. if(!$this->getDataContainer()->getDescription()
  83. && !$this->getDataContainer()->getTitle()) {
  84. require_once 'Zend/Feed/Exception.php';
  85. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  86. . ' title element but a title has not been set. In addition, there'
  87. . ' is no description as required in the absence of a title.';
  88. $exception = new Zend_Feed_Exception($message);
  89. if (!$this->_ignoreExceptions) {
  90. throw $exception;
  91. } else {
  92. $this->_exceptions[] = $exception;
  93. return;
  94. }
  95. }
  96. $title = $dom->createElement('title');
  97. $root->appendChild($title);
  98. $title->nodeValue = htmlentities(
  99. $this->getDataContainer()->getTitle(),
  100. ENT_QUOTES,
  101. $this->getDataContainer()->getEncoding()
  102. );
  103. }
  104. /**
  105. * Set entry description
  106. *
  107. * @param DOMDocument $dom
  108. * @param DOMElement $root
  109. * @return void
  110. */
  111. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  112. {
  113. if(!$this->getDataContainer()->getDescription()
  114. && !$this->getDataContainer()->getTitle()) {
  115. require_once 'Zend/Feed/Exception.php';
  116. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  117. . ' description element but a description has not been set. In'
  118. . ' addition, there is no title element as required in the absence'
  119. . ' of a description.';
  120. $exception = new Zend_Feed_Exception($message);
  121. if (!$this->_ignoreExceptions) {
  122. throw $exception;
  123. } else {
  124. $this->_exceptions[] = $exception;
  125. return;
  126. }
  127. }
  128. $subtitle = $dom->createElement('description');
  129. $root->appendChild($subtitle);
  130. $subtitle->nodeValue = htmlentities(
  131. $this->getDataContainer()->getDescription(),
  132. ENT_QUOTES,
  133. $this->getDataContainer()->getEncoding()
  134. );
  135. }
  136. /**
  137. * Set date entry was last modified
  138. *
  139. * @param DOMDocument $dom
  140. * @param DOMElement $root
  141. * @return void
  142. */
  143. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  144. {
  145. if(!$this->getDataContainer()->getDateModified()) {
  146. return;
  147. }
  148. $updated = $dom->createElement('pubDate');
  149. $root->appendChild($updated);
  150. $updated->nodeValue = $this->getDataContainer()->getDateModified()
  151. ->get(Zend_Date::RSS);
  152. }
  153. /**
  154. * Set date entry was created
  155. *
  156. * @param DOMDocument $dom
  157. * @param DOMElement $root
  158. * @return void
  159. */
  160. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  161. {
  162. if (!$this->getDataContainer()->getDateCreated()) {
  163. return;
  164. }
  165. if (!$this->getDataContainer()->getDateModified()) {
  166. $this->getDataContainer()->setDateModified(
  167. $this->getDataContainer()->getDateCreated()
  168. );
  169. }
  170. }
  171. /**
  172. * Set entry authors
  173. *
  174. * @param DOMDocument $dom
  175. * @param DOMElement $root
  176. * @return void
  177. */
  178. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  179. {
  180. $authors = $this->_container->getAuthors();
  181. if ((!$authors || empty($authors))) {
  182. return;
  183. }
  184. foreach ($authors as $data) {
  185. $author = $this->_dom->createElement('author');
  186. $name = $data['name'];
  187. if (array_key_exists('email', $data)) {
  188. $name = $data['email'] . ' (' . $data['name'] . ')';
  189. }
  190. $author->nodeValue = $name;
  191. $root->appendChild($author);
  192. }
  193. }
  194. /**
  195. * Set entry enclosure
  196. *
  197. * @param DOMDocument $dom
  198. * @param DOMElement $root
  199. * @return void
  200. */
  201. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  202. {
  203. $data = $this->_container->getEnclosure();
  204. if ((!$data || empty($data))) {
  205. return;
  206. }
  207. $enclosure = $this->_dom->createElement('enclosure');
  208. $enclosure->setAttribute('type', $data['type']);
  209. $enclosure->setAttribute('length', $data['length']);
  210. $enclosure->setAttribute('url', $data['uri']);
  211. $root->appendChild($enclosure);
  212. }
  213. /**
  214. * Set link to entry
  215. *
  216. * @param DOMDocument $dom
  217. * @param DOMElement $root
  218. * @return void
  219. */
  220. protected function _setLink(DOMDocument $dom, DOMElement $root)
  221. {
  222. if(!$this->getDataContainer()->getLink()) {
  223. return;
  224. }
  225. $link = $dom->createElement('link');
  226. $root->appendChild($link);
  227. $link->nodeValue = $this->getDataContainer()->getLink();
  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. $id->nodeValue = $this->getDataContainer()->getId();
  249. if (!Zend_Uri::check($this->getDataContainer()->getId())) {
  250. $id->setAttribute('isPermaLink', 'false');
  251. }
  252. }
  253. /**
  254. * Set link to entry comments
  255. *
  256. * @param DOMDocument $dom
  257. * @param DOMElement $root
  258. * @return void
  259. */
  260. protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
  261. {
  262. $link = $this->getDataContainer()->getCommentLink();
  263. if (!$link) {
  264. return;
  265. }
  266. $clink = $this->_dom->createElement('comments');
  267. $clink->nodeValue = $link;
  268. $root->appendChild($clink);
  269. }
  270. }