Atom.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. require_once 'Zend/Feed/Writer/Renderer/Feed/Atom/Source.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Feed_Writer
  29. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Feed_Writer_Renderer_Entry_Atom
  33. extends Zend_Feed_Writer_Renderer_RendererAbstract
  34. implements Zend_Feed_Writer_Renderer_RendererInterface
  35. {
  36. /**
  37. * Constructor
  38. *
  39. * @param Zend_Feed_Writer_Entry $container
  40. * @return void
  41. */
  42. public function __construct (Zend_Feed_Writer_Entry $container)
  43. {
  44. parent::__construct($container);
  45. }
  46. /**
  47. * Render atom entry
  48. *
  49. * @return Zend_Feed_Writer_Renderer_Entry_Atom
  50. */
  51. public function render()
  52. {
  53. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  54. $this->_dom->formatOutput = true;
  55. $entry = $this->_dom->createElementNS(Zend_Feed_Writer::NAMESPACE_ATOM_10, 'entry');
  56. $this->_dom->appendChild($entry);
  57. $this->_setSource($this->_dom, $entry);
  58. $this->_setTitle($this->_dom, $entry);
  59. $this->_setDescription($this->_dom, $entry);
  60. $this->_setDateCreated($this->_dom, $entry);
  61. $this->_setDateModified($this->_dom, $entry);
  62. $this->_setLink($this->_dom, $entry);
  63. $this->_setId($this->_dom, $entry);
  64. $this->_setAuthors($this->_dom, $entry);
  65. $this->_setEnclosure($this->_dom, $entry);
  66. $this->_setContent($this->_dom, $entry);
  67. $this->_setCategories($this->_dom, $entry);
  68. foreach ($this->_extensions as $ext) {
  69. $ext->setType($this->getType());
  70. $ext->setRootElement($this->getRootElement());
  71. $ext->setDomDocument($this->getDomDocument(), $entry);
  72. $ext->render();
  73. }
  74. return $this;
  75. }
  76. /**
  77. * Set entry title
  78. *
  79. * @param DOMDocument $dom
  80. * @param DOMElement $root
  81. * @return void
  82. */
  83. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  84. {
  85. if(!$this->getDataContainer()->getTitle()) {
  86. require_once 'Zend/Feed/Exception.php';
  87. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  88. . ' atom:title element but a title has not been set';
  89. $exception = new Zend_Feed_Exception($message);
  90. if (!$this->_ignoreExceptions) {
  91. throw $exception;
  92. } else {
  93. $this->_exceptions[] = $exception;
  94. return;
  95. }
  96. }
  97. $title = $dom->createElement('title');
  98. $root->appendChild($title);
  99. $title->setAttribute('type', 'html');
  100. $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
  101. $title->appendChild($cdata);
  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. return; // unless src content or base64
  114. }
  115. $subtitle = $dom->createElement('summary');
  116. $root->appendChild($subtitle);
  117. $subtitle->setAttribute('type', 'html');
  118. $cdata = $dom->createCDATASection(
  119. $this->getDataContainer()->getDescription()
  120. );
  121. $subtitle->appendChild($cdata);
  122. }
  123. /**
  124. * Set date entry was modified
  125. *
  126. * @param DOMDocument $dom
  127. * @param DOMElement $root
  128. * @return void
  129. */
  130. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  131. {
  132. if(!$this->getDataContainer()->getDateModified()) {
  133. require_once 'Zend/Feed/Exception.php';
  134. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  135. . ' atom:updated element but a modification date has not been set';
  136. $exception = new Zend_Feed_Exception($message);
  137. if (!$this->_ignoreExceptions) {
  138. throw $exception;
  139. } else {
  140. $this->_exceptions[] = $exception;
  141. return;
  142. }
  143. }
  144. $updated = $dom->createElement('updated');
  145. $root->appendChild($updated);
  146. $text = $dom->createTextNode(
  147. $this->getDataContainer()->getDateModified()->get(Zend_Date::ISO_8601)
  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. $el = $dom->createElement('published');
  164. $root->appendChild($el);
  165. $text = $dom->createTextNode(
  166. $this->getDataContainer()->getDateCreated()->get(Zend_Date::ISO_8601)
  167. );
  168. $el->appendChild($text);
  169. }
  170. /**
  171. * Set entry authors
  172. *
  173. * @param DOMDocument $dom
  174. * @param DOMElement $root
  175. * @return void
  176. */
  177. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  178. {
  179. $authors = $this->_container->getAuthors();
  180. if ((!$authors || empty($authors))) {
  181. /**
  182. * This will actually trigger an Exception at the feed level if
  183. * a feed level author is not set.
  184. */
  185. return;
  186. }
  187. foreach ($authors as $data) {
  188. $author = $this->_dom->createElement('author');
  189. $name = $this->_dom->createElement('name');
  190. $author->appendChild($name);
  191. $root->appendChild($author);
  192. $text = $dom->createTextNode($data['name']);
  193. $name->appendChild($text);
  194. if (array_key_exists('email', $data)) {
  195. $email = $this->_dom->createElement('email');
  196. $author->appendChild($email);
  197. $text = $dom->createTextNode($data['email']);
  198. $email->appendChild($text);
  199. }
  200. if (array_key_exists('uri', $data)) {
  201. $uri = $this->_dom->createElement('uri');
  202. $author->appendChild($uri);
  203. $text = $dom->createTextNode($data['uri']);
  204. $uri->appendChild($text);
  205. }
  206. }
  207. }
  208. /**
  209. * Set entry enclosure
  210. *
  211. * @param DOMDocument $dom
  212. * @param DOMElement $root
  213. * @return void
  214. */
  215. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  216. {
  217. $data = $this->_container->getEnclosure();
  218. if ((!$data || empty($data))) {
  219. return;
  220. }
  221. $enclosure = $this->_dom->createElement('link');
  222. $enclosure->setAttribute('rel', 'enclosure');
  223. $enclosure->setAttribute('type', $data['type']);
  224. $enclosure->setAttribute('length', $data['length']);
  225. $enclosure->setAttribute('href', $data['uri']);
  226. $root->appendChild($enclosure);
  227. }
  228. protected function _setLink(DOMDocument $dom, DOMElement $root)
  229. {
  230. if(!$this->getDataContainer()->getLink()) {
  231. return;
  232. }
  233. $link = $dom->createElement('link');
  234. $root->appendChild($link);
  235. $link->setAttribute('rel', 'alternate');
  236. $link->setAttribute('type', 'text/html');
  237. $link->setAttribute('href', $this->getDataContainer()->getLink());
  238. }
  239. /**
  240. * Set entry identifier
  241. *
  242. * @param DOMDocument $dom
  243. * @param DOMElement $root
  244. * @return void
  245. */
  246. protected function _setId(DOMDocument $dom, DOMElement $root)
  247. {
  248. if(!$this->getDataContainer()->getId()
  249. && !$this->getDataContainer()->getLink()) {
  250. require_once 'Zend/Feed/Exception.php';
  251. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  252. . 'atom:id element, or as an alternative, we can use the same '
  253. . 'value as atom:link however neither a suitable link nor an '
  254. . 'id have been set';
  255. $exception = new Zend_Feed_Exception($message);
  256. if (!$this->_ignoreExceptions) {
  257. throw $exception;
  258. } else {
  259. $this->_exceptions[] = $exception;
  260. return;
  261. }
  262. }
  263. if (!$this->getDataContainer()->getId()) {
  264. $this->getDataContainer()->setId(
  265. $this->getDataContainer()->getLink());
  266. }
  267. if (!Zend_Uri::check($this->getDataContainer()->getId()) &&
  268. !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", $this->getDataContainer()->getId())) {
  269. require_once 'Zend/Feed/Exception.php';
  270. throw new Zend_Feed_Exception('Atom 1.0 IDs must be a valid URI/IRI');
  271. }
  272. $id = $dom->createElement('id');
  273. $root->appendChild($id);
  274. $text = $dom->createTextNode($this->getDataContainer()->getId());
  275. $id->appendChild($text);
  276. }
  277. /**
  278. * Set entry content
  279. *
  280. * @param DOMDocument $dom
  281. * @param DOMElement $root
  282. * @return void
  283. */
  284. protected function _setContent(DOMDocument $dom, DOMElement $root)
  285. {
  286. $content = $this->getDataContainer()->getContent();
  287. if (!$content && !$this->getDataContainer()->getLink()) {
  288. require_once 'Zend/Feed/Exception.php';
  289. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  290. . 'atom:content element, or as an alternative, at least one link '
  291. . 'with a rel attribute of "alternate" to indicate an alternate '
  292. . 'method to consume the content.';
  293. $exception = new Zend_Feed_Exception($message);
  294. if (!$this->_ignoreExceptions) {
  295. throw $exception;
  296. } else {
  297. $this->_exceptions[] = $exception;
  298. return;
  299. }
  300. }
  301. if (!$content) {
  302. return;
  303. }
  304. $element = $dom->createElement('content');
  305. $element->setAttribute('type', 'xhtml');
  306. $xhtmlElement = $this->_loadXhtml($content);
  307. $xhtml = $dom->importNode($xhtmlElement, true);
  308. $element->appendChild($xhtml);
  309. $root->appendChild($element);
  310. }
  311. /**
  312. * Load a HTML string and attempt to normalise to XML
  313. */
  314. protected function _loadXhtml($content)
  315. {
  316. $xhtml = '';
  317. if (class_exists('tidy', false)) {
  318. $tidy = new tidy;
  319. $config = array(
  320. 'output-xhtml' => true,
  321. 'show-body-only' => true
  322. );
  323. $encoding = str_replace('-', '', $this->getEncoding());
  324. $tidy->parseString($content, $config, $encoding);
  325. $tidy->cleanRepair();
  326. $xhtml = (string) $tidy;
  327. } else {
  328. $xhtml = $content;
  329. }
  330. $xhtml = preg_replace(array(
  331. "/(<[\/]?)([a-zA-Z]+)/"
  332. ), '$1xhtml:$2', $xhtml);
  333. $dom = new DOMDocument('1.0', $this->getEncoding());
  334. $dom->loadXML('<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">'
  335. . $xhtml . '</xhtml:div>');
  336. return $dom->documentElement;
  337. }
  338. /**
  339. * Set entry cateories
  340. *
  341. * @param DOMDocument $dom
  342. * @param DOMElement $root
  343. * @return void
  344. */
  345. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  346. {
  347. $categories = $this->getDataContainer()->getCategories();
  348. if (!$categories) {
  349. return;
  350. }
  351. foreach ($categories as $cat) {
  352. $category = $dom->createElement('category');
  353. $category->setAttribute('term', $cat['term']);
  354. if (isset($cat['label'])) {
  355. $category->setAttribute('label', $cat['label']);
  356. } else {
  357. $category->setAttribute('label', $cat['term']);
  358. }
  359. if (isset($cat['scheme'])) {
  360. $category->setAttribute('scheme', $cat['scheme']);
  361. }
  362. $root->appendChild($category);
  363. }
  364. }
  365. /**
  366. * Append Source element (Atom 1.0 Feed Metadata)
  367. *
  368. * @param DOMDocument $dom
  369. * @param DOMElement $root
  370. * @return void
  371. */
  372. protected function _setSource(DOMDocument $dom, DOMElement $root)
  373. {
  374. $source = $this->getDataContainer()->getSource();
  375. if (!$source) {
  376. return;
  377. }
  378. $renderer = new Zend_Feed_Writer_Renderer_Feed_Atom_Source($source);
  379. $renderer->setType($this->getType());
  380. $element = $renderer->render()->getElement();
  381. $imported = $dom->importNode($element, true);
  382. $root->appendChild($imported);
  383. }
  384. }