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