Rss.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. if (!$this->getDataContainer()->getDescription()) {
  128. return;
  129. }
  130. $subtitle = $dom->createElement('description');
  131. $root->appendChild($subtitle);
  132. $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
  133. $subtitle->appendChild($text);
  134. }
  135. /**
  136. * Set date entry was last modified
  137. *
  138. * @param DOMDocument $dom
  139. * @param DOMElement $root
  140. * @return void
  141. */
  142. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  143. {
  144. if(!$this->getDataContainer()->getDateModified()) {
  145. return;
  146. }
  147. $updated = $dom->createElement('pubDate');
  148. $root->appendChild($updated);
  149. $text = $dom->createTextNode(
  150. $this->getDataContainer()->getDateModified()->get(Zend_Date::RSS)
  151. );
  152. $updated->appendChild($text);
  153. }
  154. /**
  155. * Set date entry was created
  156. *
  157. * @param DOMDocument $dom
  158. * @param DOMElement $root
  159. * @return void
  160. */
  161. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  162. {
  163. if (!$this->getDataContainer()->getDateCreated()) {
  164. return;
  165. }
  166. if (!$this->getDataContainer()->getDateModified()) {
  167. $this->getDataContainer()->setDateModified(
  168. $this->getDataContainer()->getDateCreated()
  169. );
  170. }
  171. }
  172. /**
  173. * Set entry authors
  174. *
  175. * @param DOMDocument $dom
  176. * @param DOMElement $root
  177. * @return void
  178. */
  179. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  180. {
  181. $authors = $this->_container->getAuthors();
  182. if ((!$authors || empty($authors))) {
  183. return;
  184. }
  185. foreach ($authors as $data) {
  186. $author = $this->_dom->createElement('author');
  187. $name = $data['name'];
  188. if (array_key_exists('email', $data)) {
  189. $name = $data['email'] . ' (' . $data['name'] . ')';
  190. }
  191. $text = $dom->createTextNode($name);
  192. $author->appendChild($text);
  193. $root->appendChild($author);
  194. }
  195. }
  196. /**
  197. * Set entry enclosure
  198. *
  199. * @param DOMDocument $dom
  200. * @param DOMElement $root
  201. * @return void
  202. */
  203. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  204. {
  205. $data = $this->_container->getEnclosure();
  206. if ((!$data || empty($data))) {
  207. return;
  208. }
  209. $enclosure = $this->_dom->createElement('enclosure');
  210. $enclosure->setAttribute('type', $data['type']);
  211. $enclosure->setAttribute('length', $data['length']);
  212. $enclosure->setAttribute('url', $data['uri']);
  213. $root->appendChild($enclosure);
  214. }
  215. /**
  216. * Set link to entry
  217. *
  218. * @param DOMDocument $dom
  219. * @param DOMElement $root
  220. * @return void
  221. */
  222. protected function _setLink(DOMDocument $dom, DOMElement $root)
  223. {
  224. if(!$this->getDataContainer()->getLink()) {
  225. return;
  226. }
  227. $link = $dom->createElement('link');
  228. $root->appendChild($link);
  229. $text = $dom->createTextNode($this->getDataContainer()->getLink());
  230. $link->appendChild($text);
  231. }
  232. /**
  233. * Set entry identifier
  234. *
  235. * @param DOMDocument $dom
  236. * @param DOMElement $root
  237. * @return void
  238. */
  239. protected function _setId(DOMDocument $dom, DOMElement $root)
  240. {
  241. if(!$this->getDataContainer()->getId()
  242. && !$this->getDataContainer()->getLink()) {
  243. return;
  244. }
  245. $id = $dom->createElement('guid');
  246. $root->appendChild($id);
  247. if (!$this->getDataContainer()->getId()) {
  248. $this->getDataContainer()->setId(
  249. $this->getDataContainer()->getLink());
  250. }
  251. $text = $dom->createTextNode($this->getDataContainer()->getId());
  252. $id->appendChild($text);
  253. if (!Zend_Uri::check($this->getDataContainer()->getId())) {
  254. $id->setAttribute('isPermaLink', 'false');
  255. }
  256. }
  257. /**
  258. * Set link to entry comments
  259. *
  260. * @param DOMDocument $dom
  261. * @param DOMElement $root
  262. * @return void
  263. */
  264. protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
  265. {
  266. $link = $this->getDataContainer()->getCommentLink();
  267. if (!$link) {
  268. return;
  269. }
  270. $clink = $this->_dom->createElement('comments');
  271. $text = $dom->createTextNode($link);
  272. $clink->appendChild($text);
  273. $root->appendChild($clink);
  274. }
  275. /**
  276. * Set entry categories
  277. *
  278. * @param DOMDocument $dom
  279. * @param DOMElement $root
  280. * @return void
  281. */
  282. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  283. {
  284. $categories = $this->getDataContainer()->getCategories();
  285. if (!$categories) {
  286. return;
  287. }
  288. foreach ($categories as $cat) {
  289. $category = $dom->createElement('category');
  290. if (isset($cat['scheme'])) {
  291. $category->setAttribute('domain', $cat['scheme']);
  292. }
  293. $text = $dom->createCDATASection($cat['term']);
  294. $category->appendChild($text);
  295. $root->appendChild($category);
  296. }
  297. }
  298. }