MediaWiki:Gadget-Smart patrol.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez 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
* {{Projet:JavaScript/Script|Smart patrol}}
*/

if ( mw.config.get('wgNamespaceNumber') >= 0 ) {

    mw.loader.using( 'mediawiki.util', function () {
        $( function ( $ ) {

            var link = mw.util.addPortletLink( 'p-cactions', '#', 'Smart patrol', 'ca-smart-patrol' );
            $( link ).click( function ( e ) {
                e.preventDefault();
                mw.loader.using( [ 'mediawiki.api', 'oojs-ui' ], function () {
                    askToLaunch();
                } );
            } );

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

            function getAndProcessRevisions( apicontinue ) {
                var params = {
                    'action': 'query',
                    'format': 'json',
                    'prop': 'revisions',
                    'titles': mw.config.get( 'wgPageName' ),
                    'formatversion': 2,
                    'rvprop': 'ids',
                    'rvlimit': 50
                };
                if ( apicontinue ) {
                    Object.assign( params, apicontinue );
                }
                new mw.Api()
                    .get( params )
                    .then( function ( data ) {
                        var revisions = data.query.pages[ 0 ].revisions;
                        markRevisionsAsPatrolled( revisions )
                            .then( function ( shouldContinue ) {
                                if ( shouldContinue && data[ 'continue' ] ) {
                                    getAndProcessRevisions( data[ 'continue' ] );
                                } else {
                                    window.location.reload();
                                }
                            } )
                            .fail( function ( error ) {
                                mw.notify( 'Something went wrong: ' + error, { title: 'Smart Patrol', type: 'error' } );
                            } );
                    } )
                    .fail( function ( error ) {
                        mw.notify( 'Something went wrong: ' + error, { title: 'Smart Patrol', type: 'error' } );
                    } );
            }

            function markRevisionsAsPatrolled( revisions ) {
                var deferred = $.Deferred();
                markOneAsPatrolled( revisions, 0, deferred );
                return deferred.promise();
            }

            function markOneAsPatrolled( revisions, index, deferred ) {
                var revid = revisions[ index ].revid;
                new mw.Api()
                    .postWithToken( 'patrol', {
                        'action': 'patrol',
                        'revid': revid
                    } )
                    .then( function ( info ) {
                        console.log( 'Successfully patrolled: ' + revid );
                        if ( revisions[ index + 1 ] ) {
                            markOneAsPatrolled( revisions, index + 1, deferred );
                        } else {
                            deferred.resolve( true );
                        }
                    } )
                    .fail( function ( error ) {
                        if ( error === 'notpatrollable' ) {
                            console.log( "Can't be patrolled as it's too old: " + revid );
                            deferred.resolve( false );
                        } else {
                            deferred.reject( error );
                        }
                    } );
            }

        } );
    } );
}