MediaWiki:Common.js/rcpatrol.js

/* rcpatrol.js * * Script for RuneScape:RC Patrol written by Suppa Chuppa * Original http://runescape.wikia.com/wiki/User:Suppa_chuppa/rcpatrol.js */ $(function {

// set these in accordance with your wiki's warning templates (without "Template:") var warn_templates = ["Test1", "Test2", "Warn3", "No Vandal", "Error1", "Error2", "Deu", "Pov1", "Pov2", "RDT", "Warnfl", "Charm_log_1", "Charm_log_2", "Gemw1", "Gemw2"];

// whether a user may rollback edits and block users var is_sysop = $.inArray('sysop', wgUserGroups) > -1; var is_rollback = is_sysop || $.inArray('rollback', wgUserGroups) > -1;

// add settings to the page $('#rcp_settings').empty.append('Namespaces:' +				' ' +				' ');

var $select = $(''); $select.append(' All (main) ');

for(var i = 1; i < 200; i++) { if(typeof wgFormattedNamespaces[i] !== "undefined") { $select.append('' + wgFormattedNamespaces[i] + ' '); } }

$('#namespace_s').append($select);

$('#limit_s').empty.append(' Number of edits to fetch: '); $('#anon_s').empty.append(' Only show anonymous users: '); $('#minor_s').empty.append(' Only show non-minor edits: '); $('#since_s').empty.append(' Show edits since: '); $('#submit_s').empty.append('Submit '); $('#go').click(function {	$('#go').attr('disabled', 'disabled').text('Loading...');	go; }); $('#go').after(' Commands: ');

//================== // Global variables //================== var revIds, old_revIds, pages, users, diffs, comments, tokens, timestamps, tops; var show_diff, parse_diffs; var counter = 0, working = false, edittoken, starttimestamp;

/* * */ function warn(user, i) { $.ajax({		url: wgServer + '/api.php',		data: {			'action': 'query',			'format': 'json',			'prop': 'revisions',			'rvprop': 'timestamp',			'titles': 'User talk:' + user,			'indexpageids': true		},		success: function(response) {			var page = response.query.pages[response.query.pageids[0]];			$.post( wgServer + '/api.php', {					'action': 'edit', 'title': 'User talk:' + user, 'appendtext': '\n 19:46, September 8, 2015 (UTC)', 'token': edittoken, 'minor': 'yes', 'summary': 'Warning: through RC Patrol', 'basetimestamp': page.missing ? page.revisions[0].timestamp : '', 'starttimestamp': starttimestamp },				function(response) { }, 'json' );			$('#warn-form').empty.append(' warned '); 		}	}); }

/* * Set token and timestamp values for editing */ function get_edit_info { $.ajax({		url: wgServer + '/api.php',		dataType: 'json',		data: {			'format': 'json',			'action': 'query',			'prop': 'info|revisions',			'intoken': 'edit',			'titles': 'User:Suppa chuppa',			'indexpageids': true		},		success: function(response) {			var page = response.query.pages[response.query.pageids[0]];			edittoken = page.edittoken;			starttimestamp = page.starttimestamp;		}	}); }

/* * Save the contents of a page through the API * @param: content, the content to save * @param: i, the index of the page in the pages array */ function save(content, i) { $.ajax({		url: wgServer + '/api.php',		dataType: 'json',		type: 'POST',		data: {			'action': 'edit',			'title': pages[i],			'text': content,			'summary': $('#summary').val + ' (through RC Patrol)',			'basetimestamp': starttimestamp,			'startimestamp': starttimestamp,			'token': edittoken		},		success: function(response) {		}	}); }

/* * Turns #diff into an edit form to make minor changes * @param: active, a number indicating which diff is being edited. * @param: i, the index of the revid and page in the revIds and pages arrays */ function edit_form(active, i) { $('#diff').empty.append(' ');

$.ajax({		url: wgServer + '/api.php',		dataType: 'json',		data: {			'action': 'query',			'format': 'json',			'prop': 'revisions',			'titles': pages[i],			'rvprop' : 'content',			'rvlimit': 2,			'rvstartid': old_revIds[i],			'rvdir': 'newer',			'indexpageids': true		},		success: function(response) {			var page = response.query.pages[response.query.pageids[0]];			$('#nav').empty.append('Summary:  Save cancel');			if(active < 3) {				$('#content_1').val(page.revisions[0]['*']);				$('#content_2').val(page.revisions[1]['*']);				$('#save').click(function { save($('#content_' + active).val, i); show_diff(i); get_edit_info; });			}			else {				$('#content_1').val(page.revisions[1]['*']);				$('#save').click(function { save($('#content_' + (active-2)).val, i); show_diff(i); get_edit_info; });			}			$('#summary').keydown(function(e) { if(e.which == 13) { $('#save').click; $('#commands').focus; return false; }			});			$('#cancel').click(function { show_diff(i); if(active > 2) { $('#cur-diff').click; } });		}	}); if(active > 2) { $.ajax({			url: wgServer + '/api.php',			dataType: 'json',			data: {				'action': 'query',				'format': 'json',				'prop': 'revisions',				'titles': pages[i],				'rvprop' : 'content',				'rvlimit': 1,				'indexpageids': true			},			success: function(response) {				$('#content_2').val(response.query.pages[response.query.pageids[0]].revisions[0]['*']);			}		}); } }

/* * Show the difference between the selected revision and the current * @param: i, the index of the revision in the revIds array */ function show_cur(i) { $('#cur-diff').empty.append(' Loading... '); $('#diff').attr('class', i); $.ajax({		url: wgServer + '/api.php',		dataType: 'json',		data: {			'format': 'json',			'action': 'query',			'revids': revIds[i],			'prop': 'revisions',			'rvprop': 'timestamp|parsedcomment|user|ids',			'rvdiffto': 'cur',			'rvtoken': 'rollback',			'indexpageids': 'true'		},		success: function(response) {			$('#compare-toggle').toggle;			$('#cur-diff').toggle;			$('#prev-diff').toggle;			$('#diff-options').toggle;			$('#rev-inner').toggle;			$('#comment').toggle;			$('#diff').empty.append(' ');

$('#diff tr:first-child td.diff-lineno:first-child').append(' (edit) '); $('#edit-3').click(function { edit_form(3, i); }); $('#diff tr:first-child td.diff-lineno:nth-child(2)').append(' (edit) '); $('#edit-4').click(function { edit_form(4, i); }); }	}); }

/* * Provide a link to view the difference between the shown diff and the current revision * @param: i, the index of the revision in the revIds array */ function not_top(i) { $('#cur-link').empty.append('compare to current revision'); $('#cur-diff').click(function { show_cur(i) }); $('#prev-diff').click(function {		$('#compare-toggle').toggle;		$('#prev-diff').toggle;		$('#cur-diff').empty.append('compare to current revision');		$('#cur-diff').toggle;		$('#diff-options').toggle;		$('#rev-inner').toggle;		$('#comment').toggle;		show_diff(i);	}); }

/* * Indicate that this revision is on top and provide a rollback link * @param: i, the index of the revision in the revIds array */ function top(i) { $('#cur-link').empty.append(' most current revision '); // show the rollback option if the user is a sysop or has rollback rights if(is_rollback) { $('#diff-options').append(' &bull; [rollback</a>] '); $('#rollback-link').click(function {				$.ajax({ url: wgServer + '/api.php', dataType: 'json', type: 'POST', data: { 'format': 'json', 'action': 'rollback', 'title': pages[i], 'user': users[i], 'token': tokens[i], 'summary': 'Reverted edits by ' + users[i] + ' (talk) using RC Patrol' },				success: function(response) { $('#rollback-link').empty.append(' reverted '); }			});		});	} }

/* * Append diff options based on user groups * @param: i, the index of the revision in the revIds array */ function append_diff_options(i) { $('#undo-link').click(function { $('#undo-toggle').toggle; }); $('#undo-go').click(function {		summary = $('#undo-summary').val;		if(summary.trim)			summary = ': ' + summary;		undo(summary, i);		get_edit_info;		$('#undo-form').empty.append(' undone ');	}); $('#undo-summary').keydown(function(e) {		if(e.which == 13) {			$('#undo-go').click;			$('#commands').focus;			return false;		}	});

$('#undo-form').after( ' &bull; ' +					'warn</a>' +					' ' +						'<select id="warn-templates" />' +						'<button id="warn-go">Warn ' +					' ' +				' '); for(var t = 0; t < warn_templates.length; t = t + 1) { $('#warn-templates').append('<option value="' + warn_templates[t] + '">' + warn_templates[t] + ' '); }	$('#warn-go').click(function {		warn(users[i], i);		get_edit_info;	}); $('#warn-templates').keydown(function(e) {		if(e.which == 13) {			$('#warn-go').click;			$('#commands').focus;			return false;		}	}); $('#warn-link').click(function {		$('#warn-toggle').toggle;	});

// show the block option if the user is a sysop if(is_sysop) { $('#diff-options').append(' &bull; block</a> ' +					'<select id="block-duration">' +						' 2 hours ' +						' 1 day ' +						' 3 days ' +						' 1 week ' +					' ' +					'Summary: <input type="textbox" id="block-reason" /> ' +					'<button id="block-go">Block '); $('#block-link').click(function {			var block_token, base_stamp, start_stamp;			$('#block-toggle').toggle;			$('#block-go').click(function { var expiry = $('#block-duration').val; $.ajax({					url: wgServer + '/api.php',					dataType: 'json',					data: {						'format': 'json',						'action': 'query',						'prop': 'info|revisions',						'intoken': 'block',						'titles': pages[i],						'indexpageids': true					},					success: function(response) {						block_token = response.query.pages[response.query.pageids[0]].blocktoken;						block(users[i], block_token);					}				}); });		});	}	$('#diff-options').append(' '); $('#diff').empty.append(' '); $('#nav').empty.append('<button type="button" id="prev_b">Prev <button type="button" id="next_b">Next '); if(i > 0) $('#prev_b').click(function { show_diff(i-1); }); else $('#prev_b').attr('disabled', ''); if(i < revIds.length - 1) $('#next_b').click(function { show_diff(i+1); }); else $('#next_b').attr('disabled', '');

$('#diff tr:first-child td.diff-lineno:first-child').append(' (edit)</a> '); $('#edit-1').click(function { edit_form(1, i); });

$('#diff tr:first-child td.diff-lineno:nth-child(2)').append(' (edit)</a> '); $('#edit-2').click(function { edit_form(2, i); });

$('#commands').unbind; $('#commands').keydown(function(e) {		if(e.which == 37 && i > 0) { // left arrow			$('#commands').val();			show_diff(i-1);		}		else if(e.which == 39 && i < revIds.length - 1) { // right arrow			$('#commands').val();			show_diff(i+1);		}		else if(e.which == 82 && tops[i] && is_rollback) { // 'r'			$('#commands').val();			$('#rollback-link').click;		}		else if(e.which == 85) { // 'u'			$('#commands').val();			$('#undo-link').click;			$('#undo-summary').focus;			return false;		}		else if(e.which == 87) { // 'w'			$('#warn-link').click;			$('#warn-templates').focus;			return false;		}		else if(e.which == 66 && is_sysop) { // 'b'			$('#block-link').click;			$('#block-reason').focus;			return false;		}		else if(e.which == 67) { // 'c'			$('#commands').val('');			if($('#prev-diff').css('display') == 'none')				$('#cur-diff').click;			else				$('#prev-diff').click;			return false; }	}); }

/* * Undo a selected edit through the API * @param: summary, the undo summary * @param: i, the index of the page, revision, and user in the pages, revIds, and users arrays */ function undo(summary, i) { $.ajax({		url: wgServer + '/api.php',		dataType: 'json',		type: 'POST',		data: {			'action': 'edit',			'title': pages[i],			'undo': revIds[i],			'summary': 'Undid revision ' + revIds[i] + ' by ' + users[i] + ' (talk) using RC Patrol' + summary,			'basetimestamp': starttimestamp,			'startimestamp': starttimestamp,			'token': edittoken		},		success: function {}	}); }

/* * Block a given user through the API * @param: user, user to block * @param: token, block token */ function block(user, token) { var expiry = $('#block-duration').val; $.ajax({		url: wgServer + '/api.php',		dataType: 'json',		type: 'POST',		data: {			'format': 'json',			'action': 'block',			'user': user,			'token': token,			'anononly': ,			'nocreate': ,			'autoblock': ,			'allowusertalk': ,			'expiry': expiry,			'reason': $('#block-summary').val + '(blocked through RC Patrol)'		},		success: function(response) {			$('#block-form').empty.append(' blocked ');		}	}); }

/* * Fetch options from #rcp_settings, parse diffs, and show them */ function go { // only continue if a current request is not already being processed if(working) { return; }

working = true; counter = 0; $('#rcp-loading').empty.append("Loading... Generating list...");

// clear data and fetch options revIds = [], old_revIds = [], pages = [], users = [], diffs = [], comments = [], tokens = [], timestamps = [], tops = []; var dropdown = $('#namespace').val || []; var since = $('#rcsince').val.trim; var ns = dropdown.join('|'); var limit = $('#limit').val.trim; var show = ""; if ($('#anon').attr('checked')) show = "|anon"; else if ($('#minor').attr('checked')) show = "|!minor";

/*	 * Load diff information and display when the first one becomes available */	parse_diffs = function { diffs.length = revIds.length; users.length = revIds.length; comments.length = revIds.length; tokens.length = revIds.length; timestamps.length = revIds.length;

for(var j = 0; j < revIds.length; j++) { $.ajax({				url: wgServer + '/api.php',				dataType: 'json',				data: {					'format': 'json',					'action': 'query',					'revids': revIds[j],					'prop': 'revisions',					'rvprop': 'timestamp|parsedcomment|user|ids',					'rvdiffto': 'prev',					'rvtoken': 'rollback',					'indexpageids': 'true'				},				success: function(response) {					var page = response.query.pages[response.query.pageids[0]];					var rev = page.revisions[0];					var k = $.inArray(rev.revid, revIds);					diffs[k] = rev.diff['*'];					users[k] = rev.user;					comments[k] = rev.parsedcomment;					tokens[k] = rev.rollbacktoken;					timestamps[k] = rev.timestamp;

$.ajax({						url: wgServer + '/api.php',						dataType: 'json',						data: {							'format': 'json',							'action': 'query',							'list': 'usercontribs',							'ucuser': users[k],							'ucstart': timestamps[k],							'ucend': timestamps[k]						},						success: function(response) {							var contrib = response.query.usercontribs[0];							var l = $.inArray(contrib.revid, revIds);							counter++;							var is_top = contrib.top;							if(is_top == null)								tops[l] = false;							else								tops[l] = true;							if(l == 0) {								$('#rcp-loading').empty;								show_diff(0);								$('#go').text('Parsing diffs...');								if($('#commands-toggle').is(':hidden'))									$('#commands-toggle').toggle;							}							if(counter == revIds.length) {								working = false;								$('#go').removeAttr('disabled');								$('#go').text('Submit');							}						}					}); }			});			$('#parsing-done').empty.append('(' + (j + 1) + '/' + revIds.length + ')');		}		$('#rcp-loading').append(' Waiting for server reply...');	}

/*	 * Show a selected diff * @param: i, the index of the diff information */	show_diff = function(i) { if(i > revIds.length - 1) { return; }

$('#rcp_diff').empty.append( ' '+						' '+						' '+						' ' +						' Return to diff</a> ' +						' '+						' '+						' Loading... '+						' Comparison with current revision: '+						' ');

$('#article').empty.append(' ' +						'' + pages[i] + '</a> ' +						' (view diff</a>) ' +					    ' ');

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November", "December"]; var t = timestamps[i]; t = t.substring(11,16) + ', ' + months[t.substring(5,7) - 1] + ' ' + t.substring(8,10) + ', ' + t.substring(0,4) + ' (UTC)';

$('#revision').empty.append('Diff <input id="diff-num" type="text" maxlength="3" size="1" style="text-align:right;" /> of ' + revIds.length +					     ' <button id="diff_go">Go ' +					      ' Revision ' + revIds[i] + ' on ' + t + ' by user: ' + users[i] + '</a> ' +					      '(talk</a>) '); $('#diff-num').val(i + 1); $('#diff_go').click(function { show_diff( $('#diff-num').val - 1 ); });

$('#comment').empty.append('Comment: ');

if(typeof comments[i] !== "undefined") $('#comment').append(comments[i]);

$('#comment').append(' '); $('#diff-options').empty.append(' &bull; (undo</a>)' +							' ' +								'<input type="text" id="undo-summary" />'+								'<button id="undo-go">Save ' +							' ' +						 ' '); if(tops[i]) top(i); else not_top(i); append_diff_options(i); }

var rc_data = {	'format': 'json', 'action': 'query', 'list': 'recentchanges', 'rctype': 'edit|new', 'rcshow': '!bot' + show, 'rclimit': limit, 'rcprop': 'user|title|timestamp|ids', 'rcnamespace': ns, 'rcstart': since };

if(since) { rc_data['rcdir'] = 'newer'; }

// Fetch edit information from API based on settings $.ajax({		url: wgServer + '/api.php',		dataType: 'json',		data: rc_data,		success: function(response) {			if(since == '')				response.query.recentchanges.reverse;			for (var i in response.query.recentchanges) {				var rc = response.query.recentchanges[i];				revIds.push(rc.revid);				old_revIds.push(rc.old_revid);				pages.push(rc.title);			}			$('#rcp-loading').append(' Done. Sending requests... ');			parse_diffs;		}	});

get_edit_info; }

});