Jump to content

User:Timotheus Canens/spihelper.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.
//Tim's SPI helper script
//v.1.6.9
//Adapted from [[User:Mr.Z-man/closeAFD]]
importScript('User:Timotheus Canens/displaymessage.js');

if (!Array.prototype.indexOf) {  
	Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {  
		"use strict";  
		if (this === void 0 || this === null) {  
			throw new TypeError();  
		}  
		var t = Object(this);  
		var len = t.length >>> 0;  
		if (len === 0) {  
			return -1;  
		}  
		var n = 0;  
		if (arguments.length > 0) {  
			n = Number(arguments[1]);  
			if (n !== n) { // shortcut for verifying if it's NaN  
				n = 0;  
			} else if (n !== 0 && n !== Infinity && n !== -Infinity) {  
				n = (n > 0 || -1) * Math.floor(Math.abs(n));  
			}  
		}  
		if (n >= len) {  
			return -1;  
		}  
		var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);  
		for (; k < len; k++) {  
			if (k in t && t[k] === searchElement) {  
				return k;  
			}  
		}  
		return -1;  
	};  
}

if(!String.prototype.trim)
{
	String.prototype.trim = function(){
		return this.replace(/^\s+/, '').replace(/\s+$/, '');
	};
}

if (mw.config.get('wgPageName').indexOf('Wikipedia:Sockpuppet_investigations/') != -1
		&& mw.config.get('wgPageName').indexOf('Wikipedia:Sockpuppet_investigations/SPI/') == -1
		&& mw.config.get('wgPageName').indexOf('/Archive') == -1) {
	var spiHelper_PageName = mw.config.get('wgPageName').replace(/_/g, ' ');
	var spiHelper_CaseName = spiHelper_PageName.replace(/Wikipedia:Sockpuppet investigations\//g, '');
	var spiHelper_AJAXnumber = 0;
	var spiHelper_isAdmin = (mw.config.get('wgUserGroups').toString().indexOf('sysop') != -1);
	var spiHelper_isCheckuser = (mw.config.get('wgUserGroups').toString().indexOf('checkuser') != -1);
	var spiHelper_ActionsSelected = 
	{ CU_act : false, CU_new : false, Block : false, Note: false, Close: false, Archive: false };
	if(typeof(spiHelper_watchArchive) == 'undefined') spiHelper_watchArchive = "preferences";
	if(typeof(spiHelper_watchCase) == 'undefined') spiHelper_watchCase = "preferences";
	var spiHelper_usercount = 0;
	mw.loader.load('mediawiki.user');
	var spiHelper_section_re = /^(?:===[^=]*===|=====[^=]*=====)\s*$/m;

	function spiHelper_init(){

		var pagetext = spiHelper_getPageText(spiHelper_PageName, false);
		if(!spiHelper_section_re.test(pagetext)) return; // Nothing to do here.
		var spicasestatus_re = /\{\{\s*SPI case status\s*\|?\s*(\S*?)\s*\}\}/i;
		var result = spicasestatus_re.exec(pagetext);
		var casestatus = '';
		if(result != null){
			casestatus = result[1];
		}

		var hasCURequest = (casestatus != '' && !(/^(?:close|closed|admin|moreinfo|CUdecline|hold|CUdeclined|clerk)$/i.test(casestatus)));
		var canAddCURequest = (casestatus == '' || /^(?:admin|moreinfo|hold|clerk)$/i.test(casestatus));
		var CUActiondefault = (/^(?:CU|checkuser|request|CUrequest)$/i.test(casestatus));
		var isClosed = /^(?:close|closed)$/i.test(casestatus);

		var text = '<h3>Handling SPI case</h3><ul>';
		if(hasCURequest)
			text += '<li><input type="checkbox"' + (CUActiondefault? ' checked="checked"' : '') + ' name="spiHelper_CU_Action" id="spiHelper_CU_Action" />' +
			'<label for="spiHelper_CU_Action">Act on CU request</label></li>';
		else if(canAddCURequest)
			text += '<li><input type="checkbox" checked="checked" name="spiHelper_Case_Action" id="spiHelper_Case_Action" />' +
			'<label for="spiHelper_Case_Action">Request CU or other action</label></li>';
		text += '<li><input type="checkbox" name="spiHelper_BlockTag" id="spiHelper_BlockTag" />' +
		'<label for="spiHelper_BlockTag">';
		text += spiHelper_isAdmin ? "Block/tag socks" : "Tag socks";
		text += '</label></li><li><input type="checkbox" name="spiHelper_Comment" id="spiHelper_Comment" />' +
		'<label for="spiHelper_Comment">Note/comment</label></li>';
		if(!isClosed)
			text += '<li><input type="checkbox" name="spiHelper_Close" id="spiHelper_Close" onchange="spiHelper_toggleArchive()" />' +
			'<label for="spiHelper_Close">Close case</label></li>';
		else
			text += '<li><input type="checkbox" disabled="true" name="spiHelper_Close" id="spiHelper_Close" />' +
			'<label for="spiHelper_Close">Close case</label></li>';
		if(isClosed)
			text += '<li><input type="checkbox" checked="checked" name="spiHelper_Archive" id="spiHelper_Archive" />' +
			'<label for="spiHelper_Archive">Archive case</label></li></ul>';
		else
			text += '<li><input type="checkbox" disabled="true" name="spiHelper_Archive" id="spiHelper_Archive" />' +
			'<label for="spiHelper_Archive">Archive case</label></li></ul>';
		text += '<input type="button" id="spiHelper_GenerateForm" name="spiHelper_GenerateForm" value="Continue" onclick="spiHelper_generateform()" />';
		displayMessage(text);
	}

	function spiHelper_toggleArchive(){
		document.getElementById("spiHelper_Archive").disabled = !document.getElementById("spiHelper_Close").checked;
		if(document.getElementById("spiHelper_Archive").disabled)
			document.getElementById("spiHelper_Archive").checked = false;
	}

	function spiHelper_generateform(){
		spiHelper_usercount = 0;
		if (document.getElementById("spiHelper_CU_Action") != null)
			spiHelper_ActionsSelected.CU_act = document.getElementById("spiHelper_CU_Action").checked;
		else if(document.getElementById("spiHelper_Case_Action") != null)
			spiHelper_ActionsSelected.Case_act = document.getElementById("spiHelper_Case_Action").checked;
		spiHelper_ActionsSelected.Block = document.getElementById("spiHelper_BlockTag").checked;
		spiHelper_ActionsSelected.Note = document.getElementById("spiHelper_Comment").checked;
		spiHelper_ActionsSelected.Close = document.getElementById("spiHelper_Close").checked;
		spiHelper_ActionsSelected.Archive = document.getElementById("spiHelper_Archive").checked;
		var pagetext = spiHelper_getPageText(spiHelper_PageName, false);
		if(!(spiHelper_ActionsSelected.CU_act||spiHelper_ActionsSelected.Case_act||spiHelper_ActionsSelected.Note
				||spiHelper_ActionsSelected.Close||spiHelper_ActionsSelected.Archive
				||spiHelper_ActionsSelected.Block)){
			displayMessage("");
			return;
		}
		var text = '<h3>Handling SPI case</h3>';
		if(spiHelper_ActionsSelected.CU_act){
			text += '<h4>Handling Checkuser request</h4>'
				+ '<ul><li><label for="spiHelper_CUAction">Checkuser request:</label>';
			if(spiHelper_isCheckuser){
				text += spiHelper_generateSelect('spiHelper_CUAction',
						[
						 { label: 'No action', selected : true, value: 'noaction' },
						 { label: 'Endorse for CU attention', value: 'endorse' },
						 { label: 'Decline CU', value: 'cudecline' },
						 { label: 'Place case on hold', value: 'cuhold' },
						 { label: 'Relist for another check', value: 'relist' },
						 { label: 'Request more information', value: 'cumoreinfo' },
						 { label: 'Mark as in progress', value: 'inprogress' },
						 { label: 'Mark as checked', value: 'checked' },
						 { label: 'Request clerk action', value: 'clerk' }
						 ]);
			}
			else {
				if(spiHelper_isAdmin){
					text += spiHelper_generateSelect('spiHelper_CUAction',
						[
						 { label: 'No action', selected : true, value: 'noaction' },
						 { label: 'Endorse for CU attention', value: 'endorse' },
						 { label: 'Decline CU', value: 'decline' },
						 { label: 'Place case on hold', value: 'hold' },
						 { label: 'Relist for another check', value: 'relist' },
						 { label: 'Request more information', value: 'cumoreinfo' },
						 { label: 'Mark as checked', value: 'checked' },
						 { label: 'Request clerk action', value: 'clerk'}
						 ]);
				}
				else {
					text += spiHelper_generateSelect('spiHelper_CUAction',
						[
						 { label: 'No action', selected : true, value: 'noaction' },
						 { label: 'Endorse for CU attention', value: 'endorse' },
						 { label: 'Decline CU', value: 'decline' },
						 { label: 'Place case on hold', value: 'hold' },
						 { label: 'Relist for another check', value: 'relist' },
						 { label: 'Request more information', value: 'cumoreinfo' },
						 { label: 'Mark as checked', value: 'checked' },
						 { label: 'Request admin action', value: 'admin' },
						 { label: 'Request clerk action', value: 'clerk' }						 
						 ]);
				}
			}
			text +=  '</li> <li><label for="spiHelper_CUComment">Comment:</label>'
				+ '<textarea rows="3" cols="80" name="spiHelper_CUComment" id="spiHelper_CUComment"></textarea></li></ul>';
		}
		if(spiHelper_ActionsSelected.Case_act){
			text += '<h4>Request CU or other action</h4>'
				+ '<ul><li><label for="spiHelper_Request">Action to take:</label>';
			if(spiHelper_isAdmin){
				text += spiHelper_generateSelect('spiHelper_Request',
					[
					 { label: 'No action', selected : true, value: 'noaction' },
					 { label: 'Request CU', value: 'CUrequest' },
					 { label: 'Request CU and self-endorse (clerk only)', value: 'selfendorse' },
					 { label: 'Request more information (non-CU)', value: 'moreinfo' },
					 { label: 'Put case on hold', value: 'hold' },
					 { label: 'Request clerk action', value: 'clerk' }
					]);
			}
			else {
				text += spiHelper_generateSelect('spiHelper_Request',
					[
					 { label: 'No action', selected : true, value: 'noaction' },
					 { label: 'Request CU', value: 'CUrequest' },
					 { label: 'Request CU and self-endorse (clerk only)', value: 'selfendorse' },
					 { label: 'Request more information (non-CU)', value: 'moreinfo' },
					 { label: 'Put case on hold', value: 'hold' },
					 { label: 'Request admin action', value: 'admin' },
					 { label: 'Request clerk action', value: 'clerk' }
					]);
			}
			text += '</li><li><label for="spiHelper_RequestComment">Comment:</label>'
				+ '<textarea rows="3" cols="80" name="spiHelper_RequestComment" id="spiHelper_RequestComment"></textarea></li>'
				+'</ul>';
		}
		if(spiHelper_ActionsSelected.Block ){
			if(spiHelper_isAdmin)
				text += '<h4>Blocking and tagging socks</h4>';
			else
				text += '<h4>Tagging socks</h4>';
			var checkuser_re = /\{\{\s*check(user|ip)\s*\|\s*(?:1=)?\s*([^\|\}]*?)\s*\}\}/gi;
			var results = pagetext.match(checkuser_re);
			var likelyusers = new Array(), likelyips = new Array(), possibleusers = new Array(), possibleips = new Array();
			likelyusers.push(spiHelper_CaseName);
			for(var i = 0; results != null && i < results.length; i++){
				var username = results[i].replace(checkuser_re, "$2");
				if(!(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(username)) && likelyusers.indexOf(username) == -1)
					likelyusers.push(username);
				else if(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(username) && likelyips.indexOf(username) == -1)
					likelyips.push(username);
			}
			var user_re = /\{\{\s*(?:user|vandal|IP)[^\|\}\{]*?\s*\|\s*(?:1=)?\s*([^\|\}]*?)\s*\}\}/gi;
			var userresults = pagetext.match(user_re);
			for(var i = 0; userresults != null && i < userresults.length; i++){
				var username = userresults[i].replace(user_re, "$1");
				if(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(username) && possibleips.indexOf(username) == -1
						&& likelyips.indexOf(username) == -1)
					possibleips.push(username);
				else if(possibleusers.indexOf(username) == -1 && likelyusers.indexOf(username) == -1)
					possibleusers.push(username);

			}
			if(spiHelper_isAdmin){
				text += '<ul><li><input type="checkbox" name="spiHelper_noblock" id="spiHelper_noblock" />' 
					 + '<label for="spiHelper_noblock">Do not make any blocks (this overrides the individual "Blk" boxes below)</label></li>'
					 + '<li><input type="checkbox" checked="checked" name="spiHelper_override" id="spiHelper_override" />' 
					 + '<label for="spiHelper_override">Override any existing blocks</label></li>';
				if(spiHelper_isCheckuser) {
					text += '<li><input type="checkbox" name="spiHelper_cublock" id="spiHelper_cublock" />'
						 + '<label for="spiHelper_cublock">Mark blocks as Checkuser blocks.</label></li>'
						 + '<li><input type="checkbox" name="spiHelper_cublockonly" id="spiHelper_cublockonly" />'
						 + '<label for="spiHelper_cublockonly">Suppress the usual block summary and only use {'
						 + '{checkuserblock-account}} and {' + '{checkuserblock}} (no effect if "mark blocks as CU blocks" is not checked).</label></li>';
				}
				text +='<li><input type="checkbox" checked="checked" name="spiHelper_blocknoticemaster" id="spiHelper_blocknoticemaster" />'
					 + '<label for="spiHelper_blocknoticemaster">Add talk page notice when (re)blocking the sockmaster.</label></li>'
					 + '<li><input type="checkbox" name="spiHelper_blocknoticesocks" id="spiHelper_blocknoticesocks" />' 
					 + '<label for="spiHelper_blocknoticesocks">Add talk page notice when blocking socks.</label></li>' 
					 + '<li><input type="checkbox" name="spiHelper_blanktalk" id="spiHelper_blanktalk" />' 
					 + '<label for="spiHelper_blanktalk">Blank the talk page when adding talk notices.</label></li></ul>';
				
			}
			text += '<table> <tr><th>Username</th>' + (spiHelper_isAdmin? '<th>Blk?</th><th>Dur</th><th>ACB</th><th>AB/AO</th><th>NTP</th><th>NEM</th>' : '' ) + '<th>Tag</th></tr>';

			for(var i = 0; i < likelyusers.length; i++){
				spiHelper_usercount++;
				text += spiHelper_generateBlockTableLine(likelyusers[i], true, spiHelper_usercount, spiHelper_isAdmin);
			}
			for(var i = 0; i < likelyips.length; i++){
				spiHelper_usercount++;
				text += spiHelper_generateBlockTableLine(likelyips[i], true, spiHelper_usercount, spiHelper_isAdmin);
			}
			for(var i = 0; i < possibleusers.length; i++){
				spiHelper_usercount++;
				text += spiHelper_generateBlockTableLine(possibleusers[i], false, spiHelper_usercount, spiHelper_isAdmin);
			}
			for(var i = 0; i < possibleips.length; i++){
				spiHelper_usercount++;
				text += spiHelper_generateBlockTableLine(possibleips[i], false, spiHelper_usercount, spiHelper_isAdmin);
			}
			text += '</table>';
		}
		if(spiHelper_ActionsSelected.Note){
			text += '<h4>Commenting on case</h4>'
				+ '<ul><li><label for="spiHelper_CommentType">Comment label:</label>'
				+ spiHelper_generateSelect('spiHelper_CommentType',
						[
						 { label: 'None', selected : true, value: 'none' },
						 { label: 'Clerk note', value: 'clerknote' },
						 { label: 'Administrator note', value: 'adminnote' },
						 { label: 'Note', value: 'takenote' }
						 ]) + '</li>'
						 + '<li><label for="spiHelper_CommentText">Comment:</label>'
						 + '<textarea rows="3" cols="80" name="spiHelper_CommentText" id="spiHelper_CommentText"></textarea></li></ul>';
		}
		if(spiHelper_ActionsSelected.Close){
			text += '<h4>Marking case as closed</h4>'
				+ '<ul><li><input type="checkbox" checked="checked" name="spiHelper_CloseCase" id="spiHelper_CloseCase" />'
				+ '<label for="spiHelper_CloseCase">Close this SPI case</label></li>'
				+ '<li><label for="spiHelper_Close_Comment">Closing comment: </label>'
				+ '<textarea rows="3" cols="80" name="spiHelper_Close_Comment" id="spiHelper_Close_Comment"></textarea></li></ul>';
		}
		if(spiHelper_ActionsSelected.Archive){
			text += '<h4>Archiving case</h4>'
				+ '<ul><li><input type="checkbox" checked="checked" name="spiHelper_ArchiveCase" id="spiHelper_ArchiveCase" />'
				+ '<label for="spiHelper_ArchiveCase">Archive this SPI case</label></li>';
			if(spiHelper_ActionsSelected.Block||spiHelper_ActionsSelected.CU_act||spiHelper_ActionsSelected.Close||spiHelper_ActionsSelected.Note)
				text += '<li><input type="checkbox" name="spiHelper_ArchiveSingle" id="spiHelper_ArchiveSingle" />'
					+ '<label for="spiHelper_ArchiveSingle">Perform the above action and archiving in a single edit</label></li>';
			text += '</ul>';
		}
		text += '<input type="button" id="spiHelper_performActions" name="spiHelper_performActions" value="Done" onclick="spiHelper_performActions()" />';
		displayMessage(text);
	}

	function spiHelper_performActions(){
		if(spiHelper_ActionsSelected.CU_act){
			spiHelper_ActionsSelected.CUAction = document.getElementById('spiHelper_CUAction').value;
			spiHelper_ActionsSelected.CUComment = document.getElementById('spiHelper_CUComment').value;
		}
		else if(spiHelper_ActionsSelected.Case_act){
			spiHelper_ActionsSelected.RequestComment = document.getElementById('spiHelper_RequestComment').value;
			spiHelper_ActionsSelected.RequestAction = document.getElementById('spiHelper_Request').value;
		}
		if(spiHelper_ActionsSelected.Note){
			spiHelper_ActionsSelected.CommentType = document.getElementById('spiHelper_CommentType').value;
			spiHelper_ActionsSelected.Comment = document.getElementById('spiHelper_CommentText').value;
		}
		if(spiHelper_ActionsSelected.Block){
			spiHelper_ActionsSelected.Blocks = new Array();
			spiHelper_ActionsSelected.Tags = new Array();
			if(spiHelper_isAdmin && !document.getElementById('spiHelper_noblock').checked){
				var tagmaster = document.getElementById('spiHelper_blocknoticemaster').checked;
				var tagsocks = document.getElementById('spiHelper_blocknoticesocks').checked;
				spiHelper_ActionsSelected.BlankTalk = document.getElementById('spiHelper_blanktalk').checked;
				spiHelper_ActionsSelected.TagMaster = document.getElementById('spiHelper_blocknoticemaster').checked;
				spiHelper_ActionsSelected.OverrideExisting = document.getElementById('spiHelper_override').checked;
				if(spiHelper_isCheckuser){
					spiHelper_ActionsSelected.CUBlock = document.getElementById('spiHelper_cublock').checked;
					spiHelper_ActionsSelected.CUBlockOnly = document.getElementById('spiHelper_cublockonly').checked;
				}
				for(var i = 1; i <= spiHelper_usercount; i++){
					if(document.getElementById('spiHelper_block_doblock' + i).checked){
						var noticetype = false;
						
						if(tagmaster && document.getElementById('spiHelper_block_tag' + i).value.indexOf("master") != -1){
							noticetype = "master";
						}
						else if(tagsocks && document.getElementById('spiHelper_block_tag' + i).value == "blocked"){
							noticetype = "suspectsock";
						}
						else if(tagsocks && document.getElementById('spiHelper_block_tag' + i).value != ""){
							noticetype = "sock";
						}
						
						var item = {
								username : document.getElementById('spiHelper_block_username' + i).value,
								duration : document.getElementById('spiHelper_block_duration' + i).value,
								acb : document.getElementById('spiHelper_block_acb' + i).checked,
								ab  : document.getElementById('spiHelper_block_ab' + i).checked,
								ntp : document.getElementById('spiHelper_block_tp' + i).checked,
								nem : document.getElementById('spiHelper_block_email' + i).checked,
								tpn : noticetype
						};
						
						spiHelper_ActionsSelected.Blocks.push(item);
					}
					if(document.getElementById('spiHelper_block_tag' + i).value != ""){
						var item = {
								username : document.getElementById('spiHelper_block_username' + i).value,
								tag : document.getElementById('spiHelper_block_tag' + i).value,
						};
						spiHelper_ActionsSelected.Tags.push(item);
					}
				}
			}
			else {
				for(var i = 1; i <= spiHelper_usercount; i++){
					if(document.getElementById('spiHelper_block_tag' + i).value != ""){
						var item = {
								username : document.getElementById('spiHelper_block_username' + i).value,
								tag : document.getElementById('spiHelper_block_tag' + i).value,
						};
						spiHelper_ActionsSelected.Tags.push(item);
					}
				}
			}
		}
		if(spiHelper_ActionsSelected.Close){
			spiHelper_ActionsSelected.Close = document.getElementById('spiHelper_CloseCase').checked;
			spiHelper_ActionsSelected.CloseComment = document.getElementById('spiHelper_Close_Comment').value;
		}
		if(spiHelper_ActionsSelected.Archive){
			spiHelper_ActionsSelected.Archive = document.getElementById('spiHelper_ArchiveCase').checked;
			if(document.getElementById('spiHelper_ArchiveSingle') != null)
				spiHelper_ActionsSelected.ArchiveSingle = document.getElementById('spiHelper_ArchiveSingle').checked;
			else
				spiHelper_ActionsSelected.ArchiveSingle = true;
		}
		displayMessage('<ul id="spiHelper_status"></ul><ul id="spiHelper_finish"></ul>');
		document.getElementById('spiHelper_finish').innerHTML += '<span id="spiHelper_finished_wrapper"><span id="spiHelper_finished_main" style="display:none"><li id="spiHelper_done"><b>Done (<a href="/wiki/'+encodeURI(spiHelper_PageName)+'?action=purge" title="'+spiHelper_PageName+'">Reload page</a>)</b></li></span></span>';
		var token = spiHelper_getToken(true);
		var pagetext = spiHelper_getPageText(spiHelper_PageName, true);
		var editsummary = "";

		var spicasestatus_re = /\{\{\s*SPI case status\s*\|?\s*(\S*?)\s*\}\}/i;
		var result = spicasestatus_re.exec(pagetext);
		if(result == null){
			pagetext = pagetext.replace("===", "\{\{SPI case status\}\}\n===");
			result = spicasestatus_re.exec(pagetext);
		}
		var casestatustext = result[0], casestatus = result[1];

		if(spiHelper_ActionsSelected.CU_act){
			var CUAction = spiHelper_ActionsSelected.CUAction;
			var CUComment = spiHelper_ActionsSelected.CUComment;
			if(CUAction != 'noaction')
				casestatus = CUAction;
			if(CUAction == "checked"){
				editsummary = "Marking request as checked";
			}
			else if(CUAction == "inprogress"){
				if(!/\{\{Inprogress\}\}/i.test(CUComment))
					CUComment = "\{\{Inprogress\}\} - " + CUComment;
				editsummary = "Marking request in progress";
			}
			else if(CUAction == "decline"){
				if(!/\{\{(?:Decline|Decline-IP)\}\}/i.test(CUComment))
					CUComment = "\{\{Decline\}\} - " + CUComment;
				editsummary = "Declining checkuser";
			}
			else if(CUAction == "cudecline"){
				if(!/\{\{(?:CUdeclined|Cudecline|declined)\}\}/i.test(CUComment))
					CUComment = "\{\{Cudecline\}\} - " + CUComment;
				editsummary = "Declining checkuser";
			}
			else if(CUAction == "endorse"){
				if(!/\{\{(?:Endorse|Selfendorse|Requestandendorse)\}\}/i.test(CUComment))
					CUComment = "\{\{Endorse\}\} - " + CUComment;
				editsummary = "Endorsing for checkuser attention";
			}
			else if(CUAction == "cumoreinfo"){
				if(!/\{\{moreinfo\}\}/i.test(CUComment))
					CUComment = "\{\{moreinfo\}\} - " + CUComment;
				editsummary = "Requesting additional information";
			}
			else if(CUAction == "relist"){
				if(!/\{\{relisted\}\}/i.test(CUComment))
					CUComment = "\{\{relisted\}\} - " + CUComment;
				editsummary = "Relisting case for another check";
			}
			else if(CUAction == "hold" || CUAction == "cuhold"){
				if(!/\{\{onhold\}\}/i.test(CUComment))
					CUComment = "\{\{onhold\}\} - " + CUComment;
				editsummary = "Placing checkuser request on hold";
			}
			else if(CUAction == 'admin'){
				CUComment = "\{\{awaitingadmin\}\} - " + CUComment;
				editsummary = "Requesting admin action";
			}
			else if(CUAction == "clerk"){
				CUComment = "\{\{Clerk Request\}\} - " + CUComment;
				editsummary = "Requesting clerk action";
			}
			if(CUComment != ''){
				CUComment += " \~\~\~\~";
				if(pagetext.indexOf("\n----") == -1)
					pagetext += "\n----";
				pagetext = pagetext.replace(/\s*\n----/, '\n*' + CUComment + '\n----');
			}
		}

		if(spiHelper_ActionsSelected.Case_act){
			var RequestComment = spiHelper_ActionsSelected.RequestComment;
			var RequestAction = spiHelper_ActionsSelected.RequestAction;
			var newtext = '\n*';
			if(RequestAction == "selfendorse"){
				casestatus = "endorse";
				editsummary = "Adding checkuser request (self-endorsed for checkuser attention)";
				newtext += "{" + "{Requestandendorse}} ";
			}
			else if(RequestAction != "noaction"){
				casestatus = RequestAction;
				if(RequestAction == "CUrequest"){
					editsummary = "Adding checkuser request";
					if(!/\{\{CURequest\}\}/i.test(RequestComment))
						newtext += "{" + "{CURequest}} ";
				}
				else if(RequestAction == "moreinfo"){
					editsummary = "Requesting additional information";
					if(!/\{\{moreinfo\}\}/i.test(RequestComment))
						newtext += "{" + "{moreinfo}} ";
				}
				else if(RequestAction == "hold"){
					editsummary = "Putting case on hold";
					if(!/\{\{onhold\}\}/i.test(RequestComment))
						newtext += "{" + "{onhold}} ";
				}
				else if(RequestAction == "admin"){
					editsummary = "Requesting admin action";
					newtext += "\{\{awaitingadmin\}\} ";
				}
				else if(RequestAction == "clerk"){
					editsummary = "Requesting clerk action";
					newtext += "\{\{Clerk Request\}\} ";
				}
			}
			else {
				// noaction
				if(RequestComment != '')
					editsummary = "Comment";
			}
			if(RequestAction != "noaction" && RequestComment != '')
				newtext += "- ";
			newtext += RequestComment + " \~\~\~\~";
			if(pagetext.indexOf("\n----") == -1)
				pagetext += "\n----";
			if(RequestAction != "noaction" || RequestComment != '')
				pagetext = pagetext.replace(/\s*\n----/, newtext + '\n----');
		}
		if(spiHelper_ActionsSelected.Block){
			var sockmaster = "";
			var sockcount = 0;
			for(var i = 0; i < spiHelper_ActionsSelected.Tags.length; i++){
				var isIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(spiHelper_ActionsSelected.Tags[i].username);
				if(isIP) continue; // do not support tagging IPs
				if(spiHelper_ActionsSelected.Tags[i].tag.indexOf("master") != -1){
					sockmaster = spiHelper_ActionsSelected.Tags[i].username;
					continue;
				}
				sockcount ++;
			}
			if(sockcount > 0 && sockmaster == "") sockmaster = prompt("Please enter the name of the sockmaster: ", spiHelper_CaseName);
			
			if(spiHelper_isAdmin){
				for(var i = 0; i < spiHelper_ActionsSelected.Blocks.length; i++){
					var isIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(spiHelper_ActionsSelected.Blocks[i].username);
					var block_summary = "Abusing [[WP:SOCK|multiple accounts]]: Please see: [[w:en:" + spiHelper_PageName + "]]";
					if(spiHelper_isCheckuser){
						if(spiHelper_ActionsSelected.CUBlock){
							var cublock_template = isIP ? ('{' + '{checkuserblock}}') : ('{' + '{checkuserblock-account}}');
							if(spiHelper_ActionsSelected.CUBlockOnly)
								block_summary = cublock_template;
							else
								block_summary = cublock_template + ': ' + block_summary;
						}
					}
					spiHelper_blockUser(
							spiHelper_ActionsSelected.Blocks[i].username,
							spiHelper_ActionsSelected.Blocks[i].duration,
							token,
							block_summary,
							spiHelper_ActionsSelected.OverrideExisting,
							(isIP ? spiHelper_ActionsSelected.Blocks[i].ab : false),
							spiHelper_ActionsSelected.Blocks[i].acb,
							(isIP ? false : spiHelper_ActionsSelected.Blocks[i].ab),
							spiHelper_ActionsSelected.Blocks[i].ntp,
							spiHelper_ActionsSelected.Blocks[i].nem);
					if(spiHelper_ActionsSelected.TagMaster && spiHelper_ActionsSelected.Blocks[i].username == sockmaster)
						spiHelper_ActionsSelected.Blocks[i].tpn = "master";
					if(sockmaster != null && sockmaster != "" && spiHelper_ActionsSelected.Blocks[i].tpn){
						var newtext = '';
						if(spiHelper_ActionsSelected.Blocks[i].tpn.indexOf("sock") != -1){
							newtext = '== Blocked as a sockpuppet ==\n{' + '{subst:sockblock|master=' + sockmaster;
							if(spiHelper_ActionsSelected.Blocks[i].ntp)
								newtext += '|notalk=yes';
							if(spiHelper_ActionsSelected.Blocks[i].tpn.indexOf("suspect") != -1)
								newtext += '|suspected=yes';
							newtext += '|sig=yes}}';
						}
						else {
							newtext = '== Blocked for sockpuppetry ==\n{' + '{subst:sockblock|masterblock=yes|period=' + spiHelper_ActionsSelected.Blocks[i].duration;
							if(spiHelper_ActionsSelected.Blocks[i].ntp)
								newtext += '|notalk=yes';
							newtext += '|evidence= [[' + spiHelper_PageName + ']]|sig=yes}}';
						}
						if(!spiHelper_ActionsSelected.BlankTalk){
							var oldtext = spiHelper_getPageText("User talk:" + spiHelper_ActionsSelected.Blocks[i].username, true);
							if(oldtext != "")
								newtext = oldtext + '\n' + newtext;	
						}
						spiHelper_editPage("User talk:" + spiHelper_ActionsSelected.Blocks[i].username,
								newtext, token, "Adding sockpuppetry block notice per [[" + spiHelper_PageName+ "]]", false, null, "nochange");
					}
				}
			}
			if(sockmaster != null && sockmaster != ""){
				var checkConfirmedCat = false;
				var checkSuspectedCat = false;
				for(var i = 0; i < spiHelper_ActionsSelected.Tags.length; i++){
					var isIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(spiHelper_ActionsSelected.Tags[i].username);
					if(isIP) continue; // do not support tagging IPs
					var tagtext = "";
					if(spiHelper_ActionsSelected.Tags[i].tag == "blocked")
					{
						tagtext = "{" + "{sockpuppet|1=" + sockmaster + "|2=blocked}}";
						checkSuspectedCat = true;
					}
					else if(spiHelper_ActionsSelected.Tags[i].tag == "proven")
					{
						tagtext = "{" + "{sockpuppet|1=" + sockmaster + "|2=proven}}";
						checkConfirmedCat = true;						
					}
					else if(spiHelper_ActionsSelected.Tags[i].tag == "confirmed")
					{
						tagtext = "{" + "{sockpuppet|1=" + sockmaster + "|2=confirmed}}";
						checkConfirmedCat = true;
					}
					else if(spiHelper_ActionsSelected.Tags[i].tag == "master")
						tagtext = "{" + "{sockmaster|blocked}}";
					else if(spiHelper_ActionsSelected.Tags[i].tag == "sockmasterchecked")
						tagtext = "{" + "{sockmaster|blocked|checked=yes}}";
					spiHelper_editPage("User:" + spiHelper_ActionsSelected.Tags[i].username, tagtext, token, "Adding sockpuppetry tag per [[" + spiHelper_PageName+ "]]", false, null, "nochange");
				}
				
				if(checkConfirmedCat)
				{
					var catname = "Category:Wikipedia sockpuppets of " + sockmaster;
					var cattext = spiHelper_getPageText(catname, false);
					if(cattext == "")
					{
						spiHelper_editPage(catname,"{" + "{sockpuppet category}}", token, "Creating sockpuppet category per [[" + spiHelper_PageName + "]]", true, null, "nochange");
					}
				}
				if(checkSuspectedCat)
				{
					var catname = "Category:Suspected Wikipedia sockpuppets of " + sockmaster;
					var cattext = spiHelper_getPageText(catname, false);
					if(cattext == "")
					{
						spiHelper_editPage(catname,"{" + "{sockpuppet category}}", token, "Creating sockpuppet category per [[" + spiHelper_PageName + "]]", true, null, "nochange");
					}	
				}
			}
		}
		if(spiHelper_ActionsSelected.Note){
			if(pagetext.indexOf("\n----") == -1)
				pagetext += "\n----";
			if(spiHelper_ActionsSelected.Comment != ''){
				var newtext = '\n*';
				if(spiHelper_ActionsSelected.CommentType != 'none')
					newtext += "\{\{" + spiHelper_ActionsSelected.CommentType + "\}\} ";
				newtext += spiHelper_ActionsSelected.Comment + " \~\~\~\~\n";
				pagetext = pagetext.replace(/\s*\n----/, newtext + '----');
				if(!/comment/i.test(editsummary)){
					if(editsummary != "")
						editsummary += ", comment";
					else
						editsummary = "Comment";
				}
			}

		}
		if(spiHelper_ActionsSelected.Close){
			if(pagetext.indexOf("\n----") == -1)
				pagetext += "\n----";
			var newtext = '\n';
			if(spiHelper_ActionsSelected.CloseComment != '')
				newtext += '*' + spiHelper_ActionsSelected.CloseComment + " \~\~\~\~\n";
			casestatus = 'close';
			pagetext = pagetext.replace(/\s*\n----/, newtext + '----');
			if(editsummary != "")
				editsummary += ", marking case as closed";
			else
				editsummary = "Marking case as closed";
		}

		pagetext = pagetext.replace(casestatustext, "\{\{SPI case status|" + casestatus + "\}\}");
		casestatustext = "\{\{SPI case status|" + casestatus + "\}\}";

		if(spiHelper_ActionsSelected.Archive){
			if(spiHelper_ActionsSelected.ArchiveSingle){
				spiHelper_archiveCase(pagetext, editsummary, token);
			}
			else{
				var onComplete = function () {
					spiHelper_archiveCase(pagetext, "", token);
					document.getElementById('spiHelper_finished_main').style.display = '';
				};
				spiHelper_editPage(spiHelper_PageName, pagetext, token, editsummary, false, onComplete, spiHelper_watchCase);
			}
		}
		else
			spiHelper_editPage(spiHelper_PageName, pagetext, token, editsummary, false, null, spiHelper_watchCase);
		if(!spiHelper_ActionsSelected.Archive || spiHelper_ActionsSelected.ArchiveSingle)
			document.getElementById('spiHelper_finished_main').style.display = '';
	}

	function spiHelper_getToken(show) {
		return mw.user.tokens.get('csrfToken');
	}

	function spiHelper_archiveCase(pagetext, editsummary, token) {
		var archivename = spiHelper_PageName + "/Archive";				
		var caseHtml = document.getElementsByTagName('body')[0].innerHTML;
		var reqArchiveHtml = new XMLHttpRequest();
		var url = "https://en.wikipedia.org/wiki/" + archivename;
		reqArchiveHtml.open("GET",url);				
		reqArchiveHtml.onreadystatechange = function(){			
			if(reqArchiveHtml.readyState == 4) {
				if(reqArchiveHtml.status == 404 || spiHelper_getPostExpandSize(caseHtml) + spiHelper_getPostExpandSize(reqArchiveHtml.responseText) < 1)
					spiHelper_archiveCaseFinish(pagetext, editsummary, token);
				else
				{
					for(var i = 1; spiHelper_getPageText(archivename + "/" +i) != ""; i++);				
					var newarchivename = archivename + "/" + i;
					var moveReq = new XMLHttpRequest();
					document.getElementById('spiHelper_finished_wrapper').innerHTML = '<span id="spiHelper_AJAX_finished_'+spiHelper_AJAXnumber+'" style="display:none">' + document.getElementById('spiHelper_finished_wrapper').innerHTML + '</span>';
					var func_id = spiHelper_AJAXnumber;
					spiHelper_AJAXnumber++;
					document.getElementById('spiHelper_status').innerHTML += '<li id="spiHelper_move'+func_id+'">Moving <a href="/wiki/'+encodeURI(archivename)+'" title="'+archivename+'">'+archivename+'</a> to <a href="/wiki/' + encodeURI(newarchivename) + 'title="' + newarchivename + '">' +  newarchivename + '</a></li>';
					var params = "action=move&format=json&from="+encodeURIComponent(archivename) + "&to=" + encodeURIComponent(newarchivename) + "&reason=" + encodeURIComponent("Moving archive to avoid exceeding post expand size limit") + "&noredirect=1&token=" + encodeURIComponent(token);
					let url = mw.config.get('wgScriptPath') + "/api.php";
					moveReq.open("POST", url, true);
					moveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					moveReq.setRequestHeader("Content-length", params.length);
					moveReq.setRequestHeader("Connection", "close");
					moveReq.onreadystatechange = function() {
						if(moveReq.readyState == 4 && moveReq.status == 200) {							
							try {							
								var response = JSON.parse(moveReq.responseText);								
								if(response['move'] != undefined)
								{
									document.getElementById('spiHelper_move'+func_id).innerHTML = 'Moved to <a href="/wiki/'+encodeURI(newarchivename)+'" title="'+newarchivename+'">'+newarchivename+'</a>';
									spiHelper_archiveCaseFinish(pagetext, editsummary, token);							
									document.getElementById('spiHelper_AJAX_finished_'+func_id).style.display = '';
									delete moveReq;
									return;
								}	
							}
							catch(e) { } 
							document.getElementById('spiHelper_move'+func_id).innerHTML = '<div style="color:red"><b>Move failed on <a href="/wiki/'+encodeURI(archivename)+'" title="'+archivename+'">'+archivename+'</a></b></div>'; alert(req.responseText);
							delete moveReq;							
						}
						else if(moveReq.readyState == 4 && moveReq.status > 399)
						{
							document.getElementById('spiHelper_move'+func_id).innerHTML = '<div style="color:red"><b>Move failed on <a href="/wiki/'+encodeURI(archivename)+'" title="'+archivename+'">'+archivename+'</a></b></div>'; alert(req.responseText);
							delete moveReq;
						}
					}
					moveReq.send(params);					
				}
				delete reqArchiveHtml;
			}
		};
		reqArchiveHtml.send(null);		
	}

	function spiHelper_archiveCaseFinish(pagetext, editsummary, token){
		var spicasestatus_re = /\{\{\s*SPI case status\s*\|?\s*(\S*?)\s*\}\}/gi;
		pagetext = pagetext.replace(spicasestatus_re, '');
		var header = pagetext.substring(0, pagetext.search(spiHelper_section_re));
		header = header.replace(/<!--[\s\S]*?-->/g, '').trim();
		var newarchivetext = pagetext.substring(pagetext.search(spiHelper_section_re));
		var archivename = spiHelper_PageName + "/Archive";		
		if(editsummary != "")
			editsummary += ", archiving case to [[" + archivename + "]]";
		else
			editsummary = "Archiving case to [[" + archivename + "]]";
		spiHelper_editPage(spiHelper_PageName, header, token, editsummary, false, null, spiHelper_watchCase);
		var archivetext = spiHelper_getPageText(archivename, true);
		if(archivetext == "")
			archivetext = "__" + "TOC__\n\{\{SPIarchive notice|1=" + spiHelper_CaseName + "\}\}\n\{\{SPIpriorcases\}\}";
		else
			archivetext = archivetext.replace(/<br\s*\/>\s*\{\{SPIpriorcases\}\}/gi, "\n\{\{SPIpriorcases}}"); // fmt fix whenever needed.
		archivetext += "\n" + newarchivetext;
		spiHelper_editPage(archivename, archivetext, token, "Archiving case from [[" + spiHelper_PageName + "]]", false, null, spiHelper_watchArchive);
	}

	function spiHelper_editPage(title, newtext, token, summary, createonly, onComplete, watch) {
		document.getElementById('spiHelper_finished_wrapper').innerHTML = '<span id="spiHelper_AJAX_finished_'+spiHelper_AJAXnumber+'" style="display:none">' + document.getElementById('spiHelper_finished_wrapper').innerHTML + '</span>';
		var func_id = spiHelper_AJAXnumber;
		spiHelper_AJAXnumber++;
		document.getElementById('spiHelper_status').innerHTML += '<li id="spiHelper_edit'+func_id+'">Editing <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a></li>';
		var req = new XMLHttpRequest();
		var params = "action=edit&format=json&token="+encodeURIComponent(token)+"&title="+encodeURIComponent(title)+"&text="+encodeURIComponent(newtext)+"&notminor=1&watchlist="+encodeURIComponent(watch)+"&summary="+encodeURIComponent(summary);
		if(createonly)
			params += "&createonly=1";
		url = mw.config.get('wgScriptPath') + "/api.php";
		req.open("POST", url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		req.onreadystatechange = function() {
			if(req.readyState == 4 && req.status == 200) {
				response = eval('(' + req.responseText + ')');
				try {
					if (response['edit']['result'] == "Success") {
						document.getElementById('spiHelper_edit'+func_id).innerHTML = 'Saved <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a>';
						if(onComplete != null)
							onComplete();
					} else {
						document.getElementById('spiHelper_edit'+func_id).innerHTML = '<div style="color:red"><b>Edit failed on <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a></b></div>. Error info:' +response['error']['code'] + ' : ' + response['error']['info'];
					}
				}
				catch(err) {
					document.getElementById('spiHelper_edit'+func_id).innerHTML = '<div style="color:red"><b>Edit failed on <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a></b></div>'; alert(req.responseText);
				}
				document.getElementById('spiHelper_AJAX_finished_'+func_id).style.display = '';
				delete req;
			}
		};
		req.send(params);
	}

	function spiHelper_blockUser(user, duration, token, reason, reblock, anononly, accountcreation, autoblock, talkpage, email ) {
		document.getElementById('spiHelper_finished_wrapper').innerHTML = '<span id="spiHelper_AJAX_finished_'+spiHelper_AJAXnumber+'" style="display:none">' + document.getElementById('spiHelper_finished_wrapper').innerHTML + '</span>';
		var func_id = spiHelper_AJAXnumber;
		spiHelper_AJAXnumber++;
		document.getElementById('spiHelper_status').innerHTML += '<li id="spiHelper_block'+escape(user)+'">Blocking <a href="/wiki/User:'+encodeURI(user)+'" title="User:'+user+'">'+user+'</a></li>';
		var req = new XMLHttpRequest();
		var params = "action=block&format=json&token="+encodeURIComponent(token)+"&user="+encodeURIComponent(user)+"&expiry="+encodeURIComponent(duration)+"&reason="+encodeURIComponent(reason);
		if( reblock ) params += "&reblock=1";
		if( anononly ) params += "&anononly=1";
		if( accountcreation  ) params += "&nocreate=1";
		if( autoblock ) params += "&autoblock=1";
		if( !talkpage ) params += "&allowusertalk=1";
		if( email ) params += "&noemail=1";
		url = mw.config.get('wgScriptPath') + "/api.php";
		req.open("POST", url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		req.onreadystatechange = function() {
			if(req.readyState == 4 && req.status == 200) {
				response = eval('(' + req.responseText + ')');
				try {
					if (response['block']) {
						document.getElementById('spiHelper_block'+escape(user)).innerHTML = 'Blocked <a href="/wiki/User:'+encodeURI(user)+'" title="User:'+user+'">'+user+'</a>';
					} else {
						document.getElementById('spiHelper_block'+escape(user)).innerHTML = '<div style="color:red"><b>Block failed on <a href="/wiki/User:'+encodeURI(user)+'" title="User:'+user+'">'+user+'</a></b></div>. Error info:' +response['error']['code'] + ' : ' + response['error']['info'];
					}
				}
				catch(err) {
					document.getElementById('spiHelper_block'+escape(user)).innerHTML = '<div style="color:red"><b>Blockfailed on <a href="/wiki/User:'+encodeURI(user)+'" title="User:'+user+'">'+user+'</a></b></div>';
				}
				document.getElementById('spiHelper_AJAX_finished_'+func_id).style.display = '';
				delete req;
			}
		};
		req.send(params);
	}

	function escapeHtmlChars(original){
		return original
		.replace(/&/g, "&amp;")
		.replace(/</g, "&lt;")
		.replace(/>/g, "&gt;")
		.replace(/"/g, "&quot;")
		.replace(/'/g, "&#039;");
	}
	function spiHelper_generateBlockTableLine(name, defaultblock, id, admin){
		var row = '<tr>';
		row += '<td><input type="text" name="spiHelper_block_username' + id + '" id="spiHelper_block_username' + id + 
		'" value="' + escapeHtmlChars(name)  + '" /></td>';
		if(admin){
			row += '<td><input type="checkbox" name="spiHelper_block_doblock' + id + '" id="spiHelper_block_doblock' + id + 
			'" ' + (defaultblock ? 'checked="checked" ' : '') + '/></td>';
			row += '<td><input type="text" name="spiHelper_block_duration' + id + '" id="spiHelper_block_duration' + id + 
			'" value="indefinite" /></td>';
			row += '<td><input type="checkbox" name="spiHelper_block_acb' + id + '" id="spiHelper_block_acb' + id + '" checked="checked" /></td>';
			row += '<td><input type="checkbox" name="spiHelper_block_ab' + id + '" id="spiHelper_block_ab' + id + '" checked="checked" /></td>';
			row += '<td><input type="checkbox" name="spiHelper_block_tp' + id + '" id="spiHelper_block_tp' + id + '" /></td>';
			row += '<td><input type="checkbox" name="spiHelper_block_email' + id + '" id="spiHelper_block_email' + id + '" /></td>';
		}
		row += '<td><select name="spiHelper_block_tag' + id + '" id="spiHelper_block_tag' + id + '" >' +
		'<option value=""> None </option>' +
		'<option value="blocked"> Suspected sock </option>' +
		'<option value="proven"> Proven sock </option>' +
		'<option value="confirmed"> CU confirmed sock </option>' +
		'<option value="master"> Blocked master </option>' +
		'<option value="sockmasterchecked"> CU confirmed master </option>' +
		'</select></td></tr>';
		return row;
	}

	function spiHelper_getPageText(title, show) {
		var func_id = spiHelper_AJAXnumber;
		spiHelper_AJAXnumber++;
		if(show){
			document.getElementById('spiHelper_status').innerHTML += '<li id="spiHelper_get'+func_id+'">Getting <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a></li>';
		}
		var req = new XMLHttpRequest();
		req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(title), false);
		req.send(null);
		var response = eval('(' + req.responseText + ')');
		pageid = response['query']['pageids'][0];
		if (pageid == "-1") {
			if(show){
				document.getElementById('spiHelper_get'+func_id).innerHTML = '<a class="new" href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a> does not exist';
			}
			delete req;
			return '';
		}
		pagetext = response['query']['pages'][pageid]['revisions'][0]['*'];
		delete req;
		if(show){
			document.getElementById('spiHelper_get'+func_id).innerHTML = 'Got <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a>';
		}
		return pagetext;
	}	

	function spiHelper_getPostExpandSize(html)
	{		
		// returns the fraction of post-expand include size used (max 1)
		var pattern = /Post‐expand include size: \d+\/\d+ bytes/;
		var pesLine = html.match(pattern)[0];
		return eval(pesLine.split(' ')[3]);
	}

	function spiHelper_generateSelect(title, options, onchange){
		var text = '<select name="' + title + '" id="' + title +'" ';
		if(onchange != null)
			text += 'onchange = "' + onchange + '" ';
		text+= '>';
		for(var i = 0; i < options.length; i ++){
			var o = options[i];
			text += '<option value="' + o.value + '" ';
			if(o.selected)
				text += 'selected="selected" ';
			text += '>' + o.label + '</option>';
		}
		text += "</select>";
		return text;
	}

	function spiHelper_oneClickArchive(){
		var pagetext = spiHelper_getPageText(spiHelper_PageName, false);
		if(!spiHelper_section_re.test(pagetext)){
			alert("Looks like the page has been archived already.");
			return;
		}
		displayMessage('<ul id="spiHelper_status"></ul><ul id="spiHelper_finish"></ul>');
		document.getElementById('spiHelper_finish').innerHTML += '<span id="spiHelper_finished_wrapper"><span id="spiHelper_finished_main" style="display:none"><li id="spiHelper_done"><b>Done (<a href="/wiki/'+encodeURI(spiHelper_PageName)+'?action=purge" title="'+spiHelper_PageName+'">Reload page</a>)</b></li></span></span>';
		var token = spiHelper_getToken(true);
		spiHelper_archiveCase(pagetext, '', token);
		document.getElementById('spiHelper_finished_main').style.display = '';
	}

	function spiHelper_addLink() {
		mw.loader.using('mediawiki.util').then(function() {
			mw.util.addPortletLink("p-cactions", "javascript:spiHelper_init()", "SPI", "ca-spiHelper", "SPI");
			if(mw.config.get('wgCategories').toString().indexOf('SPI cases awaiting archive') != -1)
				mw.util.addPortletLink("p-cactions", "javascript:spiHelper_oneClickArchive()", "SPI-Archive", "ca-spiHelperArchive", "SPI-Archive");
		});
	}
	$(spiHelper_addLink);
}