Jump to content

User:Dudemanfellabra/Sandbox.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.
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","&nbsp;– ").replace("Z","&nbsp;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);