MediaWiki:FilterToOversight.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.
/**
 * List all deletion log entries that match a specific pattern
 *
 * @rev 1 (2012-10-21)
 * @author Rillke, 2012
 */
// List the global variables for jsHint-Validation. Please make sure that it passes http://jshint.com/
// Scheme: globalVariable:allowOverwriting[, globalVariable:allowOverwriting][, globalVariable:allowOverwriting]
/*global jQuery:false, mediaWiki:false*/
 
// Set jsHint-options. You should not set forin or undef to false if your script does not validate.
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, curly:false, browser:true*/
 
( function ( $, mw ) {
"use strict";
 
var dll;
 
dll = {
	/**
	* Creates the UI.
	*
	* @example
	*      dll.createUI();
	*
	* @context {dll}
	*/
	createUI: function( ) {
		if ('edit' === mw.config.get('wgAction')) return;
	
		// Change heading
		dll.$heading = $('h1');
		var $headingInner = dll.$heading.find('span');
		$headingInner = $headingInner.length ? $headingInner : dll.$heading;
		$headingInner.text("Deletion Log List - Events that could be oversighted");
		dll.$heading.after($.parseHTML('<small>Listing all log events that look like they have to be better suppressed. You must not use this tool for evil [=not against Rillke :=)]</small>'));
		
		document.title = "Deletion Log List - Events that could be oversighted";
		
		dll.$container = $('<div>').attr({
			'id': 'dll-main-container'
		});

		dll.$startNode = $('<a>').attr({
			href: '#change_start_date',
			title: "Change start date"
		}).on({
			click: function(e) {
				e.preventDefault();
			}
		}).text("Starting from now ($Now) looking into the past ".replace('$Now', new Date().toLocaleString())).appendTo(dll.$container);
		
		var _oscillate = function() {
			dll.$loadImage.delay(4000).fadeTo(3600, 0.2).delay(6000).fadeTo(3600, 0.6, _oscillate);
		};
		
		var img = '//upload.wikimedia.org/wikipedia/commons/a/ad/PrebleLogBook.jpg';
		
		dll.$innerContainer = $('<div>').css({
			'min-height': 700,
			'min-width': 617,
			'position': 'relative'
		}).appendTo(dll.$container);
		dll.$loadImage = $('<div>').fadeTo(0, 0).css({
			'background-image': 'url(\"' + img + '\")',
			height: 700,
			width: 617,
			position: 'absolute',
			top: '0.1em'
		}).appendTo(dll.$innerContainer);
		$('<img>').hide().on( 'load', function() {
			dll.$loadImage.fadeTo(1600, 0.6, _oscillate);
		}).attr({
			src: img
		}).appendTo(dll.$container);
		
		dll.$list = $('<ol>').css({
			'list-style-type': 'decimal',
			'position': 'absolute',
			'overflow': 'auto',
			'height': '100%',
			'text-shadow': '0 1px 0 rgba(255, 255, 255, 0.5)'
		}).appendTo(dll.$innerContainer);
		dll.$list.items = [];
		
		var stopOscillation = function() {
			dll.$loadImage.stop(true).fadeTo(500, 0.15);
		};
		
		var _onErr = function() {
			// ERR
		};
		
		var _success = function(result) {
			$.each(result.query.logevents, function(i, le) {
				if ($.inArray(le.action, ['event', 'revision', 'delete']) > -1 && /(?:non.{0,5}public|personal.{0,5}inf|priva(?:te|cy).{0,5}inf)/i.test(le.comment)) dll.addLine(le);
			});
			if (dll.$list.items.length < 50) {
				dll.queryAPI(result['query-continue'].logevents.lestart, '', _success, _onErr);
			} else {
				stopOscillation();
			}
		};
		
		dll.queryAPI('', '', _success, _onErr);
		
		$('#mw-content-text').html("").append(dll.$container);
	},
	addLine: function(le) {
		var gap;
		switch (le.action) {
			case 'revision':
				if ('oldimage' === le['0']) {
					gap = " (hide) old image " + le['1'] + ' ';
				} else {
					gap = "revision of ";
				}
				break;
			case 'event':
				gap = "event " + le['0'] + ' ';
				break;
			case 'delete':
				if (/^Deleted old revision \d{14}/.test(le.comment)) {
					gap = "old image revision " + le.comment.match(/\d{14}/)[0] + ' ';
				} else {
					gap = "";
				}
				break;
			default:
				gap = " <unknown action " + le.action + "> ";
				break;
		}
		
		var nsFm = mw.config.get('wgFormattedNamespaces'),
			$date = $('<a>', { 
				title: dll.getDateFromMWDate(le.timestamp).toLocaleString(), 
				text: le.timestamp.replace(/[T|Z]/g, ' '),
				href: mw.util.wikiScript() + '?' + $.param({
					title: 'Special:Log',
					limit: 5,
					type: 'delete',
					offset: Number(le.timestamp.replace(/[^\d]/g, ''))+1
				})
			}),
			$user = $('<a>', { href: mw.util.getUrl(nsFm[2] + le.user), text: le.user }),
			$gap = $('<span>', { text: gap }),
			$page = $('<a>', { href: mw.util.getUrl(le.title), text: le.title }),
			$comment = $('<i>', { html: le.parsedcomment }),
			$li = $('<li>').append($date, ' ', $user, " deleted ", $gap , $page, " because ", $comment).appendTo(dll.$list);
		dll.$list.items.push($li);
	},
	/** Helper
		@param {String} stTimestamp  MediaWiki-Timestamp of format YYYY-MM-DDThh:mm:ssZ 
		@return {Object} JavaScript Date-Object
	**/
	getDateFromMWDate: function( stTimestamp ) {
		var regex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/;
		var m1 = stTimestamp.match(regex);
		return new Date(m1[1], m1[2]-1, m1[3], m1[4], m1[5], m1[6]); // Wer hat sich diesen Unsinn ausgedacht?
	},
	queryAPI: function(startdate, user, cb, errCb) {
		var params = {
			action: 'query',
			list: 'logevents',
			lelimit: 500,
			letype: 'delete',
			leprop: 'ids|title|type|user|timestamp|comment|parsedcomment|details'
		};
		if (startdate) params.lestart = startdate;
		if (user) params.leuser = user;
		
		mw.loader.using(['ext.gadget.libAPI', 'mediawiki.util'], function() {
			mw.libs.commons.api.query(params, {
				method: 'POST',
				cache: false,
				cb: cb,
				errCb: errCb
			});
		});
	}
};
 
// Expose globally
window.deletionLogLister = dll;
 
 
// Fire off the rest as soon as the dom is ready
$( document ).ready( dll.createUI );
 
}( jQuery, mediaWiki ));