User:Rillke/checkCat.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.
/*
 * CheckCategories HotCat Extension - 
 * removes the template when categorizing with HotCat and
 * adds a link "Categories are Ok" to the category-section
 *
 * <nowiki>
 *
 * @rev 1 (2012-02-01)
 * @author [[User:Rillke]], 2012
 * @source User:Rillke/checkCat.js @wikimedia Commons
 */

/*global mw:false, $:false, alert:false*/
/*jshint curly:false*/

mw.loader.using(['mediawiki.user', 'mediawiki.util'], function() {
'use strict';
if (6 !== mw.config.get('wgNamespaceNumber') || $('.checkcategories').length === 0) return;

var chCatRE = /\{\{[Cc]heck[ _]categories[^\}\{]*\}\}/g;

// Remove "check categories" when using HotCat
$('body').delegate( '#hotcatCommitForm', 'submit', function (evt) {
	this.wpTextbox1.value = this.wpTextbox1.value.replace(chCatRE, '');
	this.wpSummary.value = 'Removing [[Template:Check categories|{'+'{Check categories}}]] ' + this.wpSummary.value;
	return true;
});

// Add Ok-Link to the cats panel
var $okLink = $('<a>', { href: '#', text: 'Categories are OK!' }).click(function(e) {
	e.preventDefault();
	var $el = $(this);
	$el.unbind('click');
	var doEdit = function(result) {
		if (!result) return;
		$el.text('Doing.');
		var text = result.replace(chCatRE, '');
		if (text === result) {
			$el.text('Template not found!');
			return;
		}
		var params = {
			action: 'edit',
			title: mw.config.get('wgPageName'),
			nocreate: 1,
			redirect: 1,
			summary: 'Script: Categories are checked and Ok. You can help [[Category:Media needing category review|reviewing]]!',
			text: text,
			token: (mw.user.tokens.get('csrfToken')),
			format: 'json'
		};
 
		var editDone = function(editStat) {
			if (!editStat) return;
			if (editStat.error) {
				alert('Unable to remove "Check categories" with the API\n' + result.error.code + '\n' + result.error.info);
				$el.text('Edit-Error!');
			} else {
				$el.text('Edit Done.');
			}
			$('.checkcategories').fadeOut();
		};
		$el.text('Doing..');
		$.post(mw.util.wikiScript('api'), params, editDone);
	};
	$el.text('Doing');
	$.ajax({
		url: mw.config.get('wgScript'),
		data: { 'action': 'raw', title: mw.config.get('wgPageName').replace(/ /g, '_'), maxage: 0, smaxage: 0 },
		dataType: 'text',
		error: function() {
			$el.text('Error!');
		},
		success: doEdit,
		type: 'GET',
		cache: false
	});
});
$(function() {
	$('#catlinks').find('ul:first').append($('<li>').append($okLink));
});

});
// </nowiki>