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 trigger_error('getInfo() not implemented in '.get_class($this), E_USER_WARNING
);
36 // plugin introspection methods
37 // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
38 function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t; }
39 function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
40 function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?
$c:''); }
42 // localisation methods
45 * use this function to access plugin language strings
46 * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
47 * e.g. when info plugin is querying plugins for information about themselves.
49 * @param $id id of the string to be retrieved
50 * @return string string in appropriate language or english if not available
52 function getLang($id) {
53 if (!$this->localised
) $this->setupLocale();
55 return (isset($this->lang
[$id]) ?
$this->lang
[$id] : '');
61 * retrieve a language dependent file and pass to xhtml renderer for display
62 * plugin equivalent of p_locale_xhtml()
64 * @param $id id of language dependent wiki page
65 * @return string parsed contents of the wiki page in xhtml format
67 function locale_xhtml($id) {
68 return p_cached_output($this->localFN($id));
73 * prepends appropriate path for a language dependent filename
74 * plugin equivalent of localFN()
76 function localFN($id) {
78 $plugin = $this->getPluginName();
79 $file = DOKU_PLUGIN
.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
80 if(!@file_exists
($file)){
81 //fall back to english
82 $file = DOKU_PLUGIN
.$plugin.'/lang/en/'.$id.'.txt';
89 * reads all the plugins language dependent strings into $this->lang
90 * this function is automatically called by getLang()
92 function setupLocale() {
93 if ($this->localised
) return;
95 global $conf; // definitely don't invoke "global $lang"
96 $path = DOKU_PLUGIN
.$this->getPluginName().'/lang/';
100 // don't include once, in case several plugin components require the same language file
101 @include
($path.'en/lang.php');
102 if ($conf['lang'] != 'en') @include
($path.$conf['lang'].'/lang.php');
105 $this->localised
= true;
108 // configuration methods
112 * use this function to access plugin configuration variables
114 function getConf($setting){
116 if (!$this->configloaded
){ $this->loadConfig(); }
118 return $this->conf
[$setting];
123 * merges the plugin's default settings with any local settings
124 * this function is automatically called through getConf()
126 function loadConfig(){
129 $defaults = $this->readDefaultSettings();
130 $plugin = $this->getPluginName();
132 foreach ($defaults as $key => $value) {
133 if (isset($conf['plugin'][$plugin][$key])) continue;
134 $conf['plugin'][$plugin][$key] = $value;
137 $this->configloaded
= true;
138 $this->conf
=& $conf['plugin'][$plugin];
142 * read the plugin's default configuration settings from conf/default.php
143 * this function is automatically called through getConf()
145 * @return array setting => value
147 function readDefaultSettings() {
149 $path = DOKU_PLUGIN
.$this->getPluginName().'/conf/';
152 if (@file_exists
($path.'default.php')) {
153 include($path.'default.php');
160 * Loads a given helper plugin (if enabled)
162 * @author Esther Brunner <wikidesign@gmail.com>
164 * @param $name name of plugin to load
165 * @param $msg message to display in case the plugin is not available
167 * @return object helper plugin object
169 function loadHelper($name, $msg){
170 if (!plugin_isdisabled($name)) $obj =& plugin_load('helper',$name);
172 if (is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.",-1);
176 // standard functions for outputing email addresses and links
177 // use these to avoid having to duplicate code to produce links in line with the installation configuration
181 * standardised function to generate an email link according to obfuscation settings
183 function email($email, $name='', $class='', $more='') {
184 if (!$email) return $name;
185 $email = obfuscate($email);
186 if (!$name) $name = $email;
187 $class = "class='".($class ?
$class : 'mail')."'";
188 return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
193 * standardised function to generate an external link according to conf settings
195 function external_link($link, $title='', $class='', $target='', $more='') {
198 $link = htmlentities($link);
199 if (!$title) $title = $link;
200 if (!$target) $target = $conf['target']['extern'];
201 if ($conf['relnofollow']) $more .= ' rel="nofollow"';
203 if ($class) $class = " class='$class'";
204 if ($target) $target = " target='$target'";
205 if ($more) $more = " ".trim($more);
207 return "<a href='$link'$class$target$more>$title</a>";
211 * output text string through the parser, allows dokuwiki markup to be used
212 * very ineffecient for small pieces of data - try not to use
214 function render($text, $format='xhtml') {
215 return p_render($format, p_get_instructions($text),$info);
219 * Allow the plugin to prevent DokuWiki creating a second instance of itself
221 * @return bool true if the plugin can not be instantiated more than once
223 function isSingleton() {
227 // deprecated functions
228 function plugin_localFN($id) { return $this->localFN($id); }
229 function plugin_locale_xhtml($id) { return $this->locale_xhtml($id); }
230 function plugin_email($e, $n='', $c='', $m='') { return $this->email($e, $n, $c, $m); }
231 function plugin_link($l, $t='', $c='', $to='', $m='') { return $this->external_link($l, $t, $c, $to, $m); }
232 function plugin_render($t, $f='xhtml') { return $this->render($t, $f); }