User:JackPotte/smart patrol.js

From Meta, a Wikimedia project coordination wiki

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*
* This script adds a tab which allows a mass patrolling on the current page. Because it was pointless to mark as patrolled 10 intermediate versions when we can read the difference between the first and the last
*/

var totalRevisions;
var treatedRevisions = 0;

if (mw.config.get('wgNamespaceNumber') >= 0) {
  mw.loader.using( [ 'mediawiki.util', 'mediawiki.api', 'oojs-ui' ], function() {
    mw.util.addPortletLink( 'p-cactions', 'javascript:markAllAsPatrolled()', 'Smart patrol', 'Smart patrol' );
  } );
}

function markAllAsPatrolled() {
  new OO.ui.confirm('Are you sure you want to patrol all the page revisions?').then( function( response ) {
    if ( response === true ) {
        getRevisionsList();
    }
  } );
}

function getRevisionsList() {
  var api = new mw.Api();
  api.get( {
	'action': 'query',
	'format': 'json',
	'prop': 'revisions',
	'titles': mw.config.get( 'wgPageName' ),
	'formatversion': '2',
	'rvprop': 'ids|user',
	'rvlimit': 'max'
  } ).then( function ( data ) {
    var revisions = data.query.pages[ 0 ].revisions;
    //var user = revisions[ 0 ].user;
    totalRevisions = revisions.length;
    revisions.forEach( function ( revision ) {
      treatedRevisions++;
      //if ( user === revision.user ) {
        markRevAsPatrolled( revision.revid );
      //}
    } );

  } ).fail( function ( error ) {
      mw.notification.notify( 'Something went wrong: ' + error, { title: 'Smart Patrol', type: 'error' } );
  } );
}

function markRevAsPatrolled( revid ) {
  var api = new mw.Api();
  api.postWithToken( 'patrol', {
	'action': 'patrol',
	'revid': revid

  } ).then( function ( info ) {
    console.log( 'Success for ' + revid, { title: 'Smart Patrol', type: 'info' } );
    if ( totalRevisions == treatedRevisions ) { window.location.reload(); }

  } ).fail( function ( error ) {
    console.log( 'Something went wrong: ' + error, { title: 'Smart Patrol', type: 'error' } );
  } );
}