1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
<?php
/**
* The include paths change after this file is included from commandLine.inc,
* meaning that require_once() fails to detect that it is including the same
* file again. We use DIY C-style protection as a workaround.
*/
if (!defined('SITE_CONFIGURATION')) {
define('SITE_CONFIGURATION', 1);
/**
* This is a class used to hold configuration settings, particularly for multi-wiki sites.
*
*/
class SiteConfiguration {
var $suffixes = array();
var $wikis = array();
var $settings = array();
var $localVHosts = array();
/** */
function get( $settingName, $wiki, $suffix, $params = array(), $wikiTags = array() ) {
if ( array_key_exists( $settingName, $this->settings ) ) {
$thisSetting =& $this->settings[$settingName];
do {
if ( array_key_exists( $wiki, $thisSetting ) ) {
$retval = $thisSetting[$wiki];
break;
}
foreach ( $wikiTags as $tag ) {
if ( array_key_exists( $tag, $thisSetting ) ) {
$retval = $thisSetting[$tag];
break 2;
}
}
if ( array_key_exists( $suffix, $thisSetting ) ) {
$retval = $thisSetting[$suffix];
break;
}
if ( array_key_exists( 'default', $thisSetting ) ) {
$retval = $thisSetting['default'];
break;
}
$retval = null;
} while ( false );
} else {
$retval = NULL;
}
if ( !is_null( $retval ) && count( $params ) ) {
foreach ( $params as $key => $value ) {
$retval = $this->doReplace( '$' . $key, $value, $retval );
}
}
return $retval;
}
/** Type-safe string replace; won't do replacements on non-strings */
function doReplace( $from, $to, $in ) {
if( is_string( $in ) ) {
return str_replace( $from, $to, $in );
} elseif( is_array( $in ) ) {
foreach( $in as $key => $val ) {
$in[$key] = $this->doReplace( $from, $to, $val );
}
return $in;
} else {
return $in;
}
}
/** */
function getAll( $wiki, $suffix, $params, $wikiTags = array() ) {
$localSettings = array();
foreach ( $this->settings as $varname => $stuff ) {
$value = $this->get( $varname, $wiki, $suffix, $params, $wikiTags );
if ( !is_null( $value ) ) {
$localSettings[$varname] = $value;
}
}
return $localSettings;
}
/** */
function getBool( $setting, $wiki, $suffix, $wikiTags = array() ) {
return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
}
/** */
function &getLocalDatabases() {
return $this->wikis;
}
/** */
function initialise() {
}
/** */
function extractVar( $setting, $wiki, $suffix, &$var, $params, $wikiTags = array() ) {
$value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
if ( !is_null( $value ) ) {
$var = $value;
}
}
/** */
function extractGlobal( $setting, $wiki, $suffix, $params, $wikiTags = array() ) {
$value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
if ( !is_null( $value ) ) {
$GLOBALS[$setting] = $value;
}
}
/** */
function extractAllGlobals( $wiki, $suffix, $params, $wikiTags = array() ) {
foreach ( $this->settings as $varName => $setting ) {
$this->extractGlobal( $varName, $wiki, $suffix, $params, $wikiTags );
}
}
/**
* Work out the site and language name from a database name
* @param $db
*/
function siteFromDB( $db ) {
$site = NULL;
$lang = NULL;
foreach ( $this->suffixes as $suffix ) {
if ( $suffix === '' ) {
$site = '';
$lang = $db;
break;
} elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
$site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
$lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
break;
}
}
$lang = str_replace( '_', '-', $lang );
return array( $site, $lang );
}
/** */
function isLocalVHost( $vhost ) {
return in_array( $vhost, $this->localVHosts );
}
}
}
|