Rss.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. public function __construct (Zend_Feed_Writer_Entry $container)
  36. {
  37. parent::__construct($container);
  38. }
  39. public function render()
  40. {
  41. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  42. $this->_dom->formatOutput = true;
  43. $entry = $this->_dom->createElement('item');
  44. $this->_dom->appendChild($entry);
  45. $this->_setTitle($this->_dom, $entry);
  46. $this->_setDescription($this->_dom, $entry);
  47. $this->_setDateCreated($this->_dom, $entry);
  48. $this->_setDateModified($this->_dom, $entry);
  49. $this->_setLink($this->_dom, $entry);
  50. $this->_setId($this->_dom, $entry);
  51. $this->_setAuthors($this->_dom, $entry);
  52. $this->_setEnclosure($this->_dom, $entry);
  53. $this->_setCommentLink($this->_dom, $entry);
  54. foreach ($this->_extensions as $ext) {
  55. $ext->setType($this->getType());
  56. $ext->setRootElement($this->getRootElement());
  57. $ext->setDomDocument($this->getDomDocument(), $entry);
  58. $ext->render();
  59. }
  60. return $this;
  61. }
  62. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  63. {
  64. if(!$this->getDataContainer()->getDescription()
  65. && !$this->getDataContainer()->getTitle()) {
  66. require_once 'Zend/Feed/Exception.php';
  67. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  68. . ' title element but a title has not been set. In addition, there'
  69. . ' is no description as required in the absence of a title.';
  70. $exception = new Zend_Feed_Exception($message);
  71. if (!$this->_ignoreExceptions) {
  72. throw $exception;
  73. } else {
  74. $this->_exceptions[] = $exception;
  75. return;
  76. }
  77. }
  78. $title = $dom->createElement('title');
  79. $root->appendChild($title);
  80. $title->nodeValue = htmlentities(
  81. $this->getDataContainer()->getTitle(),
  82. ENT_QUOTES,
  83. $this->getDataContainer()->getEncoding()
  84. );
  85. }
  86. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  87. {
  88. if(!$this->getDataContainer()->getDescription()
  89. && !$this->getDataContainer()->getTitle()) {
  90. require_once 'Zend/Feed/Exception.php';
  91. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  92. . ' description element but a description has not been set. In'
  93. . ' addition, there is no title element as required in the absence'
  94. . ' of a description.';
  95. $exception = new Zend_Feed_Exception($message);
  96. if (!$this->_ignoreExceptions) {
  97. throw $exception;
  98. } else {
  99. $this->_exceptions[] = $exception;
  100. return;
  101. }
  102. }
  103. $subtitle = $dom->createElement('description');
  104. $root->appendChild($subtitle);
  105. $subtitle->nodeValue = htmlentities(
  106. $this->getDataContainer()->getDescription(),
  107. ENT_QUOTES,
  108. $this->getDataContainer()->getEncoding()
  109. );
  110. }
  111. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  112. {
  113. if(!$this->getDataContainer()->getDateModified()) {
  114. return;
  115. }
  116. $updated = $dom->createElement('pubDate');
  117. $root->appendChild($updated);
  118. $updated->nodeValue = $this->getDataContainer()->getDateModified()
  119. ->get(Zend_Date::RSS);
  120. }
  121. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  122. {
  123. if (!$this->getDataContainer()->getDateCreated()) {
  124. return;
  125. }
  126. if (!$this->getDataContainer()->getDateModified()) {
  127. $this->getDataContainer()->setDateModified(
  128. $this->getDataContainer()->getDateCreated()
  129. );
  130. }
  131. }
  132. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  133. {
  134. $authors = $this->_container->getAuthors();
  135. if ((!$authors || empty($authors))) {
  136. return;
  137. }
  138. foreach ($authors as $data) {
  139. $author = $this->_dom->createElement('author');
  140. $name = $data['name'];
  141. if (array_key_exists('email', $data)) {
  142. $name = $data['email'] . ' (' . $data['name'] . ')';
  143. }
  144. $author->nodeValue = $name;
  145. $root->appendChild($author);
  146. }
  147. }
  148. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  149. {
  150. $data = $this->_container->getEnclosure();
  151. if ((!$data || empty($data))) {
  152. return;
  153. }
  154. $enclosure = $this->_dom->createElement('enclosure');
  155. $enclosure->setAttribute('type', $data['type']);
  156. $enclosure->setAttribute('length', $data['length']);
  157. $enclosure->setAttribute('url', $data['uri']);
  158. $root->appendChild($enclosure);
  159. }
  160. protected function _setLink(DOMDocument $dom, DOMElement $root)
  161. {
  162. if(!$this->getDataContainer()->getLink()) {
  163. return;
  164. }
  165. $link = $dom->createElement('link');
  166. $root->appendChild($link);
  167. $link->nodeValue = $this->getDataContainer()->getLink();
  168. }
  169. protected function _setId(DOMDocument $dom, DOMElement $root)
  170. {
  171. if(!$this->getDataContainer()->getId()
  172. && !$this->getDataContainer()->getLink()) {
  173. return;
  174. }
  175. $id = $dom->createElement('guid');
  176. $root->appendChild($id);
  177. if (!$this->getDataContainer()->getId()) {
  178. $this->getDataContainer()->setId(
  179. $this->getDataContainer()->getLink());
  180. }
  181. $id->nodeValue = $this->getDataContainer()->getId();
  182. if (!Zend_Uri::check($this->getDataContainer()->getId())) {
  183. $id->setAttribute('isPermaLink', 'false');
  184. }
  185. }
  186. protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
  187. {
  188. $link = $this->getDataContainer()->getCommentLink();
  189. if (!$link) {
  190. return;
  191. }
  192. $clink = $this->_dom->createElement('comments');
  193. $clink->nodeValue = $link;
  194. $root->appendChild($clink);
  195. }
  196. }