Sixtest Wiki:API4/Ajax

(function ajax {	"use strict";	// TODO: major cleanup, combine some ajax calls (.get, .getToken and .post requests are almost the same)	var extension, paused, pausedIntervalChecker = 500, pausedCheckState, settings = {}, splitInto;	extension = $API.create.extension('Ajax');	settings.paused = false;	settings.max = 3;	extension.pause = function (state) {		settings.paused = state !== undefined ? !!state : !settings.paused;	};	pausedCheckState = function (func) {		var interval, check;		check = function {			if (!settings.paused) {				clearInterval(interval);				func;			}		};		interval = setInterval(check, pausedIntervalChecker);	};	splitInto = function (arr, max) {		var i = 0, len = arr.length, result, pos;		max = max || settings.max;		for (i; i < len; i += 1) {			pos = parseInt(i / max, 10);			result[pos] = result[pos] || [];			result[pos][result[pos].length] = arr[i];		}		return result; };	extension.get = function (pagenames, callback) { var results = [], len, ajax, get, done = 0, check, max, i = 0; len = pagenames.length; check = function (i, j, success) { return function (response) { results[j] = success ? response : ''; done += 1; if (done === max || settings.max) { callback(results, i); done = 0; results = []; i += 1; if (i < len) { ajax(i + 1); }				}			};		};		ajax = function (i) { pausedCheckState(function {				var len, j = 0;				max = pagenames[i].length;				for (j; j < max; j += 1) {					$.ajax({ url: pagenames[i][j].pagename + '?action=raw', cache: false, type: 'GET', success: check(i, j, true), error: check(i, j, false) });				}			});		};		ajax(0); };	extension.post = function (pagenames, texts, callback) { var done = 0, submit, data = [], i, len, func, pagesExist, check, ajax, results = []; pagenames = extension.unthrottle(pagenames); texts = extension.unthrottle(texts); i = 0; len = pagenames.length; for (i; i < len; i += 1) { data[i] = { name: pagenames[i], text: texts[i] };		}		data = extension.throttle(data); check = function (i, j, success) { var max = data[i].length; return function (response) { results[j] = success ? response : ''; done += 1; if (done === max) { if (typeof callback === 'function') { callback(results, i); }					i += 1; done = 0; if (i < len) { ajax(i); }				}			};		};		ajax = function (i) { var max = data[i].length; results = []; pausedCheckState(function {				var len, j = 0;				for (j; j < max; j += 1) {					if (data[i][j].text !== '') {						$.ajax({ url: '/api.php', type: 'POST', data : { 'action': 'edit', 'title': data[i][j].name, 'format': 'json', 'text': data[i][j].text, 'token': $API.user.vars.edittoken, 'minor': 'minor', 'summary': 'JS edit test.' },							success: check(i, j, true), error: check(i, j, false) });					}				}			});		};		ajax(0); };	extension.unthrottle = function (arrs) { var newArr = [], merge; merge = function (arr) { var i = 0, len = arr.length; for (i; i < len; i += 1) { if (typeof arr[i] === 'object' && arr[i].length) { merge(arr[i]); } else { newArr[newArr.length] = arr[i]; }			}		};		merge(arrs); return newArr; };	extension.throttle = function (obj, amount) { var property, arr, split; split = function (arr, arrayLength) { var i = 0, curr, len = arr.length, arrs = []; if (arrayLength >= 1) { for (i; i < len; i += 1) { curr = Math.floor(i / arrayLength); if (arrs[curr] === undefined) { arrs[curr] = []; }					arrs[curr][arrs[curr].length] = arr[i]; }				i = 0; len = arrs.length; return arrs; }			return false; };		amount = amount || 3; if (obj.length === undefined) { arr = []; for (property in obj) { if (obj.hasOwnProperty(property)) { obj[property].pagename = property; arr[arr.length] = obj[property]; }			}		}		arr = split((arr || obj), amount); return arr; };	extension.getToken = function (callback) { var ajaxFunc; ajaxFunc = function { return function { $.ajax({					url: '/api.php',					type: 'GET',					dataType: 'json',					data: {						'action': 'query',						'format': 'json',						'prop': 'info',						'intoken': 'edit',						'titles': 'Main Page'					},					success: function (obj) {						var querypages, querypage;						if (obj !== undefined && obj.query !== undefined && obj.query.pages !== undefined) {							querypages = obj.query.pages;						}						for (querypage in querypages) {							if (querypages.hasOwnProperty(querypage) && querypages[querypage].edittoken) {								$API.user.vars.edittoken = querypages[querypage].edittoken;								callback;							}						}					}				}); };		};		if ($API.user.vars.edittoken !== undefined && $API.user.vars.edittoken !== '') { callback; } else { /* Get edittoken only if they don't have one yet */ if ($API.user === undefined) { $API.user = {}; }			pausedCheckState(ajaxFunc); }	};	extension.format = function (data, callback) { data = extension.throttle(data, settings.max); extension.get(data, function (responses, dataIndex) {			var regex, i, len, property, match, j, jlen, names = [], texts = [], cdata;			cdata = data[dataIndex];			i = 0;			len = cdata.length;			for (i; i < len; i += 1) {				names[i] = cdata[i].pagename;				texts[i] = responses[i];				for (property in cdata[i]) {					if (cdata[i].hasOwnProperty(property) && texts[i]) {						regex = new RegExp("(\\|\\s*" + property + "\\s*=\\s*)([\\s\\S]+?)[\\n\\s]+(\\|[\\n\\s]*[\\s\\S]+=|\\}\\}[\\s\\n]*$)");						match = texts[i].match(regex);						if (match !== null) {							texts[i] = texts[i].replace(match[0], match[1] + cdata[i][property] + "\n" + match[3]);						}					}				}			}			callback(names, texts);		}); };	extension.update = function (data, callback) { extension.getToken(function {			extension.format(data, function (names, texts) { extension.post(names, texts, callback); });		});	}; });