Board Thread:New Features/@comment-1526346-20160904055140/@comment-452-20160906053800

Since I've already written a replacement editor which parses wikitext and performs a number of autoreplacements, I've repurposed sections of it to create a category editor, mostly just to show how easy this is.

You want to quickly edit categories, in English, without loading the full editor: this script does that, feel free to use it. If not, then it's a proof of concept.

How it works is simply grabbing [[Category:*]] from a page with regex, and displaying them to you in a textarea, one per line - and if sort keys are present in the source, they're displayed. When you press the save button, the original categories are completely removed, and the edited categories are added.

Doing it this way sidesteps issues with case sensitivity, underscores, and replacing them individually.

However, the script also strips all underscores from all links, because I like using two stones at once to kill the same bird. If you don't want to auto-replace underscores with spaces in all links, that can be disabled.

Currently, the categories names are displayed without the category prefix. I chose this for simplicity and to conserve width, but this can also be changed.

Currently, an "edit categories" button is added in the category selector, but it can be added anywhere you like.

If there are categories duplicated in the source, both are displayed in the textarea and removed from the source.

There is no duplicate or validity detection on saving - the onus is on the user to ensure the category is correct. (However, underscores are stripped on submit also.)

It also lists other categories added by templates in a disabled textarea, just so there's no confusion about why not all categories are editable.

If you'd like to use it, add the script to your personal javascript - or site javascript if you like, but you should probably test it yourself for a while - I have only done this once before.

I've tested this on one page, and saved my changes only once. I don't need to update any categories myself, so I won't be using it during my regular editing, but if you find any bugs, I'd be happy to troubleshoot them and update the script to fix it.

It's incredibly likely that some form of non-standard categories may break this script.
 * One issue I know about is that the category would probably end up as  .  Basically all instances of "Category:" within the category link are stripped, and I think the chances of that happening are so low that I don't care to fix it.  If anyone ever using this script /does/ encounter that situation, I'll address it.
 * If someone edits the page after you press the edit button but before you press save, it will overwrite their edit without a warning that the page has been edited. I'm not planning on implementing any edit-collision detection/handling/sidestepping.
 * If there's a category in the source and in a template, it's only listed once, in the source field, removing it will remove it from the source, and reloading the page and pressing the button again will show the other category.
 * If the category module is disabled in user preferences, no button is shown.
 * No monobook support
 * I have absolutely no idea what errors may happen if used on a page which has no categories.

/* QuickCatEdit by 452, because it's too hard for Wikia Staff */ $(function {	$("#articleCategories ul").append( $("", { class:"last" }).append(		 $(" ", { class:"wikia-button secondary add", id:"CategorySelectEdit", type:"button", html:"Edit categories" }).click(function{ QuickCatEdit; return false; })		) ) }); function showResult(message, result) { //from NullEditButton if (typeof window.QuickCatEditNotify != "undefined") window.QuickCatEditNotify.hide; window.QuickCatEditNotify = new BannerNotification(message, result).show; } RegExp.escape = function(text) { return text.replace(/[-[\]{}*+?.,\\^$|#\s]/g, "\\$&"); };

function QuickCatEdit { var ajaxIndicator = window.ajaxIndicator || 'http://images.wikia.nocookie.net/common/skins/common/images/ajax.gif'; $.makeModal({		id: "ModalTable",		width: $(window).width - 254,		style: "min-height:452px"	}); $("#ModalTable h1").css("margin-bottom",0).html("Edit Categories  Loading "); $("#ModalTable .modalContent").css({		"text-align":"center",		overflow: "auto",	}) .append(		$(" ", { id:"SubmitButton", style:"height: 2em;margin: 0 1em;", html:"Save categories" })	).append($(" "));

$.get('/index.php?curid=' + mw.config.get("wgArticleId") + '&action=raw&maxage=0&smaxage=0', function(data) {		var thecats;		window.thepage = data;		allLinksArray = window.thepage.match(/\[\[.[^\]]*]]/g);		if (allLinksArray) {			$.each( allLinksArray, function(k, v) { if (v.indexOf("_") !== -1) { window.thepage = window.thepage.replace(new RegExp(RegExp.escape(v), 'g'), v.replace(/_/g, " ")); }			});		}		thecats = window.thepage.match(/\[\[Category:.[^\]]*]]/gi);		$('#ajaxProgress').css("display","none");

$("#ModalTable .modalContent").append(			$(" ", { id:"EditCats", style:"width: 90%;height: "+((thecats.length+1)*1.2)+"em;" })		);

$(thecats).each(function(k,v){			window.thepage = window.thepage.replace(new RegExp(RegExp.escape("\n"+v), 'g'), "");			$("#EditCats").append(v.slice(11,-2)+"\n");		});

$.getJSON('/api.php?action=query&format=json&prop=categories&titles='+mw.config.get('wgPageName')+'&clprop=sortkey').done(function (data) {			if ($(data).size) {				$("#ModalTable .modalContent")					.append($(" "))					.append($(" ", { html: "Other categories added by templates" }) )					.append( $(" ", { id:"NonEditCats", disabled:"disabled", style:"width: 90%;height: "+((thecats.length+1)*1.2)+"em;" }) );				$.each( data.query.pages[Object.keys(data.query.pages)].categories, function(k, v) { if (thecats.indexOf("") == -1) $("#NonEditCats").append(v.title.substring(9)+(v.sortkeyprefix.length?"|":"")+v.sortkeyprefix+"\n"); });				$("#NonEditCats").css({ "height": (($("#NonEditCats").val.split("\n").length)*1.2)+"em"})			}		}); });

$('#SubmitButton').on('click', function {		newCats = $("#ModalTable textarea").val.replace(/_/g, " ").split("\n");		$.each( newCats, function(k, v) { if(!v.trim.length) return; v = v.replace(/\[/g, "").replace(/Category:/gi, "").replace(/\]/g, ""); window.thepage += "\n"; });

$('#ajaxProgress').css("display","inline-block"); showResult("Saving edit...", "confirm"); var params = { format: 'json', action: 'edit', title: mw.config.get('wgPageName'), text: window.thepage, token: mw.user.tokens.get("editToken"), summary: "Updating categories", }		new mw.Api.post(params).fail(function(result) {			$('#ajaxProgress').css("display","none");			showResult("Error: check console", "error");			console.log(result);		}).done(function(result) {			if (result.error) {				$('#ajaxProgress').css("display","none");				showResult("Error: check console\n"+result.error.info, "error");				console.log(result.error);			} else {				$('#ajaxProgress').css("display","none");				showResult("Edit saved, click here for diff, or reload page to see changes", "confirm");			}		}); }); }