SiteConfiguration.php 13 KB

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