Jump to content

User:Rcsprinter123/rfppclerk.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.
/**
 * 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
var response_open_type={'ch':true,'chck':true,'q':true,'ques':true,'n':true,'note':true};
 
$(function () {
	if(wgPageName=="Wikipedia:Requests_for_page_protection" &&
			(wgAction=='edit' || wgAction=='submit' || wgAction=='view' || wgAction=''))
		mw.util.addPortletLink('p-tb', 'javascript:rfppclerk()', 'Automatic clerking',
			'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=text.indexOf('==',rfse_end+2)+2;
        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("{{adminbacklog}}")<0){
                        if(text.indexOf("{{noadminbacklog}}")>0)
			        end_text=end_text.replace("{{noadminbacklog}}","{{adminbacklog}}");
                        else{
                                var firstSection=end_text.indexOf("\n==");
                                end_text=end_text.substring(0,firstSection)+"<noinclude>{{adminbacklog}}</noinclude>"+end_text.substring(firstSection);
                        }
			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-automatic 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++){
		//check if section has received a formal admin response 
		if(isClosed(ls[x])){
			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 isClosed(section){
	var responses=section.match(/\{\{rfpp\|\w*(?=\||\}\})/gi);
	if(responses==null)
		return false;
	for(var x=0;x<responses.length;x++){
		if(!response_open_type[responses[x].substr(7)])
			return true;
	}
	return false;
}
 
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;
}