[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/ -> PluginTrait.php (source)

   1  <?php
   2  
   3  /**
   4   * Do not inherit directly from this class, instead inherit from the specialized
   5   * ones in lib/plugin
   6   */
   7  trait DokuWiki_PluginTrait {
   8  
   9      protected $localised = false;        // set to true by setupLocale() after loading language dependent strings
  10      protected $lang = array();           // array to hold language dependent strings, best accessed via ->getLang()
  11      protected $configloaded = false;     // set to true by loadConfig() after loading plugin configuration variables
  12      protected $conf = array();           // array to hold plugin settings, best accessed via ->getConf()
  13  
  14      /**
  15       * @see DokuWiki_PluginInterface::getInfo()
  16       */
  17      public function getInfo() {
  18          $parts = explode('_', get_class($this));
  19          $info = DOKU_PLUGIN . '/' . $parts[2] . '/plugin.info.txt';
  20          if(file_exists($info)) return confToHash($info);
  21  
  22          msg(
  23              'getInfo() not implemented in ' . get_class($this) . ' and ' . $info . ' not found.<br />' .
  24              'Verify you\'re running the latest version of the plugin. If the problem persists, send a ' .
  25              'bug report to the author of the ' . $parts[2] . ' plugin.', -1
  26          );
  27          return array(
  28              'date' => '0000-00-00',
  29              'name' => $parts[2] . ' plugin',
  30          );
  31      }
  32  
  33      /**
  34       * @see DokuWiki_PluginInterface::isSingleton()
  35       */
  36      public function isSingleton() {
  37          return true;
  38      }
  39  
  40      /**
  41       * @see DokuWiki_PluginInterface::loadHelper()
  42       */
  43      public function loadHelper($name, $msg = true) {
  44          $obj = plugin_load('helper', $name);
  45          if(is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.", -1);
  46          return $obj;
  47      }
  48  
  49      // region introspection methods
  50  
  51      /**
  52       * @see DokuWiki_PluginInterface::getPluginType()
  53       */
  54      public function getPluginType() {
  55          list($t) = explode('_', get_class($this), 2);
  56          return $t;
  57      }
  58  
  59      /**
  60       * @see DokuWiki_PluginInterface::getPluginName()
  61       */
  62      public function getPluginName() {
  63          list(/* $t */, /* $p */, $n) = explode('_', get_class($this), 4);
  64          return $n;
  65      }
  66  
  67      /**
  68       * @see DokuWiki_PluginInterface::getPluginComponent()
  69       */
  70      public function getPluginComponent() {
  71          list(/* $t */, /* $p */, /* $n */, $c) = explode('_', get_class($this), 4);
  72          return (isset($c) ? $c : '');
  73      }
  74  
  75      // endregion
  76      // region localization methods
  77  
  78      /**
  79       * @see DokuWiki_PluginInterface::getLang()
  80       */
  81      public function getLang($id) {
  82          if(!$this->localised) $this->setupLocale();
  83  
  84          return (isset($this->lang[$id]) ? $this->lang[$id] : '');
  85      }
  86  
  87      /**
  88       * @see DokuWiki_PluginInterface::locale_xhtml()
  89       */
  90      public function locale_xhtml($id) {
  91          return p_cached_output($this->localFN($id));
  92      }
  93  
  94      /**
  95       * @see DokuWiki_PluginInterface::localFN()
  96       */
  97      public function localFN($id, $ext = 'txt') {
  98          global $conf;
  99          $plugin = $this->getPluginName();
 100          $file = DOKU_CONF . 'plugin_lang/' . $plugin . '/' . $conf['lang'] . '/' . $id . '.' . $ext;
 101          if(!file_exists($file)) {
 102              $file = DOKU_PLUGIN . $plugin . '/lang/' . $conf['lang'] . '/' . $id . '.' . $ext;
 103              if(!file_exists($file)) {
 104                  //fall back to english
 105                  $file = DOKU_PLUGIN . $plugin . '/lang/en/' . $id . '.' . $ext;
 106              }
 107          }
 108          return $file;
 109      }
 110  
 111      /**
 112       * @see DokuWiki_PluginInterface::setupLocale()
 113       */
 114      public function setupLocale() {
 115          if($this->localised) return;
 116  
 117          global $conf, $config_cascade; // definitely don't invoke "global $lang"
 118          $path = DOKU_PLUGIN . $this->getPluginName() . '/lang/';
 119  
 120          $lang = array();
 121  
 122          // don't include once, in case several plugin components require the same language file
 123          @include($path . 'en/lang.php');
 124          foreach($config_cascade['lang']['plugin'] as $config_file) {
 125              if(file_exists($config_file . $this->getPluginName() . '/en/lang.php')) {
 126                  include($config_file . $this->getPluginName() . '/en/lang.php');
 127              }
 128          }
 129  
 130          if($conf['lang'] != 'en') {
 131              @include($path . $conf['lang'] . '/lang.php');
 132              foreach($config_cascade['lang']['plugin'] as $config_file) {
 133                  if(file_exists($config_file . $this->getPluginName() . '/' . $conf['lang'] . '/lang.php')) {
 134                      include($config_file . $this->getPluginName() . '/' . $conf['lang'] . '/lang.php');
 135                  }
 136              }
 137          }
 138  
 139          $this->lang = $lang;
 140          $this->localised = true;
 141      }
 142  
 143      // endregion
 144      // region configuration methods
 145  
 146      /**
 147       * @see DokuWiki_PluginInterface::getConf()
 148       */
 149      public function getConf($setting, $notset = false) {
 150  
 151          if(!$this->configloaded) {
 152              $this->loadConfig();
 153          }
 154  
 155          if(isset($this->conf[$setting])) {
 156              return $this->conf[$setting];
 157          } else {
 158              return $notset;
 159          }
 160      }
 161  
 162      /**
 163       * @see DokuWiki_PluginInterface::loadConfig()
 164       */
 165      public function loadConfig() {
 166          global $conf;
 167  
 168          $defaults = $this->readDefaultSettings();
 169          $plugin = $this->getPluginName();
 170  
 171          foreach($defaults as $key => $value) {
 172              if(isset($conf['plugin'][$plugin][$key])) continue;
 173              $conf['plugin'][$plugin][$key] = $value;
 174          }
 175  
 176          $this->configloaded = true;
 177          $this->conf =& $conf['plugin'][$plugin];
 178      }
 179  
 180      /**
 181       * read the plugin's default configuration settings from conf/default.php
 182       * this function is automatically called through getConf()
 183       *
 184       * @return    array    setting => value
 185       */
 186      protected function readDefaultSettings() {
 187  
 188          $path = DOKU_PLUGIN . $this->getPluginName() . '/conf/';
 189          $conf = array();
 190  
 191          if(file_exists($path . 'default.php')) {
 192              include($path . 'default.php');
 193          }
 194  
 195          return $conf;
 196      }
 197  
 198      // endregion
 199      // region output methods
 200  
 201      /**
 202       * @see DokuWiki_PluginInterface::email()
 203       */
 204      public function email($email, $name = '', $class = '', $more = '') {
 205          if(!$email) return $name;
 206          $email = obfuscate($email);
 207          if(!$name) $name = $email;
 208          $class = "class='" . ($class ? $class : 'mail') . "'";
 209          return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
 210      }
 211  
 212      /**
 213       * @see DokuWiki_PluginInterface::external_link()
 214       */
 215      public function external_link($link, $title = '', $class = '', $target = '', $more = '') {
 216          global $conf;
 217  
 218          $link = htmlentities($link);
 219          if(!$title) $title = $link;
 220          if(!$target) $target = $conf['target']['extern'];
 221          if($conf['relnofollow']) $more .= ' rel="nofollow"';
 222  
 223          if($class) $class = " class='$class'";
 224          if($target) $target = " target='$target'";
 225          if($more) $more = " " . trim($more);
 226  
 227          return "<a href='$link'$class$target$more>$title</a>";
 228      }
 229  
 230      /**
 231       * @see DokuWiki_PluginInterface::render_text()
 232       */
 233      public function render_text($text, $format = 'xhtml') {
 234          return p_render($format, p_get_instructions($text), $info);
 235      }
 236  
 237      // endregion
 238  }