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);
36 trigger_error('getInfo() not implemented in '.get_class($this).' and '.$info.' not found', E_USER_WARNING
);
39 // plugin introspection methods
40 // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
41 function getPluginType() {
42 list($t) = explode('_', get_class($this), 2);
45 function getPluginName() {
46 list($t, $p, $n) = explode('_', get_class($this), 4);
49 function getPluginComponent() {
50 list($t, $p, $n, $c) = explode('_', get_class($this), 4);
51 return (isset($c)?
$c:'');
54 // localisation methods
57 * use this function to access plugin language strings
58 * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
59 * e.g. when info plugin is querying plugins for information about themselves.
61 * @param $id id of the string to be retrieved
62 * @return string string in appropriate language or english if not available
64 function getLang($id) {
65 if (!$this->localised
) $this->setupLocale();
67 return (isset($this->lang
[$id]) ?
$this->lang
[$id] : '');
73 * retrieve a language dependent file and pass to xhtml renderer for display
74 * plugin equivalent of p_locale_xhtml()
76 * @param $id id of language dependent wiki page
77 * @return string parsed contents of the wiki page in xhtml format
79 function locale_xhtml($id) {
80 return p_cached_output($this->localFN($id));
85 * prepends appropriate path for a language dependent filename
86 * plugin equivalent of localFN()
88 function localFN($id) {
90 $plugin = $this->getPluginName();
91 $file = DOKU_PLUGIN
.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
92 if(!@file_exists
($file)){
93 //fall back to english
94 $file = DOKU_PLUGIN
.$plugin.'/lang/en/'.$id.'.txt';
101 * reads all the plugins language dependent strings into $this->lang
102 * this function is automatically called by getLang()
104 function setupLocale() {
105 if ($this->localised
) return;
107 global $conf; // definitely don't invoke "global $lang"
108 $path = DOKU_PLUGIN
.$this->getPluginName().'/lang/';
112 // don't include once, in case several plugin components require the same language file
113 @include
($path.'en/lang.php');
114 if ($conf['lang'] != 'en') @include
($path.$conf['lang'].'/lang.php');
117 $this->localised
= true;
120 // configuration methods
124 * use this function to access plugin configuration variables
126 function getConf($setting){
128 if (!$this->configloaded
){ $this->loadConfig(); }
130 return $this->conf
[$setting];
135 * merges the plugin's default settings with any local settings
136 * this function is automatically called through getConf()
138 function loadConfig(){
141 $defaults = $this->readDefaultSettings();
142 $plugin = $this->getPluginName();
144 foreach ($defaults as $key => $value) {
145 if (isset($conf['plugin'][$plugin][$key])) continue;
146 $conf['plugin'][$plugin][$key] = $value;
149 $this->configloaded
= true;
150 $this->conf
=& $conf['plugin'][$plugin];
154 * read the plugin's default configuration settings from conf/default.php
155 * this function is automatically called through getConf()
157 * @return array setting => value
159 function readDefaultSettings() {
161 $path = DOKU_PLUGIN
.$this->getPluginName().'/conf/';
164 if (@file_exists
($path.'default.php')) {
165 include($path.'default.php');
172 * Loads a given helper plugin (if enabled)
174 * @author Esther Brunner <wikidesign@gmail.com>
176 * @param $name name of plugin to load
177 * @param $msg message to display in case the plugin is not available
179 * @return object helper plugin object
181 function loadHelper($name, $msg){
182 if (!plugin_isdisabled($name)){
183 $obj =& plugin_load('helper',$name);
187 if (is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.",-1);
191 // standard functions for outputing email addresses and links
192 // use these to avoid having to duplicate code to produce links in line with the installation configuration
196 * standardised function to generate an email link according to obfuscation settings
198 function email($email, $name='', $class='', $more='') {
199 if (!$email) return $name;
200 $email = obfuscate($email);
201 if (!$name) $name = $email;
202 $class = "class='".($class ?
$class : 'mail')."'";
203 return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
208 * standardised function to generate an external link according to conf settings
210 function external_link($link, $title='', $class='', $target='', $more='') {
213 $link = htmlentities($link);
214 if (!$title) $title = $link;
215 if (!$target) $target = $conf['target']['extern'];
216 if ($conf['relnofollow']) $more .= ' rel="nofollow"';
218 if ($class) $class = " class='$class'";
219 if ($target) $target = " target='$target'";
220 if ($more) $more = " ".trim($more);
222 return "<a href='$link'$class$target$more>$title</a>";
226 * output text string through the parser, allows dokuwiki markup to be used
227 * very ineffecient for small pieces of data - try not to use
229 function render($text, $format='xhtml') {
230 return p_render($format, p_get_instructions($text),$info);
234 * Allow the plugin to prevent DokuWiki creating a second instance of itself
236 * @return bool true if the plugin can not be instantiated more than once
238 function isSingleton() {
242 // deprecated functions
243 function plugin_localFN($id) { return $this->localFN($id); }
244 function plugin_locale_xhtml($id) { return $this->locale_xhtml($id); }
245 function plugin_email($e, $n='', $c='', $m='') { return $this->email($e, $n, $c, $m); }
246 function plugin_link($l, $t='', $c='', $to='', $m='') { return $this->external_link($l, $t, $c, $to, $m); }
247 function plugin_render($t, $f='xhtml') { return $this->render($t, $f); }