PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/syntax.php

https://gitlab.com/michield/dokuwiki
PHP | 310 lines | 113 code | 38 blank | 159 comment | 17 complexity | a07c5b37ef8ea86e8e5fae7b18c13c56 MD5 | raw file
  1. <?php
  2. /**
  3. * Syntax Plugin Prototype
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <andi@splitbrain.org>
  7. */
  8. // must be run within Dokuwiki
  9. if(!defined('DOKU_INC')) die();
  10. /**
  11. * All DokuWiki plugins to extend the parser/rendering mechanism
  12. * need to inherit from this class
  13. */
  14. class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode {
  15. var $allowedModesSetup = false;
  16. var $localised = false; // set to true by setupLocale() after loading language dependent strings
  17. var $lang = array(); // array to hold language dependent strings, best accessed via ->getLang()
  18. var $configloaded = false; // set to true by loadConfig() after loading plugin configuration variables
  19. var $conf = array(); // array to hold plugin settings, best accessed via ->getConf()
  20. /**
  21. * General Info
  22. *
  23. * Needs to return a associative array with the following values:
  24. *
  25. * author - Author of the plugin
  26. * email - Email address to contact the author
  27. * date - Last modified date of the plugin in YYYY-MM-DD format
  28. * name - Name of the plugin
  29. * desc - Short description of the plugin (Text only)
  30. * url - Website with more information on the plugin (eg. syntax description)
  31. */
  32. function getInfo(){
  33. $parts = explode('_',get_class($this));
  34. $info = DOKU_PLUGIN.'/'.$parts[2].'/plugin.info.txt';
  35. if(@file_exists($info)) return confToHash($info);
  36. trigger_error('getInfo() not implemented in '.get_class($this).' and '.$info.' not found', E_USER_WARNING);
  37. return array();
  38. }
  39. /**
  40. * Syntax Type
  41. *
  42. * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
  43. */
  44. function getType(){
  45. trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
  46. }
  47. /**
  48. * Allowed Mode Types
  49. *
  50. * Defines the mode types for other dokuwiki markup that maybe nested within the
  51. * plugin's own markup. Needs to return an array of one or more of the mode types
  52. * defined in $PARSER_MODES in parser.php
  53. */
  54. function getAllowedTypes() {
  55. return array();
  56. }
  57. /**
  58. * Paragraph Type
  59. *
  60. * Defines how this syntax is handled regarding paragraphs. This is important
  61. * for correct XHTML nesting. Should return one of the following:
  62. *
  63. * 'normal' - The plugin can be used inside paragraphs
  64. * 'block' - Open paragraphs need to be closed before plugin output
  65. * 'stack' - Special case. Plugin wraps other paragraphs.
  66. *
  67. * @see Doku_Handler_Block
  68. */
  69. function getPType(){
  70. return 'normal';
  71. }
  72. /**
  73. * Handler to prepare matched data for the rendering process
  74. *
  75. * This function can only pass data to render() via its return value - render()
  76. * may be not be run during the object's current life.
  77. *
  78. * Usually you should only need the $match param.
  79. *
  80. * @param string $match The text matched by the patterns
  81. * @param int $state The lexer state for the match
  82. * @param int $pos The character position of the matched text
  83. * @param Doku_Handler $handler Reference to the Doku_Handler object
  84. * @return array Return an array with all data you want to use in render
  85. */
  86. function handle($match, $state, $pos, Doku_Handler &$handler){
  87. trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
  88. }
  89. /**
  90. * Handles the actual output creation.
  91. *
  92. * The function must not assume any other of the classes methods have been run
  93. * during the object's current life. The only reliable data it receives are its
  94. * parameters.
  95. *
  96. * The function should always check for the given output format and return false
  97. * when a format isn't supported.
  98. *
  99. * $renderer contains a reference to the renderer object which is
  100. * currently handling the rendering. You need to use it for writing
  101. * the output. How this is done depends on the renderer used (specified
  102. * by $format
  103. *
  104. * The contents of the $data array depends on what the handler() function above
  105. * created
  106. *
  107. * @param $format string output format being rendered
  108. * @param $renderer Doku_Renderer reference to the current renderer object
  109. * @param $data array data created by handler()
  110. * @return boolean rendered correctly?
  111. */
  112. function render($format, Doku_Renderer &$renderer, $data) {
  113. trigger_error('render() not implemented in '.get_class($this), E_USER_WARNING);
  114. }
  115. /**
  116. * There should be no need to override these functions
  117. */
  118. function accepts($mode) {
  119. if (!$this->allowedModesSetup) {
  120. global $PARSER_MODES;
  121. $allowedModeTypes = $this->getAllowedTypes();
  122. foreach($allowedModeTypes as $mt) {
  123. $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
  124. }
  125. $idx = array_search(substr(get_class($this), 7), (array) $this->allowedModes);
  126. if ($idx !== false) {
  127. unset($this->allowedModes[$idx]);
  128. }
  129. $this->allowedModesSetup = true;
  130. }
  131. return parent::accepts($mode);
  132. }
  133. // plugin introspection methods
  134. // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
  135. function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t; }
  136. function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
  137. /**
  138. * Get the name of the component of the current class
  139. *
  140. * @return string component name
  141. */
  142. function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
  143. // localisation methods
  144. /**
  145. * getLang($id)
  146. *
  147. * use this function to access plugin language strings
  148. * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
  149. * e.g. when info plugin is querying plugins for information about themselves.
  150. *
  151. * @param string $id id of the string to be retrieved
  152. * @return string string in appropriate language or english if not available
  153. */
  154. function getLang($id) {
  155. if (!$this->localised) $this->setupLocale();
  156. return (isset($this->lang[$id]) ? $this->lang[$id] : '');
  157. }
  158. /**
  159. * locale_xhtml($id)
  160. *
  161. * retrieve a language dependent wiki page and pass to xhtml renderer for display
  162. * plugin equivalent of p_locale_xhtml()
  163. *
  164. * @param string $id id of language dependent wiki page
  165. * @return string parsed contents of the wiki page in xhtml format
  166. */
  167. function locale_xhtml($id) {
  168. return p_cached_output($this->localFN($id));
  169. }
  170. /**
  171. * localFN($id)
  172. * prepends appropriate path for a language dependent filename
  173. * plugin equivalent of localFN()
  174. */
  175. function localFN($id) {
  176. global $conf;
  177. $plugin = $this->getPluginName();
  178. $file = DOKU_CONF.'/plugin_lang/'.$plugin.'/'.$conf['lang'].'/'.$id.'.txt';
  179. if (!@file_exists($file)){
  180. $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
  181. if(!@file_exists($file)){
  182. //fall back to english
  183. $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
  184. }
  185. }
  186. return $file;
  187. }
  188. /**
  189. * setupLocale()
  190. * reads all the plugins language dependent strings into $this->lang
  191. * this function is automatically called by getLang()
  192. */
  193. function setupLocale() {
  194. if ($this->localised) return;
  195. global $conf; // definitely don't invoke "global $lang"
  196. $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
  197. $lang = array();
  198. // don't include once, in case several plugin components require the same language file
  199. @include($path.'en/lang.php');
  200. if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
  201. $this->lang = $lang;
  202. $this->localised = true;
  203. }
  204. // configuration methods
  205. /**
  206. * getConf($setting)
  207. *
  208. * use this function to access plugin configuration variables
  209. */
  210. function getConf($setting) {
  211. if(!$this->configloaded) { $this->loadConfig(); }
  212. return $this->conf[$setting];
  213. }
  214. /**
  215. * loadConfig()
  216. * merges the plugin's default settings with any local settings
  217. * this function is automatically called through getConf()
  218. */
  219. function loadConfig() {
  220. global $conf;
  221. $defaults = $this->readDefaultSettings();
  222. $plugin = $this->getPluginName();
  223. foreach($defaults as $key => $value) {
  224. if(isset($conf['plugin'][$plugin][$key])) continue;
  225. $conf['plugin'][$plugin][$key] = $value;
  226. }
  227. $this->configloaded = true;
  228. $this->conf =& $conf['plugin'][$plugin];
  229. }
  230. /**
  231. * read the plugin's default configuration settings from conf/default.php
  232. * this function is automatically called through getConf()
  233. *
  234. * @return array setting => value
  235. */
  236. function readDefaultSettings() {
  237. $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
  238. $conf = array();
  239. if(@file_exists($path.'default.php')) {
  240. include($path.'default.php');
  241. }
  242. return $conf;
  243. }
  244. /**
  245. * Loads a given helper plugin (if enabled)
  246. *
  247. * @author Esther Brunner <wikidesign@gmail.com>
  248. *
  249. * @param string $name name of plugin to load
  250. * @param bool $msg if a message should be displayed in case the plugin is not available
  251. *
  252. * @return object helper plugin object
  253. */
  254. function loadHelper($name, $msg = true) {
  255. if(!plugin_isdisabled($name)) {
  256. $obj = plugin_load('helper', $name);
  257. } else {
  258. $obj = null;
  259. }
  260. if(is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.", -1);
  261. return $obj;
  262. }
  263. /**
  264. * Allow the plugin to prevent DokuWiki from reusing an instance
  265. *
  266. * @return bool false if the plugin has to be instantiated
  267. */
  268. function isSingleton() {
  269. return true;
  270. }
  271. }
  272. //Setup VIM: ex: et ts=4 :