Jump to content

User:Amalthea/MakeRollbacker.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
MakeRollbacker = function(name)
{
    if (typeof(MakeRollbackerConfig) == "undefined") MakeRollbackerConfig = {};
    var groupReason = MakeRollbackerConfig.groupReason || "Trusted user";
    var sectionHeader = MakeRollbackerConfig.sectionHeader || "Rollback";
    var sectionBody = MakeRollbackerConfig.sectionBody || ("{{subst:"+"User:HJ Mitchell/Rollback}}~~"+"~~");
    
    var user;
    var user = (mw.config.get('wgTitle').indexOf('/') != -1) ? (mw.config.get('wgTitle').split('/')[0]) : mw.config.get('wgTitle');
    var eUser = encodeURIComponent(user.replace(/ /g, "_"));
    
    jsMsg("Getting tokens");
    
    var req = sajax_init_object();
    req.open("GET", mw.config.get('wgScriptPath') + "/api.php?format=json&action=query&list=users&ustoken=userrights&ususers="+eUser+"&prop=info&intoken=edit&indexpageids&titles=User%20talk:"+eUser, false);
    req.send("format=json&action=userrights&user=" + eUser + "&add=rollbacker&bot=1&reason=" + encodeURIComponent(groupReason) + "&token=" + encodeURIComponent(userrightstoken));
    var info = eval("(" + req.responseText + ")");
    if (info.error)
    {
        jsMsg("Error when getting the tokens");
        return false;
    }
    
    var userrightstoken = info.query.users[0].userrightstoken;
    var edittoken = info.query.pages[info.query.pageids[0]].edittoken;
    

    jsMsg("Setting bit");

    var req = sajax_init_object();
    var postdata = "format=json&action=userrights&user=" + eUser + "&add=rollbacker&token=" + encodeURIComponent(userrightstoken)+"&reason=" + encodeURIComponent(groupReason);
    req.open("POST", mw.config.get('wgScriptPath') + "/api.php", true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", postdata.length);
    req.onreadystatechange = function()
    {
        if(req.readyState == 4 && req.status == 200)
        {
            var info = eval("(" + req.responseText + ")");
            if (info.error)
            {
                jsMsg("Error when setting the group on "+user);
                return false;
            }
            
            if (info.userrights.added.length==0)
            {
                jsMsg("User already is in the Rollbacker group");
                return false;
            }

            jsMsg("Notifying user");

            var req2 = sajax_init_object();
            var postdata2 = "format=json&action=edit&title=User_talk:" + eUser + "&section=new&notminor=1&summary=" + encodeURIComponent(sectionHeader) + "&text="+encodeURIComponent(sectionBody)+"&token=" + encodeURIComponent(edittoken);
            req2.open("POST", mw.config.get('wgScriptPath') + "/api.php", true);
            req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            req2.setRequestHeader("Content-length", postdata2.length);
            req2.onreadystatechange = function()
            {
                if(req2.readyState == 4 && req2.status == 200)
                {
                    var info = eval("(" + req2.responseText + ")");
                    if (info.error)
                    {
                        jsMsg("Error when setting the group on "+user);
                        return false;
                    }
                    jsMsg("Done making "+user+" a Rollbacker");
                }
            };
            req2.send(postdata2);
        }
    };
    req.send(postdata);
}
 
if( mw.config.get('wgNamespaceNumber') == 2 || mw.config.get('wgNamespaceNumber') == 3 )
{
    addOnloadHook(function(){addPortletLink( "p-cactions", "javascript:MakeRollbacker()", (typeof(MakeRollbackerConfig)!="undefined"&&MakeRollbackerConfig.portletTitle)||"+rollback", "portlet-make-rollbacker");});
}