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 label = document.createTextNode('Loading...');	var config = mw.config.get(['wgNamespaceNumber', 'wgCanonicalNamespace']);	var start;	var categories = [];	importArticle({ type: 'script', article: 'u:dev:MediaWiki:Placement.js' });	// Main 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(event) {			event.preventDefault;			list.append(label);			document.querySelector('div#content.page-content').insertBefore(list, document.querySelector('div#mw-content-text.mw-body-content'));			if (RUNNING==0){ start = new Date; }			wantedCategories.getCategories;		},		getCategories: function(continueFrom){			// console.log('continueFrom:', continueFrom);			RUNNING++;			var params = {				action: 'query',				generator: 'allpages',				gaplimit: 'max',				prop: 'categories',				cllimit: 'max',				clshow: '!hidden',				gapnamespace: config.wgNamespaceNumber			};			if (!!continueFrom) {Object.assign(params, continueFrom);}			api.get(params)			.then(function(data){ // console.log('then', data); if (!data.error && data.query && data.query.pages) { Object.keys(data.query.pages).forEach(function(ID){						var item = data.query.pages[ID];						if (item.categories && item.categories.length>0) {							var newC = item.categories.reduce(function(a, o){ if (categories.indexOf(o.title) == -1) {a.push(o.title);} return a;							}, []);							if (newC.length>0) {categories.concat(newC);}						}					}); console.log('categories', categories); console.log('data', data); if (data.continue) { // continueName - get value by the key that isn't just "continue" - could be "fucontinue", "aicontinue", "gaicontinue" var continueName = data.continue[Object.keys(data.continue).filter(function(k) {							return k !== 'continue';						})[0]]; label.textContent = 'Loading from: \d+$/, ).replace(/^\d+\|/, ) + ' ...'; wantedCategories.getCategories(data.continue); } else { label.textContent = 'Done loading! Took '+(((new Date)-start)/1000)+' seconds.'; wantedCategories.parseCategories; }				} else { console.log('early stop, something went wrong:', data); label = 'Early stop, either there\'s no unused files or something went wrong.'; return; }			});		},		parseCategories: function {			var wanted = [];			if (categories.length>0) {				categories.sort;				for (var i = 0; i<categories.length; i = i+49) {					var _cats = categories.splice(i, i+49);					api.get({action:'query', titles: _cats}).then(function(data){ if (data && data.query && data.query.pages && data.query.pages['-1']) { for (a = -1; data.query.pages[a]; a--){ var cat = data.query.pages[a]; var link = document.createElement('a'); link.href = '/wiki/' + cat.title; link.innerHTML = cat.title; var bullet = document.createElement('li'); bullet.id = bulletId; bullet.append(link); list.append(bullet); wanted.push(cat.title); }						} else {categories.splice;} });				}			}			label.textContent = label.textContent + ' Loaded ' + (wanted.length||0) + ' wanted categories.';		}	};	mw.loader.using('mediawiki.api').then(wantedCategories.init); });