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