3 * DokuWiki Plugin base class
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Christopher Smith <chris@jalakai.co.uk>
10 * Do not inherit directly from this class, instead inherit from the specialized
13 class DokuWiki_Plugin {
15 var $localised = false; // set to true by setupLocale() after loading language dependent strings
16 var $lang = array(); // array to hold language dependent strings, best accessed via ->getLang()
17 var $configloaded = false; // set to true by loadConfig() after loading plugin configuration variables
18 var $conf = array(); // array to hold plugin settings, best accessed via ->getConf()
23 * Needs to return a associative array with the following values:
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)
33 $parts = explode('_',get_class($this));
34 $info = DOKU_PLUGIN.'/'.$parts[2].'/plugin.info.txt';
35 if(@file_exists($info)) return confToHash($info);
37 msg('getInfo() not implemented in '.get_class($this).
38 ' and '.$info.' not found.<br />This is a bug in the '.
39 $parts[2].' plugin and should be reported to the '.
42 'date' => '0000-00-00',
43 'name' => $parts[2].' plugin',
47 // plugin introspection methods
48 // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
49 function getPluginType() {
50 list($t) = explode('_', get_class($this), 2);
53 function getPluginName() {
54 list($t, $p, $n) = explode('_', get_class($this), 4);
57 function getPluginComponent() {
58 list($t, $p, $n, $c) = explode('_', get_class($this), 4);
59 return (isset($c)?$c:'');
62 // localisation methods
65 * use this function to access plugin language strings
66 * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
67 * e.g. when info plugin is querying plugins for information about themselves.
69 * @param $id id of the string to be retrieved
70 * @return string string in appropriate language or english if not available
72 function getLang($id) {
73 if (!$this->localised) $this->setupLocale();
75 return (isset($this->lang[$id]) ? $this->lang[$id] : '');
81 * retrieve a language dependent file and pass to xhtml renderer for display
82 * plugin equivalent of p_locale_xhtml()
84 * @param $id id of language dependent wiki page
85 * @return string parsed contents of the wiki page in xhtml format
87 function locale_xhtml($id) {
88 return p_cached_output($this->localFN($id));
93 * prepends appropriate path for a language dependent filename
94 * plugin equivalent of localFN()
96 function localFN($id) {
98 $plugin = $this->getPluginName();
99 $file = DOKU_CONF.'/plugin_lang/'.$plugin.'/'.$conf['lang'].'/'.$id.'.txt';
100 if (!@file_exists($file)){
101 $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
102 if(!@file_exists($file)){
103 //fall back to english
104 $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
112 * reads all the plugins language dependent strings into $this->lang
113 * this function is automatically called by getLang()
115 function setupLocale() {
116 if ($this->localised) return;
118 global $conf; // definitely don't invoke "global $lang"
119 $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
123 // don't include once, in case several plugin components require the same language file
124 @include($path.'en/lang.php');
125 if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
128 $this->localised = true;
131 // configuration methods
135 * use this function to access plugin configuration variables
137 function getConf($setting){
139 if (!$this->configloaded){ $this->loadConfig(); }
141 return $this->conf[$setting];
146 * merges the plugin's default settings with any local settings
147 * this function is automatically called through getConf()
149 function loadConfig(){
152 $defaults = $this->readDefaultSettings();
153 $plugin = $this->getPluginName();
155 foreach ($defaults as $key => $value) {
156 if (isset($conf['plugin'][$plugin][$key])) continue;
157 $conf['plugin'][$plugin][$key] = $value;
160 $this->configloaded = true;
161 $this->conf =& $conf['plugin'][$plugin];
165 * read the plugin's default configuration settings from conf/default.php
166 * this function is automatically called through getConf()
168 * @return array setting => value
170 function readDefaultSettings() {
172 $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
175 if (@file_exists($path.'default.php')) {
176 include($path.'default.php');
183 * Loads a given helper plugin (if enabled)
185 * @author Esther Brunner <wikidesign@gmail.com>
187 * @param $name name of plugin to load
188 * @param $msg message to display in case the plugin is not available
190 * @return object helper plugin object
192 function loadHelper($name, $msg){
193 if (!plugin_isdisabled($name)){
194 $obj =& plugin_load('helper',$name);
198 if (is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.",-1);
202 // standard functions for outputing email addresses and links
203 // use these to avoid having to duplicate code to produce links in line with the installation configuration
207 * standardised function to generate an email link according to obfuscation settings
209 function email($email, $name='', $class='', $more='') {
210 if (!$email) return $name;
211 $email = obfuscate($email);
212 if (!$name) $name = $email;
213 $class = "class='".($class ? $class : 'mail')."'";
214 return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
219 * standardised function to generate an external link according to conf settings
221 function external_link($link, $title='', $class='', $target='', $more='') {
224 $link = htmlentities($link);
225 if (!$title) $title = $link;
226 if (!$target) $target = $conf['target']['extern'];
227 if ($conf['relnofollow']) $more .= ' rel="nofollow"';
229 if ($class) $class = " class='$class'";
230 if ($target) $target = " target='$target'";
231 if ($more) $more = " ".trim($more);
233 return "<a href='$link'$class$target$more>$title</a>";
237 * output text string through the parser, allows dokuwiki markup to be used
238 * very ineffecient for small pieces of data - try not to use
240 function render($text, $format='xhtml') {
241 return p_render($format, p_get_instructions($text),$info);
245 * Allow the plugin to prevent DokuWiki from reusing an instance
247 * @return bool false if the plugin has to be instantiated
249 function isSingleton() {
253 // deprecated functions
254 function plugin_localFN($id) { return $this->localFN($id); }
255 function plugin_locale_xhtml($id) { return $this->locale_xhtml($id); }
256 function plugin_email($e, $n='', $c='', $m='') { return $this->email($e, $n, $c, $m); }
257 function plugin_link($l, $t='', $c='', $to='', $m='') { return $this->external_link($l, $t, $c, $to, $m); }
258 function plugin_render($t, $f='xhtml') { return $this->render($t, $f); }