User:Eraserhead1/rfppClerk.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:Eraserhead1/rfppClerk. |
/**
* Semi-automated clerking of [[WP:RFPP]].
* See [[User:Rami R/rfppClerk]] for instructions.
*/
var pr_timeout=6;//time until being moved to fulfilled section (in hours)
var fr_timeout=12;//time until being removed from fulfilled section (in hours)
var rpc_now;//will hold the current date
var rpc_offset=0;//timezone offset to GMT/UTC (in mili-seconds)
var rpc_backlog=10;//backlog threshold
var rpc_nobacklog=4;//backlog cleared threshold
$(function () {
if(wgPageName=="Wikipedia:Requests_for_page_protection" &&
(wgAction=='edit' || wgAction=='submit'))
addPortletLink('p-tb', 'javascript:rfppClerk()', 'RFPP clerk',
't-rfpp-clerk', 'WP:RFPP semi-automatic clerk', '', '');
});
function rfppClerk(){
var text = document.getElementById('wpTextbox1').value;
var pr_start=text.indexOf("\n====",text.search(/==\s*Current requests for protection\s*==/));
var pr_end=text.search(/\n==\s*Current requests for unprotection\s*==/);
var ur_start=text.indexOf('\n====',pr_end);
var ur_end=text.search(/\n==\s*Current requests for edits to a protected pa/);
var rfse_start=text.indexOf('\n====',ur_end);
var rfse_end=text.search(/\n==\s*Fulfilled/denied requests\s*==/);
var fr_start=rfse_end+30;
if(pr_start>pr_end || pr_start<0) pr_start=pr_end;
if(ur_start>ur_end || ur_start<0) ur_start=ur_end;
if(rfse_start>rfse_end || rfse_start<0) rfse_start=rfse_end;
rpc_now=new Date();
rpc_offset=rpc_now.getTimezoneOffset()*60000;
var pr_section=text.substring(pr_start,pr_end);
var pr_ls=text2list(pr_section);
var ur_section=text.substring(ur_start,ur_end);
var ur_ls=text2list(ur_section);
var rfse_section=text.substring(rfse_start,rfse_end);
var rfse_ls=text2list(rfse_section);
var fr_section=text.substring(fr_start);
var fr_ls=text2list(fr_section);
var tmp;
var from_pr_ls;
var from_ur_ls;
var from_rfse_ls;
var from_fr_ls;
var pending;
var backlogmsg="";
tmp=findExpired(pr_ls,pr_timeout);
pending=tmp[0];
from_pr_ls=tmp[1];
pr_ls=tmp[2];
tmp=findExpired(ur_ls,pr_timeout);
pending+=tmp[0];
from_ur_ls=tmp[1];
ur_ls=tmp[2];
tmp=findExpired(rfse_ls,pr_timeout);
pending+=tmp[0];
from_rfse_ls=tmp[1];
rfse_ls=tmp[2];
tmp=findExpired(fr_ls,fr_timeout);
pending+=tmp[0];
from_fr_ls=tmp[1];
fr_ls=tmp[2];
var end_text= text.substring(0,pr_start);
for(var x=0;x<pr_ls.length;x++)
end_text+=pr_ls[x];
end_text+=text.substring(pr_end,ur_start);
for(var x=0;x<ur_ls.length;x++)
end_text+=ur_ls[x];
end_text+=text.substring(ur_end,rfse_start);
for(var x=0;x<rfse_ls.length;x++)
end_text+=rfse_ls[x];
end_text+=text.substring(rfse_end,fr_start);
for(var x=0;x<from_pr_ls.length;x++)
end_text+=from_pr_ls[x];
for(var x=0;x<from_ur_ls.length;x++)
end_text+=from_ur_ls[x];
for(var x=0;x<from_rfse_ls.length;x++)
end_text+=from_rfse_ls[x];
for(var x=0;x<fr_ls.length;x++)
end_text+=fr_ls[x];
//backlog?
if(pending>=rpc_backlog){
if(text.indexOf("{{noadminbacklog}}")>0){
end_text=end_text.replace("{{noadminbacklog}}","{{adminbacklog}}");
backlogmsg=", noticeboard is backlogged";
}
}
else if(pending<=rpc_nobacklog && text.indexOf("{{adminbacklog}}")>0){
end_text=end_text.replace("{{adminbacklog}}","{{noadminbacklog}}");
backlogmsg=", backlog cleared";
}
document.getElementById('wpTextbox1').value=end_text;
//now for the edit summary:
document.getElementById('wpSummary').value=
"Semi-auto-clerking: [PR: "+from_pr_ls.length+
" | UR: "+from_ur_ls.length+
" | RfSE: "+from_rfse_ls.length+
" | FR: "+from_fr_ls.length+
"] ("+pending+" reports pending"+
backlogmsg+")";
}
function findExpired(ls,timeout){
var noresponse=0;
var done_ls=new Array();
var not_done_ls=new Array();
for(var x=0;x<ls.length;x++){
//first check if section has received a formal admin response
if(ls[x].toLowerCase().indexOf('{{rfpp|')>0){
if((rpc_now-getLastDate(ls[x])+rpc_offset)>timeout*3600000)
done_ls.push(ls[x]);
else
not_done_ls.push(ls[x]);
}
else{
noresponse++;
not_done_ls.push(ls[x]);
}
}
return [noresponse, done_ls, not_done_ls];
}
function getLastDate(section){
var pat=/(\d\d):(\d\d), (\d\d?) (\S+) (\d\d\d\d) \(UTC\)/g;
var result=0;
var dates=section.match(pat);
for(var x=0;x<dates.length;x++){
var date=parseDate(dates[x]);
if(date>result)
result=date;
}
return result;
}
/**
* parses the date.
* @param txt the date as a string. expected to match standard en.wiki format
* @return the date as a Date object
*/
function parseDate(txt){
var hhmm=txt.substring(0,5);
var second_space=txt.indexOf(' ',7);
var third_space=txt.indexOf(' ',second_space+1);
var day=txt.substring(7,second_space);
var month=txt.substring(second_space,third_space);
var year=txt.substring(third_space+1,third_space+5);
return new Date(month+" "+day+", "+year+" "+hhmm+":00");
}
/**
* breakup a large text section into its subsections
* (assumed to be 4th level).
* @param text the text.
* @return an array of subsections.
*/
function text2list(text){
var list=new Array();
var start_index=text.indexOf('\n====');
var end_index=text.indexOf('\n====',start_index+1);;
while(end_index>0){
list.push(text.substring(start_index,end_index));
start_index=end_index;
end_index=text.indexOf('\n====',start_index+1);
}
if(start_index>=0)
list.push(text.substring(start_index));
return list;
}