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