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