User:Walrus068/mediawiki.js

var MediaWiki = (function {	function urlencode(params) {		var i, param, encoded = [];		if (Array.isArray(params)) {			for (i = 0; i < params.length; i += 1) {				param = params[i];				if (Array.isArray(param)) {					encoded.push(param[0] + "=" + encodeURIComponent(param[1]));				} else {					encoded.push(param, params[i + 1]);					i += 1;				}			}		} else if (typeof params === "object") {			for (i in params) {				if (params.hasOwnProperty(i)) {					encoded.push(i + "=" + encodeURIComponent(params[i]));				}			}		} else {			throw new TypeError("not a valid non-string sequence or mapping object");		}		return encoded.join("&");	}

function MediaWiki { this.api = wgServer + wgScriptPath + "/api.php?format=json"; this.token = null; }	MediaWiki.prototype.get = function get(params, cb) { var xhr = new XMLHttpRequest, self = this; params = params || ""; if (typeof params !== "string") { params = urlencode(params); }		xhr.onload = function { var e, res = this.responseText; res = JSON.parse(res); e = res.error; if (e) { throw new Error(e.code + ": " + e.info); }			if (typeof cb === "function") { cb.call(self, res); }		};		xhr.open("GET", this.api + "&" + params); xhr.send; };	MediaWiki.protoype.post = function post(params, cb) { var xhr = new XMLHttpRequest; if (typeof params !== "string") { params = urlencode(params); }		xhr.onload = function { var e, res = this.responseText; res = JSON.parse(res); e = res.error; if (e) { throw new Error(e.code + ": " + e.info); }			if (typeof cb === "function") { cb.call(self, res); }		};		xhr.open("POST", this.api); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(params); };	MediaWiki.prototype.fetchToken(title, callback) { var data = "action=query&prop=info|revisions&intoken=edit&rvprop=timestamp&indexpageids=1&titles=" + title; this.get(data, function (j) {			var query = j.query,				page = query.pages[query.pageids[0]],				edittoken = page.edittoken;			this.token = edittoken;			callback.call(this, edittoken, page.starttimestamp, page.revisions[0].timestamp, page.title);		}); };	return MediaWiki; });

function Page(title, mw) { this.title = typeof title === "string" ? title : wgPageName; this.mw = mw; } Page.prototype.read = function read(callback) { var data = { "action": "query", "prop": "revisions", "rvprop": "content", "titles": this.title, "indexpageids": 1 },		self = this; this.mw.get(data, function (j) {		var query = j.query,			pageid = query.pageids[0];		callback.call(self, query.pages[pageid].revisions[0]["*"]);	}); }; Page.prototype.write = function write(text, summary, minor, bot, callback) { var self = this; this.mw.fetchToken(this.title, function (token, start, base, title) {		var data = {			"title": title,			"starttimestamp": start,			"basetimestamp": base,			"token": token		};		if (typeof summary === "string") {			data.summary = summary;		}		if (minor) {			data.minor = 1;		}		if (bot) {			data.bot = 1;		}		self.mw.post(data, function (j) { callback.call(self, j.edit.result === "Success"); });	}); };