/extensions/Configure/settings/ConfigurationSettings.php

https://github.com/wiki-data/wiki-data · PHP · 408 lines · 262 code · 56 blank · 90 comment · 51 complexity · dfc4ebf6d0c5f86cb68fab2c43fe6998 MD5 · raw file

  1. <?php
  2. if ( !defined( 'MEDIAWIKI' ) ) die();
  3. /**
  4. * Hold configuration settings access
  5. * @ingroup Extensions
  6. */
  7. class ConfigurationSettings {
  8. protected $types, $initialized = false;
  9. // Core settings
  10. protected $settings, $arrayDefs, $emptyValues, $editRestricted,
  11. $viewRestricted, $notEditableSettings, $settingsVersion;
  12. protected $extensionsObjects = null;
  13. // Cache
  14. protected $cache = array();
  15. public static function singleton( $types ) {
  16. static $instances = array();
  17. if ( !isset( $instances[$types] ) )
  18. $instances[$types] = new self( $types );
  19. return $instances[$types];
  20. }
  21. /**
  22. * Constructor
  23. * private, use ConfigurationSettings::sigleton() to get an instance
  24. */
  25. protected function __construct( $types ) {
  26. $this->types = $types;
  27. }
  28. /**
  29. * Load messages and initialise static variables
  30. */
  31. protected function loadSettingsDefs() {
  32. if ( $this->initialized ) return;
  33. $this->initialized = true;
  34. wfProfileIn( __METHOD__ );
  35. require( dirname( __FILE__ ) . '/Settings-core.php' );
  36. $this->settings = $settings;
  37. $this->arrayDefs = $arrayDefs;
  38. $this->emptyValues = $emptyValues;
  39. $this->editRestricted = $editRestricted;
  40. $this->viewRestricted = $viewRestricted;
  41. $this->notEditableSettings = $notEditableSettings;
  42. $this->settingsVersion = $settingsVersion;
  43. wfProfileOut( __METHOD__ );
  44. }
  45. protected function loadExtensions() {
  46. if ( is_array( $this->extensionsObjects ) )
  47. return;
  48. wfProfileIn( __METHOD__ );
  49. global $wgConfigureAdditionalExtensions;
  50. $coreExtensions = TxtDef::loadFromFile( dirname( __FILE__ ) . '/Settings-ext.txt', array( 'key' => 'name' ) );
  51. $extensions = array_merge( $coreExtensions, $wgConfigureAdditionalExtensions );
  52. usort( $extensions, array( __CLASS__, 'compExt' ) );
  53. $list = array();
  54. foreach( $extensions as $ext ) {
  55. $ext = new WebExtension( $ext );
  56. #if( $ext->isInstalled() ) {
  57. $list[] = $ext;
  58. #}
  59. }
  60. $this->extensionsObjects = $list;
  61. wfProfileOut( __METHOD__ );
  62. }
  63. /**
  64. * Get an array of WebExtensions objects
  65. *
  66. * @return array
  67. */
  68. public function getAllExtensionsObjects() {
  69. $this->loadExtensions();
  70. return $this->extensionsObjects;
  71. }
  72. /**
  73. * Get an extension object by name
  74. */
  75. public function getExtension( $name ) {
  76. $this->loadExtensions();
  77. foreach( $this->extensionsObjects as $ext )
  78. if ( $ext->getName() == $name )
  79. return $ext;
  80. return null;
  81. }
  82. /**
  83. * Callback to sort extensions
  84. */
  85. public static function compExt( $e1, $e2 ) {
  86. return strcasecmp( $e1['name'], $e2['name'] );
  87. }
  88. /**
  89. * Get settings, grouped by section
  90. *
  91. * @return array
  92. */
  93. public function getSettings() {
  94. wfProfileIn( __METHOD__ );
  95. $this->loadSettingsDefs();
  96. $ret = array();
  97. if( ( $this->types & CONF_SETTINGS_CORE ) == CONF_SETTINGS_CORE ) {
  98. $ret = $this->settings;
  99. }
  100. if( ( $this->types & CONF_SETTINGS_EXT ) == CONF_SETTINGS_EXT ) {
  101. static $extArr = null;
  102. if( is_null( $extArr ) ) {
  103. $extArr = array();
  104. foreach( $this->getAllExtensionsObjects() as $ext ) {
  105. if( !$ext->isUsable() )
  106. continue;
  107. $extSettings = $ext->getSettings();
  108. if( $ext->useVariable() )
  109. $extSettings[$ext->getVariable()] = 'bool';
  110. if( count( $extSettings ) )
  111. $extArr['mw-extensions'][$ext->getName()] = $extSettings;
  112. }
  113. }
  114. $ret += $extArr;
  115. }
  116. wfProfileOut( __METHOD__ );
  117. return $ret;
  118. }
  119. /**
  120. * Get a simple array with all config settings
  121. *
  122. * @return array
  123. */
  124. public function getAllSettings() {
  125. if( isset( $this->cache['all'] ) ) {
  126. return $this->cache['all'];
  127. }
  128. wfProfileIn( __METHOD__ );
  129. $this->loadSettingsDefs();
  130. $arr = array();
  131. foreach( $this->getSettings() as $section ) {
  132. foreach( $section as $group ) {
  133. foreach( $group as $var => $type ) {
  134. $arr[$var] = $type;
  135. }
  136. }
  137. }
  138. $this->cache['all'] = $arr;
  139. wfProfileOut( __METHOD__ );
  140. return $this->cache['all'];
  141. }
  142. /**
  143. * Get the list of settings that are view restricted
  144. *
  145. * @return array
  146. */
  147. public function getViewRestricted() {
  148. $this->loadSettingsDefs();
  149. wfProfileIn( __METHOD__ );
  150. $ret = array();
  151. if ( ( $this->types & CONF_SETTINGS_CORE ) == CONF_SETTINGS_CORE ) {
  152. $ret += $this->viewRestricted;
  153. }
  154. if ( ( $this->types & CONF_SETTINGS_EXT ) == CONF_SETTINGS_EXT ) {
  155. foreach ( $this->getAllExtensionsObjects() as $ext ) {
  156. $ret = array_merge( $ret, $ext->getViewRestricted() );
  157. }
  158. }
  159. wfProfileOut( __METHOD__ );
  160. return $ret;
  161. }
  162. /**
  163. * Get the list of settings that are edit restricted
  164. *
  165. * @return array
  166. */
  167. public function getEditRestricted() {
  168. $this->loadSettingsDefs();
  169. wfProfileIn( __METHOD__ );
  170. $ret = array();
  171. if ( ( $this->types & CONF_SETTINGS_CORE ) == CONF_SETTINGS_CORE ) {
  172. $ret += $this->editRestricted;
  173. }
  174. if ( ( $this->types & CONF_SETTINGS_EXT ) == CONF_SETTINGS_EXT ) {
  175. foreach ( $this->getAllExtensionsObjects() as $ext ) {
  176. $ret = array_merge( $ret, $ext->getEditRestricted() );
  177. }
  178. }
  179. wfProfileOut( __METHOD__ );
  180. return $ret;
  181. }
  182. /**
  183. * Get the list of settings that aren't editable by anybody
  184. *
  185. * @return array
  186. */
  187. public function getUneditableSettings() {
  188. if ( isset( $this->cache['uneditable'] ) )
  189. return $this->cache['uneditable'];
  190. $this->loadSettingsDefs();
  191. wfProfileIn( __METHOD__ );
  192. $notEditable = array();
  193. if ( ( $this->types & CONF_SETTINGS_CORE ) == CONF_SETTINGS_CORE ) {
  194. $notEditable += $this->notEditableSettings;
  195. }
  196. if ( ( $this->types & CONF_SETTINGS_EXT ) == CONF_SETTINGS_EXT ) {
  197. $notEditable += array(); // Nothing for extensions
  198. }
  199. global $wgConf, $wgConfigureNotEditableSettings, $wgConfigureEditableSettings;
  200. $notEditable = array_merge( $notEditable, $wgConf->getUneditableSettings() );
  201. if ( !count( $wgConfigureNotEditableSettings ) && count( $wgConfigureEditableSettings ) &&
  202. ( $this->types & CONF_SETTINGS_CORE ) == CONF_SETTINGS_CORE ) {
  203. // Only disallow core settings, not extensions settings!
  204. $coreSettings = array();
  205. foreach( $this->settings as $section ) {
  206. foreach( $section as $group ) {
  207. foreach( $group as $var => $type ) {
  208. $coreSettings[] = $var;
  209. }
  210. }
  211. }
  212. $wgConfigureNotEditableSettings = array_diff( $coreSettings, $wgConfigureEditableSettings );
  213. }
  214. $notEditable = array_merge( $notEditable,
  215. $wgConfigureNotEditableSettings );
  216. wfProfileOut( __METHOD__ );
  217. return $this->cache['uneditable'] = $notEditable;
  218. }
  219. /**
  220. * Get a list of editable settings
  221. *
  222. * @return array
  223. */
  224. public function getEditableSettings() {
  225. if ( isset( $this->cache['editable'] ) )
  226. return $this->cache['editable'];
  227. $this->cache['editable'] = array();
  228. $this->loadSettingsDefs();
  229. wfProfileIn( __METHOD__ );
  230. global $wgConfigureEditableSettings;
  231. if( count( $wgConfigureEditableSettings ) ) {
  232. foreach( $wgConfigureEditableSettings as $setting ) {
  233. $this->cache['editable'][$setting] = $this->getSettingType( $setting );
  234. }
  235. // We'll need to add extensions settings
  236. if ( ( $this->types & CONF_SETTINGS_EXT ) == CONF_SETTINGS_EXT ) {
  237. foreach ( $this->getAllExtensionsObjects() as $ext ) {
  238. $this->cache['editable'] += $ext->getSettings();
  239. }
  240. }
  241. return $this->cache['editable'];
  242. }
  243. $notEdit = $this->getUneditableSettings();
  244. $settings = $this->getAllSettings();
  245. foreach ( $notEdit as $setting )
  246. unset( $settings[$setting] );
  247. wfProfileOut( __METHOD__ );
  248. return $this->cache['editable'] = $settings;
  249. }
  250. /**
  251. * Get settings to be snapshoted
  252. *
  253. * @return array
  254. */
  255. public function getSnapshotSettings() {
  256. static $alwaysSnapshot = array( 'wgGroupPermissions', 'wgImplicitGroups', 'wgAutopromote' );
  257. global $wgConfigureEditableSettings;
  258. if( count( $wgConfigureEditableSettings ) ) {
  259. $settings = $wgConfigureEditableSettings;
  260. } else {
  261. $settings = array_keys( $this->getEditableSettings() );
  262. }
  263. $settings = array_unique( array_merge( $settings, $alwaysSnapshot ) );
  264. return $settings;
  265. }
  266. /**
  267. * Get the list of all arrays settings, mapping setting name to its type
  268. *
  269. * @return array
  270. */
  271. public function getArrayDefs() {
  272. if ( isset( $this->cache['array'] ) )
  273. return $this->cache['array'];
  274. $list = array();
  275. $this->loadSettingsDefs();
  276. wfProfileIn( __METHOD__ );
  277. if ( ( $this->types & CONF_SETTINGS_CORE ) == CONF_SETTINGS_CORE ) {
  278. $list += $this->arrayDefs;
  279. }
  280. if ( ( $this->types & CONF_SETTINGS_EXT ) == CONF_SETTINGS_EXT ) {
  281. foreach ( $this->getAllExtensionsObjects() as $ext ) {
  282. $list += $ext->getArrayDefs();
  283. }
  284. }
  285. wfProfileOut( __METHOD__ );
  286. return $this->cache['array'] = $list;
  287. }
  288. /**
  289. * Get an array of settings which should have specific values when they're
  290. * empty
  291. *
  292. * @return array
  293. */
  294. public function getEmptyValues() {
  295. if ( isset( $this->cache['empty'] ) )
  296. return $this->cache['empty'];
  297. wfProfileIn( __METHOD__ );
  298. $list = array();
  299. if ( ( $this->types & CONF_SETTINGS_CORE ) == CONF_SETTINGS_CORE ) {
  300. $list += $this->emptyValues;
  301. }
  302. if ( ( $this->types & CONF_SETTINGS_EXT ) == CONF_SETTINGS_EXT ) {
  303. foreach ( $this->getAllExtensionsObjects() as $ext ) {
  304. $list += $ext->getEmptyValues();
  305. }
  306. }
  307. wfProfileOut( __METHOD__ );
  308. return $this->cache['empty'] = $list;
  309. }
  310. /**
  311. * Return true if the setting is available in this version of MediaWiki
  312. *
  313. * @return bool
  314. */
  315. public function isSettingAvailable( $setting ) {
  316. $this->loadSettingsDefs();
  317. if ( !array_key_exists( $setting, $this->getAllSettings() ) )
  318. return false;
  319. if ( !array_key_exists( $setting, $this->settingsVersion ) )
  320. return true;
  321. global $wgVersion;
  322. foreach ( $this->settingsVersion[$setting] as $test ) {
  323. list( $ver, $comp ) = $test;
  324. if ( !version_compare( $wgVersion, $ver, $comp ) )
  325. return false;
  326. }
  327. return true;
  328. }
  329. /**
  330. * Get the type of a setting
  331. *
  332. * @param $setting String: setting name
  333. * @return mixed
  334. */
  335. public function getSettingType( $setting ) {
  336. $settings = $this->getAllSettings();
  337. if ( isset( $settings[$setting] ) )
  338. return $settings[$setting];
  339. else
  340. return false;
  341. }
  342. /**
  343. * Get the array type of a setting
  344. *
  345. * @param $setting String: setting name
  346. */
  347. public function getArrayType( $setting ) {
  348. $arr = $this->getArrayDefs();
  349. return isset( $arr[$setting] ) ?
  350. $arr[$setting] : null;
  351. }
  352. }