mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/MultimediaViewer
synced 2024-11-24 08:13:38 +00:00
Merge "Userinfo provider"
This commit is contained in:
commit
ed912adff1
|
@ -152,6 +152,7 @@ call_user_func( function() {
|
|||
'mmv.provider.ImageInfo.js',
|
||||
'mmv.provider.FileRepoInfo.js',
|
||||
'mmv.provider.ThumbnailInfo.js',
|
||||
'mmv.provider.UserInfo.js',
|
||||
'mmv.provider.Image.js',
|
||||
),
|
||||
|
||||
|
|
|
@ -128,6 +128,7 @@ class MultimediaViewerHooks {
|
|||
'tests/qunit/provider/mmv.provider.ImageInfo.test.js',
|
||||
'tests/qunit/provider/mmv.provider.FileRepoInfo.test.js',
|
||||
'tests/qunit/provider/mmv.provider.ThumbnailInfo.test.js',
|
||||
'tests/qunit/provider/mmv.provider.UserInfo.test.js',
|
||||
'tests/qunit/provider/mmv.provider.Image.test.js',
|
||||
'tests/qunit/mmv.lightboxinterface.test.js',
|
||||
'tests/qunit/mmv.ui.categories.test.js',
|
||||
|
|
|
@ -121,6 +121,12 @@
|
|||
*/
|
||||
this.thumbnailInfoProvider = new mw.mmv.provider.ThumbnailInfo( this.api );
|
||||
|
||||
/**
|
||||
* @property {mw.mmv.provider.UserInfo}
|
||||
* @private
|
||||
*/
|
||||
this.userInfoProvider = new mw.mmv.provider.UserInfo( this.api );
|
||||
|
||||
/**
|
||||
* @property {mw.mmv.provider.ImageUsage}
|
||||
* @private
|
||||
|
@ -438,32 +444,11 @@
|
|||
if ( imageData.lastUploader ) {
|
||||
gfpid = this.profileStart( 'gender-fetch' );
|
||||
|
||||
// TODO: Reuse the api member, fix everywhere.
|
||||
// Fetch the gender from the uploader's home wiki
|
||||
// TODO this is ugly as hell, let's fix this in core.
|
||||
new mw.Api( {
|
||||
ajax: {
|
||||
url: repoData.apiUrl || mw.util.wikiScript( 'api' ),
|
||||
dataType: 'jsonp'
|
||||
}
|
||||
} ).get( {
|
||||
action: 'query',
|
||||
list: 'users',
|
||||
ususers: imageData.lastUploader,
|
||||
usprop: 'gender'
|
||||
} ).done( function ( data ) {
|
||||
var gender = 'unknown';
|
||||
|
||||
this.userInfoProvider.get( imageData.lastUploader, repoData ).done( function ( gender ) {
|
||||
viewer.profileEnd( gfpid );
|
||||
|
||||
if ( data && data.query && data.query.users &&
|
||||
data.query.users[0] && data.query.users[0].gender ) {
|
||||
gender = data.query.users[0].gender;
|
||||
}
|
||||
|
||||
ui.setUserPageLink( repoData, imageData.lastUploader, gender );
|
||||
} ).fail( function () {
|
||||
mw.log( 'Gender fetch with ID ' + gfpid + ' failed, probably due to cross-domain API request.' );
|
||||
mw.log( 'Gender fetch with ID ' + gfpid + ' failed' );
|
||||
ui.setUserPageLink( repoData, imageData.lastUploader, 'unknown' );
|
||||
} );
|
||||
}
|
||||
|
|
91
resources/mmv/provider/mmv.provider.UserInfo.js
Normal file
91
resources/mmv/provider/mmv.provider.UserInfo.js
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* This file is part of the MediaWiki extension MultimediaViewer.
|
||||
*
|
||||
* MultimediaViewer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* MultimediaViewer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
( function ( mw, oo, $ ) {
|
||||
|
||||
/**
|
||||
* @class mw.mmv.provider.UserInfo
|
||||
* Gets user information (currently just the gender).
|
||||
* See https://www.mediawiki.org/wiki/API:Users
|
||||
* @extends mw.mmv.provider.Api
|
||||
* @inheritdoc
|
||||
* @param {mw.Api} api
|
||||
*/
|
||||
function UserInfo( api ) {
|
||||
mw.mmv.provider.Api.call( this, api );
|
||||
}
|
||||
oo.inheritClass( UserInfo, mw.mmv.provider.Api );
|
||||
|
||||
/**
|
||||
* @method
|
||||
* Runs an API GET request to get the user info.
|
||||
* @param {string} username
|
||||
* @param {mw.mmv.model.Repo} repoInfo
|
||||
* @return {jQuery.Promise<mw.mmv.provider.UserInfo.Gender>} gender
|
||||
*/
|
||||
UserInfo.prototype.get = function( username, repoInfo ) {
|
||||
var provider = this,
|
||||
ajaxOptions = {},
|
||||
cacheKey = username;
|
||||
|
||||
// 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
|
||||
// merge process yet, and other sites might even use a shared DB for images
|
||||
// without CentralAuth. Too bad for them.)
|
||||
// For InstantCommons images we need to get user data directly from the repo's API.
|
||||
if ( repoInfo.apiUrl ) {
|
||||
ajaxOptions.url = repoInfo.apiUrl;
|
||||
ajaxOptions.dataType = 'jsonp';
|
||||
cacheKey = cacheKey + '|' + repoInfo.apiUrl; // local and remote user names could conflict
|
||||
}
|
||||
|
||||
if ( !this.cache[cacheKey] ) {
|
||||
this.cache[cacheKey] = this.api.get( {
|
||||
action: 'query',
|
||||
list: 'users',
|
||||
ususers: username,
|
||||
usprop: 'gender'
|
||||
}, ajaxOptions ).then( function( data ) {
|
||||
return provider.getQueryField( 'users', data );
|
||||
} ).then( function( users ) {
|
||||
if ( users[0] && users[0].gender ) {
|
||||
return users[0].gender;
|
||||
} else {
|
||||
return $.Deferred().reject( 'error in provider, user info not found' );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
return this.cache[cacheKey];
|
||||
};
|
||||
|
||||
/**
|
||||
* Gender of the user (can be set at preferences, UNKNOWN means they did not set it).
|
||||
* This is mainly used for translations, so in wikis where there are no grammatic genders
|
||||
* it is not used much.
|
||||
* (This should really belong to a model, but there is no point in having a user model if we
|
||||
* only need a single property.)
|
||||
* @enum {string} mw.mmv.provider.UserInfo.Gender
|
||||
*/
|
||||
UserInfo.Gender = {
|
||||
MALE: 'male',
|
||||
FEMALE: 'female',
|
||||
UNKNOWN: 'unknown'
|
||||
};
|
||||
|
||||
mw.mmv.provider.UserInfo = UserInfo;
|
||||
}( mediaWiki, OO, jQuery ) );
|
123
tests/qunit/provider/mmv.provider.UserInfo.test.js
Normal file
123
tests/qunit/provider/mmv.provider.UserInfo.test.js
Normal file
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* This file is part of the MediaWiki extension MultimediaViewer.
|
||||
*
|
||||
* MultimediaViewer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* MultimediaViewer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
( function ( mw, $ ) {
|
||||
QUnit.module( 'mmv.provider.UserInfo', QUnit.newMwEnvironment() );
|
||||
|
||||
QUnit.test( 'UserInfo constructor sanity check', 1, function ( assert ) {
|
||||
var api = { get: function() {} },
|
||||
userInfoProvider = new mw.mmv.provider.UserInfo( api );
|
||||
|
||||
assert.ok( userInfoProvider );
|
||||
} );
|
||||
|
||||
QUnit.asyncTest( 'UserInfo get test', 5, function ( assert ) {
|
||||
var apiCallCount = 0,
|
||||
api = { get: function() {
|
||||
apiCallCount++;
|
||||
return $.Deferred().resolve( {
|
||||
query: {
|
||||
users: [
|
||||
{
|
||||
userid: 4587601,
|
||||
name: 'Catrope',
|
||||
gender: 'male'
|
||||
}
|
||||
]
|
||||
}
|
||||
} );
|
||||
} },
|
||||
username = 'Catrope',
|
||||
repoInfo = {},
|
||||
foreignRepoInfo = { apiUrl: 'http://example.com/api.php' },
|
||||
userInfoProvider = new mw.mmv.provider.UserInfo( api );
|
||||
|
||||
userInfoProvider.get( username, repoInfo ).then( function( gender ) {
|
||||
assert.strictEqual( gender, mw.mmv.provider.UserInfo.Gender.MALE, 'gender is set correctly' );
|
||||
} ).then( function() {
|
||||
assert.strictEqual( apiCallCount, 1 );
|
||||
// call the data provider a second time to check caching
|
||||
return userInfoProvider.get( username, repoInfo );
|
||||
} ).then( function() {
|
||||
assert.strictEqual( apiCallCount, 1 );
|
||||
// call a third time with a different user to check caching
|
||||
return userInfoProvider.get( 'OtherUser', repoInfo );
|
||||
} ).then( function() {
|
||||
assert.strictEqual( apiCallCount, 2 );
|
||||
// call again with a different repo to check caching
|
||||
return userInfoProvider.get( username, foreignRepoInfo );
|
||||
} ).then( function() {
|
||||
assert.strictEqual( apiCallCount, 3 );
|
||||
QUnit.start();
|
||||
} );
|
||||
} );
|
||||
|
||||
QUnit.asyncTest( 'UserInfo missing data test', 1, function ( assert ) {
|
||||
var api = { get: function() {
|
||||
return $.Deferred().resolve( {} );
|
||||
} },
|
||||
username = 'Catrope',
|
||||
repoInfo = {},
|
||||
userInfoProvider = new mw.mmv.provider.UserInfo( api );
|
||||
|
||||
userInfoProvider.get( username, repoInfo ).fail( function() {
|
||||
assert.ok( true, 'promise rejected when no data is returned' );
|
||||
QUnit.start();
|
||||
} );
|
||||
} );
|
||||
|
||||
QUnit.asyncTest( 'UserInfo missing user test', 1, function ( assert ) {
|
||||
var api = { get: function() {
|
||||
return $.Deferred().resolve( {
|
||||
query: {
|
||||
users: []
|
||||
}
|
||||
} );
|
||||
} },
|
||||
username = 'Catrope',
|
||||
repoInfo = {},
|
||||
userInfoProvider = new mw.mmv.provider.UserInfo( api );
|
||||
|
||||
userInfoProvider.get( username, repoInfo ).fail( function() {
|
||||
assert.ok( true, 'promise rejected when user is missing' );
|
||||
QUnit.start();
|
||||
} );
|
||||
} );
|
||||
|
||||
QUnit.asyncTest( 'UserInfo missing gender test', 1, function ( assert ) {
|
||||
var api = { get: function() {
|
||||
return $.Deferred().resolve( {
|
||||
query: {
|
||||
users: [
|
||||
{
|
||||
userid: 4587601,
|
||||
name: 'Catrope'
|
||||
}
|
||||
]
|
||||
}
|
||||
} );
|
||||
} },
|
||||
username = 'Catrope',
|
||||
repoInfo = {},
|
||||
userInfoProvider = new mw.mmv.provider.UserInfo( api );
|
||||
|
||||
userInfoProvider.get( username, repoInfo ).fail( function() {
|
||||
assert.ok( true, 'promise rejected when gender is missing' );
|
||||
QUnit.start();
|
||||
} );
|
||||
} );
|
||||
}( mediaWiki, jQuery ) );
|
Loading…
Reference in a new issue