[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/ -> pluginutils.php (source)

   1  <?php
   2  /**
   3   * Utilities for handling plugins
   4   *
   5   * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
   6   * @author     Andreas Gohr <andi@splitbrain.org>
   7   */
   8  
   9  // plugin related constants
  10  if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  11  $plugin_types = array('admin','syntax','action','renderer', 'helper');
  12  
  13  /**
  14   * Returns a list of available plugins of given type
  15   *
  16   * @param $type  string, plugin_type name;
  17   *               the type of plugin to return,
  18   *               use empty string for all types
  19   * @param $all   bool;
  20   *               false to only return enabled plugins,
  21   *               true to return both enabled and disabled plugins
  22   *
  23   * @return       array of plugin names
  24   *
  25   * @author Andreas Gohr <andi@splitbrain.org>
  26   */
  27  function plugin_list($type='',$all=false){
  28    $plugins = array();
  29    if ($dh = opendir(DOKU_PLUGIN)) {
  30      while (false !== ($plugin = readdir($dh))) {
  31        if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
  32        if (is_file(DOKU_PLUGIN.$plugin)) continue;
  33  
  34        // if required, skip disabled plugins
  35        if (!$all && plugin_isdisabled($plugin)) continue;
  36  
  37        if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
  38            $plugins[] = $plugin;
  39        } else {
  40          if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
  41            while (false !== ($component = readdir($dp))) {
  42              if (substr($component,0,1) == '.' || strtolower(substr($component, -4)) != ".php") continue;
  43              if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
  44                $plugins[] = $plugin.'_'.substr($component, 0, -4);
  45              }
  46            }
  47          closedir($dp);
  48          }
  49        }
  50      }
  51      closedir($dh);
  52    }
  53    return $plugins;
  54  }
  55  
  56  /**
  57   * Loads the given plugin and creates an object of it
  58   *
  59   * @author Andreas Gohr <andi@splitbrain.org>
  60   *
  61   * @param  $type string     type of plugin to load
  62   * @param  $name string     name of the plugin to load
  63   * @param  $new  bool       true to return a new instance of the plugin, false to use an already loaded instance
  64   * @return objectreference  the plugin object or null on failure
  65   */
  66  function &plugin_load($type,$name,$new=false){
  67    //we keep all loaded plugins available in global scope for reuse
  68    global $DOKU_PLUGINS;
  69  
  70    //plugin already loaded?
  71    if(!empty($DOKU_PLUGINS[$type][$name])){
  72      if ($new) {
  73        $class = $type.'_plugin_'.$name;
  74        return class_exists($class) ? new $class : null;
  75      } else {
  76        return $DOKU_PLUGINS[$type][$name];
  77      }
  78    }
  79  
  80    //try to load the wanted plugin file
  81    if (@file_exists(DOKU_PLUGIN."$name/$type.php")){
  82      include_once(DOKU_PLUGIN."$name/$type.php");
  83    }else{
  84      list($plugin, $component) = preg_split("/_/",$name, 2);
  85      if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
  86          return null;
  87      }
  88    }
  89  
  90    //construct class and instantiate
  91    $class = $type.'_plugin_'.$name;
  92    if (!class_exists($class)) return null;
  93  
  94    $DOKU_PLUGINS[$type][$name] = new $class;
  95    return $DOKU_PLUGINS[$type][$name];
  96  }
  97  
  98  function plugin_isdisabled($name) { return @file_exists(DOKU_PLUGIN.$name.'/disabled'); }
  99  function plugin_enable($name) { return @unlink(DOKU_PLUGIN.$name.'/disabled'); }
 100  function plugin_disable($name) { return @touch(DOKU_PLUGIN.$name.'/disabled'); }


Generated: Tue Dec 2 01:30:01 2008 Cross-referenced by PHPXref 0.7