[25027] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * DokuWiki Plugin base class
|
---|
| 4 | *
|
---|
| 5 | * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
---|
| 6 | * @author Christopher Smith <[email protected]>
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Do not inherit directly from this class, instead inherit from the specialized
|
---|
| 11 | * ones in lib/plugin
|
---|
| 12 | */
|
---|
| 13 | class DokuWiki_Plugin {
|
---|
| 14 |
|
---|
| 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()
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * General Info
|
---|
| 22 | *
|
---|
| 23 | * Needs to return a associative array with the following values:
|
---|
| 24 | *
|
---|
| 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)
|
---|
| 31 | */
|
---|
| 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 |
|
---|
| 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 '.
|
---|
| 40 | 'plugin author.',-1);
|
---|
| 41 | return array(
|
---|
| 42 | 'date' => '0000-00-00',
|
---|
| 43 | 'name' => $parts[2].' plugin',
|
---|
| 44 | );
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 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);
|
---|
| 51 | return $t;
|
---|
| 52 | }
|
---|
| 53 | function getPluginName() {
|
---|
| 54 | list($t, $p, $n) = explode('_', get_class($this), 4);
|
---|
| 55 | return $n;
|
---|
| 56 | }
|
---|
| 57 | function getPluginComponent() {
|
---|
| 58 | list($t, $p, $n, $c) = explode('_', get_class($this), 4);
|
---|
| 59 | return (isset($c)?$c:'');
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // localisation methods
|
---|
| 63 | /**
|
---|
| 64 | * getLang($id)
|
---|
| 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.
|
---|
| 68 | *
|
---|
| 69 | * @param $id id of the string to be retrieved
|
---|
| 70 | * @return string string in appropriate language or english if not available
|
---|
| 71 | */
|
---|
| 72 | function getLang($id) {
|
---|
| 73 | if (!$this->localised) $this->setupLocale();
|
---|
| 74 |
|
---|
| 75 | return (isset($this->lang[$id]) ? $this->lang[$id] : '');
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * locale_xhtml($id)
|
---|
| 80 | *
|
---|
| 81 | * retrieve a language dependent file and pass to xhtml renderer for display
|
---|
| 82 | * plugin equivalent of p_locale_xhtml()
|
---|
| 83 | *
|
---|
| 84 | * @param $id id of language dependent wiki page
|
---|
| 85 | * @return string parsed contents of the wiki page in xhtml format
|
---|
| 86 | */
|
---|
| 87 | function locale_xhtml($id) {
|
---|
| 88 | return p_cached_output($this->localFN($id));
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * localFN($id)
|
---|
| 93 | * prepends appropriate path for a language dependent filename
|
---|
| 94 | * plugin equivalent of localFN()
|
---|
| 95 | */
|
---|
| 96 | function localFN($id) {
|
---|
| 97 | global $conf;
|
---|
| 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';
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | return $file;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * setupLocale()
|
---|
| 112 | * reads all the plugins language dependent strings into $this->lang
|
---|
| 113 | * this function is automatically called by getLang()
|
---|
| 114 | */
|
---|
| 115 | function setupLocale() {
|
---|
| 116 | if ($this->localised) return;
|
---|
| 117 |
|
---|
| 118 | global $conf; // definitely don't invoke "global $lang"
|
---|
| 119 | $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
|
---|
| 120 |
|
---|
| 121 | $lang = array();
|
---|
| 122 |
|
---|
| 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');
|
---|
| 126 |
|
---|
| 127 | $this->lang = $lang;
|
---|
| 128 | $this->localised = true;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | // configuration methods
|
---|
| 132 | /**
|
---|
| 133 | * getConf($setting)
|
---|
| 134 | *
|
---|
| 135 | * use this function to access plugin configuration variables
|
---|
| 136 | */
|
---|
| 137 | function getConf($setting){
|
---|
| 138 |
|
---|
| 139 | if (!$this->configloaded){ $this->loadConfig(); }
|
---|
| 140 |
|
---|
| 141 | return $this->conf[$setting];
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * loadConfig()
|
---|
| 146 | * merges the plugin's default settings with any local settings
|
---|
| 147 | * this function is automatically called through getConf()
|
---|
| 148 | */
|
---|
| 149 | function loadConfig(){
|
---|
| 150 | global $conf;
|
---|
| 151 |
|
---|
| 152 | $defaults = $this->readDefaultSettings();
|
---|
| 153 | $plugin = $this->getPluginName();
|
---|
| 154 |
|
---|
| 155 | foreach ($defaults as $key => $value) {
|
---|
| 156 | if (isset($conf['plugin'][$plugin][$key])) continue;
|
---|
| 157 | $conf['plugin'][$plugin][$key] = $value;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | $this->configloaded = true;
|
---|
| 161 | $this->conf =& $conf['plugin'][$plugin];
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * read the plugin's default configuration settings from conf/default.php
|
---|
| 166 | * this function is automatically called through getConf()
|
---|
| 167 | *
|
---|
| 168 | * @return array setting => value
|
---|
| 169 | */
|
---|
| 170 | function readDefaultSettings() {
|
---|
| 171 |
|
---|
| 172 | $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
|
---|
| 173 | $conf = array();
|
---|
| 174 |
|
---|
| 175 | if (@file_exists($path.'default.php')) {
|
---|
| 176 | include($path.'default.php');
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | return $conf;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * Loads a given helper plugin (if enabled)
|
---|
| 184 | *
|
---|
| 185 | * @author Esther Brunner <[email protected]>
|
---|
| 186 | *
|
---|
| 187 | * @param $name name of plugin to load
|
---|
| 188 | * @param $msg message to display in case the plugin is not available
|
---|
| 189 | *
|
---|
| 190 | * @return object helper plugin object
|
---|
| 191 | */
|
---|
| 192 | function loadHelper($name, $msg){
|
---|
| 193 | if (!plugin_isdisabled($name)){
|
---|
| 194 | $obj =& plugin_load('helper',$name);
|
---|
| 195 | }else{
|
---|
| 196 | $obj = null;
|
---|
| 197 | }
|
---|
| 198 | if (is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.",-1);
|
---|
| 199 | return $obj;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 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
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
| 206 | * email
|
---|
| 207 | * standardised function to generate an email link according to obfuscation settings
|
---|
| 208 | */
|
---|
| 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>";
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * external_link
|
---|
| 219 | * standardised function to generate an external link according to conf settings
|
---|
| 220 | */
|
---|
| 221 | function external_link($link, $title='', $class='', $target='', $more='') {
|
---|
| 222 | global $conf;
|
---|
| 223 |
|
---|
| 224 | $link = htmlentities($link);
|
---|
| 225 | if (!$title) $title = $link;
|
---|
| 226 | if (!$target) $target = $conf['target']['extern'];
|
---|
| 227 | if ($conf['relnofollow']) $more .= ' rel="nofollow"';
|
---|
| 228 |
|
---|
| 229 | if ($class) $class = " class='$class'";
|
---|
| 230 | if ($target) $target = " target='$target'";
|
---|
| 231 | if ($more) $more = " ".trim($more);
|
---|
| 232 |
|
---|
| 233 | return "<a href='$link'$class$target$more>$title</a>";
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 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
|
---|
| 239 | */
|
---|
| 240 | function render($text, $format='xhtml') {
|
---|
| 241 | return p_render($format, p_get_instructions($text),$info);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /**
|
---|
| 245 | * Allow the plugin to prevent DokuWiki from reusing an instance
|
---|
| 246 | *
|
---|
| 247 | * @return bool false if the plugin has to be instantiated
|
---|
| 248 | */
|
---|
| 249 | function isSingleton() {
|
---|
| 250 | return true;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 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); }
|
---|
| 259 | }
|
---|