Jump to content

User:SD0001/deleted-metadata-link.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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 to deletedrevisions API output for deleted pages
// And a link to snippet of deleted text for AFD/PROD/G13 deleted content
$.ready.then(function() {
	if (mw.config.get('wgArticleId') === 0 && !!$('.mw-warning-with-logexcerpt:has(".mw-logline-delete")').length) {

		var apiParams = {
	        action: 'query',
	        prop:'deletedrevisions',
	        titles: mw.config.get('wgPageName').replace(/_/g, ' '),
	        drvprop: 'user|timestamp|tags|size',
	        drvlimit: 'max',
	        formatversion: 2
	    };
	    var link = '/w/api.php?' + $.param(apiParams);
	
	    $('.mw-warning-with-logexcerpt:first').before(
	        $('<div>').addClass('deleted-metadata-link').append(
	            'See ',
	            $('<a>').attr('href', link).text('deleted revisions')
	         )
	    );
	    
	    mw.loader.using(['mediawiki.api', 'ext.gadget.morebits']).then(function() {
		    new mw.Api().get(apiParams).then(function(data) {
		    	var user = data.query.pages[0].deletedrevisions[0].user;	
		    	var ts = new Morebits.date(data.query.pages[0].deletedrevisions[0].timestamp);	
		    	$('.deleted-metadata-link').append(
		    		': last edited by ',
		    		$('<a>').attr('href', '/wiki/User:' + encodeURIComponent(user)).text(user),
		    		' at ' + ts.format('HH:mm, D MMMM YYYY') + ' (' + getTimeZoneString() + ')'
		    	);
		    	var $latestLogLine = $('.mw-logline-delete').first();
		    	if ($latestLogLine.length) {
		    		var $firstLinkInComment = $latestLogLine.find('.comment a').first();
	    			var date = parseLocalDate($latestLogLine.find('a').first().text());
	    			if (date.isValid()) {
	    				if ($firstLinkInComment.is('[href^="/wiki/Wikipedia:Articles_for_deletion"]')) {
			    			getRevidLinkFromDate('User:SDZeroBot/AfD grid', date).then(function(link) {
			    				$('.deleted-metadata-link').append(' <a href="' + link + '">Look up snippet in AfD grid</a>');
			    			});
			    		} else if ($firstLinkInComment.is('[href^="/wiki/Wikipedia:PROD"]')) {
			    			getRevidLinkFromDate('User:SDZeroBot/PROD grid', date).then(function(link) {
			    				$('.deleted-metadata-link').append(' <a href="' + link + '">Look up snippet in PROD grid</a>');
			    			});
			    		} else if ($firstLinkInComment.is('[href^="/wiki/Wikipedia:Criteria_for_speedy_deletion#G13"]') || 
			    			$firstLinkInComment.is('[href^="/wiki/Wikipedia:CSD#G13"]')) {
			    			getRevidLinkFromDate('User:SDZeroBot/G13 Watch', date, true).then(function(link) {
			    				$('.deleted-metadata-link').append(' <a href="' + link + '">Look up snippet in G13 Watch</a>');
			    			});
			    		}	
	    			}
		    	}
		    });
	    });
	}
});

function parseLocalDate(dateText) {
	var date = new Morebits.date(dateText + ' (UTC)');
	var userTimeZoneOffset = parseInt(mw.user.options.get('timecorrection').split('|')[1]);
	return date.subtract(userTimeZoneOffset, 'minutes');
}

function getTimeZoneString() {
	var timecorrection = -new Date().getTimezoneOffset();
	var negative = false;
	if (timecorrection < 0) {
		timecorrection = -timecorrection;
		negative = true;
	}
	return 'UTC' + (negative ? '–' : '+') + (timecorrection / 60);
}


function getRevidLinkFromDate(page, date, postLog) {
	return new mw.Api().get({
		titles: page,
		prop: 'revisions',
		rvstart: postLog ? date.add(1, 'day').toISOString() : date.toISOString(),
		rvlimit: 1,
		formatversion: 2
	}).then(function(data) {
		return data.query.pages[0].revisions[0].revid;
	}).then(function(revid) {
		return '/w/index.php?title=' + encodeURIComponent(page) + '&oldid=' + revid + 
			'#:~:text=' + encodeURIComponent(Morebits.pageNameNorm); // jump to text in Chrome
	});
}