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. I believe this is also how the category module works, since it always moves categories to the bottom of the page.

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

However, my 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 some categories are not listed.

If you'd like to use it, add the script below to your personal javascript - or your wiki 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 or anyone else who uses the script finds any bugs, I'd be happy to troubleshoot them and update my script to fix it.

It's incredibly likely that some kind of non-standard category formatting may cause the script to fail, either by simply ignoring it, the category becoming malformed in some way. Any errors introduced by my script should be reported to me so they can be addressed.

I already know of a range of possible issues. These are not "bugs", as a bug is something unintended. The points listed below are simply cases which I have deliberately not handled. The chances of these things happening in normal use are so low that I don't care to address them unless they actually become an problem for someone. If anyone ever using this script does encounter any of the situations here, let me known an I'll address them.
 * The category would probably end up as  .  If anyone does use a category with this format, I'd love to know why.
 * 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, as it is the responsibility of the user to examine the diff of their edit.  The confirmation message links to the diff, but if desired I can automatically show a popup diff after saving.
 * If there's a category in the source and in a template, it's only listed in the source field, removing it removes it from the source. Reloading the page and pressing the edit categories button again will show the other category. (I did not look into whether or not it was possible to detect the duplicate, because it didn't really seem worth the time.)
 * If the category module is disabled in user preferences, no button is shown. This is an intended feature, listed here just in case someone with the category module disabled tries to use it.  If someone with the category module disabled does wish to use it anyway, please let me know.
 * No monobook support. If anyone who uses monobook would like to use this script, please let me know.
 * I have absolutely no idea what errors may happen if used on a page which has no categories. But there's absolutely no reason for anyone to click "edit categories" 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");			}		}); }); }