Jump to content

User:Amalthea/MakeReviewer.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Amalthea (talk | contribs)
m capitalization
Amalthea (talk | contribs)
Don't notify if he already is a reviewer
Line 41: Line 41:
{
{
jsMsg("Error when setting the group on "+user);
jsMsg("Error when setting the group on "+user);
return false;
}
if (info.userrights.added.length==0)
{
jsMsg("User already is in the Reviewer group");
return false;
return false;
}
}

Revision as of 09:05, 21 June 2010

MakeReviewer = function(name)
{
    if (typeof(MakeReviewerConfig) == "undefined") MakeReviewerConfig = {};
    var groupReason = MakeReviewerConfig.groupReason || "User can be trusted with Reviewer";
    var sectionHeader = MakeReviewerConfig.sectionHeader || "You are now a Reviewer";
    var sectionBody = MakeReviewerConfig.sectionBody || ("{{subst:"+"reviewer-notice}} ~~"+"~~");
    
    var user;
    var user = (wgTitle.indexOf('/') != -1) ? (wgTitle.split('/')[0]) : wgTitle;
    var eUser = encodeURIComponent(user.replace(/ /g, "_"));
    
    jsMsg("Getting tokens");
    
    var req = sajax_init_object();
    req.open("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=reviewer&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=reviewer&token=" + encodeURIComponent(userrightstoken)+"&reason=" + encodeURIComponent(groupReason);
    req.open("POST", 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 Reviewer 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", 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 Reviewer");
                }
            };
            req2.send(postdata2);
        }
    };
    req.send(postdata);
}
 
if( wgNamespaceNumber == 2 || wgNamespaceNumber == 3 )
{
    addOnloadHook(function(){addPortletLink( "p-cactions", "javascript:MakeReviewer()", "Make Reviewer", "portlet-make-reviewer");});
}