Merge "Skip userinfo request if no message depends on the gender."

This commit is contained in:
jenkins-bot 2014-04-23 23:47:34 +00:00 committed by Gerrit Code Review
commit 476a0be245
6 changed files with 65 additions and 12 deletions

View file

@ -740,6 +740,9 @@ $wgResourceModules += array(
'messages' => array( 'messages' => array(
'multimediaviewer-file-page', 'multimediaviewer-file-page',
'multimediaviewer-desc-nil', 'multimediaviewer-desc-nil',
// messages that are gender-dependent (we need to check if they really depend on the gender):
'multimediaviewer-userpage-link',
), ),
), ),

View file

@ -67,8 +67,10 @@
* @property {mw.mmv.provider.UserInfo} * @property {mw.mmv.provider.UserInfo}
* @private * @private
*/ */
this.userInfoProvider = new mw.mmv.provider.UserInfo( new mw.mmv.Api( 'userinfo' ), this.userInfoProvider = new mw.mmv.provider.UserInfo( new mw.mmv.Api( 'userinfo' ), {
{ maxage: apiCacheMaxAge } ); useApi: this.needGender(),
maxage: apiCacheMaxAge
} );
/** /**
* @property {mw.mmv.provider.ImageUsage} * @property {mw.mmv.provider.ImageUsage}
@ -82,7 +84,7 @@
* @private * @private
*/ */
this.globalUsageProvider = new mw.mmv.provider.GlobalUsage( new mw.mmv.Api( 'globalusage' ), { this.globalUsageProvider = new mw.mmv.provider.GlobalUsage( new mw.mmv.Api( 'globalusage' ), {
doNotUseApi: !mw.config.get( 'wgMultimediaViewer' ).globalUsageAvailable, useApi: mw.config.get( 'wgMultimediaViewer' ).globalUsageAvailable,
maxage: apiCacheMaxAge maxage: apiCacheMaxAge
} ); } );
// replace with this one to test global usage on a local wiki without going through all the // replace with this one to test global usage on a local wiki without going through all the
@ -112,6 +114,22 @@
MMVP = MultimediaViewer.prototype; MMVP = MultimediaViewer.prototype;
/**
* Check if we need to fetch gender information. This relies on the fact that
* multimediaviewer-userpage-link is the only message which takes a gender parameter.
* @return {boolean}
* FIXME there has to be a better way than this
*/
MMVP.needGender = function () {
var male, female, unknown;
male = mw.message( 'multimediaviewer-userpage-link', '_', 'male' ).text();
female = mw.message( 'multimediaviewer-userpage-link', '_', 'female' ).text();
unknown = mw.message( 'multimediaviewer-userpage-link', '_', 'unknown' ).text();
return male !== female || male !== unknown || female !== unknown;
};
/** /**
* Initialize the lightbox interface given an array of thumbnail * Initialize the lightbox interface given an array of thumbnail
* objects. * objects.

View file

@ -30,7 +30,7 @@
* @cfg {number} [apiLimit=100] number of entries to get from the API. If there are * @cfg {number} [apiLimit=100] number of entries to get from the API. If there are
* more pages than this, we won't have an accurate count. * more pages than this, we won't have an accurate count.
* (Also, influences query performance.) * (Also, influences query performance.)
* @cfg {boolean} [doNotUseApi=false] If true, always returns an empty result immediately, * @cfg {boolean} [useApi=true] If false, always returns an empty result immediately,
* without doing an actual API call. Used when the GlobalUsage extension (and thus the * without doing an actual API call. Used when the GlobalUsage extension (and thus the
* API) is not available. * API) is not available.
* @cfg {number} [dataLimit=10] number of entries to actually put into the model. * @cfg {number} [dataLimit=10] number of entries to actually put into the model.
@ -39,7 +39,7 @@
*/ */
function GlobalUsage( api, options ) { function GlobalUsage( api, options ) {
options = $.extend( { options = $.extend( {
doNotUseApi: false, useApi: true,
apiLimit: 100, apiLimit: 100,
dataLimit: 10 dataLimit: 10
}, options ); }, options );
@ -57,7 +57,7 @@
var provider = this, var provider = this,
fileUsage; fileUsage;
if ( this.options.doNotUseApi ) { if ( !this.options.useApi ) {
fileUsage = new mw.mmv.model.FileUsage( file, mw.mmv.model.FileUsage.Scope.GLOBAL, fileUsage = new mw.mmv.model.FileUsage( file, mw.mmv.model.FileUsage.Scope.GLOBAL,
[], 0, false ); [], 0, false );
fileUsage.fake = true; fileUsage.fake = true;

View file

@ -15,7 +15,7 @@
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>. * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
*/ */
( function ( mw, oo ) { ( function ( mw, $, oo ) {
/** /**
* Gets user information (currently just the gender). * Gets user information (currently just the gender).
@ -25,10 +25,14 @@
* @constructor * @constructor
* @param {mw.Api} api * @param {mw.Api} api
* @param {Object} [options] * @param {Object} [options]
* @cfg {boolean} [useApi=true] If false, always returns an empty result immediately,
* without doing an actual API call. Used when the current language does not have genders.
* @cfg {number} [maxage] cache expiration time, in seconds * @cfg {number} [maxage] cache expiration time, in seconds
* Will be used for both client-side cache (maxage) and reverse proxies (s-maxage) * Will be used for both client-side cache (maxage) and reverse proxies (s-maxage)
*/ */
function UserInfo( api, options ) { function UserInfo( api, options ) {
options = $.extend( { useApi: true }, options );
mw.mmv.provider.Api.call( this, api, options ); mw.mmv.provider.Api.call( this, api, options );
} }
oo.inheritClass( UserInfo, mw.mmv.provider.Api ); oo.inheritClass( UserInfo, mw.mmv.provider.Api );
@ -40,10 +44,22 @@
* @return {jQuery.Promise.<mw.mmv.model.User>} user * @return {jQuery.Promise.<mw.mmv.model.User>} user
*/ */
UserInfo.prototype.get = function( username, repoInfo ) { UserInfo.prototype.get = function( username, repoInfo ) {
var provider = this, var user,
provider = this,
ajaxOptions = {}, ajaxOptions = {},
cacheKey = username; cacheKey = username;
if ( !this.options.useApi ) {
// Create a user object with unknown gender without doing an API request.
// This is used when the language does not use genders, so it would be a waste of time.
// (This might maybe result in incorrect text if the message does not have a translation
// and the fallback language does have genders, but that's an extremely rare edge case
// we can just ignore.)
user = new mw.mmv.model.User( username, mw.mmv.model.User.Gender.UNKNOWN );
user.fake = true;
return $.Deferred().resolve( user );
}
// For local/shared db images the user should be visible via a local API request, // For local/shared db images the user should be visible via a local API request,
// maybe. (In practice we have Wikimedia users who haven't completed the SUL // maybe. (In practice we have Wikimedia users who haven't completed the SUL
// merge process yet, and other sites might even use a shared DB for images // merge process yet, and other sites might even use a shared DB for images
@ -75,4 +91,4 @@
}; };
mw.mmv.provider.UserInfo = UserInfo; mw.mmv.provider.UserInfo = UserInfo;
}( mediaWiki, OO ) ); }( mediaWiki, jQuery, OO ) );

View file

@ -155,15 +155,16 @@
} ); } );
} ); } );
QUnit.asyncTest( 'GlobalUsage doNotUseApi test', 2, function ( assert ) { QUnit.asyncTest( 'GlobalUsage useApi test', 3, function ( assert ) {
var api = {}, var api = { get: this.sandbox.stub().throws( 'API was invoked' ) },
options = { doNotUseApi: true }, options = { useApi: false },
file = new mw.Title( 'File:Stuff.jpg' ), file = new mw.Title( 'File:Stuff.jpg' ),
globalUsageProvider = new mw.mmv.provider.GlobalUsage( api, options ); globalUsageProvider = new mw.mmv.provider.GlobalUsage( api, options );
globalUsageProvider.get( file ).done( function( fileUsage ) { globalUsageProvider.get( file ).done( function( fileUsage ) {
assert.strictEqual( fileUsage.pages.length, 0, 'Does not return any pages' ); assert.strictEqual( fileUsage.pages.length, 0, 'Does not return any pages' );
assert.ok( fileUsage.fake ); assert.ok( fileUsage.fake );
assert.ok( !api.get.called, 'API was not called' );
QUnit.start(); QUnit.start();
} ); } );
} ); } );

View file

@ -148,4 +148,19 @@
QUnit.start(); QUnit.start();
} ); } );
} ); } );
QUnit.asyncTest( 'UserInfo fake test', 4, function ( assert ) {
var api = { get: this.sandbox.stub().throws( 'API was invoked' ) },
username = 'Catrope',
repoInfo = {},
userInfoProvider = new mw.mmv.provider.UserInfo( api, { useApi: false } );
userInfoProvider.get( username, repoInfo ).done( function( user ) {
assert.strictEqual( user.username, 'Catrope', 'username is set correctly' );
assert.strictEqual( user.gender, mw.mmv.model.User.Gender.UNKNOWN, 'gender is set to unknown' );
assert.ok( user.fake, 'fake flag is set' );
assert.ok( !api.get.called, 'API was not called' );
QUnit.start();
} );
} );
}( mediaWiki, jQuery ) ); }( mediaWiki, jQuery ) );