aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/config/core/Loader.php
blob: 4516065b079a1c069c31bca56659ce55eb448bab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php

namespace dokuwiki\plugin\config\core;

use dokuwiki\Extension\Event;

/**
 * Configuration loader
 *
 * Loads configuration meta data and settings from the various files. Honors the
 * configuration cascade and installed plugins.
 */
class Loader
{
    /** @var ConfigParser */
    protected $parser;

    /** @var string[] list of enabled plugins */
    protected $plugins;
    /** @var string current template */
    protected $template;

    /**
     * Loader constructor.
     * @param ConfigParser $parser
     * @triggers PLUGIN_CONFIG_PLUGINLIST
     */
    public function __construct(ConfigParser $parser)
    {
        global $conf;
        $this->parser = $parser;
        $this->plugins = plugin_list();
        $this->template = $conf['template'];
        // allow plugins to remove configurable plugins
        Event::createAndTrigger('PLUGIN_CONFIG_PLUGINLIST', $this->plugins);
    }

    /**
     * Read the settings meta data
     *
     * Reads the main file, plugins and template settings meta data
     *
     * @return array
     */
    public function loadMeta()
    {
        // load main file
        $meta = [];
        include DOKU_PLUGIN . 'config/settings/config.metadata.php';

        // plugins
        foreach ($this->plugins as $plugin) {
            $meta = array_merge(
                $meta,
                $this->loadExtensionMeta(
                    DOKU_PLUGIN . $plugin . '/conf/metadata.php',
                    'plugin',
                    $plugin
                )
            );
        }

        // current template
        $meta = array_merge(
            $meta,
            $this->loadExtensionMeta(
                tpl_incdir() . '/conf/metadata.php',
                'tpl',
                $this->template
            )
        );

        return $meta;
    }

    /**
     * Read the default values
     *
     * Reads the main file, plugins and template defaults
     *
     * @return array
     */
    public function loadDefaults()
    {

        // initialize array
        $conf = [];

        // plugins
        foreach ($this->plugins as $plugin) {
            $conf = array_merge(
                $conf,
                $this->loadExtensionConf(
                    DOKU_PLUGIN . $plugin . '/conf/default.php',
                    'plugin',
                    $plugin
                )
            );
        }

        // current template
        $conf = array_merge(
            $conf,
            $this->loadExtensionConf(
                tpl_incdir() . '/conf/default.php',
                'tpl',
                $this->template
            )
        );

        // load main files
        global $config_cascade;
        return array_merge(
            $conf,
            $this->loadConfigs($config_cascade['main']['default'])
        );
    }

    /**
     * Reads the language strings
     *
     * Only reads extensions, main one is loaded the usual way
     *
     * @return array
     */
    public function loadLangs()
    {
        $lang = [];

        // plugins
        foreach ($this->plugins as $plugin) {
            $lang = array_merge(
                $lang,
                $this->loadExtensionLang(
                    DOKU_PLUGIN . $plugin . '/',
                    'plugin',
                    $plugin
                )
            );
        }

        // current template
        $lang = array_merge(
            $lang,
            $this->loadExtensionLang(
                tpl_incdir() . '/',
                'tpl',
                $this->template
            )
        );

        return $lang;
    }

    /**
     * Read the local settings
     *
     * @return array
     */
    public function loadLocal()
    {
        global $config_cascade;
        return $this->loadConfigs($config_cascade['main']['local']);
    }

    /**
     * Read the protected settings
     *
     * @return array
     */
    public function loadProtected()
    {
        global $config_cascade;
        return $this->loadConfigs($config_cascade['main']['protected']);
    }

    /**
     * Read the config values from the given files
     *
     * @param string[] $files paths to config php's
     * @return array
     */
    protected function loadConfigs($files)
    {
        $conf = [];
        foreach ($files as $file) {
            $conf = array_merge($conf, $this->parser->parse($file));
        }
        return $conf;
    }

    /**
     * Read settings file from an extension
     *
     * This is used to read the settings.php files of plugins and templates
     *
     * @param string $file php file to read
     * @param string $type should be 'plugin' or 'tpl'
     * @param string $extname name of the extension
     * @return array
     */
    protected function loadExtensionMeta($file, $type, $extname)
    {
        if (!file_exists($file)) return [];
        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;

        // include file
        $meta = [];
        include $file;
        if ($meta === []) return [];

        // read data
        $data = [];
        $data[$prefix . $type . '_settings_name'] = ['fieldset'];
        foreach ($meta as $key => $value) {
            if (isset($value[0]) && $value[0] == 'fieldset') continue; //plugins only get one fieldset
            $data[$prefix . $key] = $value;
        }

        return $data;
    }

    /**
     * Read a default file from an extension
     *
     * This is used to read the default.php files of plugins and templates
     *
     * @param string $file php file to read
     * @param string $type should be 'plugin' or 'tpl'
     * @param string $extname name of the extension
     * @return array
     */
    protected function loadExtensionConf($file, $type, $extname)
    {
        if (!file_exists($file)) return [];
        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;

        // parse file
        $conf = $this->parser->parse($file);
        if (empty($conf)) return [];

        // read data
        $data = [];
        foreach ($conf as $key => $value) {
            $data[$prefix . $key] = $value;
        }

        return $data;
    }

    /**
     * Read the language file of an extension
     *
     * @param string $dir directory of the extension
     * @param string $type should be 'plugin' or 'tpl'
     * @param string $extname name of the extension
     * @return array
     */
    protected function loadExtensionLang($dir, $type, $extname)
    {
        global $conf;
        $ll = $conf['lang'];
        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;

        // include files
        $lang = [];
        if (file_exists($dir . 'lang/en/settings.php')) {
            include $dir . 'lang/en/settings.php';
        }
        if ($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
            include $dir . 'lang/' . $ll . '/settings.php';
        }

        // set up correct keys
        $strings = [];
        foreach ($lang as $key => $val) {
            $strings[$prefix . $key] = $val;
        }

        // add fieldset key
        $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname));

        return $strings;
    }
}