r83540 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83539‎ | r83540 | r83541 >
Date:19:53, 8 March 2011
Author:ialex
Status:deferred
Tags:
Comment:
* Use the new UserGetLanguageObject hook to set the language code of $wgLang
* Use the ResourceLoader to load the javascript file
Modified paths:
  • /trunk/extensions/LanguageSelector/LanguageSelector.js (modified) (history)
  • /trunk/extensions/LanguageSelector/LanguageSelector.php (modified) (history)
  • /trunk/extensions/LanguageSelector/StubAutoLang.php (deleted) (history)

Diff [purge]

Index: trunk/extensions/LanguageSelector/StubAutoLang.php
@@ -1,32 +0,0 @@
2 -<?php
3 -
4 -class StubAutoLang extends StubObject {
5 - protected $requestedLanguage;
6 -
7 - function __construct() {
8 - parent::__construct( 'wgLang' );
9 - }
10 -
11 - function __call( $name, $args ) {
12 - return $this->_call( $name, $args );
13 - }
14 -
15 - function setRequestedLanguage( $code ) {
16 - $this->requestedLanguage = $code;
17 - }
18 -
19 - //partially copied from StubObject.php. There should be a better way...
20 - function _newObject() {
21 - global $wgContLanguageCode, $wgContLang, $wgLanguageSelectorDetectLanguage;
22 -
23 - $code = $this->requestedLanguage;
24 - if (!$code) $code = wfLanguageSelectorDetectLanguage( $wgLanguageSelectorDetectLanguage );
25 -
26 - if( $code == $wgContLanguageCode ) {
27 - return $wgContLang;
28 - } else {
29 - $obj = Language::factory( $code );
30 - return $obj;
31 - }
32 - }
33 -}
\ No newline at end of file
Index: trunk/extensions/LanguageSelector/LanguageSelector.js
@@ -1,4 +1,4 @@
2 -addOnloadHook(function() {
 2+$( function() {
33 var i = 1;
44 while ( true ) {
55 var btn = document.getElementById("languageselector-commit-"+i);
@@ -21,4 +21,4 @@
2222
2323 i++;
2424 }
25 -});
\ No newline at end of file
 25+});
Index: trunk/extensions/LanguageSelector/LanguageSelector.php
@@ -74,14 +74,19 @@
7575 $wgHooks['BeforePageDisplay'][] = 'wfLanguageSelectorBeforePageDisplay';
7676 $wgHooks['GetCacheVaryCookies'][] = 'wfLanguageSelectorGetCacheVaryCookies';
7777 $wgHooks['ParserFirstCallInit'][] = 'wfLanguageSelectorSetHook';
 78+$wgHooks['UserGetLanguageObject'][] = 'wfLanguageSelectorGetLanguageCode';
7879
7980 $wgExtensionFunctions[] = 'wfLanguageSelectorExtension';
8081
8182 $wgParserOutputHooks['languageselector'] = 'wfLanguageSelectorAddJavascript';
8283
83 -$wgLanguageSelectorRequestedLanguage = null;
84 -$wgLanguageSelectorFormUsed = false;
 84+# Ressource loader
 85+$wgResourceModules['ext.languageSelector'] = array(
 86+ 'scripts' => 'LanguageSelector.js',
8587
 88+ 'localBasePath' => dirname( __FILE__ ),
 89+ 'remoteExtPath' => 'LanguageSelector'
 90+);
8691
8792 $dir = dirname(__FILE__) . '/';
8893 $wgExtensionMessagesFiles['LanguageSelector'] = $dir . 'LanguageSelector.i18n.php';
@@ -94,74 +99,95 @@
95100 }
96101
97102 function wfLanguageSelectorExtension() {
98 - global $wgLanguageSelectorLanguages, $wgLanguageSelectorDetectLanguage,
99 - $wgLanguageSelectorRequestedLanguage, $wgLanguageSelectorLocation,
100 - $wgLanguageSelectorShowAll, $wgCommandLineMode;
101 - global $wgUser, $wgLang, $wgRequest, $wgCookiePrefix, $wgCookiePath, $wgHooks;
 103+ global $wgLanguageSelectorLanguages, $wgLanguageSelectorLocation,
 104+ $wgLanguageSelectorShowAll, $wgHooks;
102105
103106 if ( $wgLanguageSelectorLanguages === null ) {
104107 $wgLanguageSelectorLanguages = array_keys( Language::getLanguageNames( !$wgLanguageSelectorShowAll ) );
105108 sort( $wgLanguageSelectorLanguages );
106109 }
107110
 111+ if ( $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_MANUAL && $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_AT_TOP_OF_TEXT ) {
 112+ switch ( $wgLanguageSelectorLocation ) {
 113+ case LANGUAGE_SELECTOR_IN_TOOLBOX:
 114+ $wgHooks['SkinTemplateToolboxEnd'][] = 'wfLanguageSelectorSkinHook';
 115+ break;
 116+ default:
 117+ $wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'wfLanguageSelectorSkinTemplateOutputPageBeforeExec';
 118+ break;
 119+ }
 120+ }
 121+}
 122+
 123+function wfLanguageSelectorGetLanguageCode( $user, &$code ) {
 124+ global $wgLanguageSelectorLanguages, $wgLanguageSelectorDetectLanguage,
 125+ $wgCommandLineMode, $wgRequest, $wgContLang;
 126+
108127 if ( $wgCommandLineMode ) {
109128 return true;
110129 }
111130
112131 $setlang = $wgRequest->getVal( 'setlang' );
113 - if ( $setlang && !in_array( $setlang, $wgLanguageSelectorLanguages ) )
114 - $setlang = null; //ignore invalid
 132+ if ( $setlang && !in_array( $setlang, $wgLanguageSelectorLanguages ) ) {
 133+ $setlang = null; // ignore invalid
 134+ }
115135
116136 if ( $setlang ) {
117 - setcookie( $wgCookiePrefix . 'LanguageSelectorLanguage', $setlang, 0, $wgCookiePath );
118 - $wgLanguageSelectorRequestedLanguage = $setlang;
 137+ $wgRequest->response()->setcookie( 'LanguageSelectorLanguage', $setlang );
 138+ $requestedLanguage = $setlang;
119139 } else {
120 - $wgLanguageSelectorRequestedLanguage = @$_COOKIE[$wgCookiePrefix.'LanguageSelectorLanguage'];
 140+ $requestedLanguage = $wgRequest->getCookie( 'LanguageSelectorLanguage' );
121141 }
122142
123 - if ( $setlang && !$wgUser->isAnon() ) {
124 - if ( $setlang != $wgUser->getOption( 'language' ) ) {
125 - $wgUser->setOption( 'language', $wgLanguageSelectorRequestedLanguage );
126 - $wgUser->saveSettings();
127 - // Reset $wgLang so that user's language is immediately changed
128 - $wgLang = new StubUserLang;
 143+ if ( $setlang && !$user->isAnon() ) {
 144+ if ( $setlang != $user->getOption( 'language' ) ) {
 145+ $user->setOption( 'language', $requestedLanguage );
 146+ $user->saveSettings();
 147+ $code = $requestedLanguage;
129148 }
130149 }
131150
132 - if ( !$wgRequest->getVal( 'uselang' ) && $wgUser->isAnon() ) {
133 - if ( $wgLanguageSelectorRequestedLanguage || $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_USE_CONTENT_LANG ) {
134 - $wgLang = new StubAutoLang;
135 - $wgLang->setRequestedLanguage( $wgLanguageSelectorRequestedLanguage );
136 - }
137 - }
 151+ if ( !$wgRequest->getVal( 'uselang' ) && $user->isAnon() ) {
 152+ if ( $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_USE_CONTENT_LANG ) {
 153+ if ( $requestedLanguage ) {
 154+ $code = $requestedLanguage;
 155+ } else {
 156+ $languages = $wgRequest->getAcceptLang();
138157
139 - if ( $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_MANUAL && $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_AT_TOP_OF_TEXT ) {
140 - switch ( $wgLanguageSelectorLocation ) {
141 - case LANGUAGE_SELECTOR_IN_TOOLBOX:
142 - $wgHooks['SkinTemplateToolboxEnd'][] = 'wfLanguageSelectorSkinHook';
143 - break;
144 - default:
145 - $wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'wfLanguageSelectorSkinTemplateOutputPageBeforeExec';
146 - break;
 158+ // see if the content language is accepted by the client.
 159+ if ( $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_PREFER_CONTENT_LANG
 160+ || !array_key_exists( $wgContLang->getCode(), $languages ) )
 161+ {
 162+
 163+ // look for a language that is acceptable to the client
 164+ // and known to the wiki.
 165+ foreach( $languages as $reqCode => $q ) {
 166+ if ( in_array( $reqCode, $wgLanguageSelectorLanguages ) ) {
 167+ $code = $reqCode;
 168+ break;
 169+ }
 170+ }
 171+ }
 172+ }
147173 }
148174 }
149175
 176+ return true;
150177 }
151178
152179 function wfLanguageSelectorBeforePageDisplay( &$out ) {
153 - global $wgExtensionAssetsPath, $wgLanguageSelectorLocation, $wgLanguageSelectorFormUsed;
 180+ global $wgLanguageSelectorLocation;
154181
 182+ if ( $wgLanguageSelectorLocation == LANGUAGE_SELECTOR_MANUAL ) {
 183+ return true;
 184+ }
 185+
155186 if ( $wgLanguageSelectorLocation == LANGUAGE_SELECTOR_AT_TOP_OF_TEXT ) {
156187 $html = wfLanguageSelectorHTML( $out->getTitle() );
157188 $out->mBodytext = $html . $out->mBodytext;
158189 }
159190
160 - if ( $wgLanguageSelectorFormUsed ||
161 - $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_MANUAL )
162 - {
163 - $out->addScriptFile( $wgExtensionAssetsPath .
164 - '/LanguageSelector/LanguageSelector.js' );
165 - }
 191+ $out->addModules( 'ext.languageSelector' );
166192
167193 return true;
168194 }
@@ -254,76 +280,6 @@
255281 return true;
256282 }
257283
258 -function wfLanguageSelectorDetectLanguage( $mode ) {
259 - global $wgContLang, $wgLanguageSelectorLanguages;
260 -
261 - $contLang = $wgContLang->getCode();
262 -
263 - if ( !$mode || $mode == LANGUAGE_SELECTOR_USE_CONTENT_LANG ) {
264 - return $contLang;
265 - }
266 -
267 - /**
268 - * get accepted languages from Accept-Languages
269 - * HTTP header.
270 - */
271 - $accept = @$_SERVER["HTTP_ACCEPT_LANGUAGE"];
272 -
273 - if ( empty( $accept ) )
274 - return $contLang;
275 -
276 - $accept = explode( ',', $accept );
277 -
278 - /**
279 - * normalize accepted languages
280 - */
281 - $languages = array();
282 - foreach ( $accept as $lan ) {
283 - @list( $value, $qpart ) = explode( ';', trim( $lan ) );
284 - $match = array();
285 - if( !isset( $qpart ) ) {
286 - $languages[$value] = 1.0;
287 - } elseif( preg_match( '/q\s*=\s*(\d*\.\d+)/', $qpart, $match ) ) {
288 - $languages[$value] = floatval( $match[1] );
289 - }
290 - }
291 -
292 - /**
293 - * see if the content language is accepted by the
294 - * client.
295 - */
296 - if ( $mode == LANGUAGE_SELECTOR_PREFER_CONTENT_LANG && array_key_exists( $contLang, $languages ) ) {
297 - return $contLang;
298 - }
299 -
300 - arsort( $languages, SORT_NUMERIC );
301 -
302 - /**
303 - * look for a language that is acceptable to the client
304 - * and known to the wiki.
305 - */
306 - foreach( $languages as $code => $q ) {
307 - /**
308 - * TODO: only accept languages for which an implementation exists.
309 - * this is disabled, because it's slow. Note that this code is
310 - * executed for every page request!
311 - */
312 - /*
313 - global $IP;
314 - $langfile="$IP/languages/Language".str_replace('-', '_', ucfirst($code)).".php";
315 - if(!file_exists($langfile)) {
316 - continue;
317 - }
318 - */
319 -
320 - if ( in_array( $code, $wgLanguageSelectorLanguages ) ) {
321 - return $code;
322 - }
323 - }
324 -
325 - return $contLang;
326 -}
327 -
328284 function wfLanguageSelectorAddNewAccount( $u ) {
329285 global $wgUser, $wgLang;
330286
@@ -339,19 +295,16 @@
340296 }
341297
342298 function wfLanguageSelectorAddJavascript( $outputPage, $parserOutput, $data ) {
343 - global $wgLanguageSelectorFormUsed;
344 -
345 - $wgLanguageSelectorFormUsed = true;
 299+ $outputPage->addModules( 'ext.languageSelector' );
346300 }
347301
348302 function wfLanguageSelectorHTML( Title $title, $style = null, $class = null, $selectorstyle = null, $buttonstyle = null, $showCode = null ) {
349303 global $wgLanguageSelectorLanguages, $wgLang, $wgContLang, $wgScript,
350 - $wgLanguageSelectorShowCode, $wgLanguageSelectorFormUsed;
 304+ $wgLanguageSelectorShowCode;
351305
352306 if ( $showCode === null ) {
353307 $showCode = $wgLanguageSelectorShowCode;
354308 }
355 - $wgLanguageSelectorFormUsed = true;
356309
357310 static $id = 0;
358311 $id += 1;

Status & tagging log