(function () {
var ns1 = 0, ns2 = 1, query = {
action: 'query',
list: 'allpages',
aplimit: 'max',
apfilterredir: 'nonredirects',
format: 'json'
};
var interval = 100;
var ns1l = {}, ns2l = {},
p1 = false, p2 = false;
var d1 = {}, d2 = {}, i1, i2;
var w8er1 = $.Deferred(), w8er2 = $.Deferred();
console.log('1st stage:', ns1);
i1 = setInterval(function () {
if (p1) return;
p1 = true;
d1 = getData(ns1, d1.qc || '');
d1.done(function (data) {
if (!data || !data.qc || !data.data) {
clearInterval(i1);
i1 = null;
console.log('1st stage nearly completed');
}
if (!data || !data.data) {
clearInterval(i1);
i1 = null;
w8er1.resolve(ns1l);
return;
}
data.data.forEach(function (v) {
ns1l[v.title] = {
pageid: v.pageid,
ns: v.ns,
title: v.title
};
});// each data1
p1 = false;
if (!i1) w8er1.resolve(ns1l);
})
.fail(function (data) {
clearInterval(i1);
i1 = null;
console.log('stage1 fail', data);
w8er1.resolve(ns1l);
});
}, interval);// i1
console.log('2nd stage:', ns2);
i2 = setInterval(function () {
if (p2) return;
p2 = true;
d2 = getData(ns2, d2.qc || '');
d2.done(function (data) {
if (!data || !data.qc || !data.data) {
clearInterval(i2);
i2 = null;
console.log('2st stage nearly completed');
}
if (!data || !data.data) {
clearInterval(i2);
i2 = null;
w8er2.resolve(ns2l);
return;
}
data.data.forEach(function (v) {
ns2l[v.title.replace(/.*?\:/, '')] = {
pageid: v.pageid,
ns: v.ns,
title: v.title
};
});// each data2
p2 = false;
if (!i2) w8er2.resolve(ns2l);
})
.fail(function (data) {
clearInterval(i2);
i2 = null;
console.log('stage2 fail', data);
w8er2.resolve(ns2l);
});
}, interval);// i2
$.when(w8er1, w8er2).then(function (list1, list2) {
var list = [], s = '';
console.log('3rd stage: filter', {list1: list1, list2: list2});
$.each(list2, function (i, v) {
if (!list1[i]) {
s = '# ' + i + ' [[' + v.title + ']] ' + location.origin + '/wiki/' + v.title.replace(/ /g, '_');
list.push(s);
console.log('# ' + i, '[[' + v.title + ']]', location.origin + '/wiki/' + v.title.replace(/ /g, '_'));
}
});
console.warn('done');
console.warn('**********');
console.log(list.join('\n'));
console.warn('**********');
});// when.then
function getData (ns, apfrom) {
var def = $.Deferred();
console.log('resolving', ns, apfrom);
$.getJSON('/api.php', $.extend({}, query, {apnamespace: ns, apfrom: apfrom}))
.done(function (data) {
if (!data || !data.query || !data.query.allpages || data.error) {
def.reject(data);
return;
}
//console.log('getdata done', ns, apfrom, data);
def.qc = data['query-continue'] ? data['query-continue'].allpages.apfrom : null;
def.resolve({data: data.query.allpages, qc: def.qc});
})
.fail(function (data) {
console.log('getdata fail', ns, apfrom, data);
def.reject(data);
return;
});
return def;
}// getData
})();