xref: /dokuwiki/lib/plugins/admin.php (revision f1f771344f46009891de4c83bfff6043da81231b)
1<?php
2/**
3 * Admin Plugin Prototype
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Christopher Smith <chris@jalakai.co.uk>
7 */
8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10
11/**
12 * All DokuWiki plugins to extend the admin function
13 * need to inherit from this class
14 */
15class DokuWiki_Admin_Plugin {
16
17  var $localised = false;        // set to true by setupLocale() after loading language dependent strings
18  var $lang = array();           // array to hold language dependent strings, best accessed via ->getLang()
19  var $configloaded = false;     // set to true by loadConfig() after loading plugin configuration variables
20
21  /**
22   * General Info
23   *
24   * Needs to return a associative array with the following values:
25   *
26   * author - Author of the plugin
27   * email  - Email address to contact the author
28   * date   - Last modified date of the plugin in YYYY-MM-DD format
29   * name   - Name of the plugin
30   * desc   - Short description of the plugin (Text only)
31   * url    - Website with more information on the plugin (eg. syntax description)
32   */
33  function getInfo(){
34    trigger_error('getInfo() not implemented in '.get_class($this), E_USER_WARNING);
35  }
36
37  function getMenuText($language) {
38      $menutext = $this->getLang('menu');
39      if (!$menutext) {
40        $info = $this->getInfo();
41        $menutext = $info['name'].' ...';
42      }
43      return $menutext;
44  }
45
46  function getMenuSort() {
47    return 1000;
48  }
49
50  function handle() {
51    trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
52  }
53
54  function html() {
55    trigger_error('html() not implemented in '.get_class($this), E_USER_WARNING);
56  }
57
58  // private methods (maybe a dokuwiki plugin base class is required for these)
59
60  // plugin introspection methods
61  // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
62  function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t;  }
63
64  function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
65  function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
66
67  // localisation methods
68  /**
69   * getLang($id)
70   * use this function to access plugin language strings
71   * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
72   * e.g. when info plugin is querying plugins for information about themselves.
73   *
74   * @param   $id     id of the string to be retrieved
75   * @return  string  string in appropriate language or english if not available
76   */
77  function getLang($id) {
78    if (!$this->localised) $this->setupLocale();
79
80    return (isset($this->lang[$id]) ? $this->lang[$id] : '');
81  }
82
83  /**
84   * locale_xhtml($id)
85   *
86   * retrieve a language dependent file and pass to xhtml renderer for display
87   * plugin equivalent of p_locale_xhtml()
88   *
89   * @param   $id     id of language dependent wiki page
90   * @return  string  parsed contents of the wiki page in xhtml format
91   */
92  function locale_xhtml($id) {
93    return p_cached_xhtml($this->localFN($id));
94  }
95
96  /**
97   * localFN($id)
98   * prepends appropriate path for a language dependent filename
99   * plugin equivalent of localFN()
100   */
101  function localFN($id) {
102    global $conf;
103    $plugin = $this->getPluginName();
104    $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
105    if(!@file_exists($file)){
106      //fall back to english
107      $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
108    }
109    return $file;
110  }
111
112  /**
113   *  setupLocale()
114   *  reads all the plugins language dependent strings into $this->lang
115   *  this function is automatically called by getLang()
116   */
117  function setupLocale() {
118    if ($this->localised) return;
119
120    global $conf;            // definitely don't invoke "global $lang"
121    $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
122
123    $lang = array();
124
125    // don't include once, in case several plugin components require the same language file
126    @include($path.'en/lang.php');
127    if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
128
129    $this->lang = $lang;
130    $this->localised = true;
131  }
132
133  // configuration methods
134  /**
135   * getConf($id)
136   *
137   * use this function to access plugin configuration variables
138   */
139  function getConf($id){
140    global $conf;
141
142    $plugin = $this->getPluginName();
143
144    if (!$this->configloaded){
145      if ($pconf = $this->loadConfig() !== false){
146        foreach ($pconf as $key => $value){
147          if (isset($conf['plugin'][$plugin][$key])) continue;
148          $conf['plugin'][$plugin][$key] = $value;
149        }
150        $this->configloaded = true;
151      }
152    }
153
154    return $conf['plugin'][$plugin][$id];
155  }
156
157  /**
158   * loadConfig()
159   * reads all plugin configuration variables into $this->conf
160   * this function is automatically called by getConf()
161   */
162  function loadConfig(){
163
164    $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
165    $conf = array();
166
167    if (!@file_exists($path.'default.php')) return false;
168
169    // load default config file
170    include($path.'default.php');
171
172    return $conf;
173  }
174
175  // standard functions for outputing email addresses and links
176  // use these to avoid having to duplicate code to produce links in line with the installation configuration
177  function email($email, $name='', $class='', $more='') {
178    if (!$email) return $name;
179    $email = obfuscate($email);
180    if (!$name) $name = $email;
181    $class = "class='".($class ? $class : 'mail')."'";
182    return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
183  }
184
185  function external_link($link, $title='', $class='', $target='', $more='') {
186    global $conf;
187
188    $link = htmlentities($link);
189    if (!$title) $title = $link;
190    if (!$target) $target = $conf['target']['extern'];
191    if ($conf['relnofollow']) $more .= ' rel="nofollow"';
192
193    if ($class) $class = " class='$class'";
194    if ($target) $target = " target='$target'";
195    if ($more) $more = " ".trim($more);
196
197    return "<a href='$link'$class$target$more>$title</a>";
198  }
199
200  // output text string through the parser, allows dokuwiki markup to be used
201  // very ineffecient for small pieces of data - try not to use
202  function render($text, $format='xhtml') {
203    return p_render($format, p_get_instructions($text),$info);
204  }
205
206  // deprecated functions
207  function plugin_localFN($id) { return $this->localFN($id); }
208  function plugin_locale_xhtml($id) { return $this->locale_xhtml($id); }
209  function plugin_email($e, $n='', $c='', $m='') { return $this->email($e, $n, $c, $m); }
210  function plugin_link($l, $t='', $c='', $to='', $m='') { return $this->external_link($l, $t, $c, $to, $m); }
211  function plugin_render($t, $f='xhtml') { return $this->render($t, $f); }
212
213}
214//Setup VIM: ex: et ts=4 enc=utf-8 :