User:Mdaniels5757/PurgeTab.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * Add buttons (located in the "more" tab for vector users) to
 * purge, hard purge (forcerecursivelinkupdate=1), and null edit.
 * 
 * See documentation at [[User:Mdaniels5757/PurgeTab]]
 *
 * @source https://en.wikisource.org/wiki/MediaWiki:Gadget-PurgeTab.js
 * @revision revid 13911215 (10:32, 23 February 2024)
 */
 
$(function () {
	if (!mw.config.get('wgArticleId')) {
		return;
	}

	let purgePortlet = mw.util.addPortletLink(
		'p-cactions', '#', 'Purge', 'ca-purge',
		'Purge cache for this page', '*'
	);
	$(purgePortlet).on('click', e => {
		doPurge(false);
		e.preventDefault();
	});

	let hardpurgePortlet = mw.util.addPortletLink(
		'p-cactions', '#', 'Hard purge', 'ca-purge-hardpurge',
		'Purge with forced recursive-link table update', ','
	);
	$(hardpurgePortlet).on('click', e => {
		doPurge(true);
		e.preventDefault();
	});

	let nulleditPortlet = mw.util.addPortletLink(
		'p-cactions', '#', 'Null edit', 'ca-nulledit',
		'Perform a null edit on this page', '0'
	);
	$(nulleditPortlet).on('click', e => {
		doNullEdit();
		e.preventDefault();
	});
});

function doPurge (hard) {
	let params = {
		action: 'purge',
		pageids: mw.config.get('wgArticleId')
	};
	if (hard) {
		params.forcerecursivelinkupdate = 1;
		params.redirects = 1;
	}
	new mw.Api().post(params)
		.catch(httpErrorHandler)
		.then(() => location.reload());
}

function doNullEdit () {
	new mw.Api().postWithEditToken({
		action: 'edit',
		pageid: mw.config.get('wgArticleId'),
		appendtext: '',
		watchlist: 'nochange',
		nocreate: '1',
	})
	.catch(httpErrorHandler)
	.then(() => location.reload());
}

function httpErrorHandler (code, details) {
	let msg;
	switch (code) {
		case 'http':
			msg = 'HTTP error: ' + details.xhr.statusText;
			break;
		case 'ok-but-empty':
			msg = 'Received empty response.';
			break;
		default:
			msg = details.error.info;
	}
	mw.notify(msg, {title: 'Purge failed', type: 'error', tag: 'purge'});
}

/**
* [[Category:User scripts]]
*/