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