first commit. dokuwiki.
[h2N7SspZmY.git] / inc / plugin.php
blob3645347399904d6f04bee7351ffc0193dda91791
1 <?php
2 /**
3 * DokuWiki Plugin base class
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Christopher Smith <chris@jalakai.co.uk>
7 */
9 /**
10 * Do not inherit directly from this class, instead inherit from the specialized
11 * ones in lib/plugin
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()
20 /**
21 * General Info
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)
32 function getInfo(){
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);
43 return $t;
45 function getPluginName() {
46 list($t, $p, $n) = explode('_', get_class($this), 4);
47 return $n;
49 function getPluginComponent() {
50 list($t, $p, $n, $c) = explode('_', get_class($this), 4);
51 return (isset($c)?$c:'');
54 // localisation methods
55 /**
56 * getLang($id)
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] : '');
70 /**
71 * locale_xhtml($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));
83 /**
84 * localFN($id)
85 * prepends appropriate path for a language dependent filename
86 * plugin equivalent of localFN()
88 function localFN($id) {
89 global $conf;
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';
96 return $file;
99 /**
100 * setupLocale()
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/';
110 $lang = array();
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');
116 $this->lang = $lang;
117 $this->localised = true;
120 // configuration methods
122 * getConf($setting)
124 * use this function to access plugin configuration variables
126 function getConf($setting){
128 if (!$this->configloaded){ $this->loadConfig(); }
130 return $this->conf[$setting];
134 * loadConfig()
135 * merges the plugin's default settings with any local settings
136 * this function is automatically called through getConf()
138 function loadConfig(){
139 global $conf;
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/';
162 $conf = array();
164 if (@file_exists($path.'default.php')) {
165 include($path.'default.php');
168 return $conf;
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);
184 }else{
185 $obj = null;
187 if (is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.",-1);
188 return $obj;
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
195 * email
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>";
207 * external_link
208 * standardised function to generate an external link according to conf settings
210 function external_link($link, $title='', $class='', $target='', $more='') {
211 global $conf;
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() {
239 return false;
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); }