Writer.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * @category Zend
  23. * @package Zend_Feed_Writer
  24. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Feed_Writer
  28. {
  29. /**
  30. * Namespace constants
  31. */
  32. const NAMESPACE_ATOM_03 = 'http://purl.org/atom/ns#';
  33. const NAMESPACE_ATOM_10 = 'http://www.w3.org/2005/Atom';
  34. const NAMESPACE_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  35. const NAMESPACE_RSS_090 = 'http://my.netscape.com/rdf/simple/0.9/';
  36. const NAMESPACE_RSS_10 = 'http://purl.org/rss/1.0/';
  37. /**
  38. * Feed type constants
  39. */
  40. const TYPE_ANY = 'any';
  41. const TYPE_ATOM_03 = 'atom-03';
  42. const TYPE_ATOM_10 = 'atom-10';
  43. const TYPE_ATOM_ANY = 'atom';
  44. const TYPE_RSS_090 = 'rss-090';
  45. const TYPE_RSS_091 = 'rss-091';
  46. const TYPE_RSS_091_NETSCAPE = 'rss-091n';
  47. const TYPE_RSS_091_USERLAND = 'rss-091u';
  48. const TYPE_RSS_092 = 'rss-092';
  49. const TYPE_RSS_093 = 'rss-093';
  50. const TYPE_RSS_094 = 'rss-094';
  51. const TYPE_RSS_10 = 'rss-10';
  52. const TYPE_RSS_20 = 'rss-20';
  53. const TYPE_RSS_ANY = 'rss';
  54. /**
  55. * PluginLoader instance used by component
  56. *
  57. * @var Zend_Loader_PluginLoader_Interface
  58. */
  59. protected static $_pluginLoader = null;
  60. /**
  61. * Path on which to search for Extension classes
  62. *
  63. * @var array
  64. */
  65. protected static $_prefixPaths = array();
  66. /**
  67. * Array of registered extensions by class postfix (after the base class
  68. * name) across four categories - data containers and renderers for entry
  69. * and feed levels.
  70. *
  71. * @var array
  72. */
  73. protected static $_extensions = array(
  74. 'entry' => array(),
  75. 'feed' => array(),
  76. 'entryRenderer' => array(),
  77. 'feedRenderer' => array()
  78. );
  79. /**
  80. * Set plugin loader for use with Extensions
  81. *
  82. * @param Zend_Loader_PluginLoader_Interface
  83. */
  84. public static function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
  85. {
  86. self::$_pluginLoader = $loader;
  87. }
  88. /**
  89. * Get plugin loader for use with Extensions
  90. *
  91. * @return Zend_Loader_PluginLoader_Interface
  92. */
  93. public static function getPluginLoader()
  94. {
  95. if (!isset(self::$_pluginLoader)) {
  96. require_once 'Zend/Loader/PluginLoader.php';
  97. self::$_pluginLoader = new Zend_Loader_PluginLoader(array(
  98. 'Zend_Feed_Writer_Extension_' => 'Zend/Feed/Writer/Extension/',
  99. ));
  100. }
  101. return self::$_pluginLoader;
  102. }
  103. /**
  104. * Add prefix path for loading Extensions
  105. *
  106. * @param string $prefix
  107. * @param string $path
  108. * @return void
  109. */
  110. public static function addPrefixPath($prefix, $path)
  111. {
  112. $prefix = rtrim($prefix, '_');
  113. $path = rtrim($path, DIRECTORY_SEPARATOR);
  114. self::getPluginLoader()->addPrefixPath($prefix, $path);
  115. }
  116. /**
  117. * Add multiple Extension prefix paths at once
  118. *
  119. * @param array $spec
  120. * @return void
  121. */
  122. public static function addPrefixPaths(array $spec)
  123. {
  124. if (isset($spec['prefix']) && isset($spec['path'])) {
  125. self::addPrefixPath($spec['prefix'], $spec['path']);
  126. }
  127. foreach ($spec as $prefixPath) {
  128. if (isset($prefixPath['prefix']) && isset($prefixPath['path'])) {
  129. self::addPrefixPath($prefixPath['prefix'], $prefixPath['path']);
  130. }
  131. }
  132. }
  133. /**
  134. * Register an Extension by name
  135. *
  136. * @param string $name
  137. * @return void
  138. * @throws Zend_Feed_Exception if unable to resolve Extension class
  139. */
  140. public static function registerExtension($name)
  141. {
  142. $feedName = $name . '_Feed';
  143. $entryName = $name . '_Entry';
  144. $feedRendererName = $name . '_Renderer_Feed';
  145. $entryRendererName = $name . '_Renderer_Entry';
  146. if (self::isRegistered($name)) {
  147. if (self::getPluginLoader()->isLoaded($feedName)
  148. || self::getPluginLoader()->isLoaded($entryName)
  149. || self::getPluginLoader()->isLoaded($feedRendererName)
  150. || self::getPluginLoader()->isLoaded($entryRendererName)) {
  151. return;
  152. }
  153. }
  154. try {
  155. self::getPluginLoader()->load($feedName);
  156. self::$_extensions['feed'][] = $feedName;
  157. } catch (Zend_Loader_PluginLoader_Exception $e) {
  158. }
  159. try {
  160. self::getPluginLoader()->load($entryName);
  161. self::$_extensions['entry'][] = $entryName;
  162. } catch (Zend_Loader_PluginLoader_Exception $e) {
  163. }
  164. try {
  165. self::getPluginLoader()->load($feedRendererName);
  166. self::$_extensions['feedRenderer'][] = $feedRendererName;
  167. } catch (Zend_Loader_PluginLoader_Exception $e) {
  168. }
  169. try {
  170. self::getPluginLoader()->load($entryRendererName);
  171. self::$_extensions['entryRenderer'][] = $entryRendererName;
  172. } catch (Zend_Loader_PluginLoader_Exception $e) {
  173. }
  174. if (!self::getPluginLoader()->isLoaded($feedName)
  175. && !self::getPluginLoader()->isLoaded($entryName)
  176. && !self::getPluginLoader()->isLoaded($feedRendererName)
  177. && !self::getPluginLoader()->isLoaded($entryRendererName)
  178. ) {
  179. require_once 'Zend/Feed/Exception.php';
  180. throw new Zend_Feed_Exception('Could not load extension: ' . $name
  181. . 'using Plugin Loader. Check prefix paths are configured and extension exists.');
  182. }
  183. }
  184. /**
  185. * Is a given named Extension registered?
  186. *
  187. * @param string $extensionName
  188. * @return boolean
  189. */
  190. public static function isRegistered($extensionName)
  191. {
  192. $feedName = $extensionName . '_Feed';
  193. $entryName = $extensionName . '_Entry';
  194. $feedRendererName = $extensionName . '_Renderer_Feed';
  195. $entryRendererName = $extensionName . '_Renderer_Entry';
  196. if (in_array($feedName, self::$_extensions['feed'])
  197. || in_array($entryName, self::$_extensions['entry'])
  198. || in_array($feedRendererName, self::$_extensions['feedRenderer'])
  199. || in_array($entryRendererName, self::$_extensions['entryRenderer'])
  200. ) {
  201. return true;
  202. }
  203. return false;
  204. }
  205. /**
  206. * Get a list of extensions
  207. *
  208. * @return array
  209. */
  210. public static function getExtensions()
  211. {
  212. return self::$_extensions;
  213. }
  214. /**
  215. * Reset class state to defaults
  216. *
  217. * @return void
  218. */
  219. public static function reset()
  220. {
  221. self::$_pluginLoader = null;
  222. self::$_prefixPaths = array();
  223. self::$_extensions = array(
  224. 'entry' => array(),
  225. 'feed' => array(),
  226. 'entryRenderer' => array(),
  227. 'feedRenderer' => array()
  228. );
  229. }
  230. /**
  231. * Register core (default) extensions
  232. *
  233. * @return void
  234. */
  235. public static function registerCoreExtensions()
  236. {
  237. self::registerExtension('DublinCore');
  238. self::registerExtension('Content');
  239. self::registerExtension('Atom');
  240. self::registerExtension('Slash');
  241. self::registerExtension('WellFormedWeb');
  242. self::registerExtension('Threading');
  243. self::registerExtension('ITunes');
  244. }
  245. /**
  246. * Replaces XML special characters with entities.
  247. *
  248. * @param string $string
  249. * @param string $encoding
  250. * @return string
  251. */
  252. public static function xmlentities($string, $encoding)
  253. {
  254. return str_replace('&#039;', '&apos;', htmlspecialchars(
  255. $string, ENT_QUOTES, $encoding
  256. ));
  257. }
  258. }