User:Mikevoir/WantedCategories.js

(function{	// Double load protection	if (document.querySelector('a#tu-wc')) {return;}	// Dependencies	var api;	var RUNNING = 0;	var list = document.createElement('ul');	var checked = [];	importArticle({ type: 'script', article: 'u:dev:MediaWiki:Placement.js' });	// Mapn object	var wantedCategories = {		init: function {			api = new mw.Api;			mw.hook('dev.placement').add(function(placement) { placement.script('WantedCategories'); $(placement.element('tools'))[placement.type('prepend')](   			    $('').append($('').attr({ href: '#', id: 't-wc' }).text('Wanted Categories').click(wantedCategories.loadUI))    			); });		},		loadUI: function {			document.querySelector('div#content.page-content').insertBefore(list, document.querySelector('div#mw-content-text.mw-body-content'));			wantedCategories.getCategories;		},		getCategories: function(continueFrom){			if ((!continueFrom||'string' !== typeof continueFrom) && RUNNING>0) {return;}			RUNNING++;			console.log('start running loop #' +RUNNING+' from ', (continueFrom||'__START__'));			var nextContinue;			var params = {				action: 'query',				list: 'allpages',				aplimit: 'max'			};			if (continueFrom && 'string' == typeof continueFrom) {params.apcontinue = continueFrom;}			api.get(params)			.then(function(data){ console.log('then', data); wantedCategories.parseCategories(data); if (data.continue && data.continue.apcontinue) {wantedCategories.getCategories(data.continue.apcontinue);} else {document.querySelector('div#content.page-content').insertBefore(document.createTextNode('Done loading'), list);} });		},		parseCategories: function(data) {			if (data.error || !data.query || !data.query.allpages || data.query.allpages.length == 0) {				console.log('early stop:', data);				document.querySelector('div#content.page-content').insertBefore(document.createTextNode('Done loading'), list);				return;			}			var pages = [];			data.query.allpages.forEach(function(page){ pages.push(page.title); });			// Break in 50s as that's the base limit of api			for (var i = 0; i < pages.length; i += 49) {			   var chunk = pages.slice(i, i + 49);				api.get({action:'query', titles: chunk, prop: 'categories', clshow: '!hidden', cllimit: 'max'}).then(function(_pages){ if (_pages && _pages.query && _pages.query.pages) { Object.keys(_pages.query.pages).forEach(function(id){							if (_pages.query.pages[id] && _pages.query.pages[id].categories) {								var allCats = [];								_pages.query.pages[id].categories.forEach(function(cat){ if (checked.indexOf(cat.title) == -1){ allCats.push(cat.title); }									checked.push(cat.title); });								for (var e = 0; e < allCats.length; e += 49) {								   var categs = allCats.slice(e, e + 49);									api.get({action:'query', titles: categs}).then(function(categories){ console.log('categories', categories); if (categories && categories.query && categories.query.pages && categories.query.pages['-1']) { var a = -1; while (categories.query.pages[a]) { var link = document.createElement('a'); link.href = '/wiki/' + categories.query.pages[a].title; link.innerHTML = categories.query.pages[a].title; var bullet = document.createElement('li'); bullet.append(link); list.append(bullet); a--; }										}									});								}							}						});					}				}				);			}		}	};	mw.loader.using('mediawiki.api').then(wantedCategories.init); });