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.createElement('div');	label.innerHTML = 'Loading wanted categories...';	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'));			document.querySelector('div#content.page-content').insertBefore(label, list);			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 = categories.concat(newC);}						}					}); //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]].replace(/\|\d+$/, ).replace(/^\d+\|/, ); wantedCategories.getCategories(data.continue); } else { label.innerHTML = '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 label1 = document.createElement('span');			var count = 0			label1.innerHTML = count;			label1.classList.add('WC-count');			if (categories.length>0) {				categories.sort;				label.append(' Loaded ', label1, ' wanted categories.');				for (var i = 0; i<categories.length; i = i+49) {					var _cats = categories.slice(i, i+49);					//console.log('_cats', _cats);					api.get({action:'query', titles: _cats, prop: 'info'}).then(function(data){ //console.log('cats:', data); if (data && data.query && data.query.pages && data.query.pages['-1']) { var a = -1; console.log(data.query.pages[a]); while (data.query.pages[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.append(link); list.append(bullet); a--; count++; document.querySelector('.WC-count').innerHTML = count; }						} else {categories.splice;} });				}			}		}	};	mw.loader.using('mediawiki.api').then(wantedCategories.init); });