User:Matěj Suchánek/patrolRevisions.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
( function ( mw, $ ) {
var api;
function markAsPatrolled( rcid ) {
return api.postWithToken( 'patrol', {
'action': 'patrol',
'rcid': rcid,
} );
}
function processNext( queue ) {
if ( queue.length === 0 ) {
return $.Deferred().resolve();
}
const rcid = queue.pop();
return markAsPatrolled( rcid )
.then( function () {
return processNext( queue );
} );
}
function getRevToRcIdMapping( response ) {
var revToRcIdMapping = {};
response.query.recentchanges.forEach( function ( info ) {
revToRcIdMapping[ info.revid ] = info.rcid;
} );
return revToRcIdMapping;
}
function queryRevToRcIdMapping( query ) {
return api.get( query ).then( getRevToRcIdMapping );
}
function notifyOfMissingButton( text ) {
mw.loader.using( [ 'mediawiki.jqueryMsg' ] ).then( function () {
mw.messages.set( {
'gadget-patrolRevisions-notice': 'The <nowiki>[' + text + ']</nowiki> button could not be inserted. '
+ 'There was probably a change in the interface. '
+ 'Please make a notice about this on the [[d:User talk:Matěj Suchánek/patrolRevisions.js|talk page]].'
} );
mw.notify(
$( '<div>' + mw.message( 'gadget-patrolRevisions-notice' ).parse() + '</div>' ),
{ autoHide: false, type: 'warn' }
);
} );
}
function onHistory() {
if ( mw.config.get( 'wgAction' ) !== 'history' ) {
return;
}
queryRevToRcIdMapping( {
'action': 'query',
'list': 'recentchanges',
'rcprop': 'ids',
'rcshow': 'unpatrolled',
'rclimit': 'max',
'rctype': [ 'edit', 'new' ],
'rctitle': mw.config.get( 'wgPageName' ),
} ).then( function ( revToRcIdMapping ) {
var length = Object.values( revToRcIdMapping ).length,
queue = [];
$( function () {
$( 'ul.mw-contributions-list > li' ).each( function () {
var $this = $( this );
const
revid = +$this.data( 'mw-revid' ),
rcid = revToRcIdMapping[ revid ];
if ( rcid ) {
$this.addClass( 'row-unpatrolled' );
queue.push( rcid );
return --length !== 0;
}
} );
if ( queue.length === 0 ) {
return;
}
var $anchor = $( '.mw-history-revisionactions' ), // admins only
after = true;
if ( $anchor.length === 0 ) {
$anchor = $( '.mw-history-compareselectedversions' );
after = false;
}
if ( $anchor.length === 0 ) {
notifyOfMissingButton( 'Mark all revisions as patrolled' );
return;
}
mw.util.addCSS( '.row-unpatrolled { background-color: #fff0c2 !important; }' );
var $el = $( '<div>' )
.addClass( 'mark-all-as-patrolled' )
.css( {
'float': 'right',
'clear': 'both',
} )
.append(
'[',
$( '<a>' )
.attr( 'href', '#' )
.text( 'Mark all revisions as patrolled' )
.on( 'click', function ( event ) {
event.preventDefault();
$( this ).replaceWith( $.createSpinner( {
size: 'small',
type: 'inline',
} ) );
processNext( queue )
.then( function () {
$( '.mark-all-as-patrolled' ).remove();
$( '.row-unpatrolled' ).removeClass( 'row-unpatrolled' );
mw.notify( 'All revisions have been marked as patrolled' );
} /* TODO: report error */ );
} ),
']'
);
if ( after ) {
$anchor.after( $el );
} else {
$anchor.append( $el );
}
} );
} );
}
function onUserContribs() {
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Contributions' ) {
return;
}
if ( !mw.config.get( 'wgRelevantUserName' ) ) {
return;
}
queryRevToRcIdMapping( {
'action': 'query',
'list': 'recentchanges',
'rcprop': 'ids',
'rcshow': 'unpatrolled',
'rclimit': 'max',
'rctype': [ 'edit', 'new' ],
'rcuser': mw.config.get( 'wgRelevantUserName' ),
} ).then( function ( revToRcIdMapping ) {
var queue = [],
length = Object.values( revToRcIdMapping ).length,
marker = $( '<abbr>' )
.attr( 'title', 'This change hasn\'t been patrolled yet' )
.addClass( 'unpatrolled' )
.html( '!' );
$( function () {
$( '.mw-contributions-list > li' ).each( function () {
var $this = $( this );
const
revid = +$this.data( 'mw-revid' ),
rcid = revToRcIdMapping[ revid ];
if ( rcid ) {
$this.find( '.mw-changeslist-separator' ).eq( 1 ).after( [ ' ', marker.clone() ] );
queue.push( rcid );
return --length !== 0;
}
} );
if ( queue.length === 0 ) {
return;
}
var $anchor = $( 'section.mw-pager-body, div.mw-pager-navigation-bar' ).first();
if ( $anchor.length === 0 ) {
notifyOfMissingButton( 'Mark all revisions as patrolled' );
return;
}
$anchor.before(
$( '<div>' )
.addClass( 'mark-all-as-patrolled' )
.css( {
'float': 'right',
} )
.append(
'[',
$( '<a>' )
.attr( 'href', '#' )
.text( 'Mark all revisions as patrolled' )
.on( 'click', function ( event ) {
event.preventDefault();
$( this ).replaceWith( $.createSpinner( {
size: 'small',
type: 'inline',
} ) );
processNext( queue )
.then( function () {
$( '.mark-all-as-patrolled' ).remove();
$( '.unpatrolled' ).remove();
mw.notify( 'All user\'s displayed revisions have been marked as patrolled' );
} /* TODO: report error */ );
} ),
']'
)
);
} );
} );
}
function onDiff( $diff ) {
if ( !mw.config.get( 'wgDiffNewId' ) || !mw.config.get( 'wgDiffOldId' ) ) {
return;
}
var $el = $diff.find( '.diff-multi' );
if ( $el.length !== 1 ) {
return;
}
const
newId = mw.config.get( 'wgDiffNewId' ),
oldId = mw.config.get( 'wgDiffOldId' );
api.get( {
'formatversion': 2,
'action': 'query',
'prop': 'revisions',
'titles': mw.config.get( 'wgPageName' ),
'rvprop': [ 'ids', 'timestamp' ],
'rvlimit': 'max',
'rvdir': 'older',
'rvstartid': newId,
'rvendid': oldId,
} ).then( function ( resp ) {
var revisions = resp.query.pages[0].revisions;
const len = revisions.length;
if ( len < 3 ) {
return {};
}
return queryRevToRcIdMapping( {
'action': 'query',
'list': 'recentchanges',
'rcprop': 'ids',
'rcshow': 'unpatrolled',
'rclimit': 'max',
'rctype': [ 'edit', 'new' ],
'rctitle': mw.config.get( 'wgPageName' ),
'rcdir': 'older',
'rcstart': revisions[0].timestamp,
'rcend': revisions[len-1].timestamp,
} );
} ).then( function ( revToRcIdMapping ) {
var queue = [];
for ( var revid in revToRcIdMapping ) {
if ( oldId < revid && revid <= newId ) {
queue.push( revToRcIdMapping[ revid ] );
}
}
if ( queue.length === 0 ) {
return;
}
$( function () {
$el
.append(
$( '<div>' )
.addClass( 'mark-revisions-as-patrolled' )
.append(
'[',
$( '<a>' )
.attr( 'href', '#' )
.text( 'Mark all revisions as patrolled' )
.on( 'click', function ( event ) {
event.preventDefault();
$( this ).replaceWith( $.createSpinner( {
size: 'small',
type: 'inline',
} ) );
processNext( queue )
.then( function () {
$el.find( '.mark-revisions-as-patrolled' ).remove();
$diff.find( '#mw-diff-ntitle4 .patrollink' ).remove();
mw.notify( 'All revisions have been marked as patrolled' );
} /* TODO: report error */ );
} ),
']'
)
);
} );
} );
}
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util', 'jquery.spinner' ] ).then( function () {
api = new mw.Api();
onHistory();
onUserContribs();
mw.hook( 'wikipage.diff' ).add( onDiff );
} );
} )( mediaWiki, jQuery );