[ Index ]
 

Code source de DokuWiki 2006-11-06

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/plugins/ -> base.php (source)

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


Généré le : Tue Apr 3 20:47:31 2007 par Balluche grâce à PHPXref 0.7