MediaWiki:Gadget-apiUtils.js

var i18n = { reload_confirmation : 'This will reload the page. Continue?' }

window.purgeTitle = function(title) { title = title ? title : mw.config.get('wgPageName'); return new mw.Api.postWithToken('csrf', {		action: 'purge',		titles : mw.config.get('wgPageName')	}); }

window.purgeWithReload = function(title) { return purgeTitle(title).then(function(data) {		location.reload;	}); }

window.purgeWithConfirmationAndReload = function(title) { if (! confirm(i18n.reload_confirmation)) return $.Deferred.reject; return purgeWithReload(title); }

window.blankEdit = function(title) { title = title ? title : mw.config.get('wgPageName'); return new mw.Api.postWithToken('csrf', {		action : 'edit',		title : title,		appendtext : '',	}); }

window.blankEditWithReload = function(title) { return blankEdit(title).then(function(data) {		location.reload;	}); }

window.blankEditWithConfirmationAndReload = function(title) { if (! confirm(i18n.reload_confirmation)) return $.Deferred.reject; return blankEditWithReload(title); }

window.doesPageExist = function(title) { return new mw.Api.get({		action : 'query',		prop: 'revisions',		titles: title,		rvprop : 'ids'	}).then(function(data) {		if (data.query.pages[-1]) {			return false;		}		return true;	}); }

window.getWikitext = function(title) { console.log('getting wikitext'); return new mw.Api.get({		action : 'query',		prop:'revisions',		rvprop: 'content',		rvlimit: 1,		titles: title	}).then(function(data) {		console.log('got wikitext (apiUtils)');		for (var p in data.query.pages) {			var text = data.query.pages[p].revisions[0]['*'];			return text;		}	}); }

window.savePage = function(title, text) { if (! title) return $.Deferred.resolve; return new mw.Api.postWithToken('csrf', {		action : 'edit',		title : title,		text : text	}); }