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