Index: trunk/phase3/languages/LanguageConverter.php |
— | — | @@ -16,7 +16,6 @@ |
17 | 17 | * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com> |
18 | 18 | */ |
19 | 19 | class LanguageConverter { |
20 | | - var $mPreferredVariant = ''; // The User's preferred variant |
21 | 20 | var $mMainLanguageCode; |
22 | 21 | var $mVariants, $mVariantFallbacks, $mVariantNames; |
23 | 22 | var $mTablesLoaded = false; |
— | — | @@ -30,8 +29,10 @@ |
31 | 30 | var $mFlags; |
32 | 31 | var $mDescCodeSep = ':', $mDescVarSep = ';'; |
33 | 32 | var $mUcfirst = false; |
| 33 | + var $mConvRuleTitle = false; |
| 34 | + var $mURLVariant; |
| 35 | + var $mUserVariant; |
34 | 36 | var $mHeaderVariant; |
35 | | - var $mConvRuleTitle = false; |
36 | 37 | |
37 | 38 | const CACHE_VERSION_KEY = 'VERSION 6'; |
38 | 39 | |
— | — | @@ -137,53 +138,94 @@ |
138 | 139 | * @public |
139 | 140 | */ |
140 | 141 | function getPreferredVariant( $fromUser = true, $fromHeader = false ) { |
141 | | - global $wgUser, $wgRequest, $wgVariantArticlePath, |
142 | | - $wgDefaultLanguageVariant, $wgOut; |
| 142 | + global $wgDefaultLanguageVariant; |
143 | 143 | |
144 | | - // see if the preference is set in the request |
145 | | - $req = $wgRequest->getText( 'variant' ); |
| 144 | + $req = $this->getURLVariant(); |
146 | 145 | |
147 | | - if ( !$req ) { |
148 | | - $req = $wgRequest->getVal( 'uselang' ); |
149 | | - } |
150 | | - |
151 | | - if ( $fromUser && !$req ) { |
| 146 | + if ( $fromUser && !isset( $req ) ) { |
152 | 147 | $req = $this->getUserVariant(); |
153 | 148 | } |
154 | 149 | |
155 | | - if ( $fromHeader && !$req ) { |
| 150 | + if ( $fromHeader && !isset( $req ) ) { |
156 | 151 | $req = $this->getHeaderVariant(); |
157 | 152 | } |
158 | 153 | |
159 | | - if ( $wgDefaultLanguageVariant && !$req ) { |
160 | | - $req = $wgDefaultLanguageVariant; |
| 154 | + if ( $wgDefaultLanguageVariant && !isset( $req ) ) { |
| 155 | + $req = $this->validateVariant( $wgDefaultLanguageVariant ); |
161 | 156 | } |
162 | 157 | |
163 | | - if ( in_array( $req, $this->mVariants ) ) { |
| 158 | + // This function, unlike the other get*Variant functions, is |
| 159 | + // not memoized (i.e. there return value is not cached) since |
| 160 | + // new information might appear during processing after this |
| 161 | + // is first called. |
| 162 | + if ( isset( $req ) ) { |
164 | 163 | return $req; |
165 | 164 | } |
166 | 165 | return $this->mMainLanguageCode; |
167 | 166 | } |
168 | 167 | |
169 | 168 | /** |
170 | | - * Determine the user has a variant set. |
| 169 | + * Validate the variant |
| 170 | + * @param string $v the variant to validate |
| 171 | + * @returns mixed returns the variant if it is valid, null otherwise |
| 172 | + */ |
| 173 | + function validateVariant( $v = null ) { |
| 174 | + if ( isset( $v ) && in_array( $v, $this->mVariants ) ) { |
| 175 | + return $v; |
| 176 | + } |
| 177 | + return null; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Get the variant specified in the URL |
171 | 182 | * |
172 | 183 | * @returns mixed variant if one found, false otherwise. |
173 | 184 | */ |
| 185 | + function getURLVariant() { |
| 186 | + global $wgRequest; |
| 187 | + $ret = null; |
| 188 | + |
| 189 | + if ( $this->mURLVariant ) { |
| 190 | + return $this->mURLVariant; |
| 191 | + } |
| 192 | + |
| 193 | + // see if the preference is set in the request |
| 194 | + $ret = $wgRequest->getText( 'variant' ); |
| 195 | + |
| 196 | + if ( !isset( $ret ) ) { |
| 197 | + $ret = $wgRequest->getVal( 'uselang' ); |
| 198 | + } |
| 199 | + |
| 200 | + return $this->mURLVariant = $this->validateVariant( $ret ); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Determine if the user has a variant set. |
| 205 | + * |
| 206 | + * @returns mixed variant if one found, false otherwise. |
| 207 | + */ |
174 | 208 | function getUserVariant() { |
175 | 209 | global $wgUser; |
| 210 | + $ret = null; |
176 | 211 | |
| 212 | + // memoizing this function wreaks havoc on parserTest.php |
| 213 | + /* if ( $this->mUserVariant ) { */ |
| 214 | + /* return $this->mUserVariant; */ |
| 215 | + /* } */ |
| 216 | + |
177 | 217 | // get language variant preference from logged in users |
178 | 218 | // Don't call this on stub objects because that causes infinite |
179 | 219 | // recursion during initialisation |
180 | 220 | if ( $wgUser->isLoggedIn() ) { |
181 | | - return $wgUser->getOption( 'variant' ); |
| 221 | + $ret = $wgUser->getOption( 'variant' ); |
182 | 222 | } |
183 | 223 | else { |
184 | 224 | // figure out user lang without constructing wgLang to avoid |
185 | 225 | // infinite recursion |
186 | | - return $wgUser->getOption( 'language' ); |
| 226 | + $ret = $wgUser->getOption( 'language' ); |
187 | 227 | } |
| 228 | + |
| 229 | + return $this->mUserVariant = $this->validateVariant( $ret ); |
188 | 230 | } |
189 | 231 | |
190 | 232 | |
— | — | @@ -194,24 +236,22 @@ |
195 | 237 | */ |
196 | 238 | function getHeaderVariant() { |
197 | 239 | global $wgRequest; |
| 240 | + $ret = null; |
198 | 241 | |
199 | 242 | if ( $this->mHeaderVariant ) { |
200 | 243 | return $this->mHeaderVariant; |
201 | 244 | } |
202 | 245 | |
203 | 246 | // see if some supported language variant is set in the |
204 | | - // http header, but we don't set the mPreferredVariant |
205 | | - // variable in case this is called before the user's |
206 | | - // preference is loaded |
| 247 | + // http header. |
207 | 248 | |
208 | 249 | $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' ); |
209 | | - if ( !$acceptLanguage ) { |
210 | | - return false; |
| 250 | + if ( !$acceptLanguage ) { // not using isset because getHeader returns false |
| 251 | + return null; |
211 | 252 | } |
212 | 253 | |
213 | 254 | // explode by comma |
214 | 255 | $result = explode( ',', strtolower( $acceptLanguage ) ); |
215 | | - |
216 | 256 | $languages = array(); |
217 | 257 | |
218 | 258 | foreach ( $result as $elem ) { |
— | — | @@ -228,32 +268,36 @@ |
229 | 269 | foreach ( $languages as $language ) { |
230 | 270 | // strip whitespace |
231 | 271 | $language = trim( $language ); |
232 | | - if ( in_array( $language, $this->mVariants ) ) { |
233 | | - $this->mHeaderVariant = $language; |
234 | | - return $language; |
235 | | - } else { |
236 | | - // To see if there are fallbacks of current language. |
237 | | - // We record these fallback variants, and process |
238 | | - // them later. |
239 | | - $fallbacks = $this->getVariantFallbacks( $language ); |
240 | | - if ( is_string( $fallbacks ) ) { |
241 | | - $fallback_languages[] = $fallbacks; |
242 | | - } elseif ( is_array( $fallbacks ) ) { |
243 | | - $fallback_languages = |
244 | | - array_merge( $fallback_languages, |
245 | | - $fallbacks ); |
246 | | - } |
| 272 | + $this->mHeaderVariant = $this->validateVariant( $language ); |
| 273 | + if ( isset( $this->mHeaderVariant ) ) { |
| 274 | + break; |
247 | 275 | } |
| 276 | + |
| 277 | + // To see if there are fallbacks of current language. |
| 278 | + // We record these fallback variants, and process |
| 279 | + // them later. |
| 280 | + $fallbacks = $this->getVariantFallbacks( $language ); |
| 281 | + if ( is_string( $fallbacks ) ) { |
| 282 | + $fallback_languages[] = $fallbacks; |
| 283 | + } elseif ( is_array( $fallbacks ) ) { |
| 284 | + $fallback_languages = |
| 285 | + array_merge( $fallback_languages, |
| 286 | + $fallbacks ); |
| 287 | + } |
248 | 288 | } |
249 | 289 | |
250 | | - // process fallback languages now |
251 | | - $fallback_languages = array_unique( $fallback_languages ); |
252 | | - foreach ( $fallback_languages as $language ) { |
253 | | - if ( in_array( $language, $this->mVariants ) ) { |
254 | | - $this->mHeaderVariant = $language; |
255 | | - return $language; |
| 290 | + if ( !isset( $this->mHeaderVariant ) ) { |
| 291 | + // process fallback languages now |
| 292 | + $fallback_languages = array_unique( $fallback_languages ); |
| 293 | + foreach ( $fallback_languages as $language ) { |
| 294 | + $this->mHeaderVariant = $this->validateVariant( $language ); |
| 295 | + if ( isset( $this->mHeaderVariant ) ) { |
| 296 | + break; |
| 297 | + } |
256 | 298 | } |
257 | 299 | } |
| 300 | + |
| 301 | + return $this->mHeaderVariant; |
258 | 302 | } |
259 | 303 | |
260 | 304 | /** |
— | — | @@ -295,10 +339,10 @@ |
296 | 340 | |
297 | 341 | if ( !$toVariant ) { |
298 | 342 | $toVariant = $this->getPreferredVariant(); |
| 343 | + if ( !$toVariant ) { |
| 344 | + return $text; |
| 345 | + } |
299 | 346 | } |
300 | | - if ( !in_array( $toVariant, $this->mVariants ) ) { |
301 | | - return $text; |
302 | | - } |
303 | 347 | |
304 | 348 | /* we convert everything except: |
305 | 349 | 1. html markups (anything between < and >) |
— | — | @@ -457,7 +501,7 @@ |
458 | 502 | $convTable = $convRule->getConvTable(); |
459 | 503 | $action = $convRule->getRulesAction(); |
460 | 504 | foreach ( $convTable as $variant => $pair ) { |
461 | | - if ( !in_array( $variant, $this->mVariants ) ) { |
| 505 | + if ( !$this->validateVariant( $variant ) ) { |
462 | 506 | continue; |
463 | 507 | } |
464 | 508 | |
— | — | @@ -851,7 +895,7 @@ |
852 | 896 | $t = explode( '/', $title, 3 ); |
853 | 897 | $c = count( $t ); |
854 | 898 | if ( $c > 1 && $t[0] == 'Conversiontable' ) { |
855 | | - if ( in_array( $t[1], $this->mVariants ) ) { |
| 899 | + if ( $this->validateVariant( $t[1] ) ) { |
856 | 900 | $this->reloadTables(); |
857 | 901 | } |
858 | 902 | } |