User:Gary/show upload deletion logs.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.
/*
	SHOW UPLOAD DELETION LOGS
	Description: After clicking on a link to a deleted file, this script shows links to a file's deletion logs for convenience.
*/

if (typeof(showUploadDeletionLogs) == 'undefined') showUploadDeletionLogs = {};
if (typeof(unsafeWindow) != 'undefined')
{
	mw = unsafeWindow.mw;
}

function showUploadDeletionLogsFunction()
{
	// Set default configuration
	if (typeof(showUploadDeletionLogs) == 'undefined') showUploadDeletionLogs = {};
	if (typeof(showUploadDeletionLogs.linkToFileEdit) == 'undefined') showUploadDeletionLogs.linkToFileEdit = false;
	
	// Viewing an article, replace links to deleted images with links to file edit page, if preference is set.
	if ((mw.config.get('wgAction') == 'view' || mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit') && mw.config.get('wgPageName') != 'Special:Upload')
	{
		$('.new').each(function()
		{
			var a = $(this);
			var href = a.attr('href');
			
			if (href && href.indexOf('wpDestFile=') != -1)
			{
				if (showUploadDeletionLogs.linkToFileEdit) a.attr('href', generateFileEditLink(href))
				else a.attr('href', generateFileUploadLink(href));
			}
		});
	}
	// Clicked on a link to a deleted image, from an article
	else if (mw.config.get('wgPageName') == 'Special:Upload' && location.href.indexOf('wpDestFile=') != -1 && $('#contentSub2').length)
	{
		var editLink = $('<a class="external" href="' + generateFileEditLink(location.href) + '">Edit file</a>');
		var sub = $('#contentSub2').children().last().append(' / ').append(editLink);
	}
}

function generateFileEditLink(url)
{
	var wpDestFileIndex = url.indexOf('wpDestFile=');
	if (url.indexOf('&', wpDestFileIndex) != -1) var endIndex = url.indexOf('&', wpDestFileIndex);
	else var endIndex = url.length;
	return mw.config.get('wgScript') + '?title=File:' + url.substring(wpDestFileIndex + 'wpDestFile='.length, endIndex) + '&action=edit&redlink=1';	
}

function generateFileUploadLink(url)
{
	var wpDestFileIndex = url.indexOf('wpDestFile=');
	if (url.indexOf('&', wpDestFileIndex) != -1) var endIndex = url.indexOf('&', wpDestFileIndex)
	else var endIndex = url.length;
	return mw.config.get('wgScript') + '?title=Special:Upload&wpDestFile=' + url.substring(wpDestFileIndex + 'wpDestFile='.length, endIndex);	
}

$(document).ready(function()
{
	showUploadDeletionLogsFunction();
});