xref: /dokuwiki/lib/plugins/config/core/Loader.php (revision 0698489178cc785eb3098fdacfa0fe28a55365eb)
16850fd4aSAndreas Gohr<?php
26850fd4aSAndreas Gohr
36850fd4aSAndreas Gohrnamespace dokuwiki\plugin\config\core;
46850fd4aSAndreas Gohr
5cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
6cbb44eabSAndreas Gohr
76850fd4aSAndreas Gohr/**
86850fd4aSAndreas Gohr * Configuration loader
96850fd4aSAndreas Gohr *
106850fd4aSAndreas Gohr * Loads configuration meta data and settings from the various files. Honors the
116850fd4aSAndreas Gohr * configuration cascade and installed plugins.
126850fd4aSAndreas Gohr */
138c7c53b0SAndreas Gohrclass Loader
148c7c53b0SAndreas Gohr{
156850fd4aSAndreas Gohr    /** @var ConfigParser */
166850fd4aSAndreas Gohr    protected $parser;
176850fd4aSAndreas Gohr
186850fd4aSAndreas Gohr    /** @var string[] list of enabled plugins */
196850fd4aSAndreas Gohr    protected $plugins;
206850fd4aSAndreas Gohr    /** @var string current template */
216850fd4aSAndreas Gohr    protected $template;
226850fd4aSAndreas Gohr
236850fd4aSAndreas Gohr    /**
246850fd4aSAndreas Gohr     * Loader constructor.
256850fd4aSAndreas Gohr     * @param ConfigParser $parser
2680302b1fSAndreas Gohr     * @triggers PLUGIN_CONFIG_PLUGINLIST
276850fd4aSAndreas Gohr     */
28d868eb89SAndreas Gohr    public function __construct(ConfigParser $parser)
29d868eb89SAndreas Gohr    {
306850fd4aSAndreas Gohr        global $conf;
316850fd4aSAndreas Gohr        $this->parser = $parser;
326850fd4aSAndreas Gohr        $this->plugins = plugin_list();
336850fd4aSAndreas Gohr        $this->template = $conf['template'];
3480302b1fSAndreas Gohr        // allow plugins to remove configurable plugins
35cbb44eabSAndreas Gohr        Event::createAndTrigger('PLUGIN_CONFIG_PLUGINLIST', $this->plugins);
366850fd4aSAndreas Gohr    }
376850fd4aSAndreas Gohr
386850fd4aSAndreas Gohr    /**
396850fd4aSAndreas Gohr     * Read the settings meta data
406850fd4aSAndreas Gohr     *
416850fd4aSAndreas Gohr     * Reads the main file, plugins and template settings meta data
426850fd4aSAndreas Gohr     *
436850fd4aSAndreas Gohr     * @return array
446850fd4aSAndreas Gohr     */
45d868eb89SAndreas Gohr    public function loadMeta()
46d868eb89SAndreas Gohr    {
476850fd4aSAndreas Gohr        // load main file
48467c1427SAndreas Gohr        $meta = [];
496850fd4aSAndreas Gohr        include DOKU_PLUGIN . 'config/settings/config.metadata.php';
506850fd4aSAndreas Gohr
516850fd4aSAndreas Gohr        // plugins
526850fd4aSAndreas Gohr        foreach ($this->plugins as $plugin) {
5391109d52SAndreas Gohr            $meta = array_merge(
546850fd4aSAndreas Gohr                $meta,
556850fd4aSAndreas Gohr                $this->loadExtensionMeta(
5691109d52SAndreas Gohr                    DOKU_PLUGIN . $plugin . '/conf/metadata.php',
576850fd4aSAndreas Gohr                    'plugin',
586850fd4aSAndreas Gohr                    $plugin
596850fd4aSAndreas Gohr                )
606850fd4aSAndreas Gohr            );
616850fd4aSAndreas Gohr        }
626850fd4aSAndreas Gohr
636850fd4aSAndreas Gohr        // current template
6491109d52SAndreas Gohr        $meta = array_merge(
656850fd4aSAndreas Gohr            $meta,
666850fd4aSAndreas Gohr            $this->loadExtensionMeta(
6791109d52SAndreas Gohr                tpl_incdir() . '/conf/metadata.php',
686850fd4aSAndreas Gohr                'tpl',
696850fd4aSAndreas Gohr                $this->template
706850fd4aSAndreas Gohr            )
716850fd4aSAndreas Gohr        );
726850fd4aSAndreas Gohr
736850fd4aSAndreas Gohr        return $meta;
746850fd4aSAndreas Gohr    }
756850fd4aSAndreas Gohr
766850fd4aSAndreas Gohr    /**
776850fd4aSAndreas Gohr     * Read the default values
786850fd4aSAndreas Gohr     *
796850fd4aSAndreas Gohr     * Reads the main file, plugins and template defaults
806850fd4aSAndreas Gohr     *
816850fd4aSAndreas Gohr     * @return array
826850fd4aSAndreas Gohr     */
835d2c5d7eSGerrit Uitslag    public function loadDefaults()
845d2c5d7eSGerrit Uitslag    {
8595775ac7SChristoph Ziehr
8695775ac7SChristoph Ziehr        // initialize array
87467c1427SAndreas Gohr        $conf = [];
8895775ac7SChristoph Ziehr
896850fd4aSAndreas Gohr        // plugins
906850fd4aSAndreas Gohr        foreach ($this->plugins as $plugin) {
9191109d52SAndreas Gohr            $conf = array_merge(
926850fd4aSAndreas Gohr                $conf,
936850fd4aSAndreas Gohr                $this->loadExtensionConf(
946850fd4aSAndreas Gohr                    DOKU_PLUGIN . $plugin . '/conf/default.php',
956850fd4aSAndreas Gohr                    'plugin',
966850fd4aSAndreas Gohr                    $plugin
976850fd4aSAndreas Gohr                )
986850fd4aSAndreas Gohr            );
996850fd4aSAndreas Gohr        }
1006850fd4aSAndreas Gohr
1016850fd4aSAndreas Gohr        // current template
10291109d52SAndreas Gohr        $conf = array_merge(
1036850fd4aSAndreas Gohr            $conf,
1046850fd4aSAndreas Gohr            $this->loadExtensionConf(
1056850fd4aSAndreas Gohr                tpl_incdir() . '/conf/default.php',
1066850fd4aSAndreas Gohr                'tpl',
1076850fd4aSAndreas Gohr                $this->template
1086850fd4aSAndreas Gohr            )
1096850fd4aSAndreas Gohr        );
1106850fd4aSAndreas Gohr
11103ae5b30SChristoph Ziehr        // load main files
11203ae5b30SChristoph Ziehr        global $config_cascade;
1135d2c5d7eSGerrit Uitslag        return array_merge(
11495775ac7SChristoph Ziehr            $conf,
11595775ac7SChristoph Ziehr            $this->loadConfigs($config_cascade['main']['default'])
11695775ac7SChristoph Ziehr        );
1176850fd4aSAndreas Gohr    }
1186850fd4aSAndreas Gohr
1196850fd4aSAndreas Gohr    /**
1205675a07cSAndreas Gohr     * Reads the language strings
1215675a07cSAndreas Gohr     *
1225675a07cSAndreas Gohr     * Only reads extensions, main one is loaded the usual way
1235675a07cSAndreas Gohr     *
1245675a07cSAndreas Gohr     * @return array
1255675a07cSAndreas Gohr     */
126d868eb89SAndreas Gohr    public function loadLangs()
127d868eb89SAndreas Gohr    {
128467c1427SAndreas Gohr        $lang = [];
1295675a07cSAndreas Gohr
1305675a07cSAndreas Gohr        // plugins
1315675a07cSAndreas Gohr        foreach ($this->plugins as $plugin) {
13291109d52SAndreas Gohr            $lang = array_merge(
1335675a07cSAndreas Gohr                $lang,
1345675a07cSAndreas Gohr                $this->loadExtensionLang(
1355675a07cSAndreas Gohr                    DOKU_PLUGIN . $plugin . '/',
1365675a07cSAndreas Gohr                    'plugin',
1375675a07cSAndreas Gohr                    $plugin
1385675a07cSAndreas Gohr                )
1395675a07cSAndreas Gohr            );
1405675a07cSAndreas Gohr        }
1415675a07cSAndreas Gohr
1425675a07cSAndreas Gohr        // current template
14391109d52SAndreas Gohr        $lang = array_merge(
1445675a07cSAndreas Gohr            $lang,
14591109d52SAndreas Gohr            $this->loadExtensionLang(
1465675a07cSAndreas Gohr                tpl_incdir() . '/',
1475675a07cSAndreas Gohr                'tpl',
1485675a07cSAndreas Gohr                $this->template
1495675a07cSAndreas Gohr            )
1505675a07cSAndreas Gohr        );
1515675a07cSAndreas Gohr
1525675a07cSAndreas Gohr        return $lang;
1535675a07cSAndreas Gohr    }
1545675a07cSAndreas Gohr
1555675a07cSAndreas Gohr    /**
1566850fd4aSAndreas Gohr     * Read the local settings
1576850fd4aSAndreas Gohr     *
1586850fd4aSAndreas Gohr     * @return array
1596850fd4aSAndreas Gohr     */
160d868eb89SAndreas Gohr    public function loadLocal()
161d868eb89SAndreas Gohr    {
1626850fd4aSAndreas Gohr        global $config_cascade;
1636850fd4aSAndreas Gohr        return $this->loadConfigs($config_cascade['main']['local']);
1646850fd4aSAndreas Gohr    }
1656850fd4aSAndreas Gohr
1666850fd4aSAndreas Gohr    /**
1676850fd4aSAndreas Gohr     * Read the protected settings
1686850fd4aSAndreas Gohr     *
1696850fd4aSAndreas Gohr     * @return array
1706850fd4aSAndreas Gohr     */
171d868eb89SAndreas Gohr    public function loadProtected()
172d868eb89SAndreas Gohr    {
1736850fd4aSAndreas Gohr        global $config_cascade;
1746850fd4aSAndreas Gohr        return $this->loadConfigs($config_cascade['main']['protected']);
1756850fd4aSAndreas Gohr    }
1766850fd4aSAndreas Gohr
1776850fd4aSAndreas Gohr    /**
1786850fd4aSAndreas Gohr     * Read the config values from the given files
1796850fd4aSAndreas Gohr     *
1806850fd4aSAndreas Gohr     * @param string[] $files paths to config php's
1816850fd4aSAndreas Gohr     * @return array
1826850fd4aSAndreas Gohr     */
183d868eb89SAndreas Gohr    protected function loadConfigs($files)
184d868eb89SAndreas Gohr    {
185467c1427SAndreas Gohr        $conf = [];
1866850fd4aSAndreas Gohr        foreach ($files as $file) {
1876850fd4aSAndreas Gohr            $conf = array_merge($conf, $this->parser->parse($file));
1886850fd4aSAndreas Gohr        }
1896850fd4aSAndreas Gohr        return $conf;
1906850fd4aSAndreas Gohr    }
1916850fd4aSAndreas Gohr
1926850fd4aSAndreas Gohr    /**
1936850fd4aSAndreas Gohr     * Read settings file from an extension
1946850fd4aSAndreas Gohr     *
1956850fd4aSAndreas Gohr     * This is used to read the settings.php files of plugins and templates
1966850fd4aSAndreas Gohr     *
1976850fd4aSAndreas Gohr     * @param string $file php file to read
1986850fd4aSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
1996850fd4aSAndreas Gohr     * @param string $extname name of the extension
2006850fd4aSAndreas Gohr     * @return array
2016850fd4aSAndreas Gohr     */
202d868eb89SAndreas Gohr    protected function loadExtensionMeta($file, $type, $extname)
203d868eb89SAndreas Gohr    {
204467c1427SAndreas Gohr        if (!file_exists($file)) return [];
2056850fd4aSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
2066850fd4aSAndreas Gohr
2076850fd4aSAndreas Gohr        // include file
208467c1427SAndreas Gohr        $meta = [];
2096850fd4aSAndreas Gohr        include $file;
210467c1427SAndreas Gohr        if ($meta === []) return [];
2116850fd4aSAndreas Gohr
2126850fd4aSAndreas Gohr        // read data
213467c1427SAndreas Gohr        $data = [];
2146850fd4aSAndreas Gohr        $data[$prefix . $type . '_settings_name'] = ['fieldset'];
2156850fd4aSAndreas Gohr        foreach ($meta as $key => $value) {
216*06984891SEduardo Mozart de Oliveira            if (isset($value[0]) && $value[0] == 'fieldset') continue; //plugins only get one fieldset
2176850fd4aSAndreas Gohr            $data[$prefix . $key] = $value;
2186850fd4aSAndreas Gohr        }
2196850fd4aSAndreas Gohr
2206850fd4aSAndreas Gohr        return $data;
2216850fd4aSAndreas Gohr    }
2226850fd4aSAndreas Gohr
2236850fd4aSAndreas Gohr    /**
2246850fd4aSAndreas Gohr     * Read a default file from an extension
2256850fd4aSAndreas Gohr     *
2266850fd4aSAndreas Gohr     * This is used to read the default.php files of plugins and templates
2276850fd4aSAndreas Gohr     *
2286850fd4aSAndreas Gohr     * @param string $file php file to read
2296850fd4aSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
2306850fd4aSAndreas Gohr     * @param string $extname name of the extension
2316850fd4aSAndreas Gohr     * @return array
2326850fd4aSAndreas Gohr     */
233d868eb89SAndreas Gohr    protected function loadExtensionConf($file, $type, $extname)
234d868eb89SAndreas Gohr    {
235467c1427SAndreas Gohr        if (!file_exists($file)) return [];
2366850fd4aSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
2376850fd4aSAndreas Gohr
2386850fd4aSAndreas Gohr        // parse file
2396850fd4aSAndreas Gohr        $conf = $this->parser->parse($file);
240467c1427SAndreas Gohr        if (empty($conf)) return [];
2416850fd4aSAndreas Gohr
2426850fd4aSAndreas Gohr        // read data
243467c1427SAndreas Gohr        $data = [];
2446850fd4aSAndreas Gohr        foreach ($conf as $key => $value) {
2456850fd4aSAndreas Gohr            $data[$prefix . $key] = $value;
2466850fd4aSAndreas Gohr        }
2476850fd4aSAndreas Gohr
2486850fd4aSAndreas Gohr        return $data;
2496850fd4aSAndreas Gohr    }
2505675a07cSAndreas Gohr
2515675a07cSAndreas Gohr    /**
2525675a07cSAndreas Gohr     * Read the language file of an extension
2535675a07cSAndreas Gohr     *
2545675a07cSAndreas Gohr     * @param string $dir directory of the extension
2555675a07cSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
2565675a07cSAndreas Gohr     * @param string $extname name of the extension
2575675a07cSAndreas Gohr     * @return array
2585675a07cSAndreas Gohr     */
259d868eb89SAndreas Gohr    protected function loadExtensionLang($dir, $type, $extname)
260d868eb89SAndreas Gohr    {
2615675a07cSAndreas Gohr        global $conf;
2625675a07cSAndreas Gohr        $ll = $conf['lang'];
2635675a07cSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
2645675a07cSAndreas Gohr
2655675a07cSAndreas Gohr        // include files
266467c1427SAndreas Gohr        $lang = [];
2675675a07cSAndreas Gohr        if (file_exists($dir . 'lang/en/settings.php')) {
2685675a07cSAndreas Gohr            include $dir . 'lang/en/settings.php';
2695675a07cSAndreas Gohr        }
2705675a07cSAndreas Gohr        if ($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
2715675a07cSAndreas Gohr            include $dir . 'lang/' . $ll . '/settings.php';
2725675a07cSAndreas Gohr        }
2735675a07cSAndreas Gohr
2745675a07cSAndreas Gohr        // set up correct keys
275467c1427SAndreas Gohr        $strings = [];
2765675a07cSAndreas Gohr        foreach ($lang as $key => $val) {
2775675a07cSAndreas Gohr            $strings[$prefix . $key] = $val;
2785675a07cSAndreas Gohr        }
2795675a07cSAndreas Gohr
2805675a07cSAndreas Gohr        // add fieldset key
281d6fc72e1SAndreas Gohr        $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname));
2825675a07cSAndreas Gohr
2835675a07cSAndreas Gohr        return $strings;
2845675a07cSAndreas Gohr    }
2856850fd4aSAndreas Gohr}
286