xref: /dokuwiki/lib/plugins/admin.php (revision cd7fd4a2c78ff5083a121a441142649ab5f6acad)
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  function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
64  function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
65
66  // localisation methods
67  /**
68   * getLang($id)
69   * use this function to access plugin language strings
70   * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
71   * e.g. when info plugin is querying plugins for information about themselves.
72   *
73   * @param   $id     id of the string to be retrieved
74   * @return  string  string in appropriate language or english if not available
75   */
76  function getLang($id) {
77    if (!$this->localised) $this->setupLocale();
78
79    return (isset($this->lang[$id]) ? $this->lang[$id] : '');
80  }
81
82  /**
83   * locale_xhtml($id)
84   *
85   * retrieve a language dependent file and pass to xhtml renderer for display
86   * plugin equivalent of p_locale_xhtml()
87   *
88   * @param   $id     id of language dependent wiki page
89   * @return  string  parsed contents of the wiki page in xhtml format
90   */
91  function locale_xhtml($id) {
92    return p_cached_xhtml($this->localFN($id));
93  }
94
95  /**
96   * localFN($id)
97   * prepends appropriate path for a language dependent filename
98   * plugin equivalent of localFN()
99   */
100  function localFN($id) {
101    global $conf;
102    $plugin = $this->getPluginName();
103    $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
104    if(!@file_exists($file)){
105      //fall back to english
106      $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
107    }
108    return $file;
109  }
110
111  /**
112   *  setupLocale()
113   *  reads all the plugins language dependent strings into $this->lang
114   *  this function is automatically called by getLang()
115   */
116  function setupLocale() {
117    if ($this->localised) return;
118
119    global $conf;            // definitely don't invoke "global $lang"
120    $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
121
122    $lang = array();
123
124    // don't include once, in case several plugin components require the same language file
125    @include($path.'en/lang.php');
126    if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
127
128    $this->lang = $lang;
129    $this->localised = true;
130  }
131
132  // configuration methods
133  /**
134   * getConf($id)
135   *
136   * use this function to access plugin configuration variables
137   */
138  function getConf($id){
139    global $conf;
140
141    $plugin = $this->getPluginName();
142
143    if (!$this->configloaded){
144      if ($pconf = $this->loadConfig() !== false){
145        foreach ($pconf as $key => $value){
146          if (isset($conf['plugin'][$plugin][$key])) continue;
147          $conf['plugin'][$plugin][$key] = $value;
148        }
149        $this->configloaded = true;
150      }
151    }
152
153    return $conf['plugin'][$plugin][$id];
154  }
155
156  /**
157   * loadConfig()
158   * reads all plugin configuration variables into $this->conf
159   * this function is automatically called by getConf()
160   */
161  function loadConfig(){
162
163    $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
164    $conf = array();
165
166    if (!@file_exists($path.'default.php')) return false;
167
168    // load default config file
169    include($path.'default.php');
170
171    return $conf;
172  }
173
174  // standard functions for outputing email addresses and links
175  // use these to avoid having to duplicate code to produce links in line with the installation configuration
176  function email($email, $name='', $class='', $more='') {
177    if (!$email) return $name;
178    $email = obfuscate($email);
179    if (!$name) $name = $email;
180    $class = "class='".($class ? $class : 'mail')."'";
181    return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
182  }
183
184  function external_link($link, $title='', $class='', $target='', $more='') {
185    global $conf;
186
187    $link = htmlentities($link);
188    if (!$title) $title = $link;
189    if (!$target) $target = $conf['target']['extern'];
190    if ($conf['relnofollow']) $more .= ' rel="nofollow"';
191
192    if ($class) $class = " class='$class'";
193    if ($target) $target = " target='$target'";
194    if ($more) $more = " ".trim($more);
195
196    return "<a href='$link'$class$target$more>$title</a>";
197  }
198
199  // output text string through the parser, allows dokuwiki markup to be used
200  // very ineffecient for small pieces of data - try not to use
201  function render($text, $format='xhtml') {
202    return p_render($format, p_get_instructions($text),$info);
203  }
204
205  // deprecated functions
206  function plugin_localFN($id) { return $this->localFN($id); }
207  function plugin_locale_xhtml($id) { return $this->locale_xhtml($id); }
208  function plugin_email($e, $n='', $c='', $m='') { return $this->email($e, $n, $c, $m); }
209  function plugin_link($l, $t='', $c='', $to='', $m='') { return $this->external_link($l, $t, $c, $to, $m); }
210  function plugin_render($t, $f='xhtml') { return $this->render($t, $f); }
211
212}
213//Setup VIM: ex: et ts=4 enc=utf-8 :