MediaWiki:Common.js/User:Bawolff/sandbox/catStats

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

/*

*/
/*global Bawolff, addOnloadHook, os_initHandlers */
/*jslint browser:true */

mw.loader.load( '/w/index.php?title=' + 'User:Bawolff/mwapilib.js' + '&action=raw&ctype=text/javascript' );

/*
This generates statistics from categories

At the moment, it only does numb of articles added to category at specfic time range.
might do something else (graphs?) later

Note, this doesn't do very much error handling (I'm lazy). It relies on the api to yell at us for invalid input
*/

if (!window.Bawolff) {
    var Bawolff = {};
}
Bawolff.catStats = {};


Bawolff.catStats.findNumb = function (opt) {
/*
example opt:
{cat: "category:Published", limit: 5000, ns: "0", start: "2008-01-01T00:00:00Z", end ""2009-01-01T00:00:00Z"}
ns is optional
*/
    var req = new Bawolff.mwapi.Request({action:"query", rawcontinue: "", list: "categorymembers", cmlimit: opt.limit, cmprop: "timestamp", cmtitle: opt.cat, cmsort: "timestamp", cmend: opt.end, cmstart: opt.start});
    if (opt.ns) {
        req.setParam("cmnamespace", opt.ns);
    }

    req.send(function (res) {Bawolff.catStats.dispRes(res, opt);});

}

Bawolff.catStats.setup = function () {
    var container = document.getElementById('catStats');
    if (!container) {
        return false;
    }

    container.innerHTML = '<form id="catStatsForm" action="#" method="GET" onsubmit="return Bawolff.catStats.submit()"><fieldset><legend>CatStats</legend><label for="category">Category: </label><input id="category" name="category" value="Category:Published" type="value" step="0.001"/><br/><label for="start">From (<kbd>YYYY-MM-DDTHH:MM:SS</kbd>): </label><input id="start" name="start" value="2004-01-01T00:00:00Z" type="datetime" step="1"/> <label for="end"> Until (<kbd>YYYY-MM-DDTHH:MM:SS</kbd>): </label><input id="end" name="end" value="2009-01-01T00:00:00Z" type="datetime" step="1"/><br/><label for="ns">Namespaces (number separated by <kbd>|</kbd>; blank for all): </label><input id="ns" name="ns" value="0" length="4" type="value"/><input type="hidden" name="ns14" value="1"/><br/><input type="submit" value="Get stats"/></fieldset></form><h2>Results</h2><ul id="catStatsRes"></ul>';

    os_initHandlers('category','catStatsForm', document.getElementById('category'));
}

Bawolff.catStats.submit = function () {
//Handles onsubmit from form
    var input = {};
//example input = {cat: "category:Published", limit: 5000, ns: "0", start: "2008-01-01T00:00:00Z", end: "2009-01-01T00:00:00Z"}
    var form = document.getElementById('catStatsForm');
    input.cat = form.category.value;
    input.start = form.start.value;
    input.end = form.end.value;
    if (form.ns.value.match(/^[0-9\|]*$/)) {
        input.ns = form.ns.value;
    }
    else {
        alert('Invalid namespace specified. (If you just wanted talk and main namespace, type "0|1" (without quotes)');
    }
    //limit should be 5000 for sysop/bot, 500 for plain folk
    if (mw.config.get('wgUserGroups') && (mw.config.get('wgUserGroups').join("").indexOf("sysop") !== -1 || mw.config.get('wgUserGroups').join("").indexOf("bot") !== -1)) {
        input.limit = 5000; //should be 5000 for sysop
    }
    else {
        input.limit = 500; //should be 5000 for sysop
    }

    Bawolff.catStats.findNumb(input);
    return false; //no submit

}

Bawolff.catStats.dispRes = function(res, opt) {
    var list = document.getElementById("catStatsRes");
    var container = document.createElement("li");

    var cm = res.getElementsByTagName("cm");

    if (res.getElementsByTagName('query-continue')[0]) { //If there is more than the max query
        container.appendChild(document.createTextNode('More than '));
    }
    container.appendChild(document.createTextNode(cm.length + " articles appeared in "));
    var a = document.createElement('a');
    a.href = mw.config.get('wgArticlePath').replace("$1", encodeURIComponent(opt.cat));
    a.appendChild(document.createTextNode(opt.cat));
    container.appendChild(a);
    container.appendChild(document.createTextNode(" between the dates of " + cm[0].getAttribute('timestamp') + " and " + cm[cm.length-1].getAttribute('timestamp') ));

    if (opt.ns) {
        container.appendChild(document.createTextNode(" in namespace(s): " + opt.ns.replace(/\|/g, ", ").replace(/\b0\b/, "main")));
    }

    list.appendChild(container);

}


$(Bawolff.catStats.setup);
//