Loader.php 7.1 KB

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