MediaWiki:Gadget-PatrolEntirePage.js: Difference between revisions

From Pikipedia, the Pikmin wiki
Jump to navigation Jump to search
(Created page with "// Wiki patrol entire page // Adds a "Patrol entire page" button to the toolbar of a wiki. // by Espyo (function() { 'use strict'; //Spinner element. If the wiki's spinner.js is loaded, this is a spinner object. Otherwise, just a span. var patrolEntirePageSpinner = null; //Button a element. var patrolEntirePageA = null; //List of revisions to be patrolled. var revsToPatrol = []; //Number of revisions patrolled so far. var revsPatrol...")
 
mNo edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
// Wiki patrol entire page
// Adds a "Patrol entire page" link to the wiki's toolbox
// Adds a "Patrol entire page" button to the toolbar of a wiki.
// by Espyo
// by Espyo


(function() {
$(function() {
     'use strict';
     'use strict';


 
     // Spinner element. If the wiki's spinner.js is loaded, this is a spinner object. Otherwise, just a span.
     //Spinner element. If the wiki's spinner.js is loaded, this is a spinner object. Otherwise, just a span.
     var patrolEntirePageSpinner = null;
     var patrolEntirePageSpinner = null;
     //Button a element.
     // Button a element.
     var patrolEntirePageA = null;
     var patrolEntirePageA = null;
     //List of revisions to be patrolled.
     // List of revisions to be patrolled.
     var revsToPatrol = [];
     var revsToPatrol = [];
     //Number of revisions patrolled so far.
     // Number of revisions patrolled so far.
     var revsPatrolled = 0;
     var revsPatrolled = 0;


 
     // Can this user even do patrols, and is it a non-Special page?
     //Can this user even do patrols?
     function canPatrol() {
     function canPatrol() {
         return mw.user.tokens.values.patrolToken.length > 4;
         return mw.user.tokens.values.patrolToken.length > 4 && mw.config.get('wgCanonicalNamespace') !== 'Special';
     }
     }


 
     // Create the button in the toolbox.
     //Create the button in the toolbar.
     function createPatrolEntirePageButton() {
     function createPatrolEntirePageButton() {
         var toolsNav = document.getElementById('p-tb');
         var toolsNav = document.getElementById('p-tb');
Line 34: Line 30:
         toolsUl.appendChild(patrolEntirePageLi);
         toolsUl.appendChild(patrolEntirePageLi);
         patrolEntirePageLi.id = 't-patrolentirepagescript';
         patrolEntirePageLi.id = 't-patrolentirepagescript';
        patrolEntirePageLi.className = 'mw-list-item';


         patrolEntirePageA = document.createElement('a');
         patrolEntirePageA = document.createElement('a');
         patrolEntirePageLi.appendChild(patrolEntirePageA);
         patrolEntirePageLi.appendChild(patrolEntirePageA);
         patrolEntirePageA.innerHTML = 'Patrol entire page';
         patrolEntirePageA.innerHTML = 'Patrol entire page';
        patrolEntirePageA.href = 'javascript:;';
         patrolEntirePageA.title =
         patrolEntirePageA.title =
             'Use this to patrol all edits on this page. ' +
             'Use this to patrol all edits on this page. ' +
Line 45: Line 43:
     }
     }


 
     // Obtains a list of unpatrolled edits from the recent changes.
     //Obtains a list of unpatrolled edits from the recent changes.
     // That is the only possible place to fetch unpatrolled edits from.
     //That is the only possible place to fetch unpatrolled edits from.
     function fetchUnpatrolledEdits() {
     function fetchUnpatrolledEdits() {
         if(!canPatrol()) {
         if(!canPatrol()) {
Line 72: Line 69:
     }
     }


 
     // Shows a spinner in place of the button, and hides the button.
     //Shows a spinner in place of the button, and hides the button.
     function showSpinner() {
     function showSpinner() {
         if(patrolEntirePageSpinner == null) {
         if(patrolEntirePageSpinner == null) {
Line 88: Line 84:
     }
     }


 
     // Shows the button, and hides the spinner if any.
     //Shows the button, and hides the spinner if any.
     function showButton() {
     function showButton() {
         if(patrolEntirePageSpinner != null) {
         if(patrolEntirePageSpinner != null) {
Line 97: Line 92:
     }
     }


     //Parses the list of unpatrolled edits received from the API.
     // Parses the list of unpatrolled edits received from the API.
     //It also prepares the button for the next step.
     // It also prepares the button for the next step.
     function parseUnpatrolledEdits(data) {
     function parseUnpatrolledEdits(data) {
         //DEBUG:
         // DEBUG:
         //console.log(data);
         // console.log(data);


         for(var e = 0; e < data.query.recentchanges.length; e++) {
         for(var e = 0; e < data.query.recentchanges.length; e++) {
Line 127: Line 122:
     }
     }


 
     // Starts the full patrolling process.
     //Starts the full patrolling process.
     function doPatrol() {
     function doPatrol() {
         showSpinner();
         showSpinner();
Line 138: Line 132:
             console.log('MediaWiki patrol entire page script -- patrolling RC ID ' + rcid + '.');
             console.log('MediaWiki patrol entire page script -- patrolling RC ID ' + rcid + '.');


             //DEBUG:
             // DEBUG:
             //handleRevPatrolled();
             // handleRevPatrolled();


             api.postWithToken(
             api.postWithToken(
Line 157: Line 151:
     }
     }


 
     // Handles one of the revisions being successfully patrolled.
     //Handles one of the revisions being successfully patrolled.
     function handleRevPatrolled(data) {
     function handleRevPatrolled(data) {
         revsPatrolled++;
         revsPatrolled++;
Line 170: Line 163:
     }
     }


 
     // Handles an error while trying to patrol a revision.
     //Handles an error while trying to patrol a revision.
     function handlePatrolError(code, data) {
     function handlePatrolError(code, data) {
         showButton();
         showButton();
Line 180: Line 172:
         );
         );
     }
     }


     createPatrolEntirePageButton();
     createPatrolEntirePageButton();
})();
});

Latest revision as of 15:13, August 14, 2023

// Adds a "Patrol entire page" link to the wiki's toolbox
// by Espyo

$(function() {
    'use strict';

    // Spinner element. If the wiki's spinner.js is loaded, this is a spinner object. Otherwise, just a span.
    var patrolEntirePageSpinner = null;
    // Button a element.
    var patrolEntirePageA = null;
    // List of revisions to be patrolled.
    var revsToPatrol = [];
    // Number of revisions patrolled so far.
    var revsPatrolled = 0;

    // Can this user even do patrols, and is it a non-Special page?
    function canPatrol() {
        return mw.user.tokens.values.patrolToken.length > 4 && mw.config.get('wgCanonicalNamespace') !== 'Special';
    }

    // Create the button in the toolbox.
    function createPatrolEntirePageButton() {
        var toolsNav = document.getElementById('p-tb');
        if(!toolsNav) return;
        var toolsUl = toolsNav.children[1].children[0];
        if(!toolsUl) return;
        if(!canPatrol()) return;

        var patrolEntirePageLi = document.createElement('li');
        toolsUl.appendChild(patrolEntirePageLi);
        patrolEntirePageLi.id = 't-patrolentirepagescript';
        patrolEntirePageLi.className = 'mw-list-item';

        patrolEntirePageA = document.createElement('a');
        patrolEntirePageLi.appendChild(patrolEntirePageA);
        patrolEntirePageA.innerHTML = 'Patrol entire page';
        patrolEntirePageA.href = 'javascript:;';
        patrolEntirePageA.title =
            'Use this to patrol all edits on this page. ' +
            'This makes use of the recent changes, ' +
            'so edits older than the recent changes limit will not be patrolled.';
        patrolEntirePageA.onclick = fetchUnpatrolledEdits;
    }

    // Obtains a list of unpatrolled edits from the recent changes.
    // That is the only possible place to fetch unpatrolled edits from.
    function fetchUnpatrolledEdits() {
        if(!canPatrol()) {
            patrolEntirePageA.innerHTML = 'Patrol entire page: no permissions!';
            patrolEntirePageA.title = 'This user\'s patrol token is too short, which means this user has no patrolling permissions.';
            return;
        }

        showSpinner();

        var api = new mw.Api();

        api.get({
            action: 'query',
            list: 'recentchanges',
            rcshow: '!patrolled',
            rclimit: 500,
            rcprop: 'ids'
        }).then(
            parseUnpatrolledEdits
        ).catch(
            handlePatrolError
        );
    }

    // Shows a spinner in place of the button, and hides the button.
    function showSpinner() {
        if(patrolEntirePageSpinner == null) {
            if($.createSpinner === undefined) {
                patrolEntirePageSpinner = document.createElement('span');
                patrolEntirePageSpinner.innerHTML = '...';
            } else {
                patrolEntirePageSpinner = $.createSpinner({size:'small', type:'inline'});
            }
            $(patrolEntirePageA).after(patrolEntirePageSpinner);
        }
        $(patrolEntirePageSpinner).css('display', '');
        $(patrolEntirePageA).css('display', 'none');
    }

    // Shows the button, and hides the spinner if any.
    function showButton() {
        if(patrolEntirePageSpinner != null) {
            $(patrolEntirePageSpinner).css('display', 'none');
        }
        $(patrolEntirePageA).css('display', '');
    }

    // Parses the list of unpatrolled edits received from the API.
    // It also prepares the button for the next step.
    function parseUnpatrolledEdits(data) {
        // DEBUG:
        // console.log(data);

        for(var e = 0; e < data.query.recentchanges.length; e++) {
            if(data.query.recentchanges[e].pageid != mw.config.values.wgArticleId) continue;
            revsToPatrol.push(data.query.recentchanges[e]);
        }

        if(revsToPatrol.length == 0) {
            patrolEntirePageA.innerHTML = 'Patrol entire page: no edits to patrol';
            patrolEntirePageA.title = 'All of the edits on this page have already been patrolled.';
            patrolEntirePageA.onclick = undefined;
            showButton();
            return;
        }

        var tooltip = 'Click here to confirm that you want to patrol all changes in this page. RC IDs: ';
        for(var r = 0; r < revsToPatrol.length; r++) {
            tooltip += revsToPatrol[r].rcid + ', ';
        }
        tooltip = tooltip.substr(0, tooltip.length - 2) + '.';
        patrolEntirePageA.innerHTML = 'Patrol entire page: are you sure? (' + revsToPatrol.length + ' edit' + (revsToPatrol.length > 1 ? 's' : '') + '.)';
        patrolEntirePageA.title = tooltip;
        patrolEntirePageA.onclick = doPatrol;
        showButton();
    }

    // Starts the full patrolling process.
    function doPatrol() {
        showSpinner();

        for(var r = 0; r < revsToPatrol.length; r++) {
            var api = new mw.Api();
            var rcid = revsToPatrol[r].rcid;

            console.log('MediaWiki patrol entire page script -- patrolling RC ID ' + rcid + '.');

            // DEBUG:
            // handleRevPatrolled();

            api.postWithToken(
                'patrol',
                {
                    formatversion: 2,
                    action: 'patrol',
                    rcid: rcid
                }
            ).then(
                handleRevPatrolled
            ).catch(
                handlePatrolError
            );

        }
    }

    // Handles one of the revisions being successfully patrolled.
    function handleRevPatrolled(data) {
        revsPatrolled++;
        if(revsPatrolled == revsToPatrol.length) {
            patrolEntirePageA.innerHTML = 'Patrol entire page: finished!';
            patrolEntirePageA.title = 'The script has finished patrolling all unpatrolled edits (' + revsPatrolled + ').';
            patrolEntirePageA.onclick = undefined;
            showButton();
            return;
        }
    }

    // Handles an error while trying to patrol a revision.
    function handlePatrolError(code, data) {
        showButton();
        var api=new mw.Api();
        mw.notify(
            api.getErrorMessage(data),
            {type: code === 'noautopatrol' ? 'warn' : 'error'}
        );
    }

    createPatrolEntirePageButton();
});