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