xref: /dokuwiki/lib/plugins/config/core/Loader.php (revision 80302b1fb854195839177f073934591814b46ed2)
16850fd4aSAndreas Gohr<?php
26850fd4aSAndreas Gohr
36850fd4aSAndreas Gohrnamespace dokuwiki\plugin\config\core;
46850fd4aSAndreas Gohr
56850fd4aSAndreas Gohr/**
66850fd4aSAndreas Gohr * Configuration loader
76850fd4aSAndreas Gohr *
86850fd4aSAndreas Gohr * Loads configuration meta data and settings from the various files. Honors the
96850fd4aSAndreas Gohr * configuration cascade and installed plugins.
106850fd4aSAndreas Gohr */
116850fd4aSAndreas Gohrclass Loader {
126850fd4aSAndreas Gohr    /** @var ConfigParser */
136850fd4aSAndreas Gohr    protected $parser;
146850fd4aSAndreas Gohr
156850fd4aSAndreas Gohr    /** @var string[] list of enabled plugins */
166850fd4aSAndreas Gohr    protected $plugins;
176850fd4aSAndreas Gohr    /** @var string current template */
186850fd4aSAndreas Gohr    protected $template;
196850fd4aSAndreas Gohr
206850fd4aSAndreas Gohr    /**
216850fd4aSAndreas Gohr     * Loader constructor.
226850fd4aSAndreas Gohr     * @param ConfigParser $parser
23*80302b1fSAndreas Gohr     * @triggers PLUGIN_CONFIG_PLUGINLIST
246850fd4aSAndreas Gohr     */
256850fd4aSAndreas Gohr    public function __construct(ConfigParser $parser) {
266850fd4aSAndreas Gohr        global $conf;
276850fd4aSAndreas Gohr        $this->parser = $parser;
286850fd4aSAndreas Gohr        $this->plugins = plugin_list();
296850fd4aSAndreas Gohr        $this->template = $conf['template'];
30*80302b1fSAndreas Gohr        // allow plugins to remove configurable plugins
31*80302b1fSAndreas Gohr        trigger_event('PLUGIN_CONFIG_PLUGINLIST', $list);
326850fd4aSAndreas Gohr    }
336850fd4aSAndreas Gohr
346850fd4aSAndreas Gohr    /**
356850fd4aSAndreas Gohr     * Read the settings meta data
366850fd4aSAndreas Gohr     *
376850fd4aSAndreas Gohr     * Reads the main file, plugins and template settings meta data
386850fd4aSAndreas Gohr     *
396850fd4aSAndreas Gohr     * @return array
406850fd4aSAndreas Gohr     */
416850fd4aSAndreas Gohr    public function loadMeta() {
426850fd4aSAndreas Gohr        // load main file
436850fd4aSAndreas Gohr        $meta = array();
446850fd4aSAndreas Gohr        include DOKU_PLUGIN . 'config/settings/config.metadata.php';
456850fd4aSAndreas Gohr
466850fd4aSAndreas Gohr        // plugins
476850fd4aSAndreas Gohr        foreach($this->plugins as $plugin) {
4891109d52SAndreas Gohr            $meta = array_merge(
496850fd4aSAndreas Gohr                $meta,
506850fd4aSAndreas Gohr                $this->loadExtensionMeta(
5191109d52SAndreas Gohr                    DOKU_PLUGIN . $plugin . '/conf/metadata.php',
526850fd4aSAndreas Gohr                    'plugin',
536850fd4aSAndreas Gohr                    $plugin
546850fd4aSAndreas Gohr                )
556850fd4aSAndreas Gohr            );
566850fd4aSAndreas Gohr        }
576850fd4aSAndreas Gohr
586850fd4aSAndreas Gohr        // current template
5991109d52SAndreas Gohr        $meta = array_merge(
606850fd4aSAndreas Gohr            $meta,
616850fd4aSAndreas Gohr            $this->loadExtensionMeta(
6291109d52SAndreas Gohr                tpl_incdir() . '/conf/metadata.php',
636850fd4aSAndreas Gohr                'tpl',
646850fd4aSAndreas Gohr                $this->template
656850fd4aSAndreas Gohr            )
666850fd4aSAndreas Gohr        );
676850fd4aSAndreas Gohr
686850fd4aSAndreas Gohr        return $meta;
696850fd4aSAndreas Gohr    }
706850fd4aSAndreas Gohr
716850fd4aSAndreas Gohr    /**
726850fd4aSAndreas Gohr     * Read the default values
736850fd4aSAndreas Gohr     *
746850fd4aSAndreas Gohr     * Reads the main file, plugins and template defaults
756850fd4aSAndreas Gohr     *
766850fd4aSAndreas Gohr     * @return array
776850fd4aSAndreas Gohr     */
786850fd4aSAndreas Gohr    public function loadDefaults() {
796850fd4aSAndreas Gohr        // load main files
806850fd4aSAndreas Gohr        global $config_cascade;
816850fd4aSAndreas Gohr        $conf = $this->loadConfigs($config_cascade['main']['default']);
826850fd4aSAndreas Gohr
836850fd4aSAndreas Gohr        // plugins
846850fd4aSAndreas Gohr        foreach($this->plugins as $plugin) {
8591109d52SAndreas Gohr            $conf = array_merge(
866850fd4aSAndreas Gohr                $conf,
876850fd4aSAndreas Gohr                $this->loadExtensionConf(
886850fd4aSAndreas Gohr                    DOKU_PLUGIN . $plugin . '/conf/default.php',
896850fd4aSAndreas Gohr                    'plugin',
906850fd4aSAndreas Gohr                    $plugin
916850fd4aSAndreas Gohr                )
926850fd4aSAndreas Gohr            );
936850fd4aSAndreas Gohr        }
946850fd4aSAndreas Gohr
956850fd4aSAndreas Gohr        // current template
9691109d52SAndreas Gohr        $conf = array_merge(
976850fd4aSAndreas Gohr            $conf,
986850fd4aSAndreas Gohr            $this->loadExtensionConf(
996850fd4aSAndreas Gohr                tpl_incdir() . '/conf/default.php',
1006850fd4aSAndreas Gohr                'tpl',
1016850fd4aSAndreas Gohr                $this->template
1026850fd4aSAndreas Gohr            )
1036850fd4aSAndreas Gohr        );
1046850fd4aSAndreas Gohr
1056850fd4aSAndreas Gohr        return $conf;
1066850fd4aSAndreas Gohr    }
1076850fd4aSAndreas Gohr
1086850fd4aSAndreas Gohr    /**
1095675a07cSAndreas Gohr     * Reads the language strings
1105675a07cSAndreas Gohr     *
1115675a07cSAndreas Gohr     * Only reads extensions, main one is loaded the usual way
1125675a07cSAndreas Gohr     *
1135675a07cSAndreas Gohr     * @return array
1145675a07cSAndreas Gohr     */
1155675a07cSAndreas Gohr    public function loadLangs() {
1165675a07cSAndreas Gohr        $lang = array();
1175675a07cSAndreas Gohr
1185675a07cSAndreas Gohr        // plugins
1195675a07cSAndreas Gohr        foreach($this->plugins as $plugin) {
12091109d52SAndreas Gohr            $lang = array_merge(
1215675a07cSAndreas Gohr                $lang,
1225675a07cSAndreas Gohr                $this->loadExtensionLang(
1235675a07cSAndreas Gohr                    DOKU_PLUGIN . $plugin . '/',
1245675a07cSAndreas Gohr                    'plugin',
1255675a07cSAndreas Gohr                    $plugin
1265675a07cSAndreas Gohr                )
1275675a07cSAndreas Gohr            );
1285675a07cSAndreas Gohr        }
1295675a07cSAndreas Gohr
1305675a07cSAndreas Gohr        // current template
13191109d52SAndreas Gohr        $lang = array_merge(
1325675a07cSAndreas Gohr            $lang,
13391109d52SAndreas Gohr            $this->loadExtensionLang(
1345675a07cSAndreas Gohr                tpl_incdir() . '/',
1355675a07cSAndreas Gohr                'tpl',
1365675a07cSAndreas Gohr                $this->template
1375675a07cSAndreas Gohr            )
1385675a07cSAndreas Gohr        );
1395675a07cSAndreas Gohr
1405675a07cSAndreas Gohr        return $lang;
1415675a07cSAndreas Gohr    }
1425675a07cSAndreas Gohr
1435675a07cSAndreas Gohr    /**
1446850fd4aSAndreas Gohr     * Read the local settings
1456850fd4aSAndreas Gohr     *
1466850fd4aSAndreas Gohr     * @return array
1476850fd4aSAndreas Gohr     */
1486850fd4aSAndreas Gohr    public function loadLocal() {
1496850fd4aSAndreas Gohr        global $config_cascade;
1506850fd4aSAndreas Gohr        return $this->loadConfigs($config_cascade['main']['local']);
1516850fd4aSAndreas Gohr    }
1526850fd4aSAndreas Gohr
1536850fd4aSAndreas Gohr    /**
1546850fd4aSAndreas Gohr     * Read the protected settings
1556850fd4aSAndreas Gohr     *
1566850fd4aSAndreas Gohr     * @return array
1576850fd4aSAndreas Gohr     */
1586850fd4aSAndreas Gohr    public function loadProtected() {
1596850fd4aSAndreas Gohr        global $config_cascade;
1606850fd4aSAndreas Gohr        return $this->loadConfigs($config_cascade['main']['protected']);
1616850fd4aSAndreas Gohr    }
1626850fd4aSAndreas Gohr
1636850fd4aSAndreas Gohr    /**
1646850fd4aSAndreas Gohr     * Read the config values from the given files
1656850fd4aSAndreas Gohr     *
1666850fd4aSAndreas Gohr     * @param string[] $files paths to config php's
1676850fd4aSAndreas Gohr     * @return array
1686850fd4aSAndreas Gohr     */
1696850fd4aSAndreas Gohr    protected function loadConfigs($files) {
1706850fd4aSAndreas Gohr        $conf = array();
1716850fd4aSAndreas Gohr        foreach($files as $file) {
1726850fd4aSAndreas Gohr            $conf = array_merge($conf, $this->parser->parse($file));
1736850fd4aSAndreas Gohr        }
1746850fd4aSAndreas Gohr        return $conf;
1756850fd4aSAndreas Gohr    }
1766850fd4aSAndreas Gohr
1776850fd4aSAndreas Gohr    /**
1786850fd4aSAndreas Gohr     * Read settings file from an extension
1796850fd4aSAndreas Gohr     *
1806850fd4aSAndreas Gohr     * This is used to read the settings.php files of plugins and templates
1816850fd4aSAndreas Gohr     *
1826850fd4aSAndreas Gohr     * @param string $file php file to read
1836850fd4aSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
1846850fd4aSAndreas Gohr     * @param string $extname name of the extension
1856850fd4aSAndreas Gohr     * @return array
1866850fd4aSAndreas Gohr     */
1876850fd4aSAndreas Gohr    protected function loadExtensionMeta($file, $type, $extname) {
1886850fd4aSAndreas Gohr        if(!file_exists($file)) return array();
1896850fd4aSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
1906850fd4aSAndreas Gohr
1916850fd4aSAndreas Gohr        // include file
1926850fd4aSAndreas Gohr        $meta = array();
1936850fd4aSAndreas Gohr        include $file;
1946850fd4aSAndreas Gohr        if(empty($meta)) return array();
1956850fd4aSAndreas Gohr
1966850fd4aSAndreas Gohr        // read data
1976850fd4aSAndreas Gohr        $data = array();
1986850fd4aSAndreas Gohr        $data[$prefix . $type . '_settings_name'] = ['fieldset'];
1996850fd4aSAndreas Gohr        foreach($meta as $key => $value) {
2006850fd4aSAndreas Gohr            if($value[0] == 'fieldset') continue; //plugins only get one fieldset
2016850fd4aSAndreas Gohr            $data[$prefix . $key] = $value;
2026850fd4aSAndreas Gohr        }
2036850fd4aSAndreas Gohr
2046850fd4aSAndreas Gohr        return $data;
2056850fd4aSAndreas Gohr    }
2066850fd4aSAndreas Gohr
2076850fd4aSAndreas Gohr    /**
2086850fd4aSAndreas Gohr     * Read a default file from an extension
2096850fd4aSAndreas Gohr     *
2106850fd4aSAndreas Gohr     * This is used to read the default.php files of plugins and templates
2116850fd4aSAndreas Gohr     *
2126850fd4aSAndreas Gohr     * @param string $file php file to read
2136850fd4aSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
2146850fd4aSAndreas Gohr     * @param string $extname name of the extension
2156850fd4aSAndreas Gohr     * @return array
2166850fd4aSAndreas Gohr     */
2176850fd4aSAndreas Gohr    protected function loadExtensionConf($file, $type, $extname) {
2186850fd4aSAndreas Gohr        if(!file_exists($file)) return array();
2196850fd4aSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
2206850fd4aSAndreas Gohr
2216850fd4aSAndreas Gohr        // parse file
2226850fd4aSAndreas Gohr        $conf = $this->parser->parse($file);
2236850fd4aSAndreas Gohr        if(empty($conf)) return array();
2246850fd4aSAndreas Gohr
2256850fd4aSAndreas Gohr        // read data
2266850fd4aSAndreas Gohr        $data = array();
2276850fd4aSAndreas Gohr        foreach($conf as $key => $value) {
2286850fd4aSAndreas Gohr            $data[$prefix . $key] = $value;
2296850fd4aSAndreas Gohr        }
2306850fd4aSAndreas Gohr
2316850fd4aSAndreas Gohr        return $data;
2326850fd4aSAndreas Gohr    }
2335675a07cSAndreas Gohr
2345675a07cSAndreas Gohr    /**
2355675a07cSAndreas Gohr     * Read the language file of an extension
2365675a07cSAndreas Gohr     *
2375675a07cSAndreas Gohr     * @param string $dir directory of the extension
2385675a07cSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
2395675a07cSAndreas Gohr     * @param string $extname name of the extension
2405675a07cSAndreas Gohr     * @return array
2415675a07cSAndreas Gohr     */
2425675a07cSAndreas Gohr    protected function loadExtensionLang($dir, $type, $extname) {
2435675a07cSAndreas Gohr        global $conf;
2445675a07cSAndreas Gohr        $ll = $conf['lang'];
2455675a07cSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
2465675a07cSAndreas Gohr
2475675a07cSAndreas Gohr        // include files
2485675a07cSAndreas Gohr        $lang = array();
2495675a07cSAndreas Gohr        if(file_exists($dir . 'lang/en/settings.php')) {
2505675a07cSAndreas Gohr            include $dir . 'lang/en/settings.php';
2515675a07cSAndreas Gohr        }
2525675a07cSAndreas Gohr        if($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
2535675a07cSAndreas Gohr            include $dir . 'lang/' . $ll . '/settings.php';
2545675a07cSAndreas Gohr        }
2555675a07cSAndreas Gohr
2565675a07cSAndreas Gohr        // set up correct keys
2575675a07cSAndreas Gohr        $strings = array();
2585675a07cSAndreas Gohr        foreach($lang as $key => $val) {
2595675a07cSAndreas Gohr            $strings[$prefix . $key] = $val;
2605675a07cSAndreas Gohr        }
2615675a07cSAndreas Gohr
2625675a07cSAndreas Gohr        // add fieldset key
263d6fc72e1SAndreas Gohr        $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname));
2645675a07cSAndreas Gohr
2655675a07cSAndreas Gohr        return $strings;
2665675a07cSAndreas Gohr    }
2676850fd4aSAndreas Gohr}
268