Jump to content

User:Dudemanfellabra/NRHPProgressHistory.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dudemanfellabra (talk | contribs) at 20:27, 6 May 2014 (debug). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// Test code here

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"],
    ["U.S. Minor Outlying Islands","UM"],
    ];

function ProgressPageHistoryButton() {
    if (wgPageName!="User:Dudemanfellabra/AltSandbox") 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 = '__NOTOC__\n'
    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].replace(/\n/g,"")
            alert(Updates[i][1])
        }
        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+='==Total==\n<pre style="width:90%; height:300px; white-space:pre-wrap; overflow:scroll">\n# Date          Total           '
    for (var i=0; i<StatesArray.length; i++) {
        wikitext+=StatesArray[i][1]+'_Total        '
    }
    wikitext+='\n'
    alert(wikitext)
    for (var i=0; i<Updates.length; i++) {
        wikitext+=Updates[i][0]+"        "
        for (var j=0; j<Updates[i][1].length; j++) {
            wikitext+=Updates[i][1][j].split("|")[2].replace(/,/g,"").trim()
            for (var k=0; k<16-Updates[i][1][j].split("|")[2].replace(/,/g,"").trim().length; k++) {wikitext+=" "}
        }
        wikitext+="\n"
    }
    wikitext+='</pre>\n\n'

/*
    wikitext+='==Illustrated==\n<pre style="width:90%; height:300px; white-space:pre-wrap; overflow:scroll">\n# Date            Illustrated    %Illustrated\n'
    for (var i=0; i<Updates.length; i++) {
        wikitext+=Updates[i][0]+"          "+Updates[i][1].split("!")[2].replace(/,/g,"").trim()+"          "+Updates[i][1].split("!")[3].replace(/%/g,"").trim()+"\n"
    }
    wikitext+='</pre>\n\n'

    wikitext+='==Articled==\n<pre style="width:90%; height:300px; white-space:pre-wrap; overflow:scroll">\n# Date            Articled       %Articled\n'
    for (var i=0; i<Updates.length; i++) {
        wikitext+=Updates[i][0]+"          "+Updates[i][1].split("!")[4].replace(/,/g,"").trim()+"          "+Updates[i][1].split("!")[5].replace(/%/g,"").replace(/\|}/g,"").trim()+"\n"
    }
    wikitext+='</pre>\n\n'

    wikitext+='==Quality==\n<pre style="width:90%; height:300px; white-space:pre-wrap; overflow:scroll">\n# Date            Stubs          NRIS-only     Start+         %Start+      Unassessed Untagged     Net Quality\n'
    for (var i=0; i<Updates.length; i++) {
        wikitext+=Updates[i][0]+"          "
        if (parseFloat(Updates[i][0])<20130901) {    // quality stats were not reliable before 20130903
            wikitext+="\n"
            continue
        }
        wikitext+=Updates[i][1].split("!")[6].replace(/,/g,"").trim()+"          "
        var currentSplit=7
        if (parseFloat(Updates[i][0])>20131120) {     // NRIS-only stats did not exist before 20131120
            if (parseFloat(Updates[i][0])==20131121) {
                wikitext+="-"
            } else {
                wikitext+=Updates[i][1].split("!")[currentSplit].replace(/,/g,"").trim()
            }
            for (var j=0; j<14-Updates[i][1].split("!")[currentSplit].replace(/,/g,"").trim().length; j++) {wikitext+=" "}
            currentSplit++
        } else {
            wikitext+="-             "
        }
        wikitext+=Updates[i][1].split("!")[currentSplit].replace(/,/g,"").replace(/%/g,"").trim()
        for (var j=0; j<15-Updates[i][1].split("!")[currentSplit].replace(/,/g,"").trim().length; j++) {wikitext+=" "}
        wikitext+=Updates[i][1].split("!")[currentSplit+1].replace(/,/g,"").replace(/%/g,"").trim()
        for (var j=0; j<14-Updates[i][1].split("!")[currentSplit+1].replace(/,/g,"").trim().length; j++) {wikitext+=" "}
        wikitext+=Updates[i][1].split("!")[currentSplit+2].replace(/,/g,"").replace(/%/g,"").trim()
        for (var j=0; j<11-Updates[i][1].split("!")[currentSplit+2].replace(/,/g,"").trim().length; j++) {wikitext+=" "}
        wikitext+=Updates[i][1].split("!")[currentSplit+3].replace(/,/g,"").replace(/%/g,"").replace(/\|}/g,"").trim()
        for (var j=0; j<13-Updates[i][1].split("!")[currentSplit+3].replace(/,/g,"").trim().length; j++) {wikitext+=" "}
        if (parseFloat(Updates[i][0])>20131121) {     // Net Quality stats did not exist before 20131121
            wikitext+=Updates[i][1].split("!")[currentSplit+4].replace(/,/g,"").replace(/%/g,"").replace(/\|}/g,"").trim()+"          "
        }
        wikitext+="\n"
    }
    wikitext+='</pre>'

*/

    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.');
    });
}

addOnloadHook(ProgressPageHistoryButton)