You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

286 lines
7.1 KiB

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