User:Sixorish/Scripts/QueryAPI.js

(function (tw) {   var results = [];    var PATH_API = "/api.php";    var DIR_CONTENT = "/wiki/";    var O_QUERY_GENERIC = {        'format': 'json',        'action': 'query'    };    var O_QUERY_CATEGORY_MEMBERS = {        'generator': 'categorymembers',        'gaplimit': '100'    };    var O_QUERY_CONTENTS = {        'prop': 'revisions',        'rvprop': 'content'    };    function mixin(base) {        var i, len, prop, o;        for (i = 1, len = arguments.length; i < len; i += 1) {            o = arguments[i];            for (prop in o) {                if (o.hasOwnProperty(prop)) {                    base[prop] = o[prop];                }            }        }        return base;    }    function query(url, data, callback) {        $.ajax({ 'url': url, 'type': 'GET', 'dataType': 'json', data: mixin({}, O_QUERY_GENERIC, data), success: callback });   }    function queryCategory(category, ns, callback) {        var data = mixin({}, O_QUERY_CATEGORY_MEMBERS, O_QUERY_CONTENTS, { 'gcmtitle': 'Category:' + category, 'namespace': ns       });        function queryContinue(o) {            var qPages, p;            // Record the items in the query            qPages = (o && o.query && o.query.pages); // Generator -> o.query.pages, List -> o.query.categorymembers.            console.log(o, qPages);            for (p in qPages) {                if (qPages.hasOwnProperty(p)) {                    console.log("works");                    results.push(qPages[p]);                    return results;                }            }            if (o && o["query-continue"] && o["query-continue"].categorymembers && o["query-continue"].categorymembers.gcmcontinue) {                query(PATH_API, mixin({}, data, {                    'gcmcontinue': o["query-continue"].categorymembers.gcmcontinue                }), queryContinue);            } else {                callback(results);            }        }        query(PATH_API, mixin({}, data), queryContinue); }   tw.results = results; tw.query = query; tw.queryCategory = queryCategory; return tw; }(tw = (window.tw || {}));

/* example tw.queryCategory('Items', '', function (r) {   var i, len = r.length, str = "";    for (i = 0; i < len; i += 1) {        str += "  " + r[i].pageid + "  " + r[i].title + "  ";    } });