User:Martijn Hoekstra/Infobox statistical analyses
Appearance
Counting the number of discussions listed by AM in 2014:
Method:
- Query AM's edits to TfD in 2014. If the new revision has more instances of {{Tfd links}} than the old revision, count it as a nomination.
All diffids for TfD in 2014:
(function(){
var api = new mw.Api();
var pageprefix = "Wikipedia:Templates for discussion/Log/2014";
var continuedata = {
continue: ''
};
function get_throttle(maxpar){
var mayrun = maxpar;
var queue = [];
var decrease_queue = function(){
mayrun += 1;
var next = queue.shift();
if (next){
var res = run(next[0]);
res.done(function(endres){next[1].resolve(endres);});
}
};
var run = function(f){
mayrun -= 1;
var p = f();
p.done(decrease_queue);
return p;
};
var offer = function(f){
if (mayrun > 0){
return run(f);
} else {
var d = $.Deferred();
var p = d.promise();
queue.push([f, d]);
return p;
}
};
return offer;
}
var throttle = get_throttle(15);
function throttledget(){
var args = arguments;
var thunk = function(){
return api.get.apply(api, args);
};
return throttle(thunk);
}
var basequery = {
action: 'query',
list: 'usercontribs',
ucuser: 'Pigsonthewing',
ucprop: ['ids', 'title'],
ucnamespace: 4,
ucend: '2014-01-01T00:00:00Z',
ucstart: '2015-01-01T00:00:00Z',
uclimit: 500
};
function point_promise(x){
var d = $.Deferred();
d.resolve(x);
return d.promise();
}
function traverse_promises(arr){
return arr.reduce( function(agg, cur_p){
return agg.then(function(res){ return cur_p.then(function(cur){ res.push(cur); return res;});});
}, point_promise([]));
}
function mergeobj(target, add) {
var res = {};
var curkey;
for(curkey in target){
res[curkey] = target[curkey];
}
for(curkey in add){
res[curkey] = add[curkey];
}
return res;
}
var r1 = api.get(mergeobj(basequery, continuedata));
var running = [];
function step(queryresult){
if (queryresult.hasOwnProperty("warnings")) {
console.warn(queryresult.warnings);
}
if (queryresult.hasOwnProperty("errors")){
console.error(queryresult.errors);
}
var relevantchanges = queryresult.query.usercontribs.filter(function(contrib){
return contrib.title.indexOf(pageprefix) === 0;
});
var diffs = relevantchanges.map(function(contrib){ return { parentid: contrib.parentid, revid: contrib.revid};});
running = running.concat(diffs);
if (queryresult.hasOwnProperty("continue")){
var newreq = api.get(mergeobj(basequery, queryresult["continue"]));
return newreq.then(step);
}
else {
return point_promise(running);
}
}
var p_ids = r1.then(step);
p_ids.done(function(finalres){
console.debug("done fetching revisions!", finalres);
});
function getparsetree(revid) {
var baseprops = {
action: "parse",
prop: [],
generatexml: ""
};
baseprops.oldid = revid;
return throttledget(baseprops).then(function(result){
var xmlstring = result.parse.parsetree["*"];
var parser = new DOMParser();
return parser.parseFromString(xmlstring, "application/xml");
});
}
function isTfDNom(parentid, revid){
if (parentid === 0){ return point_promise(true); }
var p_parent_parsetree = getparsetree(parentid);
var p_current_parsetree = getparsetree(revid);
return p_parent_parsetree.then(function(parenttree){
return p_current_parsetree.then(function(currenttree){
var parenttransclusions = countTransclusions("Tfd links")(parenttree);
var childtransclusions = countTransclusions("Tfd links")(currenttree);
if(parenttransclusions < childtransclusions){
//console.debug("revision " + revid + " is a TfD nom");
return true;
} else {
return false;
}
}
);});
}
function countTransclusions(template_name){
return function(parsetree) {
var result = 0;
var iter = parsetree.evaluate("//template/title/text()", parsetree, null, XPathResult.ANY_TYPE, null);
var item = iter.iterateNext();
while(item){
if (item.nodeValue == template_name){
result += 1;
}
item = iter.iterateNext();
}
return result;
};}
var TfD_maybenoms_p_arr_p = p_ids.then(function(diffs){
return diffs.map(function(diff){
var isnom_p = isTfDNom(diff.parentid, diff.revid);
return isnom_p.then(function(isnom){ return { isnom: isnom, parentid: diff.parentid, revid: diff.revid};});
});
});
var TfD_maybenoms_p = TfD_maybenoms_p_arr_p.then(function(TfD_maybenoms_p_arr) {return traverse_promises(TfD_maybenoms_p_arr);});
TfD_maybenoms_p.done(function(TfD_maybenoms){
var TfD_noms = TfD_maybenoms.filter(function(maybenom){
return maybenom.isnom;
});
console.debug("all nominations", TfD_noms);
});
var TFD_noms_p = TfD_maybenoms_p.then(function(TfD_maybenoms){
return TfD_maybenoms.filter(function(maybenom){
return maybenom.isnom;
});
});
function knuthshuffle(array) {
var i = array.length, tmp, ri ;
while (0 !== i) {
ri = Math.floor(Math.random() * i);
i -= 1;
tmp = array[i];
array[i] = array[ri];
array[ri] = tmp;
}
return array;
}
var TfD_noms_shuffeled_p = TFD_noms_p.then(function(Tfd_noms){
return knuthshuffle(Tfd_noms);
});
function makeTable(nomids) {
var start = "{|\n! style='text-align:left;' | nominating revision\n! Link\n! Nomination\n! Outcome\n! With/against nomination";
var end = "\n|}";
function makerow(revid) {
return "|-\n|{{oldid2|" + revid + "}}\n|\n|\n|\n|\n";
}
return nomids.reduce(function(agg, next, i, arr){ return agg + makerow(next.revid); }, start) + end;
}
TfD_noms_shuffeled_p.done(function(TfD_noms_shuffeled){
var firstones = TfD_noms_shuffeled.slice(0, 25);
console.debug(firstones);
console.debug(makeTable(firstones));
});
})();