xref: /dokuwiki/lib/plugins/config/core/Loader.php (revision 95775ac735f4e08dd9c915316e4704b4f59af488)
1<?php
2
3namespace dokuwiki\plugin\config\core;
4
5use dokuwiki\Extension\Event;
6
7/**
8 * Configuration loader
9 *
10 * Loads configuration meta data and settings from the various files. Honors the
11 * configuration cascade and installed plugins.
12 */
13class Loader {
14    /** @var ConfigParser */
15    protected $parser;
16
17    /** @var string[] list of enabled plugins */
18    protected $plugins;
19    /** @var string current template */
20    protected $template;
21
22    /**
23     * Loader constructor.
24     * @param ConfigParser $parser
25     * @triggers PLUGIN_CONFIG_PLUGINLIST
26     */
27    public function __construct(ConfigParser $parser) {
28        global $conf;
29        $this->parser = $parser;
30        $this->plugins = plugin_list();
31        $this->template = $conf['template'];
32        // allow plugins to remove configurable plugins
33        Event::createAndTrigger('PLUGIN_CONFIG_PLUGINLIST', $this->plugins);
34    }
35
36    /**
37     * Read the settings meta data
38     *
39     * Reads the main file, plugins and template settings meta data
40     *
41     * @return array
42     */
43    public function loadMeta() {
44        // load main file
45        $meta = array();
46        include DOKU_PLUGIN . 'config/settings/config.metadata.php';
47
48        // plugins
49        foreach($this->plugins as $plugin) {
50            $meta = array_merge(
51                $meta,
52                $this->loadExtensionMeta(
53                    DOKU_PLUGIN . $plugin . '/conf/metadata.php',
54                    'plugin',
55                    $plugin
56                )
57            );
58        }
59
60        // current template
61        $meta = array_merge(
62            $meta,
63            $this->loadExtensionMeta(
64                tpl_incdir() . '/conf/metadata.php',
65                'tpl',
66                $this->template
67            )
68        );
69
70        return $meta;
71    }
72
73    /**
74     * Read the default values
75     *
76     * Reads the main file, plugins and template defaults
77     *
78     * @return array
79     */
80    public function loadDefaults() {
81
82        // initialize array
83        $conf = array();
84
85        // plugins
86        foreach($this->plugins as $plugin) {
87            $conf = array_merge(
88                $conf,
89                $this->loadExtensionConf(
90                    DOKU_PLUGIN . $plugin . '/conf/default.php',
91                    'plugin',
92                    $plugin
93                )
94            );
95        }
96
97        // current template
98        $conf = array_merge(
99            $conf,
100            $this->loadExtensionConf(
101                tpl_incdir() . '/conf/default.php',
102                'tpl',
103                $this->template
104            )
105        );
106
107		// load main files
108		global $config_cascade;
109		$conf = array_merge(
110			$conf,
111			$this->loadConfigs($config_cascade['main']['default'])
112		);
113
114        return $conf;
115    }
116
117    /**
118     * Reads the language strings
119     *
120     * Only reads extensions, main one is loaded the usual way
121     *
122     * @return array
123     */
124    public function loadLangs() {
125        $lang = array();
126
127        // plugins
128        foreach($this->plugins as $plugin) {
129            $lang = array_merge(
130                $lang,
131                $this->loadExtensionLang(
132                    DOKU_PLUGIN . $plugin . '/',
133                    'plugin',
134                    $plugin
135                )
136            );
137        }
138
139        // current template
140        $lang = array_merge(
141            $lang,
142            $this->loadExtensionLang(
143                tpl_incdir() . '/',
144                'tpl',
145                $this->template
146            )
147        );
148
149        return $lang;
150    }
151
152    /**
153     * Read the local settings
154     *
155     * @return array
156     */
157    public function loadLocal() {
158        global $config_cascade;
159        return $this->loadConfigs($config_cascade['main']['local']);
160    }
161
162    /**
163     * Read the protected settings
164     *
165     * @return array
166     */
167    public function loadProtected() {
168        global $config_cascade;
169        return $this->loadConfigs($config_cascade['main']['protected']);
170    }
171
172    /**
173     * Read the config values from the given files
174     *
175     * @param string[] $files paths to config php's
176     * @return array
177     */
178    protected function loadConfigs($files) {
179        $conf = array();
180        foreach($files as $file) {
181            $conf = array_merge($conf, $this->parser->parse($file));
182        }
183        return $conf;
184    }
185
186    /**
187     * Read settings file from an extension
188     *
189     * This is used to read the settings.php files of plugins and templates
190     *
191     * @param string $file php file to read
192     * @param string $type should be 'plugin' or 'tpl'
193     * @param string $extname name of the extension
194     * @return array
195     */
196    protected function loadExtensionMeta($file, $type, $extname) {
197        if(!file_exists($file)) return array();
198        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
199
200        // include file
201        $meta = array();
202        include $file;
203        if(empty($meta)) return array();
204
205        // read data
206        $data = array();
207        $data[$prefix . $type . '_settings_name'] = ['fieldset'];
208        foreach($meta as $key => $value) {
209            if($value[0] == 'fieldset') continue; //plugins only get one fieldset
210            $data[$prefix . $key] = $value;
211        }
212
213        return $data;
214    }
215
216    /**
217     * Read a default file from an extension
218     *
219     * This is used to read the default.php files of plugins and templates
220     *
221     * @param string $file php file to read
222     * @param string $type should be 'plugin' or 'tpl'
223     * @param string $extname name of the extension
224     * @return array
225     */
226    protected function loadExtensionConf($file, $type, $extname) {
227        if(!file_exists($file)) return array();
228        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
229
230        // parse file
231        $conf = $this->parser->parse($file);
232        if(empty($conf)) return array();
233
234        // read data
235        $data = array();
236        foreach($conf as $key => $value) {
237            $data[$prefix . $key] = $value;
238        }
239
240        return $data;
241    }
242
243    /**
244     * Read the language file of an extension
245     *
246     * @param string $dir directory of the extension
247     * @param string $type should be 'plugin' or 'tpl'
248     * @param string $extname name of the extension
249     * @return array
250     */
251    protected function loadExtensionLang($dir, $type, $extname) {
252        global $conf;
253        $ll = $conf['lang'];
254        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
255
256        // include files
257        $lang = array();
258        if(file_exists($dir . 'lang/en/settings.php')) {
259            include $dir . 'lang/en/settings.php';
260        }
261        if($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
262            include $dir . 'lang/' . $ll . '/settings.php';
263        }
264
265        // set up correct keys
266        $strings = array();
267        foreach($lang as $key => $val) {
268            $strings[$prefix . $key] = $val;
269        }
270
271        // add fieldset key
272        $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname));
273
274        return $strings;
275    }
276}
277