Do not use setlang to change user language
Requests using GET should only retrieve data and should have no other effect. (https://en.wikipedia.org/wiki/HTTP_GET_request) * Use API with POST to change the user interface language in user settings. * When allowed by wgULSAnonCanChangeLanguage set cookie for anonymous user or when API fails. This change uses mediawiki.cookie which requires MediaWiki 1.24+. Bug: T46649 Change-Id: Iaf6fafbf200933dfc760be69d2adf5e5efcf8245
This commit is contained in:
committed by
[[mw:User:Fomafix]]
parent
c6d5599ad8
commit
0d5c69f655
@@ -70,7 +70,8 @@ $wgResourceModules['ext.uls.init'] = array(
|
|||||||
'monobook' => 'resources/css/ext.uls-monobook.css',
|
'monobook' => 'resources/css/ext.uls-monobook.css',
|
||||||
),
|
),
|
||||||
'dependencies' => array(
|
'dependencies' => array(
|
||||||
'mediawiki.Uri',
|
'mediawiki.api',
|
||||||
|
'mediawiki.cookie',
|
||||||
'jquery.client',
|
'jquery.client',
|
||||||
'jquery.cookie',
|
'jquery.cookie',
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -60,19 +60,42 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change the language of wiki using setlang URL parameter
|
* Change the language of wiki using API or set cookie and reload the page
|
||||||
* @param {string} language Language code.
|
* @param {string} language Language code.
|
||||||
*/
|
*/
|
||||||
mw.uls.changeLanguage = function ( language ) {
|
mw.uls.changeLanguage = function ( language ) {
|
||||||
var uri = new mw.Uri( window.location.href ),
|
var deferred = new $.Deferred();
|
||||||
deferred = new $.Deferred();
|
|
||||||
|
function changeLanguageAnon() {
|
||||||
|
if ( mw.config.get( 'wgULSAnonCanChangeLanguage' ) ) {
|
||||||
|
mw.cookie.set( 'language', language );
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
deferred.done( function () {
|
deferred.done( function () {
|
||||||
uri.extend( {
|
var api;
|
||||||
setlang: language
|
|
||||||
} );
|
|
||||||
|
|
||||||
window.location.href = uri.toString();
|
if ( mw.user.isAnon() ) {
|
||||||
|
changeLanguageAnon();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
api = new mw.Api();
|
||||||
|
// @todo Change this to api.saveOption when ULS minimum MW version is 1.25
|
||||||
|
api.postWithToken( 'options', {
|
||||||
|
action: 'options',
|
||||||
|
optionname: 'language',
|
||||||
|
optionvalue: language
|
||||||
|
} )
|
||||||
|
.done( function () {
|
||||||
|
location.reload();
|
||||||
|
} )
|
||||||
|
.fail( function () {
|
||||||
|
// Set options failed. Maybe the user has logged off.
|
||||||
|
// Continue like anonymous user and set cookie.
|
||||||
|
changeLanguageAnon();
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
mw.hook( 'mw.uls.interface.language.change' ).fire( language, deferred );
|
mw.hook( 'mw.uls.interface.language.change' ).fire( language, deferred );
|
||||||
|
|||||||
Reference in New Issue
Block a user