r46523 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46522‎ | r46523 | r46524 >
Date:06:51, 29 January 2009
Author:philip
Status:reverted (Comments)
Tags:
Comment:
Move method 'getPreferredVariant' to Language class, patched by Fdcn.
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/classes/LanguageZh.php
@@ -33,7 +33,7 @@
3434 'zh-sg' => '新加坡',
3535 'zh-my' => '大马',
3636 );
37 - $this->mVariantNames = array_merge($this->mVariantNames,$names);
 37+ $this->mLanguageNames = array_merge($this->mLanguageNames,$names);
3838 }
3939
4040 function loadDefaultTables() {
Index: trunk/phase3/languages/LanguageConverter.php
@@ -18,7 +18,7 @@
1919 class LanguageConverter {
2020 var $mPreferredVariant='';
2121 var $mMainLanguageCode;
22 - var $mVariants, $mVariantFallbacks, $mVariantNames;
 22+ var $mVariants, $mVariantFallbacks, $mLanguageNames;
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->mVariantNames = $wgLanguageNames;
 62+ $this->mLanguageNames = $wgLanguageNames;
6363 $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
6464 $m = array(
6565 'begin'=>'-{',
@@ -117,85 +117,9 @@
118118 return $this->mMainLanguageCode;
119119 }
120120
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 - */
 121+ // this method body in Language class
127122 function getPreferredVariant( $fromUser = true ) {
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 -
 123+ return $this->mLangObj->getPreferredVariant( $fromUser );
200124 }
201125
202126 /**
@@ -208,10 +132,10 @@
209133 * @private
210134 */
211135 function captionConvert( $matches ) {
212 - // we convert captions except URL
213136 $toVariant = $this->getPreferredVariant();
214137 $title = $matches[1];
215138 $text = $matches[2];
 139+ // we convert captions except URL
216140 if( !strpos( $text, '://' ) )
217141 $text = $this->translate($text, $toVariant);
218142 return " $title=\"$text\"";
@@ -978,7 +902,7 @@
979903 $unidtable[$v][$from]=$to;
980904 }
981905 // syntax error, pass
982 - if (!array_key_exists($v,$this->mConverter->mVariantNames)){
 906+ if (!array_key_exists($v,$this->mConverter->mLanguageNames)){
983907 $bidtable = array();
984908 $unidtable = array();
985909 break;
@@ -996,10 +920,10 @@
997921 $varsep = $this->mConverter->mDescVarSep;
998922 $text='';
999923 foreach($this->mBidtable as $k => $v)
1000 - $text .= $this->mConverter->mVariantNames[$k]."$codesep$v$varsep";
 924+ $text .= $this->mConverter->mLanguageNames[$k]."$codesep$v$varsep";
1001925 foreach($this->mUnidtable as $k => $a)
1002926 foreach($a as $from=>$to)
1003 - $text.=$from.'⇒'.$this->mConverter->mVariantNames[$k]."$codesep$to$varsep";
 927+ $text.="$from⇒".$this->mConverter->mLanguageNames[$k]."$codesep$to$varsep";
1004928 return $text;
1005929 }
1006930
@@ -1125,7 +1049,7 @@
11261050 $this->mRuleDisplay = $rules;
11271051 } elseif ( in_array('N',$flags) ){
11281052 // proces N flag: output current variant name
1129 - $this->mRuleDisplay = $this->mConverter->mVariantNames[trim($rules)];
 1053+ $this->mRuleDisplay = $this->mConverter->mLanguageNames[trim($rules)];
11301054 } elseif ( in_array('D',$flags) ){
11311055 // proces D flag: output rules description
11321056 $this->mRuleDisplay = $this->getRulesDesc();
Index: trunk/phase3/languages/Language.php
@@ -53,7 +53,7 @@
5454 * @ingroup Language
5555 */
5656 class Language {
57 - var $mConverter, $mVariants, $mCode, $mLoaded = false;
 57+ var $mConverter, $mVariant, $mVariants, $mCode, $mLoaded = false;
5858 var $mMagicExtensions = array(), $mMagicHookDone = false;
5959
6060 static public $mLocalisationKeys = array(
@@ -2210,9 +2210,86 @@
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;
22142223
2215 - function getPreferredVariant( $fromUser = true ) {
2216 - return $this->mConverter->getPreferredVariant( $fromUser );
 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+
22172294 }
22182295
22192296 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r46526Revert r46523, r46525. Spewing errors. See below. Behaviour observed on http:......siebrand08:59, 29 January 2009

Comments

#Comment by Siebrand (talk | contribs)   09:01, 29 January 2009

Reverted in r46526 because of PHP warning and notices.

Status & tagging log