r46526 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46525‎ | r46526 | r46527 >
Date:08:59, 29 January 2009
Author:siebrand
Status:ok
Tags:
Comment:
Revert r46523, r46525. Spewing errors. See below. Behaviour observed on http://translatewiki.net.

PHP Notice: Undefined property: FakeConverter::$mMainLanguageCode in /var/www/w/languages/Language.php on line 2230
PHP Notice: Undefined property: FakeConverter::$mVariants in /var/www/w/languages/Language.php on line 2233
PHP Warning: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument in /var/www/w/languages/Language.php on line 2233
PHP Notice: Undefined property: FakeConverter::$mMainLanguageCode in /var/www/w/languages/Language.php on line 2234
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)
  • /trunk/phase3/languages/classes/LanguageZh.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/LanguageConverter.php
@@ -18,7 +18,7 @@
1919 class LanguageConverter {
2020 var $mPreferredVariant='';
2121 var $mMainLanguageCode;
22 - var $mVariants, $mVariantFallbacks, $mLanguageNames;
 22+ var $mVariants, $mVariantFallbacks, $mVariantNames;
2323 var $mTablesLoaded = false;
2424 var $mTables;
2525 var $mManualAddTables;
@@ -58,7 +58,7 @@
5959 $this->mVariants = $variants;
6060 $this->mVariantFallbacks = $variantfallbacks;
6161 global $wgLanguageNames;
62 - $this->mLanguageNames = $wgLanguageNames;
 62+ $this->mVariantNames = $wgLanguageNames;
6363 $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
6464 $m = array(
6565 'begin'=>'-{',
@@ -117,9 +117,85 @@
118118 return $this->mMainLanguageCode;
119119 }
120120
121 - // this method body in Language class
 121+ /**
 122+ * get preferred language variants.
 123+ * @param boolean $fromUser Get it from $wgUser's preferences
 124+ * @return string the preferred language code
 125+ * @public
 126+ */
122127 function getPreferredVariant( $fromUser = true ) {
123 - return $this->mLangObj->getPreferredVariant( $fromUser );
 128+ global $wgUser, $wgRequest, $wgVariantArticlePath, $wgDefaultLanguageVariant;
 129+
 130+ if($this->mPreferredVariant)
 131+ return $this->mPreferredVariant;
 132+
 133+ // figure out user lang without constructing wgLang to avoid infinite recursion
 134+ if( $fromUser )
 135+ $defaultUserLang = $wgUser->getOption( 'language' );
 136+ else
 137+ $defaultUserLang = $this->mMainLanguageCode;
 138+ $userLang = $wgRequest->getVal( 'uselang', $defaultUserLang );
 139+ // see if interface language is same as content, if not, prevent conversion
 140+ if( ! in_array( $userLang, $this->mVariants ) ){
 141+ $this->mPreferredVariant = $this->mMainLanguageCode; // no conversion
 142+ return $this->mPreferredVariant;
 143+ }
 144+
 145+ // see if the preference is set in the request
 146+ $req = $wgRequest->getText( 'variant' );
 147+ if( in_array( $req, $this->mVariants ) ) {
 148+ $this->mPreferredVariant = $req;
 149+ return $req;
 150+ }
 151+
 152+ // check the syntax /code/ArticleTitle
 153+ if($wgVariantArticlePath!=false && isset($_SERVER['SCRIPT_NAME'])){
 154+ // Note: SCRIPT_NAME probably won't hold the correct value if PHP is run as CGI
 155+ // (it will hold path to php.cgi binary), and might not exist on some very old PHP installations
 156+ $scriptBase = basename( $_SERVER['SCRIPT_NAME'] );
 157+ if(in_array($scriptBase,$this->mVariants)){
 158+ $this->mPreferredVariant = $scriptBase;
 159+ return $this->mPreferredVariant;
 160+ }
 161+ }
 162+
 163+ // get language variant preference from logged in users
 164+ // Don't call this on stub objects because that causes infinite
 165+ // recursion during initialisation
 166+ if( $fromUser && $wgUser->isLoggedIn() ) {
 167+ $this->mPreferredVariant = $wgUser->getOption('variant');
 168+ return $this->mPreferredVariant;
 169+ }
 170+
 171+ // see if default variant is globaly set
 172+ if($wgDefaultLanguageVariant != false && in_array( $wgDefaultLanguageVariant, $this->mVariants )){
 173+ $this->mPreferredVariant = $wgDefaultLanguageVariant;
 174+ return $this->mPreferredVariant;
 175+ }
 176+
 177+ # FIXME rewrite code for parsing http header. The current code
 178+ # is written specific for detecting zh- variants
 179+ if( !$this->mPreferredVariant ) {
 180+ // see if some supported language variant is set in the
 181+ // http header, but we don't set the mPreferredVariant
 182+ // variable in case this is called before the user's
 183+ // preference is loaded
 184+ $pv=$this->mMainLanguageCode;
 185+ if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
 186+ $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
 187+ $zh = strstr($header, $pv.'-');
 188+ if($zh) {
 189+ $ary = split("[,;]",$zh);
 190+ $pv = $ary[0];
 191+ }
 192+ }
 193+ // don't try to return bad variant
 194+ if(in_array( $pv, $this->mVariants ))
 195+ return $pv;
 196+ }
 197+
 198+ return $this->mMainLanguageCode;
 199+
124200 }
125201
126202 /**
@@ -132,10 +208,10 @@
133209 * @private
134210 */
135211 function captionConvert( $matches ) {
 212+ // we convert captions except URL
136213 $toVariant = $this->getPreferredVariant();
137214 $title = $matches[1];
138215 $text = $matches[2];
139 - // we convert captions except URL
140216 if( !strpos( $text, '://' ) )
141217 $text = $this->translate($text, $toVariant);
142218 return " $title=\"$text\"";
@@ -296,19 +372,12 @@
297373 if( !in_array($v,$this->mVariants) )continue;
298374 if( $action=="add" ) {
299375 foreach($t as $from=>$to) {
300 - // to ensure that $from and $to not be left blank
301 - // so $this->translate() could always return a string
302 - if ($from || $to)
303 - // more efficient than array_merge(), about 2.5 times.
304 - $this->mManualAddTables[$v][$from] = $to;
 376+ // more efficient than array_merge(), about 2.5 times.
 377+ $this->mManualAddTables[$v][$from] = $to;
305378 }
306379 }
307 - elseif ( $action=="remove" ) {
308 - foreach($t as $from=>$to) {
309 - if ($from || $to)
310 - $this->mManualRemoveTables[$v][$from] = $to;
311 - }
312 - }
 380+ elseif ( $action=="remove" )
 381+ $this->mManualRemoveTables[$v] = array_merge($this->mManualRemoveTables[$v], $t);
313382 }
314383 }
315384
@@ -909,7 +978,7 @@
910979 $unidtable[$v][$from]=$to;
911980 }
912981 // syntax error, pass
913 - if (!array_key_exists($v,$this->mConverter->mLanguageNames)){
 982+ if (!array_key_exists($v,$this->mConverter->mVariantNames)){
914983 $bidtable = array();
915984 $unidtable = array();
916985 break;
@@ -927,10 +996,10 @@
928997 $varsep = $this->mConverter->mDescVarSep;
929998 $text='';
930999 foreach($this->mBidtable as $k => $v)
931 - $text .= $this->mConverter->mLanguageNames[$k]."$codesep$v$varsep";
 1000+ $text .= $this->mConverter->mVariantNames[$k]."$codesep$v$varsep";
9321001 foreach($this->mUnidtable as $k => $a)
9331002 foreach($a as $from=>$to)
934 - $text.="$from&#8658;".$this->mConverter->mLanguageNames[$k]."$codesep$to$varsep";
 1003+ $text.=$from.'⇒'.$this->mConverter->mVariantNames[$k]."$codesep$to$varsep";
9351004 return $text;
9361005 }
9371006
@@ -1056,7 +1125,7 @@
10571126 $this->mRuleDisplay = $rules;
10581127 } elseif ( in_array('N',$flags) ){
10591128 // proces N flag: output current variant name
1060 - $this->mRuleDisplay = $this->mConverter->mLanguageNames[trim($rules)];
 1129+ $this->mRuleDisplay = $this->mConverter->mVariantNames[trim($rules)];
10611130 } elseif ( in_array('D',$flags) ){
10621131 // proces D flag: output rules description
10631132 $this->mRuleDisplay = $this->getRulesDesc();
Index: trunk/phase3/languages/Language.php
@@ -53,7 +53,7 @@
5454 * @ingroup Language
5555 */
5656 class Language {
57 - var $mConverter, $mVariant, $mVariants, $mCode, $mLoaded = false;
 57+ var $mConverter, $mVariants, $mCode, $mLoaded = false;
5858 var $mMagicExtensions = array(), $mMagicHookDone = false;
5959
6060 static public $mLocalisationKeys = array(
@@ -2210,86 +2210,9 @@
22112211 return $this->mConverter->getVariants();
22122212 }
22132213
2214 - /**
2215 - * get preferred language variants.
2216 - * moved from LanguageConverter class
2217 - * @param boolean $fromUser Get it from $wgUser's preferences
2218 - * @return string the preferred language code
2219 - * @public
2220 - */
2221 - function getPreferredVariant( $fromUser = true ) {
2222 - global $wgUser, $wgRequest, $wgVariantArticlePath, $wgDefaultLanguageVariant;
22232214
2224 - if($this->mVariant)
2225 - return $this->mVariant;
2226 -
2227 - // figure out user lang without constructing wgLang to avoid infinite recursion
2228 - if( $fromUser )
2229 - $defaultUserLang = $wgUser->getOption( 'language' );
2230 - else
2231 - $defaultUserLang = $this->mConverter->mMainLanguageCode;
2232 - $userLang = $wgRequest->getVal( 'uselang', $defaultUserLang );
2233 - // see if interface language is same as content, if not, prevent conversion
2234 - if( ! in_array( $userLang, $this->mConverter->mVariants ) ){
2235 - $this->mVariant = $this->mConverter->mMainLanguageCode; // no conversion
2236 - return $this->mVariant;
2237 - }
2238 -
2239 - // see if the preference is set in the request
2240 - $req = $wgRequest->getText( 'variant' );
2241 - if( in_array( $req, $this->mConverter->mVariants ) ) {
2242 - $this->mVariant = $req;
2243 - return $req;
2244 - }
2245 -
2246 - // check the syntax /code/ArticleTitle
2247 - if($wgVariantArticlePath!=false && isset($_SERVER['SCRIPT_NAME'])){
2248 - // Note: SCRIPT_NAME probably won't hold the correct value if PHP is run as CGI
2249 - // (it will hold path to php.cgi binary), and might not exist on some very old PHP installations
2250 - $scriptBase = basename( $_SERVER['SCRIPT_NAME'] );
2251 - if(in_array($scriptBase,$this->mConverter->mVariants)){
2252 - $this->mVariant = $scriptBase;
2253 - return $this->mPreferredVariant;
2254 - }
2255 - }
2256 -
2257 - // get language variant preference from logged in users
2258 - // Don't call this on stub objects because that causes infinite
2259 - // recursion during initialisation
2260 - if( $fromUser && $wgUser->isLoggedIn() ) {
2261 - $this->mVariant = $wgUser->getOption('variant');
2262 - return $this->mVariant;
2263 - }
2264 -
2265 - // see if default variant is globaly set
2266 - if($wgDefaultLanguageVariant != false && in_array( $wgDefaultLanguageVariant, $this->mVariants )){
2267 - $this->mVariant = $wgDefaultLanguageVariant;
2268 - return $this->mVariant;
2269 - }
2270 -
2271 - # FIXME rewrite code for parsing http header. The current code
2272 - # is written specific for detecting zh- variants
2273 - if( !$this->mVariant ) {
2274 - // see if some supported language variant is set in the
2275 - // http header, but we don't set the mPreferredVariant
2276 - // variable in case this is called before the user's
2277 - // preference is loaded
2278 - $pv=$this->mConverter->mMainLanguageCode;
2279 - if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
2280 - $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
2281 - $zh = strstr($header, $pv.'-');
2282 - if($zh) {
2283 - $ary = split("[,;]",$zh);
2284 - $pv = $ary[0];
2285 - }
2286 - }
2287 - // don't try to return bad variant
2288 - if(in_array( $pv, $this->mConverter->mVariants ))
2289 - return $pv;
2290 - }
2291 -
2292 - return $this->mConverter->mMainLanguageCode;
2293 -
 2215+ function getPreferredVariant( $fromUser = true ) {
 2216+ return $this->mConverter->getPreferredVariant( $fromUser );
22942217 }
22952218
22962219 /**
Index: trunk/phase3/languages/classes/LanguageZh.php
@@ -33,7 +33,7 @@
3434 'zh-sg' => '新加坡',
3535 'zh-my' => '大马',
3636 );
37 - $this->mLanguageNames = array_merge($this->mLanguageNames,$names);
 37+ $this->mVariantNames = array_merge($this->mVariantNames,$names);
3838 }
3939
4040 function loadDefaultTables() {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r46523Move method 'getPreferredVariant' to Language class, patched by Fdcn.philip06:51, 29 January 2009
r46525mTables' element must not be left blank, to ensure the translate() function c...philip07:43, 29 January 2009

Status & tagging log