User:Dudemanfellabra/Sandbox.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. |
This user script seems to have a documentation page at User:Dudemanfellabra/Sandbox. |
var NRISEditors=[] // ["name",[total,[title1,state1],[title2,state2],...]]
var NRISPages=[]
var TotalTested=0
function NRIStest() {
if (mw.config.get('wgPageName')!="User:NationalRegisterBot/NRISOnly/All") return;
var button=document.createElement("input")
button.setAttribute("type", "button");
button.setAttribute("value", "Who created these?");
button.setAttribute("id", "testbutton");
button.setAttribute("onclick", "TestQueryEditors()");
var thediv=document.getElementById('NRISOnly-div')
if (thediv!=null) thediv.parentNode.insertBefore(button, thediv)
var ProgressDiv = document.createElement("div")
ProgressDiv.setAttribute("id", "ProgressDiv")
ProgressDiv.setAttribute("style", "width:100px; border:1px solid black; padding:5px; background:#ffffff")
button.parentNode.insertBefore(ProgressDiv, button)
}
function TestQueryEditors() {
var button=document.getElementById("testbutton")
button.disabled=true;
var content=document.getElementById("NRISOnly-div")
var list=content.getElementsByClassName("refbegin")
for (var j=0; j<list.length; j++) {
var pages=list[j].getElementsByTagName("li")
for (var i=0; i<pages.length; i++) {
NRISPages.push(pages[i].firstChild.title)
}
}
TestQueryNextEditor(0)
}
function TestQueryNextEditor(current) {
if (current==NRISPages.length) return;
$.ajax({
dataType: "json",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'user|timestamp',
rvlimit: '1',
rvdir: 'newer',
titles: NRISPages[current],
indexpageids: true
},
error: function(ArticlejsonObject,status,errorThrown) {ArticlejsonObject.errorThrown=errorThrown},
complete: function(ArticlejsonObject,status) {
TestQueryNextEditorComplete(ArticlejsonObject,status,current)
}
});
setTimeout(function(){
TestQueryNextEditor(current+1)
}, 10);
return;
}
function TestQueryNextEditorComplete(ArticlejsonObject,status,current) {
if (status!="success") {
setTimeout(function(){ // try again after delay if rate limit reached
TestQueryNextEditor(current)
}, 250);
return;
}
var output=JSON.parse(ArticlejsonObject.responseText)
if (typeof output.query=="undefined") {alert(current+"\n"+NRISPages[current])}
for (var page in output.query.pages) {
var user = output.query.pages[page].revisions[0]['user'];
var timestamp = output.query.pages[page].revisions[0]['timestamp'];
}
timestamp = timestamp.replace("T"," – ").replace("Z"," UTC")
// query to determine state
$.ajax({
dataType: "json",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
prop: 'linkshere',
lhprop: 'title',
lhnamespace: 0,
titles: NRISPages[current]
},
error: function(ArticlejsonObject,status,errorThrown) {ArticlejsonObject.errorThrown=errorThrown},
complete: function(ArticlejsonObject,status) {
TestQueryStateFetched(ArticlejsonObject,status,current,user,timestamp)
}
});
}
function TestQueryStateFetched(ArticlejsonObject,status,current,user,timestamp) {
if (status!="success") {
setTimeout(function(){ // try again after delay if rate limit reached
TestQueryNextEditor(current)
}, 250);
return;
}
TotalTested++
var ProgressDiv=document.getElementById("ProgressDiv")
ProgressDiv.innerHTML=TotalTested+" of "+NRISPages.length
var output=JSON.parse(ArticlejsonObject.responseText)
if (typeof output.query=="undefined") {alert(current+"\n"+NRISPages[current])}
for (var page in output.query.pages) {
var linkshere=output.query.pages[page].linkshere
var state=""
for (var i=0; i<linkshere.length; i++) {
if (linkshere[i].title.indexOf("National Register of Historic Places listings")!=-1) {
state=TestQueryGetState(linkshere[i].title)
}
if (state!="") break;
}
if (state=="") state="!Unknown"
}
if (NRISEditors.length==0) {
NRISEditors[NRISEditors.length]=[user,[1,[NRISPages[current],state]]]
} else {
var match=false
for (var i=0; i<NRISEditors.length; i++) {
if (NRISEditors[i][0]==user) {
match=true
NRISEditors[i][1][0]++
NRISEditors[i][1][NRISEditors[i][1].length]=["[["+NRISPages[current]+"]]",state]
}
}
if (!match) NRISEditors[NRISEditors.length]=[user,[1,["[["+NRISPages[current]+"]]",state]]]
}
if (TotalTested==NRISPages.length) TestEditorComplete()
}
function TestQueryGetState(title) {
var temp=title.split(", ")
var state=temp[temp.length-1]
if (state=="Philadelphia") state="Pennsylvania"
if (temp[0]==state||state.indexOf(':')!=-1) {
if (title.indexOf("Chicago")!=-1) state="Illinois"
else if (title.indexOf("St. Louis")!=-1) state="Missouri"
else if (title.indexOf("Kansas City")!=-1) state="Missouri"
else if (title.indexOf("Baltimore")!=-1) state="Maryland"
else if (title.indexOf("Manhattan")!=-1) state="New York"
else if (title.indexOf("Philadelphia")!=-1) state="Pennsylvania"
else if (title.indexOf("Zion")!=-1) state="Utah"
else state=""
}
return state
}
function TestEditorComplete() {
NRISEditors.sort(function(a,b){if (a[1][0] > b[1][0]) return -1; if (a[1][0] < b[1][0]) return 1; return 0;}) // sort large to small
var resultsStr="Results\n----------\n"
for (var i=0; i<NRISEditors.length; i++) {
resultsStr+="#"+NRISEditors[i][0]+": "+NRISEditors[i][1][0]+"\n"
var ThisUsersArticles = NRISEditors[i][1].slice(1)
ThisUsersArticles.sort(function(a,b){if (a[1] < b[1]) return -1; if (a[1] > b[1]) return 1; return 0;}) // sort by state
ThisUsersArticles.unshift(NRISEditors[i][1][0])
NRISEditors[i][1]=ThisUsersArticles
}
var articlesStr="Articles by each editor\n----------\n"
for (var i=0; i<NRISEditors.length; i++) {
articlesStr+=NRISEditors[i][0]+" ("+NRISEditors[i][1][0]+"):\n"
var oldstate=NRISEditors[i][1][1][1]
var newstate=""
articlesStr+="==="+oldstate+"===\n"
for (var j=1; j<NRISEditors[i][1].length; j++) {
newstate=NRISEditors[i][1][j][1]
if (newstate!=oldstate) {articlesStr+="==="+newstate+"===\n"; oldstate=newstate}
articlesStr+="#"+NRISEditors[i][1][j][0]+"\n"
}
articlesStr+="\n"
}
var popup = open("");
var resultspre=popup.document.createElement("pre")
resultspre.innerHTML=resultsStr
var articlespre=popup.document.createElement("pre")
articlespre.innerHTML=articlesStr
var pre = popup.document.createElement("pre");
pre.innerHTML=JSON.stringify(NRISEditors,null,4)
popup.document.body.appendChild(resultspre);
popup.document.body.appendChild(articlespre);
popup.document.body.appendChild(pre);
}
$(window).load(NRIStest);