ULS Frontend: Save preferences only for named users

Add a new method isNamed in ext.uls.common to use the isNamed
method if available else use the isAnon method as usual.

mw.user.isNamed was added in MW 1.40

Bug: T337780
Change-Id: I747c042a95e8edc793a2265a15ed6ba8ae9f1997
This commit is contained in:
Abijeet
2023-08-10 19:44:25 +05:30
committed by jenkins-bot
parent 2434939475
commit 88f0045b94
4 changed files with 24 additions and 13 deletions

View File

@@ -63,7 +63,7 @@
// Track if event logging is enabled // Track if event logging is enabled
mw.hook( 'mw.uls.interface.language.change' ).fire( language ); mw.hook( 'mw.uls.interface.language.change' ).fire( language );
if ( mw.user.isAnon() ) { if ( !mw.uls.isNamed() ) {
return changeLanguageAnon(); return changeLanguageAnon();
} }
@@ -239,4 +239,13 @@
return ret; return ret;
}; };
/**
* Determine if a user is named. Wrapper method is needed since mw.user.isNamed() was added in MW 1.40
* For MW < 1.40
* @returns {boolean}
*/
mw.uls.isNamed = function () {
return typeof mw.user.isNamed === 'function' ? mw.user.isNamed() : !mw.user.isAnon();
};
}() ); }() );

View File

@@ -188,7 +188,7 @@
lang, i, language, $button, autonym; lang, i, language, $button, autonym;
// Don't let anonymous users change interface language // Don't let anonymous users change interface language
if ( !anonsAllowed && mw.user.isAnon() ) { if ( !anonsAllowed && !mw.uls.isNamed() ) {
$loginCta = $( '<p>' ) $loginCta = $( '<p>' )
.attr( 'id', 'uls-display-settings-anon-log-in-cta' ); .attr( 'id', 'uls-display-settings-anon-log-in-cta' );
autonym = $.uls.data.getAutonym( this.contentLanguage ); autonym = $.uls.data.getAutonym( this.contentLanguage );

View File

@@ -194,7 +194,7 @@
} }
function userCanChangeLanguage() { function userCanChangeLanguage() {
return mw.config.get( 'wgULSAnonCanChangeLanguage' ) || !mw.user.isAnon(); return mw.config.get( 'wgULSAnonCanChangeLanguage' ) || mw.uls.isNamed();
} }
/** /**

View File

@@ -28,20 +28,22 @@
// https://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript#Keys // https://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript#Keys
this.preferenceName = 'uls-preferences'; this.preferenceName = 'uls-preferences';
this.username = mw.user.getName(); this.username = mw.user.getName();
this.isAnon = mw.user.isAnon(); // For MW < 1.40. ext.uls.isNamed is inlined here to avoid dependency on ext.uls.common.
this.isNamed = typeof mw.user.isNamed === 'function' ? mw.user.isNamed() : !mw.user.isAnon();
this.preferences = null; this.preferences = null;
this.init(); this.init();
}; };
ULSPreferences.prototype = { ULSPreferences.prototype = {
init: function () { init: function () {
if ( this.isAnon ) { if ( this.isNamed ) {
this.preferences = mw.storage.getObject( this.preferenceName );
} else {
try { try {
this.preferences = JSON.parse( mw.user.options.get( this.preferenceName ) ); this.preferences = JSON.parse( mw.user.options.get( this.preferenceName ) );
} catch ( e ) { } catch ( e ) {
} }
} else {
this.preferences = mw.storage.getObject( this.preferenceName );
} }
if ( !$.isPlainObject( this.preferences ) ) { if ( !$.isPlainObject( this.preferences ) ) {
@@ -78,12 +80,8 @@
var self = this; var self = this;
callback = callback || function () {}; callback = callback || function () {};
if ( this.isAnon ) { if ( this.isNamed ) {
// Anonymous user. Save preferences in local storage // Registered user. Use MW APIs to change preferences
mw.storage.setObject( this.preferenceName, this.preferences );
callback.call( this, true );
} else {
// Logged in user. Use MW APIs to change preferences
new mw.Api().saveOption( new mw.Api().saveOption(
this.preferenceName, this.preferenceName,
JSON.stringify( this.preferences ) JSON.stringify( this.preferences )
@@ -92,6 +90,10 @@
} ).fail( function () { } ).fail( function () {
callback.call( self, false ); callback.call( self, false );
} ); } );
} else {
// Anonymous user. Save preferences in local storage
mw.storage.setObject( this.preferenceName, this.preferences );
callback.call( this, true );
} }
} }
}; };