xref: /dokuwiki/lib/plugins/admin.php (revision bf5d40c2ffe5ebcef76254c83a17e3955796fa0d)
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
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    trigger_error('getInfo() not implemented in '.get_class($this), E_USER_WARNING);
34  }
35
36  function getMenuText($language) {
37      $menutext = $this->getLang('menu');
38      if (!$menutext) {
39        $info = $this->getInfo();
40        $menutext = $info['name'].' ...';
41      }
42      return $menutext;
43  }
44
45  function getMenuSort() {
46    return 1000;
47  }
48
49  function handle() {
50    trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
51  }
52
53  function html() {
54    trigger_error('html() not implemented in '.get_class($this), E_USER_WARNING);
55  }
56
57  // private methods (maybe a dokuwiki plugin base class is required for these)
58
59  // plugin introspection methods
60  // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
61  function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t;  }
62
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    // don't include once, in case several plugin components require the same language file
123    @include($path.'en/lang.php');
124    if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
125
126    $this->lang = $lang;
127    $this->localised = true;
128  }
129
130  // standard functions for outputing email addresses and links
131  // use these to avoid having to duplicate code to produce links in line with the installation configuration
132  function email($email, $name='', $class='', $more='') {
133    if (!$email) return $name;
134    $email = obfuscate($email);
135    if (!$name) $name = $email;
136    $class = "class='".($class ? $class : 'mail')."'";
137    return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
138  }
139
140  function external_link($link, $title='', $class='', $target='', $more='') {
141    global $conf;
142
143    $link = htmlentities($link);
144    if (!$title) $title = $link;
145    if (!$target) $target = $conf['target']['extern'];
146    if ($conf['relnofollow']) $more .= ' rel="nofollow"';
147
148    if ($class) $class = " class='$class'";
149    if ($target) $target = " target='$target'";
150    if ($more) $more = " ".trim($more);
151
152    return "<a href='$link'$class$target$more>$title</a>";
153  }
154
155  // output text string through the parser, allows dokuwiki markup to be used
156  // very ineffecient for small pieces of data - try not to use
157  function render($text, $format='xhtml') {
158    return p_render($format, p_get_instructions($text),$info);
159  }
160
161  // deprecated functions
162  function plugin_localFN($id) { return $this->localFN($id); }
163  function plugin_locale_xhtml($id) { return $this->locale_xhtml($id); }
164  function plugin_email($e, $n='', $c='', $m='') { return $this->email($e, $n, $c, $m); }
165  function plugin_link($l, $t='', $c='', $to='', $m='') { return $this->external_link($l, $t, $c, $to, $m); }
166  function plugin_render($t, $f='xhtml') { return $this->render($t, $f); }
167
168}
169//Setup VIM: ex: et ts=4 enc=utf-8 :