User:Krinkle/Snippets/Iterate SiteMatrix in JavaScript

* @param options {Object} * - centralApiPath string: Path to api of a wiki that has the SiteMatrix installed * - onIteration function: callback for action on each wiki */ if (mw.siteMatrix === undefined) { mw.siteMatrix = {}; } mw.siteMatrix.Iterator = function Iterate(options) { var methods, i, current, list, instance = this; if (!options.centralApiPath || !options.onIteration) { throw new Error('Invalid arguments'); }	if (!(this instanceof mw.siteMatrix.Iterator)) { throw new TypeError('Illegal function call'); }	instance.start = function start { if (i !== undefined) { throw new Error('Cannot start twice'); }		$.getJSON(options.centralApiPath + '?format=json&action=sitematrix&callback=?', function (data) {			i = 0;			list = [];			if (!data || !data.sitematrix) {				return;			}			$.each(data.sitematrix, function (key, value) { var wi, wo; var group; if (key === 'count') return; group = key === 'specials' ? value : value.site; if ($.isArray(group) && group.length) { for (wi = 0; wi < group.length; wi += 1) { // Only public wikis if (group[wi].private === undefined && group[wi].closed === undefined && group[wi].fishbowl === undefined) { list.push(group[wi]); }					}				}			});			instance.next;		}); };	instance.next = function next { if (i < list.length) { current = list[i]; options.onIteration(instance, current, i, list.length); i += 1; } else { options.onComplete(instance, current, list.length); }	};	return instance; };

Usages

 * m:User:Krinkle/Tools/Global SUL