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