User:Samuele2002/Mass blocking tool.js

From Wikipedia

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.
/**
 * Copy from [[w:User:Timotheus Canens/massblock.js]] and [[mw:User:Infinite0694/Mass blocking tool.js]]
 * https://en.wikipedia.org/wiki/User:Timotheus_Canens/massblock.js
 * [[w:Special:PermaLink/676016015]]
 * @license 
 * @author [[w:en:User:Timotheus Canens]]
 */
//Tim's mass blocking tool
//Adapted from [[User:Animum/massdelete.js]]

function doMassBlock() {
    document.getElementById("wpMassBlockSubmit").disabled = true;
    var users = document.getElementById("wpMassBlockUsers").value.split("\n");
    if(users.length == 0) return;
    var wpMassBlockReasons = document.getElementById("wpMassBlockReasons").value, wpMassBlockReason = document.getElementById("wpMassBlockReason").value, blocked = 0, talkpageedited = 0, userpageedited = 0, failed = new Array(), error = new Array();
    var wpMassBlockAnononly = document.getElementById("wpMassBlockAnononly").checked, wpMassBlockNocreate = document.getElementById("wpMassBlockNocreate").checked, wpMassBlockEmail = document.getElementById("wpMassBlockEmail").checked, wpMassHideName = document.getElementById("wpMassHideName").checked, wpMassBlockAutoblock = document.getElementById("wpMassBlockAutoblock").checked, wpMassBlockTalkpage = document.getElementById("wpMassBlockTalkpage").checked, wpMassBlockReblock = document.getElementById("wpMassBlockReblock").checked;
    var wpMassBlockMessage = document.getElementById("wpMassBlockMessage").value, wpMassBlockTag = document.getElementById("wpMassBlockTag").value, wpMassBlockExpiry = document.getElementById("wpMassBlockExpiry").value, wpMassBlockExpiry1 = document.getElementById("wpMassBlockExpiry1").value ;
    var wpMassBlockSummaryTalk = document.getElementById("wpMassBlockSummaryTalk").value, wpMassBlockSummaryUser = document.getElementById("wpMassBlockSummaryUser").value, wpMassBlockExpiry = document.getElementById("wpMassBlockExpiry").value, wpMassBlockExpiry1 = document.getElementById("wpMassBlockExpiry1").value  ;
    var blocksettingstring = "&expiry=" + encodeURIComponent(wpMassBlockExpiry == ""? wpMassBlockExpiry1 : wpMassBlockExpiry) + (wpMassBlockAnononly ? "&anononly=1" : "") + (wpMassBlockNocreate ? "&nocreate=1" : "") + (wpMassBlockAutoblock ? "&autoblock=1" : "") + (wpMassBlockEmail ? "&noemail=1" : "") + (wpMassHideName ? "&hidename=1" : "") + (wpMassBlockTalkpage ? "" : "&allowusertalk=1") + (wpMassBlockReblock ? "&reblock=1" : "");
    for(i=0;i<users.length;i++) {
        var user = users[i];
        if(user.length > 0) {
            var req = new XMLHttpRequest();
            req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=info&intoken=block&titles=User:" + encodeURIComponent(user), false);
            req.send(null);
            var response = eval("(" + req.responseText + ")").query.pages;
            for(var index in response) {
                var info = response[index];
                var blocktoken = info.blocktoken;
                var postdata = "format=json"
                             + "&action=block"
                             + "&user=" + encodeURIComponent(user)
                             + "&reason=" + encodeURIComponent(wpMassBlockReasons == "other" ? wpMassBlockReason : wpMassBlockReasons + (wpMassBlockReason ? ": " + wpMassBlockReason : ""))
                             +  blocksettingstring
                             + "&token=" + encodeURIComponent(blocktoken);
                var req = new XMLHttpRequest();
                req.open("POST", wgScriptPath + "/api.php", false);
                req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                req.setRequestHeader("Content-length", postdata.length);
                req.send(postdata);
                if(eval("(" + req.responseText + ")")['block']) { //If blockd, update the blockd count and the button.
                    blocked++;
		    // now edit user and user talk.
                    if(wpMassBlockMessage != "") {
                        var postdata = "format=json&action=edit&watchlist=nochange&title=User%20talk:" + encodeURIComponent(user) + "&summary=" + encodeURIComponent(wpMassBlockSummaryTalk) 
                             + "&appendtext=" + encodeURIComponent(wpMassBlockMessage)
                             + "&token=" + encodeURIComponent(blocktoken);
                        var req = new XMLHttpRequest();
                        req.open("POST", wgScriptPath + "/api.php", false);
                        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        req.setRequestHeader("Content-length", postdata.length);
                        req.send(postdata);
                        if(eval("(" + req.responseText + ")")['edit']) { talkpageedited ++; }
                        else { //If not edited, add the title to the "failed" array and a description of the error to the "error" array.
                            failed.push("User talk:" + user);
                            error.push(eval("(" + req.responseText + ")").error.info);
                        }
		    }
                    if(wpMassBlockTag != "") {
                        var postdata = "format=json&action=edit&watchlist=nochange&title=User:" + encodeURIComponent(user) + "&summary=" + encodeURIComponent(wpMassBlockSummaryUser) 
                             + "&text=" + encodeURIComponent(wpMassBlockTag)
                             + "&token=" + encodeURIComponent(blocktoken);
                        var req = new XMLHttpRequest();
                        req.open("POST", wgScriptPath + "/api.php", false);
                        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        req.setRequestHeader("Content-length", postdata.length);
                        req.send(postdata);
                        if(eval("(" + req.responseText + ")")['edit']) { userpageedited ++; }
                        else { //If not edited, add the title to the "failed" array and a description of the error to the "error" array.
                            failed.push("User talk:" + user);
                            error.push(eval("(" + req.responseText + ")").error.info);
                        }
		    }
                    document.getElementById("wpMassBlockSubmit").value = "(" + blocked + "/" + talkpageedited + "/" + userpageedited + ")";
                } else { //If not blocked, add the title to the "failed" array and a description of the error to the "error" array.
                    failed.push("Special:Block/" + user);
                    error.push(eval("(" + req.responseText + ")").error.info);
                }
            }
        }
        if(!users[i+1]) {
            document.getElementById("wpMassBlockSubmit").value = "(" + blocked + "/" + talkpageedited + "/" + userpageedited + ")";
            if(failed.length > 0) {
                var linkedList = "";
                for(x=0; x<failed.length; x++) {
                    linkedList += "<li><a href=\"" + mw.config.get('wgScript') + "?title=" + encodeURIComponent(failed[x]) + "\">" + failed[x] + "</a>: " + error[x] + "</li>"; //Links the titles in the "failed" array
                }
                document.getElementById("wpMassBlockFailedContainer").innerHTML += '<br /><b>Failed actions:</b><ul>' + linkedList + '</ul>';
            }
            
        }
    }
}
   function fchk5(obj){
        var frm=obj.form;
        var len=obj.options.length; /* Numero di elementi a discesa */
        if(obj.selectedIndex==eval(len-1)){
            /*
               Abilita campo di input
            */
            frm.elements["wpMassBlockExpiry1"].disabled=false;
            alert("Si prega di compilare gli altri campi");
        }else{
            /*
                Disabilita il campo di input
            */
            frm.elements["wpMassBlockExpiry1"].value="";
            frm.elements["wpMassBlockExpiry1"].disabled=true;
        }
    }
function massblockform() {
    var bodyContent = (skin == "cologneblue" ? "article" : "bodyContent");
    document.getElementsByTagName("h1")[0].textContent = "Mass block";
    document.title = "Mass-blocking tool - Wikipedia, the free encyclopedia";
    document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">From Wikipedia, the free encyclopedia</h3><br />'
        + '<form id="wpMassBlock" name="wpMassBlock">'  
        + 'Use the form below to block write access from a specific IP address or username. This should be done only to prevent vandalism, and in accordance with <b>policy</b>. Fill in a specific reason below (for example, citing particular pages that were vandalized). You can block IP ranges using the CIDR syntax; the largest allowed range is /16 for IPv4 and /19 for IPv6.<br /><br />'
        + '<b>Se abusi di questo strumento, it\'s <i>your</i> fault.</b>'
        + '<div id="wpMassBlockFailedContainer"></div>'
        + '<br />'
        + '<fieldset><legend>Blocca utenti</legend>'
            + 'Utenti da bloccare (uno per riga, per favore):<br />'
                + '<textarea tabindex="1" accesskey="," name="wpMassBlockUsers" id="wpMassBlockUsers" rows="10" cols="80"></textarea>'
            + 'Messaggio nella pagina di discussione, se esistente (lasciare vuoto per non lasciare alcun messaggio):<br />'
                + '<textarea tabindex="2" accesskey="," name="wpMassBlockMessage" id="wpMassBlockMessage" rows="10" cols="80"></textarea>'
            + 'Sostituisci il testo della pagina utente con (lascia vuoto per non fare nessuna modifica):<br />'
                + '<textarea tabindex="3" accesskey="," name="wpMassBlockTag" id="wpMassBlockTag" rows="10" cols="80"></textarea>'
            + '<br /><br /><table style="background-color:transparent">'
                + '<tr><td>Common reasons:</td>'
                    + '<td><select id="wpMassBlockReasons">'
                        + '<optgroup label="Other reason">'
                            + '<option value="other">Other reason</option>'
                        + '</optgroup>'
                        + '<optgroup label="Motivi comuni">'
                            + '<option value="[[WP:Vandalism|Vandalism]]">[[WP:Vandalism|Vandalism]]</option>'
                            + '<option value="[[WP:Vandalism-only account|Vandalism-only account]] ">[[WP:Vandalism-only account|Vandalism-only account]] </option>'
                            + '<option value="Violations of the [[WP:BLP|Biographies of living persons policy]] ">Violations of the [[WP:BLP|Biographies of living persons policy]] </option>'
                            + '<option value="Using Wikipedia for [[WP:Spam|spam]] or [[WP:Advertising|advertising]] purposes">Using Wikipedia for [[WP:Spam|spam]] or [[WP:Advertising|advertising]] purposes</option>'
                            + '<option value="[[WP:Spam|Spam]] / [[WP:Advertising|advertising]]-only account">[[WP:Spam|Spam]] / [[WP:Advertising|advertising]]-only account</option>'
                            + '<option value="Creating [[WP:Attack page|attack]], [[WP:Patent nonsense|nonsense]] or other inappropriate pages">Creating [[WP:Attack page|attack]], [[WP:Patent nonsense|nonsense]] or other inappropriate pages</option>'
                            + '<option value="Creating [[WP:Copyright violations|copyright violations]]">Creating [[WP:Copyright violations|copyright violations]]</option>'
                            + '<option value="Abusing [[WP:Sock puppetry|multiple accounts]]">Abusing [[WP:Sock puppetry|multiple accounts]]</option>'
                            + '<option value="[[WP:Block#Evasion of blocks|Block evasion]]">[[WP:Block#Evasion of blocks|Block evasion]]</option>'
                            + '<option value="[[WP:No personal attacks|Personal attacks]] or [[WP:Harassment|harassment]]">[[WP:No personal attacks|Personal attacks]] or [[WP:Harassment|harassment]]</option>'
                            + '<option value="[[WP:Edit warring|Edit warring]]">[[WP:Edit warring|Edit warring]]</option>'
                            + '<option value="Violation of the [[WP:Three-revert rule|three-revert rule]] ">Violation of the [[WP:Three-revert rule|three-revert rule]] </option>'
                            + '<option value="[[WP:Disruptive editing|Disruptive editing]]">[[WP:Disruptive editing|Disruptive editing]]</option>'
                            + '<option value="[[WP:Long-term abuse|Long-term abuse]]">[[WP:Long-term abuse|Long-term abuse]]</option>'
                            + '<option value="[[m:No open proxies|Open proxy]] or [[w:Zombie computer|zombie]] ([[m:No open proxies|more info]])">[[m:No open proxies|Open proxy]] or [[w:Zombie computer|zombie]] ([[m:No open proxies|more info]])</option>'
                            + '<option value="[[WP:Arbitration enforcement|Arbitration enforcement]]">[[WP:Arbitration enforcement|Arbitration enforcement]]</option>'
                            + '<option value="Deliberately triggering the [[WP:Edit filter|Edit filter]]">Deliberately triggering the [[WP:Edit filter|Edit filter]]</option>'
                            + '<option value="[[w:ja:LTA:ISECHIKA|ISECHIKA]]">[[w:ja:LTA:ISECHIKA|ISECHIKA]]</option>'
                            + '<option value="[[WP:SOCK|sockpuppet]]">[[WP:SOCK|sockpuppet]]</option>'
                            + '<option value="Revoking talk page access: inappropriate use of user talk page while blocked">Revoking talk page access: inappropriate use of user talk page while blocked</option>'
                            + '<option value="\{\{anonblock}}">\{\{anonblock}}</option>'
                            + '<option value="\{\{schoolblock}}">\{\{schoolblock}}</option>'
                            + '<option value="\{\{blocked proxy}}">\{\{blocked proxy}}</option>'
                            + '<option value="\{\{usernameblock}}">\{\{usernameblock}}</option>'
                            + '<option value="\{\{UsernameHardBlocked}}">\{\{UsernameHardBlocked}}</option>'
                            + '<option value="\{\{spamusernameblock}}">\{\{spamusernameblock}}</option>'
                            + '<option value="\{\{softerblock}}">\{\{softerblock}}</option>'
                            + '<option value="\{\{checkuserblock}}">\{\{checkuserblock}}</option>'
                            + '<option value="\{\{checkuserblock-account}}">\{\{checkuserblock-account}}</option>'
                            + '<option value="\{\{Tor}}">\{\{Tor}}</option>'
                            + '<option value="\{\{Vaublock}}">\{\{Vaublock}}</option>'
                        + '</optgroup>'
                    + '</select></td></tr>'
            + '<tr><td>Other/additional reason:</td>'
                + '<td><input type="text" id="wpMassBlockReason" name="wpMassBlockReason" maxlength="255" size="40"/></td></tr>'
            + '<tr><td>Tempi di scadenza comuni (impostazione predefinita <b>infinito</b>):</td>'
                    + '<td><select id="wpMassBlockExpiry" onchange="fchk5(this);">'
                        + '<optgroup label="Defult time">'
                            + '<option value="indefinite">infinito</option>'
                        + '</optgroup>'
                        + '<optgroup label="Common Expiration time">'
                            + '<option value="2 hours">2 ore</option>'
                            + '<option value="1 day">1 giorno</option>'
                            + '<option value="31 hours">31 ore</option>'
                            + '<option value="2 days">2 giorni</option>'
                            + '<option value="3 days">3 giorni</option>'
                            + '<option value="1 week">1 settimana</option>'
                            + '<option value="2 weeks">2 settimane</option>'
                            + '<option value="1 month">1 mese</option>'
                            + '<option value="180 days">180 giorni</option>'
                            + '<option value="1 year">1 anno</option>'
                            + '<option value="">Altra durata</option>'
                        + '</optgroup>'
                    + '</select></td></tr>'
            + '<tr><td>Expiration time:</td>'
                + '<td><input type="text" id="wpMassBlockExpiry1" name="wpMassBlockExpiry1" maxlength="255"  disabled="disabled" /></td></tr>'
            + '<tr><td>Edit summary for talk page edit:</td>'
                + '<td><input type="text" id="wpMassBlockSummaryTalk" name="wpMassBlockSummaryTalk" maxlength="255" size="40"/></td></tr>'
            + '<tr><td>Edit summary for user page edit:</td>'
                + '<td><input type="text" id="wpMassBlockSummaryUser" name="wpMassBlockSummaryUser" maxlength="255" size="40"/></td></tr>'
                + '<tr><td>Blocca solo utenti anonimi (solo IP):</td><td><input type="checkbox" id="wpMassBlockAnononly" name="wpMassBlockAnononly" /></td></tr>'
                + '<tr><td>Attiva autoblocco (solo utenti):</td><td><input type="checkbox" id="wpMassBlockAutoblock" name="wpMassBlockAutoblock" checked="checked" /></td></tr>'
                + '<tr><td>Blocca la creazione di utenze:</td><td><input type="checkbox" id="wpMassBlockNocreate" name="wpMassBlockNocreate" checked="checked" /></td></tr>'
                + '<tr><td>Impedisci di inviare email:</td><td><input type="checkbox" id="wpMassBlockEmail" name="wpMassBlockEmail" /></td></tr>'
                + '<tr><td>Nascondi utente:</td><td><input type="checkbox" id="wpMassHideName" name="wpMassHideName" /></td></tr>'
                + '<tr><td>Impedisci di modificare la propria pagina di discussione</td><td><input type="checkbox" id="wpMassBlockTalkpage" name="wpMassBlockTalkpage" /></td></tr>'
                + '<tr><td><b>Sostituisci blocchi già attivi:</b></td><td><input type="checkbox" id="wpMassBlockReblock" name="wpMassBlockReblock" /></td></tr>'
                + '<tr><td><input type="button" id="wpMassBlockSubmit" name="wpMassBlockSubmit" value="Block" onclick="doMassBlock()" /></td></tr></fieldset>'
        + '</form>';
    document.getElementById("wpMassBlockReasons").onchange = function() {
        var maxlength = (document.getElementById("wpMassBlockReasons").value == "other" ? 255 : 253-document.getElementById("wpMassBlockReasons").value.length); //It's 25e because of the two characters (": ") in addition to the selected summary.
        document.getElementById("wpMassBlockReason").setAttribute("maxlength", maxlength);
    }
}
 
if(mw.config.get('wgNamespaceNumber') == -1 && (mw.config.get('wgPageName') == "Special:BlankPage/Massblock") && /autoconfirmed/.test(mw.config.get('wgUserGroups'))) addOnloadHook(massblockform);

	$.when(mw.loader.using('mediawiki.util'), $.ready).then(function () {
	    mw.util.addPortletLink(
	    'p-tb',
	    mw.util.getUrl('Special:BlankPage/Massblock'),
	    'Mass Block'
	    );
	});