User:PhantomTech/scripts/massRollback.js

//Mass rollback function //Forked by PhantomTech from User:Writ Keeper/Scripts/massRollback.js at Special:Permalink/1078677422 //Written by John254 and modified/rewritten by Writ Keeper with modifications by TheDJ; original is at https://en.wikipedia.org/wiki/User:John254/mass_rollback.js //Adapted from User:Mr.Z-man/rollbackSummary.js //Instructions: Selecting the "rollback all" tab when viewing a user's contributions history //will open all rollback links displayed there. (Use with caution) if(typeof wkContribsCheckboxInit === "undefined") {   wkContribsCheckboxInit = false; }

function rollbackEverythingWKMR(editSummary) {   if(editSummary === null) {       return false; }   mw.loader.using( 'mediawiki.api' ).done( function    {        const rbMetadata = {};        rbMetadata.api = new mw.Api;        rbMetadata.userName = mw.config.get("wgRelevantUserName");        rbMetadata.ipRange = (rbMetadata.userName === null);        rbMetadata.titleRegex = /title=([^&]+)/;        rbMetadata.editSummary = editSummary;        $("a[href*='action=rollback']").each(function(ind, el) {           rollbackOneThingWKMR(el, rbMetadata); } );   } );    return false; } function rollbackSomeThingsWKMR(editSummary) {   if(editSummary === null) {       return false; }   mw.loader.using( 'mediawiki.api' ).done( function    {        const rbMetadata = {};        rbMetadata.api = new mw.Api;        rbMetadata.userName = mw.config.get("wgRelevantUserName");        rbMetadata.titleRegex = /title=([^&]+)/;        rbMetadata.editSummary = editSummary;        const rollbackList = $("input.revdelIds:checked").parents("li.mw-contributions-current").find("a[href*='action=rollback']");        if(rollbackList.length <= 0)        {            mw.notify("You didn't select any edits that could be rolled back!");            return;        }        rollbackList.each(function(ind, el) {           rollbackOneThingWKMR(el, rbMetadata); } );   } ); } function rollbackOneThingWKMR(edit, rbMetadata) {   let userName; //if we're in an anonymous IP range, we have to figure out each username on its own, since they might be different for each edit. if(rbMetadata.userName === null) {       //the not clause is probably not necessary, but I'm including it just to be sure. userName = $(edit).parents("li:first").children("a.mw-anonuserlink").not(".mw-contributions-title").text; }   else {       userName = rbMetadata.userName; }   const params = {}; if( rbMetadata.editSummary !== '' ) {       params.summary = rbMetadata.editSummary; }   rbMetadata.api.rollback( decodeURIComponent(rbMetadata.titleRegex.exec(edit.href)[1]), userName, params).done( function    {        $(edit).after("reverted");        $(edit).remove;    } ); } $(document).ready(function {   if(mw.config.get("wgCanonicalSpecialPageName") === "Contributions" && $("span.mw-rollback-link").length > 0)    {        mw.loader.using("mediawiki.util").done( function {           mw.util.addPortletLink('p-cactions', '#', "rollback all", "ca-rollbackeverything", "rollback all edits displayed here"); if(!wkContribsCheckboxInit) {               const revdelLinks = $("ul.mw-contributions-list .mw-revdelundel-link"); if(revdelLinks.length > 0) {                   const revIdReg = /ids=(\d+)/; revdelLinks.each(function(ind,el){                       if($(this).children("a").length > 0)                        {                            const revId = revIdReg.exec($(this).children("a").attr("href"))[1];                            const pageTitle = /target=([^&]+)/.exec($(this).children("a").attr("href"))[1];                            $(el).prepend(" ");                            $(el).children(".revdelIds").data("index", ind);                        }                    }); }               else {                   const revIdReg = /oldid=(\d+)/; $("ul.mw-contributions-list a.mw-changeslist-date").each(function(ind,el) {                       const revId = revIdReg.exec($(this).attr("href"))[1];                        const pageTitle = /title=([^&]+)/.exec($(this).attr("href"))[1];                        $(el).before(" ");                    }); $("ul.mw-contributions-list .revdelIds").each(function(ind,el){                       $(el).data("index", ind);                    }); }               wkContribsCheckboxInit = true; }           mw.util.addPortletLink('p-cactions', '#', "rollback selected", "ca-rollbacksome", "rollback selected edits"); $("#ca-rollbackeverything").click( function(event)           {                event.preventDefault;                mw.loader.load( 'mediawiki.api' ); //start loading, while the user is in the prompt                return rollbackEverythingWKMR(prompt("Rollback all edits: Enter an edit summary, or leave blank to use the default (or hit Cancel to cancel the rollback entirely)"));            }); const rollbackSome = $("#ca-rollbacksome"); rollbackSome.click( function(event)           {                event.preventDefault;                mw.loader.load( 'mediawiki.api' ); //start loading, while the user is in the prompt                return rollbackSomeThingsWKMR(prompt("Rollback selected edits: Enter an edit summary, or leave blank to use the default (or hit Cancel to cancel the rollback entirely)"));            }); rollbackSome.data("lastSelectedIndex", -1);

$("input.revdelIds").off("click").click(               function(ev)                {                    let i;                    const lastSelectedRevdel = rollbackSome.data("lastSelectedIndex");                    const newIndex = $(this).data("index");                    if(ev.shiftKey && lastSelectedRevdel >= 0)                    {                        const checkboxArray = $("input.revdelIds");                        const start = lastSelectedRevdel;                        const stop = newIndex;                        const checkedVal = $(checkboxArray[start]).prop("checked");                        if(start < stop)                        {                            for(i = start; i <= stop; i++)                            {                                $(checkboxArray[i]).prop("checked", checkedVal);                            }                        }                        else {                           for(i = start; i >= stop; i--) {                               $(checkboxArray[i]).prop("checked", checkedVal); }                       }                    }                    rollbackSome.data("lastSelectedIndex", newIndex); });

});   } });