r60961 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60960‎ | r60961 | r60962 >
Date:05:31, 12 January 2010
Author:mah
Status:ok (Comments)
Tags:
Comment:
Further logic cleanup on getPreferredVariant & memoization of functions where possible.
Modified paths:
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/LanguageConverter.php
@@ -16,7 +16,6 @@
1717 * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com>
1818 */
1919 class LanguageConverter {
20 - var $mPreferredVariant = ''; // The User's preferred variant
2120 var $mMainLanguageCode;
2221 var $mVariants, $mVariantFallbacks, $mVariantNames;
2322 var $mTablesLoaded = false;
@@ -30,8 +29,10 @@
3130 var $mFlags;
3231 var $mDescCodeSep = ':', $mDescVarSep = ';';
3332 var $mUcfirst = false;
 33+ var $mConvRuleTitle = false;
 34+ var $mURLVariant;
 35+ var $mUserVariant;
3436 var $mHeaderVariant;
35 - var $mConvRuleTitle = false;
3637
3738 const CACHE_VERSION_KEY = 'VERSION 6';
3839
@@ -137,53 +138,94 @@
138139 * @public
139140 */
140141 function getPreferredVariant( $fromUser = true, $fromHeader = false ) {
141 - global $wgUser, $wgRequest, $wgVariantArticlePath,
142 - $wgDefaultLanguageVariant, $wgOut;
 142+ global $wgDefaultLanguageVariant;
143143
144 - // see if the preference is set in the request
145 - $req = $wgRequest->getText( 'variant' );
 144+ $req = $this->getURLVariant();
146145
147 - if ( !$req ) {
148 - $req = $wgRequest->getVal( 'uselang' );
149 - }
150 -
151 - if ( $fromUser && !$req ) {
 146+ if ( $fromUser && !isset( $req ) ) {
152147 $req = $this->getUserVariant();
153148 }
154149
155 - if ( $fromHeader && !$req ) {
 150+ if ( $fromHeader && !isset( $req ) ) {
156151 $req = $this->getHeaderVariant();
157152 }
158153
159 - if ( $wgDefaultLanguageVariant && !$req ) {
160 - $req = $wgDefaultLanguageVariant;
 154+ if ( $wgDefaultLanguageVariant && !isset( $req ) ) {
 155+ $req = $this->validateVariant( $wgDefaultLanguageVariant );
161156 }
162157
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 ) ) {
164163 return $req;
165164 }
166165 return $this->mMainLanguageCode;
167166 }
168167
169168 /**
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
171182 *
172183 * @returns mixed variant if one found, false otherwise.
173184 */
 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+ */
174208 function getUserVariant() {
175209 global $wgUser;
 210+ $ret = null;
176211
 212+ // memoizing this function wreaks havoc on parserTest.php
 213+ /* if ( $this->mUserVariant ) { */
 214+ /* return $this->mUserVariant; */
 215+ /* } */
 216+
177217 // get language variant preference from logged in users
178218 // Don't call this on stub objects because that causes infinite
179219 // recursion during initialisation
180220 if ( $wgUser->isLoggedIn() ) {
181 - return $wgUser->getOption( 'variant' );
 221+ $ret = $wgUser->getOption( 'variant' );
182222 }
183223 else {
184224 // figure out user lang without constructing wgLang to avoid
185225 // infinite recursion
186 - return $wgUser->getOption( 'language' );
 226+ $ret = $wgUser->getOption( 'language' );
187227 }
 228+
 229+ return $this->mUserVariant = $this->validateVariant( $ret );
188230 }
189231
190232
@@ -194,24 +236,22 @@
195237 */
196238 function getHeaderVariant() {
197239 global $wgRequest;
 240+ $ret = null;
198241
199242 if ( $this->mHeaderVariant ) {
200243 return $this->mHeaderVariant;
201244 }
202245
203246 // 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.
207248
208249 $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' );
209 - if ( !$acceptLanguage ) {
210 - return false;
 250+ if ( !$acceptLanguage ) { // not using isset because getHeader returns false
 251+ return null;
211252 }
212253
213254 // explode by comma
214255 $result = explode( ',', strtolower( $acceptLanguage ) );
215 -
216256 $languages = array();
217257
218258 foreach ( $result as $elem ) {
@@ -228,32 +268,36 @@
229269 foreach ( $languages as $language ) {
230270 // strip whitespace
231271 $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;
247275 }
 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+ }
248288 }
249289
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+ }
256298 }
257299 }
 300+
 301+ return $this->mHeaderVariant;
258302 }
259303
260304 /**
@@ -295,10 +339,10 @@
296340
297341 if ( !$toVariant ) {
298342 $toVariant = $this->getPreferredVariant();
 343+ if ( !$toVariant ) {
 344+ return $text;
 345+ }
299346 }
300 - if ( !in_array( $toVariant, $this->mVariants ) ) {
301 - return $text;
302 - }
303347
304348 /* we convert everything except:
305349 1. html markups (anything between < and >)
@@ -457,7 +501,7 @@
458502 $convTable = $convRule->getConvTable();
459503 $action = $convRule->getRulesAction();
460504 foreach ( $convTable as $variant => $pair ) {
461 - if ( !in_array( $variant, $this->mVariants ) ) {
 505+ if ( !$this->validateVariant( $variant ) ) {
462506 continue;
463507 }
464508
@@ -851,7 +895,7 @@
852896 $t = explode( '/', $title, 3 );
853897 $c = count( $t );
854898 if ( $c > 1 && $t[0] == 'Conversiontable' ) {
855 - if ( in_array( $t[1], $this->mVariants ) ) {
 899+ if ( $this->validateVariant( $t[1] ) ) {
856900 $this->reloadTables();
857901 }
858902 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r60974follow up r60961 just test for truth rather than using isset. Found a bug in...mah18:35, 12 January 2010

Comments

#Comment by Nikerabbit (talk | contribs)   07:42, 12 January 2010

I would just use comparison === to null, unless your intention is to suppress also all spelling errors in variable names. Isset has caused few hard to find bugs for me.

Status & tagging log