r60923 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60922‎ | r60923 | r60924 >
Date:05:53, 11 January 2010
Author:mah
Status:ok
Tags:
Comment:
cleaned up getPreferredVariant() (MUCH easier to follow the logic), found a bug or two
Modified paths:
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)
  • /trunk/phase3/tests/LanguageConverterTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/LanguageConverterTest.php
@@ -28,7 +28,7 @@
2929
3030 $wgRequest = new FauxRequest(array());
3131 $wgUser = new User;
32 - $wgContLang = Language::factory( 'tg-latn' );
 32+ $wgContLang = Language::factory( 'tg' );
3333
3434 $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false));
3535 $this->assertEquals('tg', $this->lc->getPreferredVariant(false, true));
@@ -41,7 +41,7 @@
4242 $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false));
4343 $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(false, true));
4444 $this->assertEquals('tg', $this->lc->getPreferredVariant(true, false));
45 - $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true));
 45+ $this->assertEquals('tg', $this->lc->getPreferredVariant(true, true));
4646
4747 $wgRequest->setHeader('Accept-Language', 'tg;q=1');
4848 $this->lc = new TestConverter( $this->lang, 'tg',
@@ -57,7 +57,7 @@
5858 $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false));
5959 $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(false, true));
6060 $this->assertEquals('tg', $this->lc->getPreferredVariant(true, false));
61 - $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true));
 61+ $this->assertEquals('tg', $this->lc->getPreferredVariant(true, true));
6262
6363 $wgRequest->setHeader('Accept-Language', 'en, tg-latn;q=1');
6464 $this->lc = new TestConverter( $this->lang, 'tg',
@@ -65,7 +65,7 @@
6666 $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false));
6767 $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(false, true));
6868 $this->assertEquals('tg', $this->lc->getPreferredVariant(true, false));
69 - $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true));
 69+ $this->assertEquals('tg', $this->lc->getPreferredVariant(true, true));
7070 $wgRequest->setHeader('Accept-Language', '');
7171
7272 $wgUser = User::newFromId("admin");
@@ -95,7 +95,16 @@
9696 $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, false));
9797 $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true));
9898
 99+ $wgRequest->setVal('variant', null);
 100+ $wgDefaultLanguageVariant = 'tg';
 101+ $this->lc = new TestConverter( $this->lang, 'tg',
 102+ array( 'tg', 'tg-latn' ) );
 103+ $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false));
 104+ $this->assertEquals('tg', $this->lc->getPreferredVariant(false, true));
 105+ $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, false));
 106+ $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true));
99107
 108+
100109 }
101110 }
102111
Index: trunk/phase3/languages/LanguageConverter.php
@@ -142,62 +142,52 @@
143143
144144 // see if the preference is set in the request
145145 $req = $wgRequest->getText( 'variant' );
146 - if ( in_array( $req, $this->mVariants ) ) {
147 - $this->mPreferredVariant = $req;
148 - return $this->mPreferredVariant;
149 - }
150146
151 - if ( $fromUser ) {
152 - // bug 21974, don't return $this->mPreferredVariant if
153 - // $fromUser = false
154 - if ( $this->mPreferredVariant ) {
155 - return $this->mPreferredVariant;
156 - }
157 -
158 - // figure out user lang without constructing wgLang to avoid
159 - // infinite recursion
160 - $defaultUserLang = $wgUser->getOption( 'language' );
161 -
162 - // get language variant preference from logged in users
163 - // Don't call this on stub objects because that causes infinite
164 - // recursion during initialisation
165 - if ( $wgUser->isLoggedIn() ) {
166 - $this->mPreferredVariant = $wgUser->getOption( 'variant' );
167 - }
168 -
169 - } else {
170 - $defaultUserLang = $this->mMainLanguageCode;
 147+ if ( !$req ) {
 148+ $req = $wgRequest->getVal( 'uselang' );
171149 }
172 - $userLang = $wgRequest->getVal( 'uselang', $defaultUserLang );
173150
174 - // see if interface language is same as content, if not, prevent
175 - // conversion
176 - if ( ! in_array( $userLang, $this->mVariants ) ) {
177 - // no conversion
178 - $this->mPreferredVariant = $this->mMainLanguageCode;
179 - return $this->mPreferredVariant;
180 - } elseif ( $this->mPreferredVariant ) {
181 - // if the variant was set above and it iss a variant of
182 - // the content language
183 - return $this->mPreferredVariant;
 151+ if ( $fromUser && !$req ) {
 152+ $req = $this->getUserVariant();
184153 }
185154
186 - // see if default variant is globaly set
187 - if ( $wgDefaultLanguageVariant != false
188 - && in_array( $wgDefaultLanguageVariant, $this->mVariants ) ) {
189 - $this->mPreferredVariant = $wgDefaultLanguageVariant;
190 - return $this->mPreferredVariant;
 155+ if ( $fromHeader && !$req ) {
 156+ $req = $this->getHeaderVariant();
191157 }
192158
193 - $headerVariant = $this->getHeaderVariant();
194 - if ( $fromHeader && $headerVariant ) {
195 - return $headerVariant;
 159+ if ( $wgDefaultLanguageVariant && !$req ) {
 160+ $req = $wgDefaultLanguageVariant;
196161 }
197162
 163+ if ( in_array( $req, $this->mVariants ) ) {
 164+ return $req;
 165+ }
198166 return $this->mMainLanguageCode;
199167 }
200168
201169 /**
 170+ * Determine the user has a variant set.
 171+ *
 172+ * @returns mixed variant if one found, false otherwise.
 173+ */
 174+ function getUserVariant() {
 175+ global $wgUser;
 176+
 177+ // get language variant preference from logged in users
 178+ // Don't call this on stub objects because that causes infinite
 179+ // recursion during initialisation
 180+ if ( $wgUser->isLoggedIn() ) {
 181+ return $wgUser->getOption( 'variant' );
 182+ }
 183+ else {
 184+ // figure out user lang without constructing wgLang to avoid
 185+ // infinite recursion
 186+ return $wgUser->getOption( 'language' );
 187+ }
 188+ }
 189+
 190+
 191+ /**
202192 * Determine the language variant from the Accept-Language header.
203193 *
204194 * @returns mixed variant if one found, false otherwise.
@@ -550,6 +540,7 @@
551541 if ( $wgDisableLangConversion ) return $text;
552542
553543 $plang = $this->getPreferredVariant();
 544+
554545 $tarray = StringUtils::explode( $this->mMarkup['end'], $text );
555546 $converted = '';
556547

Status & tagging log