Added loadHelper() to DokuWiki_Syntax_Plugin.
[dokuwiki.git] / lib / plugins / syntax.php
blobf3f234316444a0c2232b646902665732317f474f
1 <?php
2 /**
3 * Syntax Plugin Prototype
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();
11 /**
12 * All DokuWiki plugins to extend the parser/rendering mechanism
13 * need to inherit from this class
15 class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode {
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()
23 /**
24 * General Info
26 * Needs to return a associative array with the following values:
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)
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();
43 /**
44 * Syntax Type
46 * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
48 function getType(){
49 trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
52 /**
53 * Allowed Mode Types
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
59 function getAllowedTypes() {
60 return array();
63 /**
64 * Paragraph Type
66 * Defines how this syntax is handled regarding paragraphs. This is important
67 * for correct XHTML nesting. Should return one of the following:
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.
73 * @see Doku_Handler_Block
75 function getPType(){
76 return 'normal';
79 /**
80 * Handler to prepare matched data for the rendering process
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.
85 * Usually you should only need the $match param.
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
93 function handle($match, $state, $pos, Doku_Handler &$handler){
94 trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
97 /**
98 * Handles the actual output creation.
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.
104 * The function should always check for the given output format and return false
105 * when a format isn't supported.
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
112 * The contents of the $data array depends on what the handler() function above
113 * created
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?
120 function render($format, Doku_Renderer &$renderer, $data) {
121 trigger_error('render() not implemented in '.get_class($this), E_USER_WARNING);
126 * There should be no need to override these functions
128 function accepts($mode) {
130 if (!$this->allowedModesSetup) {
131 global $PARSER_MODES;
133 $allowedModeTypes = $this->getAllowedTypes();
134 foreach($allowedModeTypes as $mt) {
135 $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
138 $idx = array_search(substr(get_class($this), 7), (array) $this->allowedModes);
139 if ($idx !== false) {
140 unset($this->allowedModes[$idx]);
142 $this->allowedModesSetup = true;
145 return parent::accepts($mode);
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; }
154 * Get the name of the component of the current class
156 * @return string component name
158 function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
160 // localisation methods
162 * getLang($id)
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.
168 * @param string $id id of the string to be retrieved
169 * @return string string in appropriate language or english if not available
171 function getLang($id) {
172 if (!$this->localised) $this->setupLocale();
174 return (isset($this->lang[$id]) ? $this->lang[$id] : '');
178 * locale_xhtml($id)
180 * retrieve a language dependent wiki page and pass to xhtml renderer for display
181 * plugin equivalent of p_locale_xhtml()
183 * @param string $id id of language dependent wiki page
184 * @return string parsed contents of the wiki page in xhtml format
186 function locale_xhtml($id) {
187 return p_cached_output($this->localFN($id));
191 * localFN($id)
192 * prepends appropriate path for a language dependent filename
193 * plugin equivalent of localFN()
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';
206 return $file;
210 * setupLocale()
211 * reads all the plugins language dependent strings into $this->lang
212 * this function is automatically called by getLang()
214 function setupLocale() {
215 if ($this->localised) return;
217 global $conf; // definitely don't invoke "global $lang"
218 $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
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');
225 $this->lang = $lang;
226 $this->localised = true;
229 // configuration methods
231 * getConf($setting)
233 * use this function to access plugin configuration variables
235 function getConf($setting){
237 if (!$this->configloaded){ $this->loadConfig(); }
239 return $this->conf[$setting];
243 * loadConfig()
244 * merges the plugin's default settings with any local settings
245 * this function is automatically called through getConf()
247 function loadConfig(){
248 global $conf;
250 $defaults = $this->readDefaultSettings();
251 $plugin = $this->getPluginName();
253 foreach ($defaults as $key => $value) {
254 if (isset($conf['plugin'][$plugin][$key])) continue;
255 $conf['plugin'][$plugin][$key] = $value;
258 $this->configloaded = true;
259 $this->conf =& $conf['plugin'][$plugin];
263 * read the plugin's default configuration settings from conf/default.php
264 * this function is automatically called through getConf()
266 * @return array setting => value
268 function readDefaultSettings() {
270 $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
271 $conf = array();
273 if (@file_exists($path.'default.php')) {
274 include($path.'default.php');
277 return $conf;
281 * Loads a given helper plugin (if enabled)
283 * @author Esther Brunner <wikidesign@gmail.com>
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
288 * @return object helper plugin object
290 function loadHelper($name, $msg){
291 if (!plugin_isdisabled($name)){
292 $obj = plugin_load('helper',$name);
293 }else{
294 $obj = null;
296 if (is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.",-1);
297 return $obj;
301 * Allow the plugin to prevent DokuWiki from reusing an instance
303 * @return bool false if the plugin has to be instantiated
305 function isSingleton() {
306 return true;
310 //Setup VIM: ex: et ts=4 :