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:
Fomafix
2015-09-08 20:21:05 +00:00
committed by [[mw:User:Fomafix]]
parent c6d5599ad8
commit 0d5c69f655
2 changed files with 32 additions and 8 deletions

View File

@@ -70,7 +70,8 @@ $wgResourceModules['ext.uls.init'] = array(
'monobook' => 'resources/css/ext.uls-monobook.css',
),
'dependencies' => array(
'mediawiki.Uri',
'mediawiki.api',
'mediawiki.cookie',
'jquery.client',
'jquery.cookie',
),

View File

@@ -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.
*/
mw.uls.changeLanguage = function ( language ) {
var uri = new mw.Uri( window.location.href ),
deferred = new $.Deferred();
var deferred = new $.Deferred();
function changeLanguageAnon() {
if ( mw.config.get( 'wgULSAnonCanChangeLanguage' ) ) {
mw.cookie.set( 'language', language );
location.reload();
}
}
deferred.done( function () {
uri.extend( {
setlang: language
} );
var api;
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 );