2018-11-16 16:33:52 +00:00
|
|
|
( function () {
|
2016-07-25 13:35:41 +00:00
|
|
|
/**
|
|
|
|
* @param {string} apiUrl
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
var Api = function ( apiUrl ) {
|
|
|
|
this.url = apiUrl;
|
|
|
|
};
|
|
|
|
|
2020-01-24 16:21:23 +00:00
|
|
|
/**
|
|
|
|
* @class mw.libs.revisionSlider.Api
|
|
|
|
*/
|
2016-07-25 13:35:41 +00:00
|
|
|
$.extend( Api.prototype, {
|
|
|
|
url: '',
|
|
|
|
|
2018-08-22 08:00:30 +00:00
|
|
|
/**
|
|
|
|
* Fetches change tags
|
|
|
|
*
|
|
|
|
* @return {jQuery.jqXHR}
|
|
|
|
*/
|
|
|
|
|
|
|
|
fetchAvailableChangeTags: function () {
|
|
|
|
return $.ajax( {
|
|
|
|
url: this.url,
|
|
|
|
data: {
|
|
|
|
action: 'query',
|
|
|
|
list: 'tags',
|
|
|
|
tgprop: 'displayname',
|
|
|
|
tglimit: 500,
|
|
|
|
format: 'json'
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
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
|
2017-07-06 09:49:44 +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,
|
|
|
|
revContinue = data.continue,
|
2016-08-15 18:15:30 +00:00
|
|
|
genderData = options.knownUserGenders || {},
|
2018-08-22 08:00:30 +00:00
|
|
|
changeTags = options.changeTags || [],
|
2018-06-26 13:15:54 +00:00
|
|
|
unknown,
|
2016-06-27 14:00:13 +00:00
|
|
|
userNames;
|
|
|
|
|
|
|
|
if ( !revs ) {
|
2018-06-26 13:15:54 +00:00
|
|
|
return deferred.reject;
|
2016-06-27 14:00:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 08:00:30 +00:00
|
|
|
if ( changeTags && changeTags.length > 0 ) {
|
|
|
|
revs = self.getRevisionsWithNewTags( revs, changeTags );
|
|
|
|
}
|
|
|
|
|
2018-06-26 13:15:54 +00:00
|
|
|
// No need to query any gender data if masculine, feminine, and neutral are all
|
|
|
|
// the same anyway
|
|
|
|
unknown = mw.msg( 'revisionslider-label-username', 'unknown' );
|
|
|
|
if ( mw.msg( 'revisionslider-label-username', 'male' ) === unknown &&
|
|
|
|
mw.msg( 'revisionslider-label-username', 'female' ) === unknown
|
|
|
|
) {
|
2019-02-10 16:54:28 +00:00
|
|
|
return deferred.resolve( { revisions: revs, continue: revContinue } );
|
2018-06-26 13:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userNames = self.getUniqueUserNamesWithUnknownGender( revs, genderData );
|
2016-06-27 14:00:13 +00:00
|
|
|
|
2016-08-15 18:15:30 +00:00
|
|
|
userXhr = self.fetchUserGenderData( userNames )
|
2020-09-11 15:28:52 +00:00
|
|
|
.done( function ( data2 ) {
|
|
|
|
if ( typeof data2 === 'object' &&
|
|
|
|
data2.query &&
|
|
|
|
data2.query.users &&
|
|
|
|
data2.query.users.length > 0
|
2018-06-26 13:15:54 +00:00
|
|
|
) {
|
2020-09-11 15:28:52 +00:00
|
|
|
$.extend( genderData, self.getUserGenderData( data2.query.users, genderData ) );
|
2016-06-27 14:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
revs.forEach( function ( rev ) {
|
2018-08-02 07:35:37 +00:00
|
|
|
if ( 'user' in rev && rev.user in genderData ) {
|
2016-06-27 14:00:13 +00:00
|
|
|
rev.userGender = genderData[ rev.user ];
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-02-10 16:54:28 +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 || {};
|
2018-08-02 07:35:37 +00:00
|
|
|
dir = 'dir' in options ? options.dir : 'older';
|
2016-08-15 18:15:30 +00:00
|
|
|
data = {
|
|
|
|
action: 'query',
|
|
|
|
prop: 'revisions',
|
|
|
|
format: 'json',
|
2018-08-22 08:00:30 +00:00
|
|
|
rvprop: 'ids|timestamp|user|comment|parsedcomment|size|flags|tags',
|
2016-08-15 18:15:30 +00:00
|
|
|
titles: pageName,
|
|
|
|
formatversion: 2,
|
2019-02-10 16:54:28 +00:00
|
|
|
continue: '',
|
2016-08-15 18:15:30 +00:00
|
|
|
rvlimit: 500,
|
|
|
|
rvdir: dir
|
|
|
|
};
|
2016-06-27 14:00:13 +00:00
|
|
|
|
2018-08-02 07:35:37 +00:00
|
|
|
if ( 'startId' in options ) {
|
2016-06-27 14:00:13 +00:00
|
|
|
data.rvstartid = options.startId;
|
|
|
|
}
|
2018-08-02 07:35:37 +00:00
|
|
|
if ( 'endId' in options ) {
|
2016-06-27 14:00:13 +00:00
|
|
|
data.rvendid = options.endId;
|
|
|
|
}
|
2018-08-02 07:35:37 +00:00
|
|
|
if ( 'limit' in options && options.limit <= 500 ) {
|
2016-06-27 14:00:13 +00:00
|
|
|
data.rvlimit = options.limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $.ajax( {
|
|
|
|
url: this.url,
|
|
|
|
data: data
|
2016-07-25 13:35:41 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-06-26 13:15:54 +00:00
|
|
|
* Fetches gender data for maximum 50 user names
|
2016-07-25 13:35:41 +00:00
|
|
|
*
|
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: {
|
2017-01-28 11:39:47 +00:00
|
|
|
formatversion: 2,
|
2016-07-25 13:35:41 +00:00
|
|
|
action: 'query',
|
|
|
|
list: 'users',
|
|
|
|
format: 'json',
|
|
|
|
usprop: 'gender',
|
2018-06-26 13:15:54 +00:00
|
|
|
ususers: users.slice( 0, 50 ).join( '|' )
|
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[]}
|
|
|
|
*/
|
2018-06-26 13:15:54 +00:00
|
|
|
getUniqueUserNamesWithUnknownGender: function ( revs, knownUserGenders ) {
|
2016-06-27 14:00:13 +00:00
|
|
|
var allUsers = revs.map( function ( rev ) {
|
2018-06-26 13:15:54 +00:00
|
|
|
return 'user' in rev && !( 'anon' in rev ) ? rev.user : '';
|
2016-06-27 14:00:13 +00:00
|
|
|
} );
|
|
|
|
return allUsers.filter( function ( value, index, array ) {
|
2018-06-26 13:15:54 +00:00
|
|
|
return value !== '' && !( value in knownUserGenders ) && array.indexOf( value ) === index;
|
2016-06-27 14:00:13 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
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 ) {
|
2018-06-26 13:15:54 +00:00
|
|
|
return 'gender' in item && item.gender !== 'unknown';
|
2016-06-27 14:00:13 +00:00
|
|
|
} );
|
|
|
|
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;
|
2018-08-22 08:00:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object[]} revs
|
|
|
|
* @param {Object[]} changeTags
|
|
|
|
* @return {Object[]}
|
|
|
|
*/
|
|
|
|
getRevisionsWithNewTags: function ( revs, changeTags ) {
|
|
|
|
revs.forEach( function ( rev ) {
|
|
|
|
var oldTags = rev.tags,
|
|
|
|
newTags = [];
|
|
|
|
|
|
|
|
if ( oldTags.length > 0 ) {
|
|
|
|
oldTags.forEach( function ( tag ) {
|
|
|
|
changeTags.forEach( function ( changeTag ) {
|
|
|
|
if ( changeTag.name === tag ) {
|
|
|
|
tag = changeTag.displayname;
|
|
|
|
}
|
|
|
|
} );
|
2020-04-02 20:38:27 +00:00
|
|
|
// Remove hidden tags (tags with no displayname)
|
|
|
|
if ( tag ) {
|
|
|
|
newTags.push( tag );
|
|
|
|
}
|
2018-08-22 08:00:30 +00:00
|
|
|
} );
|
|
|
|
rev.tags = newTags;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return revs;
|
2016-07-25 13:35:41 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
|
|
|
|
mw.libs.revisionSlider.Api = Api;
|
2018-11-16 16:33:52 +00:00
|
|
|
}() );
|