Jump to content

User:Gary/link intermediate revisions.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
	LINK INTERMEDIATE REVISIONS
	Description: Link intermediate revisions when viewing a diff, to easily see which revisions are between the two diffs currently being viewed.
*/

function createDiffMultiLink()
{
	if (!mw.util.getParamValue('diff') || !mw.util.getParamValue('oldid')) return false;
	$.get(mw.config.get('wgScriptPath') + '/api.php', { action: 'query', prop: 'revisions', titles: mw.config.get('wgPageName'), rvprop: 'timestamp', rvstartid: mw.util.getParamValue('diff'), rvendid: mw.util.getParamValue('oldid'), rvlimit: '5000', format: 'json', indexpageids: 1 }, diffMultiCallback);
}

function twoDigitPadding(integer)
{
	var string = integer.toString();
	if (string.length == 1) return '0' + integer;
	else return integer;
}

function diffMultiCallback(response)
{
	if (!response['query'] || !response['query']['pages'] || response['query']['pageids'][0] == -1) return false;
	
	var diffMulti = $('#bodyContent .diff-multi').eq(0);
	if (!diffMulti.length) return false;
	
	var pageId = response['query']['pageids'][0];
	var page = response['query']['pages'][pageId];
	var revisions = page['revisions'];
	if (!revisions) return false;
	
	var oldid = new Date(revisions[revisions.length - 1]['timestamp']);
	oldid = oldid.getUTCFullYear().toString() + twoDigitPadding(oldid.getUTCMonth() + 1).toString() + oldid.getUTCDate().toString() + twoDigitPadding(oldid.getUTCHours() + 1).toString() + twoDigitPadding(oldid.getUTCMinutes() + 1).toString() + twoDigitPadding(oldid.getUTCSeconds() + 1).toString();
	var diff = new Date(revisions[0]['timestamp']);
	diff = diff.getUTCFullYear().toString() + twoDigitPadding(diff.getUTCMonth() + 1).toString() + diff.getUTCDate().toString() + twoDigitPadding(diff.getUTCHours() + 1).toString() + twoDigitPadding(diff.getUTCMinutes() + 1).toString() + twoDigitPadding(diff.getUTCSeconds() + 1).toString();
	
	var a = $('<a href="' + mw.config.get('wgScript') + '?title=' + mw.config.get('wgPageName') + '&action=history&offset=' + diff + '&limit=' + revisions.length + '"></a>').append(diffMulti.contents().eq(0));
	diffMulti.prepend(a);
}

$(createDiffMultiLink);