User:Dudemanfellabra/NRHPProgressHistory.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:Dudemanfellabra/NRHPProgressHistory. |
/* The following code produces the data found at [[Wikipedia:WikiProject National Register of Historic Places/Progress/History]]
by looking back through old revisions of the [[WP:NRHPPROGRESS]] page. The data can then be used to create graphs
*/
var StatesArray=[
["Alabama","AL"],
["Alaska","AK"],
["Arizona","AZ"],
["Arkansas","AR"],
["California","CA"],
["Colorado","CO"],
["Connecticut","CT"],
["Delaware","DE"],
["District of Columbia","DC"],
["Florida","FL"],
["Georgia","GA"],
["Hawaii","HI"],
["Idaho","ID"],
["Illinois","IL"],
["Indiana","IN"],
["Iowa","IA"],
["Kansas","KS"],
["Kentucky","KY"],
["Louisiana","LA"],
["Maine","ME"],
["Maryland","MD"],
["Massachusetts","MA"],
["Michigan","MI"],
["Minnesota","MN"],
["Mississippi","MS"],
["Missouri","MO"],
["Montana","MT"],
["Nebraska","NE"],
["Nevada","NV"],
["New Hampshire","NH"],
["New Jersey","NJ"],
["New Mexico","NM"],
["New York","NY"],
["North Carolina","NC"],
["North Dakota","ND"],
["Ohio","OH"],
["Oklahoma","OK"],
["Oregon","OR"],
["Pennsylvania","PA"],
["Rhode Island","RI"],
["South Carolina","SC"],
["South Dakota","SD"],
["Tennessee","TN"],
["Texas","TX"],
["Utah","UT"],
["Vermont","VT"],
["Virginia","VA"],
["Washington","WA"],
["West Virginia","WV"],
["Wisconsin","WI"],
["Wyoming","WY"],
["Puerto Rico","PR"],
["Guam","GU"],
["Virgin Islands","VI"],
["Northern Mariana Islands","MP"],
["American Samoa","AS"],
["Federated States of Micronesia","FM"],
["Palau","PW"],
["Marshall Islands","MH"],
["Minor Outlying Islands","UM"],
];
function ProgressPageHistoryButton() {
if (wgPageName!="Wikipedia:WikiProject_National_Register_of_Historic_Places/Progress/History") return
var button=document.createElement("input")
button.setAttribute("type", "button");
button.setAttribute("value", "Get Progress Page History");
button.setAttribute("id", "historybutton");
button.setAttribute("onclick", "GetProgressPageHistory()");
var content=document.getElementById('mw-content-text')
content.parentNode.insertBefore(button, content)
}
function GetProgressPageHistory() {
document.getElementById("historybutton").value="Working..."
document.getElementById("historybutton").disabled=true
var Updates = [];
var wikitext = ''
var RevsWithMapsSection = JSON.parse(
$.ajax({
dataType: "json",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'timestamp|comment|content',
rvlimit: 'max',
rvendid: '568713090',
rvsection: '2',
titles: 'Wikipedia:WikiProject National Register of Historic Places/Progress'
},
async:false
})
.responseText
);
for (var r in RevsWithMapsSection.query.pages["38959189"].revisions) {
if (RevsWithMapsSection.query.pages["38959189"].revisions[r].comment.indexOf("Updating county data as of ")>-1) {
Updates[Updates.length] = [RevsWithMapsSection.query.pages["38959189"].revisions[r].timestamp,RevsWithMapsSection.query.pages["38959189"].revisions[r]["*"]]
}
}
var RevsWithoutMapsSection = JSON.parse(
$.ajax({
dataType: "json",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'timestamp|comment|content',
rvlimit: 'max',
rvstartid: '568219687',
rvendid: '554210647',
rvsection: '1',
titles: 'Wikipedia:WikiProject National Register of Historic Places/Progress'
},
async:false
})
.responseText
);
for (var r in RevsWithoutMapsSection.query.pages["38959189"].revisions) {
if (RevsWithoutMapsSection.query.pages["38959189"].revisions[r].comment.indexOf("Updating county data as of ")>-1) {
Updates[Updates.length] = [RevsWithoutMapsSection.query.pages["38959189"].revisions[r].timestamp,RevsWithoutMapsSection.query.pages["38959189"].revisions[r]["*"]]
}
}
for (var i=0; i<Updates.length; i++) {
if (Updates[i][0]=="2013-05-23T07:37:05Z"||Updates[i][0]=="2013-05-23T07:32:15Z"||Updates[i][0]=="2013-10-14T01:15:21Z"||Updates[i][0]=="2013-05-29T07:14:43Z") {Updates.splice(i,1); i--; continue} // get rid of error/test updates
var thisrevtext=Updates[i][1]
Updates[i][1]=[];
Updates[i][1][0]=thisrevtext.match(/Total:(.|\n)*?\|}/g)[0].replace(/\n/g,"").replace(/!/g,"|")
for (var j=0; j<StatesArray.length; j++) {
var regex=new RegExp("\\|[ ]*?(\\[\\[)?.*?"+StatesArray[j][0]+"(\\]\\])?[ ]*?\\n(.|\\n)*?\\|--", "g")
Updates[i][1][Updates[i][1].length]=thisrevtext.match(regex)[0]
regex=new RegExp("\\|[ ]*?(\\[\\[)?.*?"+StatesArray[j][0]+"(\\]\\])?[ ]*?\\n", "g")
Updates[i][1][Updates[i][1].length-1]=Updates[i][1][Updates[i][1].length-1].replace(regex,"").replace(/\n/g,"") // drop state name
}
var d=new Date(Updates[i][0]);
var timestampstr=d.getUTCFullYear().toString()
var month=parseFloat(d.getUTCMonth()+1).toString()
if (month.length<2) month="0"+month
timestampstr+=month
var day=d.getUTCDate().toString()
if (day.length<2) day="0"+day
timestampstr+=day
Updates[i][0]=timestampstr
}
wikitext+='==Script output==\n<pre style="width:90%; height:300px; white-space:pre; overflow:scroll">\n# 1 Date 2 Total 3 Illustrated 4 %Illustrated 5 Articled 6 %Articled 7 Stubs 8 NRIS-only 9 Start+ 10 %Start+ 11 Unassessed 12 Untagged 13 Net_Quality '
var currentColumn=14
for (var i=0; i<StatesArray.length; i++) {
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Total '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Illustrated '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_%Illustrated '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Articled '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_%Articled '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Stubs '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_NRIS-only '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Start+ '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_%Start+ '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Unassessed '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Untagged '
if (currentColumn<100) wikitext+=' '
currentColumn++
wikitext+=currentColumn+' '+StatesArray[i][1]+'_Net_Quality '
if (currentColumn<100) wikitext+=' '
currentColumn++
}
wikitext+='\n'
for (var i=0; i<Updates.length; i++) {
wikitext+=Updates[i][0]+" "
for (var j=0; j<Updates[i][1].length; j++) {
// Total
wikitext+=Updates[i][1][j].split("|")[1].replace(/,/g,"").trim()
for (var k=0; k<20-Updates[i][1][j].split("|")[1].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
// Illustrated
wikitext+=Updates[i][1][j].split("|")[2].replace(/,/g,"").trim()
for (var k=0; k<20-Updates[i][1][j].split("|")[2].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
// %Illustrated
wikitext+=Updates[i][1][j].split("|")[3].replace(/%/g,"").trim()
for (var k=0; k<20-Updates[i][1][j].split("|")[3].replace(/%/g,"").trim().length; k++) {wikitext+=" "}
// Articled
wikitext+=Updates[i][1][j].split("|")[4].replace(/,/g,"").trim()
for (var k=0; k<20-Updates[i][1][j].split("|")[4].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
// %Articled
wikitext+=Updates[i][1][j].split("|")[5].replace(/%/g,"").trim()
for (var k=0; k<20-Updates[i][1][j].split("|")[5].replace(/%/g,"").trim().length; k++) {wikitext+=" "}
// Stubs
if (parseFloat(Updates[i][0])>=20130901) {
wikitext+=Updates[i][1][j].split("|")[6].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[6].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[6].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
} else {
wikitext+="- "
}
// NRIS-only
if (parseFloat(Updates[i][0])>20131120) {
if (parseFloat(Updates[i][0])==20131121) {
wikitext+="- "
} else {
wikitext+=Updates[i][1][j].split("|")[7].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[7].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[7].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
}
} else {
wikitext+="- "
}
// Start+
if (parseFloat(Updates[i][0])>=20130901) {
if (parseFloat(Updates[i][0])>20131120) {
wikitext+=Updates[i][1][j].split("|")[8].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[8].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[8].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
} else {
wikitext+=Updates[i][1][j].split("|")[7].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[7].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[7].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
}
} else {
wikitext+="- "
}
// %Start+
if (parseFloat(Updates[i][0])>=20130901) {
if (parseFloat(Updates[i][0])>20131120) {
wikitext+=Updates[i][1][j].split("|")[9].replace(/%/g,"").trim()
if (Updates[i][1][j].split("|")[9].replace(/%/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[9].replace(/%/g,"").trim().length; k++) {wikitext+=" "}
} else {
wikitext+=Updates[i][1][j].split("|")[8].replace(/%/g,"").trim()
if (Updates[i][1][j].split("|")[8].replace(/%/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[8].replace(/%/g,"").trim().length; k++) {wikitext+=" "}
}
} else {
wikitext+="- "
}
// Unassessed
if (parseFloat(Updates[i][0])>=20130901) {
if (parseFloat(Updates[i][0])>20131120) {
wikitext+=Updates[i][1][j].split("|")[10].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[10].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[10].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
} else {
wikitext+=Updates[i][1][j].split("|")[9].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[9].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[9].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
}
} else {
wikitext+="- "
}
// Untagged
if (parseFloat(Updates[i][0])>=20130901) {
if (parseFloat(Updates[i][0])>20131120) {
wikitext+=Updates[i][1][j].split("|")[11].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[11].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[11].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
} else {
wikitext+=Updates[i][1][j].split("|")[10].replace(/,/g,"").trim()
if (Updates[i][1][j].split("|")[10].replace(/,/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[10].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
}
} else {
wikitext+="- "
}
// Net Quality
if (parseFloat(Updates[i][0])>20131121) {
wikitext+=Updates[i][1][j].split("|")[12].replace(/%/g,"").trim()
if (Updates[i][1][j].split("|")[12].replace(/%/g,"").trim().length==0) {
wikitext+="-"
} else {
wikitext+=" "
}
for (var k=0; k<19-Updates[i][1][j].split("|")[12].replace(/%/g,"").trim().length; k++) {wikitext+=" "}
} else {
wikitext+="- "
}
}
wikitext+="\n"
}
wikitext+='</pre>\n\n'
var pagetext=getProgressHistoryWikitext("Wikipedia:WikiProject National Register of Historic Places/Progress/History")
wikitext = pagetext.substr(0,pagetext.indexOf("==Script output=="))+wikitext
DumpData({
title: wgPageName,
text: wikitext,
summary: 'Generate tables of historical data from [[WP:NRHPPROGRESS|Progress page]] to be turned into graphs over time'
});
}
function DumpData(info) {
$.ajax({
url: mw.util.wikiScript( 'api' ),
type: 'POST',
dataType: 'json',
async: false,
data: {
format: 'json',
action: 'edit',
title: info.title,
text: info.text,
summary: info.summary,
token: mw.user.tokens.get( 'editToken' )
}
})
.done (function( data ) {
if (data && data.edit && data.edit.result && data.edit.result == 'Success') {
alert("Done! Refresh the page to see the result!")
} else {
alert("The edit query returned an error.")
}
})
.fail (function() {
alert('Ajax failure.');
});
}
function getProgressHistoryWikitext(title) {
try {
var output=JSON.parse(
$.ajax({
dataType: "json",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'content',
titles: title,
indexpageids: true,
redirects: 'true'
},
async:false
})
.responseText
);
for (page in output.query.pages) {
wikitext = output.query.pages[page].revisions[0]['*'];
}
return wikitext
}
catch(err) {
return "error"
}
}
function MakeHistoryPreCopyable() {
if (wgPageName != "Wikipedia:WikiProject_National_Register_of_Historic_Places/Progress/History") return;
var pres = document.getElementsByTagName('pre');
while (pres.length>0) {
var textarea = document.createElement('textarea');
textarea.innerHTML = pres[0].innerHTML;
pres[0].parentNode.insertBefore(textarea,pres[0].nextSibling);
pres[0].parentNode.removeChild(pres[0]);
textarea.setAttribute('style', 'width: 100%; height: 300px')
textarea.setAttribute('onclick', 'this.focus();this.select()')
textarea.setAttribute('wrap', 'off')
}
}
addOnloadHook(MakeHistoryPreCopyable)
addOnloadHook(ProgressPageHistoryButton)