2016-07-25 13:35:41 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
/**
|
|
|
|
* @param {string} apiUrl
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
var Api = function ( apiUrl ) {
|
|
|
|
this.url = apiUrl;
|
|
|
|
};
|
|
|
|
|
|
|
|
$.extend( Api.prototype, {
|
|
|
|
url: '',
|
|
|
|
|
2016-06-27 14:00:13 +00:00
|
|
|
/**
|
|
|
|
* Fetches a batch of revision data, including a gender setting for users who edited the revision
|
|
|
|
*
|
|
|
|
* @param {string} pageName
|
2016-08-15 18:15:30 +00:00
|
|
|
* @param {Object} options Options
|
|
|
|
* @param {string} [options.dir='older'] Sort direction
|
|
|
|
* @param {number} [options.limit=500] Result limit
|
|
|
|
* @param {number} [options.startId] Start ID
|
|
|
|
* @param {number} [options.endId] End ID
|
|
|
|
* @param {Object} [options.knownUserGenders] Known user genders
|
2016-09-01 11:17:47 +00:00
|
|
|
* @return {jQuery.Promise}
|
2016-06-27 14:00:13 +00:00
|
|
|
*/
|
|
|
|
fetchRevisionData: function ( pageName, options ) {
|
2016-08-15 18:15:30 +00:00
|
|
|
var xhr, userXhr,
|
|
|
|
deferred = $.Deferred(),
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
xhr = this.fetchRevisions( pageName, options )
|
2016-06-27 14:00:13 +00:00
|
|
|
.done( function ( data ) {
|
|
|
|
var revs = data.query.pages[ 0 ].revisions,
|
2016-09-01 11:17:47 +00:00
|
|
|
/* eslint-disable dot-notation */
|
|
|
|
/* jshint -W024 */
|
2016-06-27 14:00:13 +00:00
|
|
|
revContinue = data.continue,
|
2016-09-01 11:17:47 +00:00
|
|
|
/* jshint +W024 */
|
|
|
|
/* eslint-enable dot-notation */
|
2016-08-15 18:15:30 +00:00
|
|
|
genderData = options.knownUserGenders || {},
|
2016-06-27 14:00:13 +00:00
|
|
|
userNames;
|
|
|
|
|
|
|
|
if ( !revs ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
userNames = self.getUserNames( revs, genderData );
|
|
|
|
|
2016-08-15 18:15:30 +00:00
|
|
|
userXhr = self.fetchUserGenderData( userNames )
|
2016-06-27 14:00:13 +00:00
|
|
|
.done( function ( data ) {
|
|
|
|
var users = typeof data !== 'undefined' ? data.query.users : [];
|
|
|
|
|
|
|
|
if ( users.length > 0 ) {
|
|
|
|
$.extend( genderData, self.getUserGenderData( users, genderData ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
revs.forEach( function ( rev ) {
|
|
|
|
if ( typeof rev.user !== 'undefined' && typeof genderData[ rev.user ] !== 'undefined' ) {
|
|
|
|
rev.userGender = genderData[ rev.user ];
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2016-08-15 18:15:30 +00:00
|
|
|
deferred.resolve( { revisions: revs, 'continue': revContinue } );
|
2016-06-27 14:00:13 +00:00
|
|
|
} )
|
2016-08-15 18:15:30 +00:00
|
|
|
.fail( deferred.reject );
|
2016-06-27 14:00:13 +00:00
|
|
|
} )
|
2016-08-15 18:15:30 +00:00
|
|
|
.fail( deferred.reject );
|
|
|
|
|
|
|
|
return deferred.promise( {
|
|
|
|
abort: function () {
|
|
|
|
xhr.abort();
|
|
|
|
if ( userXhr ) {
|
|
|
|
userXhr.abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2016-06-27 14:00:13 +00:00
|
|
|
},
|
|
|
|
|
2016-07-25 13:35:41 +00:00
|
|
|
/**
|
|
|
|
* Fetches up to 500 revisions at a time
|
|
|
|
*
|
2016-06-27 14:00:13 +00:00
|
|
|
* @param {string} pageName
|
2016-08-15 18:15:30 +00:00
|
|
|
* @param {Object} [options] Options
|
|
|
|
* @param {string} [options.dir='older'] Sort direction
|
|
|
|
* @param {number} [options.limit=500] Result limit
|
|
|
|
* @param {number} [options.startId] Start ID
|
|
|
|
* @param {number} [options.endId] End ID
|
|
|
|
* @return {jQuery.jqXHR}
|
2016-07-25 13:35:41 +00:00
|
|
|
*/
|
2016-06-27 14:00:13 +00:00
|
|
|
fetchRevisions: function ( pageName, options ) {
|
2016-08-15 18:15:30 +00:00
|
|
|
var dir, data;
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
dir = options.dir !== undefined ? options.dir : 'older';
|
|
|
|
data = {
|
|
|
|
action: 'query',
|
|
|
|
prop: 'revisions',
|
|
|
|
format: 'json',
|
|
|
|
rvprop: 'ids|timestamp|user|comment|parsedcomment|size|flags',
|
|
|
|
titles: pageName,
|
|
|
|
formatversion: 2,
|
|
|
|
'continue': '',
|
|
|
|
rvlimit: 500,
|
|
|
|
rvdir: dir
|
|
|
|
};
|
2016-06-27 14:00:13 +00:00
|
|
|
|
|
|
|
if ( options.startId !== undefined ) {
|
|
|
|
data.rvstartid = options.startId;
|
|
|
|
}
|
|
|
|
if ( options.endId !== undefined ) {
|
|
|
|
data.rvendid = options.endId;
|
|
|
|
}
|
|
|
|
if ( options.limit !== undefined && options.limit <= 500 ) {
|
|
|
|
data.rvlimit = options.limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $.ajax( {
|
|
|
|
url: this.url,
|
|
|
|
data: data
|
2016-07-25 13:35:41 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches gender data for up to 500 user names
|
|
|
|
*
|
2016-06-27 14:00:13 +00:00
|
|
|
* @param {string[]} users
|
2016-08-15 18:15:30 +00:00
|
|
|
* @return {jQuery.jqXHR}
|
2016-07-25 13:35:41 +00:00
|
|
|
*/
|
2016-06-27 14:00:13 +00:00
|
|
|
fetchUserGenderData: function ( users ) {
|
|
|
|
if ( users.length === 0 ) {
|
|
|
|
return $.Deferred().resolve();
|
|
|
|
}
|
|
|
|
return $.ajax( {
|
2016-07-25 13:35:41 +00:00
|
|
|
url: this.url,
|
|
|
|
data: {
|
|
|
|
action: 'query',
|
|
|
|
list: 'users',
|
|
|
|
format: 'json',
|
|
|
|
usprop: 'gender',
|
2016-06-27 14:00:13 +00:00
|
|
|
ususers: users.join( '|' ),
|
2016-07-25 13:35:41 +00:00
|
|
|
uslimit: 500
|
2016-06-27 14:00:13 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-08-15 18:15:30 +00:00
|
|
|
* @param {Object[]} revs
|
2016-06-27 14:00:13 +00:00
|
|
|
* @param {Object} knownUserGenders
|
|
|
|
* @return {string[]}
|
|
|
|
*/
|
|
|
|
getUserNames: function ( revs, knownUserGenders ) {
|
|
|
|
var allUsers = revs.map( function ( rev ) {
|
|
|
|
return typeof rev.user !== 'undefined' ? rev.user : '';
|
|
|
|
} );
|
|
|
|
return allUsers.filter( function ( value, index, array ) {
|
|
|
|
return value !== '' && typeof knownUserGenders[ value ] === 'undefined' && array.indexOf( value ) === index;
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-08-15 18:15:30 +00:00
|
|
|
* @param {Object[]} data
|
2016-06-27 14:00:13 +00:00
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getUserGenderData: function ( data ) {
|
|
|
|
var genderData = {},
|
|
|
|
usersWithGender = data.filter( function ( item ) {
|
|
|
|
return typeof item.gender !== 'undefined' && item.gender !== 'unknown';
|
|
|
|
} );
|
|
|
|
usersWithGender.forEach( function ( item ) {
|
|
|
|
genderData[ item.name ] = item.gender;
|
2016-07-25 13:35:41 +00:00
|
|
|
} );
|
2016-06-27 14:00:13 +00:00
|
|
|
return genderData;
|
2016-07-25 13:35:41 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
|
|
|
|
mw.libs.revisionSlider.Api = Api;
|
|
|
|
}( mediaWiki, jQuery ) );
|