User:Timotheus Canens/spicaselist.js

if (mw.config.get('wgPageName') == 'Wikipedia:Sockpuppet_investigations') { var caselisttext = ''; function getCategoryMembersByTimestamp(title){ var req = sajax_init_object; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&list=categorymembers&cmsort=timestamp&format=json&cmdir=desc&cmnamespace=4&cmlimit=500&cmtitle="+encodeURIComponent(title), false); req.send(null); var response = eval('(' + req.responseText + ')'); members = response['query']['categorymembers']; titles = new Array; for(var i = 0; i < members.length; i++){ titles.push(members[i]['title']); }		delete req; return titles; }

function getSPICaseList{ var opencases = getCategoryMembersByTimestamp("Category:SPI cases awaiting administration"); var clerkcases = getCategoryMembersByTimestamp("Category:SPI requests for pre-CheckUser review"); var CUcases = getCategoryMembersByTimestamp("Category:SPI cases awaiting a CheckUser"); var newtext = "== Open cases: not awaiting Checkuser ==\n=== Cases ===\n\n"; for(var i = 0; i < opencases.length; i++){ if(opencases[i].indexOf("Wikipedia:Sockpuppet investigations/") == -1) continue; newtext += "\{\{SPI|" + opencases[i].replace("Wikipedia:Sockpuppet investigations/", "") + "\}\}\n"; }		newtext += "== Open cases: awaiting Checkuser processing ==\nCases in this section have an open request for Checkuser that is being processed. Any user may update or act on this case, and any patroller or clerk may decide the case. However it will not be closed if the Clerks believe any further action is needed. \n* Endorsement or decline of Checkuser request may only be made by a Clerk or Checkuser.\n* If your case is not showing up here, or this section seems out of date, please click the .\n"; newtext += "=== Awaiting clerk approval ===\n"; for(var i = 0; i < clerkcases.length; i++){ if(clerkcases[i].indexOf("Wikipedia:Sockpuppet investigations/") == -1) continue; newtext += "\{\{SPI|" + clerkcases[i].replace("Wikipedia:Sockpuppet investigations/", "") + "\}\}\n"; }		newtext += "=== Awaiting Checkuser ===\n"; for(var i = 0; i < CUcases.length; i++){ if(CUcases[i].indexOf("Wikipedia:Sockpuppet investigations/") == -1) continue; newtext += "\{\{SPI|" + CUcases[i].replace("Wikipedia:Sockpuppet investigations/", "") + "\}\}\n"; }		caselisttext = newtext; jsMsg(" \n" + newtext + ' '); }	function updateList{ var text = getPageText('Wikipedia:Sockpuppet investigations'); var newtext = caselisttext + "\n\n== Quick Checkuser requests =="; text = text.replace(/==\s*Open cases: not awaiting Checkuser\s*==[\w\W]*?==\s*Quick Checkuser requests\s*==/i, newtext); editPage('Wikipedia:Sockpuppet investigations', text, getToken, 'Updating list of SPI cases'); jsMsg('Editing...Please do not leave this page. You will be informed when the edit is complete.'); }	function getToken { var req = sajax_init_object; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=info&indexpageids=1&intoken=edit&format=json&titles="+encodeURIComponent(mw.config.get('wgPageName')), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; token = response['query']['pages'][pageid]['edittoken']; delete req; return token; }	function editPage(title, newtext, token, summary) { var req = sajax_init_object; var params = "action=edit&format=json&token="+encodeURIComponent(token)+"&title="+encodeURIComponent(title)+"&text="+encodeURIComponent(newtext)+"&notminor=1&summary="+encodeURIComponent(summary); 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") { jsMsg("Edit failed: " + req.responseText); }					else jsMsg("Edit successful."); }				catch(err) { jsMsg("Edit failed: " + req.responseText); }				delete req; }		};		req.send(params); }	function getPageText(title) { var req = sajax_init_object; 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") { delete req; return ''; }		pagetext = response['query']['pages'][pageid]['revisions'][0]['*']; delete req; return pagetext; }	function addLink { mw.util.addPortletLink("p-cactions", "javascript:getSPICaseList", "caselist", "ca-spicaselist", "caselist"); }	addOnloadHook(addLink); }