PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/SiteConfiguration.php

https://bitbucket.org/brunodefraine/mediawiki
PHP | 409 lines | 215 code | 38 blank | 156 comment | 64 complexity | b84b6a192ccaf583354a142c05caf34b MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * This is a class used to hold configuration settings, particularly for multi-wiki sites.
  4. */
  5. class SiteConfiguration {
  6. /**
  7. * Array of suffixes, for self::siteFromDB()
  8. */
  9. public $suffixes = array();
  10. /**
  11. * Array of wikis, should be the same as $wgLocalDatabases
  12. */
  13. public $wikis = array();
  14. /**
  15. * The whole array of settings
  16. */
  17. public $settings = array();
  18. /**
  19. * Array of domains that are local and can be handled by the same server
  20. */
  21. public $localVHosts = array();
  22. /**
  23. * Optional callback to load full configuration data.
  24. */
  25. public $fullLoadCallback = null;
  26. /** Whether or not all data has been loaded */
  27. public $fullLoadDone = false;
  28. /**
  29. * A callback function that returns an array with the following keys (all
  30. * optional):
  31. * - suffix: site's suffix
  32. * - lang: site's lang
  33. * - tags: array of wiki tags
  34. * - params: array of parameters to be replaced
  35. * The function will receive the SiteConfiguration instance in the first
  36. * argument and the wiki in the second one.
  37. * if suffix and lang are passed they will be used for the return value of
  38. * self::siteFromDB() and self::$suffixes will be ignored
  39. */
  40. public $siteParamsCallback = null;
  41. /**
  42. * Retrieves a configuration setting for a given wiki.
  43. * @param $settingName String ID of the setting name to retrieve
  44. * @param $wiki String Wiki ID of the wiki in question.
  45. * @param $suffix String The suffix of the wiki in question.
  46. * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
  47. * @param $wikiTags Array The tags assigned to the wiki.
  48. * @return Mixed the value of the setting requested.
  49. */
  50. public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
  51. $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
  52. return $this->getSetting( $settingName, $wiki, $params );
  53. }
  54. /**
  55. * Really retrieves a configuration setting for a given wiki.
  56. *
  57. * @param $settingName String ID of the setting name to retrieve.
  58. * @param $wiki String Wiki ID of the wiki in question.
  59. * @param $params Array: array of parameters.
  60. * @return Mixed the value of the setting requested.
  61. */
  62. protected function getSetting( $settingName, $wiki, /*array*/ $params ){
  63. $retval = null;
  64. if( array_key_exists( $settingName, $this->settings ) ) {
  65. $thisSetting =& $this->settings[$settingName];
  66. do {
  67. // Do individual wiki settings
  68. if( array_key_exists( $wiki, $thisSetting ) ) {
  69. $retval = $thisSetting[$wiki];
  70. break;
  71. } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
  72. $retval = $thisSetting["+$wiki"];
  73. }
  74. // Do tag settings
  75. foreach( $params['tags'] as $tag ) {
  76. if( array_key_exists( $tag, $thisSetting ) ) {
  77. if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
  78. $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
  79. } else {
  80. $retval = $thisSetting[$tag];
  81. }
  82. break 2;
  83. } elseif( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
  84. if( !isset( $retval ) )
  85. $retval = array();
  86. $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
  87. }
  88. }
  89. // Do suffix settings
  90. $suffix = $params['suffix'];
  91. if( !is_null( $suffix ) ) {
  92. if( array_key_exists( $suffix, $thisSetting ) ) {
  93. if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
  94. $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
  95. } else {
  96. $retval = $thisSetting[$suffix];
  97. }
  98. break;
  99. } elseif( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
  100. if (!isset($retval))
  101. $retval = array();
  102. $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
  103. }
  104. }
  105. // Fall back to default.
  106. if( array_key_exists( 'default', $thisSetting ) ) {
  107. if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
  108. $retval = self::arrayMerge( $retval, $thisSetting['default'] );
  109. } else {
  110. $retval = $thisSetting['default'];
  111. }
  112. break;
  113. }
  114. } while ( false );
  115. }
  116. if( !is_null( $retval ) && count( $params['params'] ) ) {
  117. foreach ( $params['params'] as $key => $value ) {
  118. $retval = $this->doReplace( '$' . $key, $value, $retval );
  119. }
  120. }
  121. return $retval;
  122. }
  123. /**
  124. * Type-safe string replace; won't do replacements on non-strings
  125. * private?
  126. *
  127. * @param $from
  128. * @param $to
  129. * @param $in
  130. * @return string
  131. */
  132. function doReplace( $from, $to, $in ) {
  133. if( is_string( $in ) ) {
  134. return str_replace( $from, $to, $in );
  135. } elseif( is_array( $in ) ) {
  136. foreach( $in as $key => $val ) {
  137. $in[$key] = $this->doReplace( $from, $to, $val );
  138. }
  139. return $in;
  140. } else {
  141. return $in;
  142. }
  143. }
  144. /**
  145. * Gets all settings for a wiki
  146. * @param $wiki String Wiki ID of the wiki in question.
  147. * @param $suffix String The suffix of the wiki in question.
  148. * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
  149. * @param $wikiTags Array The tags assigned to the wiki.
  150. * @return Array Array of settings requested.
  151. */
  152. public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
  153. $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
  154. $localSettings = array();
  155. foreach( $this->settings as $varname => $stuff ) {
  156. $append = false;
  157. $var = $varname;
  158. if ( substr( $varname, 0, 1 ) == '+' ) {
  159. $append = true;
  160. $var = substr( $varname, 1 );
  161. }
  162. $value = $this->getSetting( $varname, $wiki, $params );
  163. if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) )
  164. $value = self::arrayMerge( $value, $GLOBALS[$var] );
  165. if ( !is_null( $value ) ) {
  166. $localSettings[$var] = $value;
  167. }
  168. }
  169. return $localSettings;
  170. }
  171. /**
  172. * Retrieves a configuration setting for a given wiki, forced to a boolean.
  173. * @param $setting String ID of the setting name to retrieve
  174. * @param $wiki String Wiki ID of the wiki in question.
  175. * @param $suffix String The suffix of the wiki in question.
  176. * @param $wikiTags Array The tags assigned to the wiki.
  177. * @return bool The value of the setting requested.
  178. */
  179. public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
  180. return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
  181. }
  182. /**
  183. * Retrieves an array of local databases
  184. *
  185. * @return array
  186. */
  187. function &getLocalDatabases() {
  188. return $this->wikis;
  189. }
  190. /**
  191. * Retrieves the value of a given setting, and places it in a variable passed by reference.
  192. * @param $setting String ID of the setting name to retrieve
  193. * @param $wiki String Wiki ID of the wiki in question.
  194. * @param $suffix String The suffix of the wiki in question.
  195. * @param $var Reference The variable to insert the value into.
  196. * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
  197. * @param $wikiTags Array The tags assigned to the wiki.
  198. */
  199. public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
  200. $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
  201. if ( !is_null( $value ) ) {
  202. $var = $value;
  203. }
  204. }
  205. /**
  206. * Retrieves the value of a given setting, and places it in its corresponding global variable.
  207. * @param $setting String ID of the setting name to retrieve
  208. * @param $wiki String Wiki ID of the wiki in question.
  209. * @param $suffix String The suffix of the wiki in question.
  210. * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
  211. * @param $wikiTags Array The tags assigned to the wiki.
  212. */
  213. public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
  214. $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
  215. $this->extractGlobalSetting( $setting, $wiki, $params );
  216. }
  217. /**
  218. * @param $setting string
  219. * @param $wiki string
  220. * @param $params array
  221. */
  222. public function extractGlobalSetting( $setting, $wiki, $params ) {
  223. $value = $this->getSetting( $setting, $wiki, $params );
  224. if ( !is_null( $value ) ) {
  225. if (substr($setting,0,1) == '+' && is_array($value)) {
  226. $setting = substr($setting,1);
  227. if ( is_array($GLOBALS[$setting]) ) {
  228. $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
  229. } else {
  230. $GLOBALS[$setting] = $value;
  231. }
  232. } else {
  233. $GLOBALS[$setting] = $value;
  234. }
  235. }
  236. }
  237. /**
  238. * Retrieves the values of all settings, and places them in their corresponding global variables.
  239. * @param $wiki String Wiki ID of the wiki in question.
  240. * @param $suffix String The suffix of the wiki in question.
  241. * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
  242. * @param $wikiTags Array The tags assigned to the wiki.
  243. */
  244. public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
  245. $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
  246. foreach ( $this->settings as $varName => $setting ) {
  247. $this->extractGlobalSetting( $varName, $wiki, $params );
  248. }
  249. }
  250. /**
  251. * Return specific settings for $wiki
  252. * See the documentation of self::$siteParamsCallback for more in-depth
  253. * documentation about this function
  254. *
  255. * @param $wiki String
  256. * @return array
  257. */
  258. protected function getWikiParams( $wiki ){
  259. static $default = array(
  260. 'suffix' => null,
  261. 'lang' => null,
  262. 'tags' => array(),
  263. 'params' => array(),
  264. );
  265. if( !is_callable( $this->siteParamsCallback ) ) {
  266. return $default;
  267. }
  268. $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
  269. # Validate the returned value
  270. if( !is_array( $ret ) ) {
  271. return $default;
  272. }
  273. foreach( $default as $name => $def ){
  274. if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
  275. $ret[$name] = $default[$name];
  276. }
  277. return $ret;
  278. }
  279. /**
  280. * Merge params between the ones passed to the function and the ones given
  281. * by self::$siteParamsCallback for backward compatibility
  282. * Values returned by self::getWikiParams() have the priority.
  283. *
  284. * @param $wiki String Wiki ID of the wiki in question.
  285. * @param $suffix String The suffix of the wiki in question.
  286. * @param $params Array List of parameters. $.'key' is replaced by $value in
  287. * all returned data.
  288. * @param $wikiTags Array The tags assigned to the wiki.
  289. * @return array
  290. */
  291. protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ){
  292. $ret = $this->getWikiParams( $wiki );
  293. if( is_null( $ret['suffix'] ) )
  294. $ret['suffix'] = $suffix;
  295. $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
  296. $ret['params'] += $params;
  297. // Automatically fill that ones if needed
  298. if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
  299. $ret['params']['lang'] = $ret['lang'];
  300. if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
  301. $ret['params']['site'] = $ret['suffix'];
  302. return $ret;
  303. }
  304. /**
  305. * Work out the site and language name from a database name
  306. * @param $db
  307. *
  308. * @return array
  309. */
  310. public function siteFromDB( $db ) {
  311. // Allow override
  312. $def = $this->getWikiParams( $db );
  313. if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
  314. return array( $def['suffix'], $def['lang'] );
  315. $site = null;
  316. $lang = null;
  317. foreach ( $this->suffixes as $suffix ) {
  318. if ( $suffix === '' ) {
  319. $site = '';
  320. $lang = $db;
  321. break;
  322. } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
  323. $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
  324. $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
  325. break;
  326. }
  327. }
  328. $lang = str_replace( '_', '-', $lang );
  329. return array( $site, $lang );
  330. }
  331. /**
  332. * Returns true if the given vhost is handled locally.
  333. * @param $vhost String
  334. * @return bool
  335. */
  336. public function isLocalVHost( $vhost ) {
  337. return in_array( $vhost, $this->localVHosts );
  338. }
  339. /**
  340. * Merge multiple arrays together.
  341. * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
  342. * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
  343. * which is not fun
  344. *
  345. * @param $array1 array
  346. *
  347. * @return array
  348. */
  349. static function arrayMerge( $array1/* ... */ ) {
  350. $out = $array1;
  351. for( $i = 1; $i < func_num_args(); $i++ ) {
  352. foreach( func_get_arg( $i ) as $key => $value ) {
  353. if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
  354. $out[$key] = self::arrayMerge( $out[$key], $value );
  355. } elseif ( !isset($out[$key]) || !$out[$key] && !is_numeric($key) ) {
  356. // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
  357. $out[$key] = $value;
  358. } elseif ( is_numeric( $key ) ) {
  359. $out[] = $value;
  360. }
  361. }
  362. }
  363. return $out;
  364. }
  365. public function loadFullData() {
  366. if ($this->fullLoadCallback && !$this->fullLoadDone) {
  367. call_user_func( $this->fullLoadCallback, $this );
  368. $this->fullLoadDone = true;
  369. }
  370. }
  371. }