User:Amalthea/MakeReviewer.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Amalthea/MakeReviewer. |
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 = (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=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", 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 Reviewer group");
return false;
}
jsMsg("Notifying user");
var req2 = sajax_init_object();
var postdata2 = "format=json&action=edit&title=User_talk:" + eUser + "§ion=new¬minor=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 Reviewer");
}
};
req2.send(postdata2);
}
};
req.send(postdata);
}
if( mw.config.get('wgNamespaceNumber') == 2 || mw.config.get('wgNamespaceNumber') == 3 )
{
addOnloadHook(function(){addPortletLink( "p-cactions", "javascript:MakeReviewer()", (typeof(MakeReviewerConfig)!="undefined"&&MakeReviewerConfig.portletTitle)||"+reviewer", "portlet-make-reviewer");});
}