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 start; var categories = []; var __DONE = 0; 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.renderCategories; },		renderCategories: function { var namespaces = [0, 6, 14, 828]; // [Main, File, Category, Module] namespaces.forEach(function(namespace){				wantedCategories.getCategories(null, namespace);			}); function waitForApi { if(__DONE < namespaces.length) { window.setTimeout(waitForApi, 100); /* this checks the flag every 100 milliseconds*/ } else { __DONE = 0; wantedCategories.parseCategories; }			}			waitForApi; },		getCategories: function(continueFrom, namespace){ // console.log('continueFrom:', continueFrom); RUNNING++; var params = { action: 'query', generator: 'allpages', gaplimit: 'max', prop: 'categories', cllimit: 'max', clshow: '!hidden', gapnamespace: namespace };			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, namespace);					} else {						__DONE++;					}				} else {					console.log('early stop, something went wrong:', data);					label.innerHTML = 'Early stop, either there\'s no wanted categories or something went wrong.';					return;				}			}); },		parseCategories: function { var count = document.createElement('span'); count.append(0); var parseDONE = false; if (categories.length>0) { categories.sort; 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;							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.innerHTML++;							}						} else {categories.splice;}					}); }			}			label.innerHTML = 'Done loading! Took '+(((new Date)-start)/1000)+' seconds.'; // Override the "loading" text label.append(' Loaded ', count, ' wanted categories.'); // "append" needed due to count being an element }	};

mw.loader.using('mediawiki.api').then(wantedCategories.init); });