User:Pathoschild/2008 babel transition.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// <source lang="javascript">
function babeltransition() {
	var editbox = document.getElementById('wpTextbox1');
	var summarybox = document.getElementById('wpSummary');
	
	var watchbox = document.getElementById('wpWatchthis');
	var minorbox = document.getElementById('wpMinoredit');
	
	var previewbutton = document.getElementById('wpPreview');
	var savebutton = document.getElementById('wpSave');
	var diffbutton = document.getElementById('wpDiff');
	
	/* delete pages */
	if(wgAction=='delete') {
		var summarybox = document.getElementById('wpReason');
		var submitbutton = document.getElementById('wpConfirmB');
	
		summarybox.value = 'Deprecated by {{[[template:user language|user language]]}}';
		submitbutton.click();
	}
	/* update user pages */
	else if(wgCanonicalNamespace=='User' || wgCanonicalNamespace=='User_talk') {
		/* update boxes */
		// expand {{babel}} into {{user *}}
		if(editbox.value.match(/{{babel/i)) {
			var oldbabel = String(editbox.value.match(/{{babel[^}]+}}/i));
			var codes = oldbabel.replace(/{{babel[^\|]*\|\s*/i,'');
			codes = codes.replace(/\s*}}/,'');
			codes = codes.split(/\s*\|\s*/);
	
			var newbabel = '';
			for(var i=0; i<codes.length; i++) {
				newbabel += '{{user ' + codes[i] + '}}\n';
			}
			newbabel = newbabel.replace(/\n$/,'');
			editbox.value = editbox.value.replace(oldbabel,newbabel);
		}
	
		// standardize syntax
		editbox.value = editbox.value.replace(/{{(?:template:|msg:)?user[_ ]+([^\|\}]*?)\s*?}}/ig,'{{user $1}}');
	
		// convert levels
		editbox.value = editbox.value.replace(/{{user ([a-z]+)-[456]}}/ig,'{{user $1-3}}');
		editbox.value = editbox.value.replace(/{{user ([a-z]+)(?:-N)?}}/ig,'{{user $1-N}}');
	
		// convert {{user *}} to {{user language}}
		editbox.value = editbox.value.replace(/{{user ([a-z\-]{2,5})-(\d|N)}}/ig,'{{user language|$1|$2}}');
		editbox.value = editbox.value.replace(/{{user }}\n/ig,'');
	
		// remove level zero (except en)
		editbox.value = editbox.value.replace(/{{user language\|([^e][^n][^\|\}]*)\|0}} *\n?/ig,'');
	
		/* fix links */
		editbox.value = editbox.value.replace(/\[\[(?:Commons|Wikipedia|WP):Babel\]\]/ig,'[[Meta:Babel templates|Babel templates]]');
	
		/* fix categories */
		editbox.value = editbox.value.replace(/{{user language\/categories/ig,'{{user language/category');
		editbox.value = editbox.value.replace(/\[\[\s*Category:User ([^\s\|\]]+)-([0-9N])[^\]]*\]\]/ig,'{{user language/category|$1|$2}}');
		editbox.value = editbox.value.replace(/{{user language\/category\|([^\|]+)\|(3|4|5|6)}}/ig, '{{user language/category|$1|3}}');
	
		/* submit */
		if(editbox.value.match(/user language\||babelold/)) {
			summarybox.value = 'updated templates (see also [[User:Pathoschild/Babel transition|how to use the old babel format]])';
		}
		else if(editbox.value.match(/user language\/categor/)) {
			summarybox.value = 'templated categories to fix sort keys (no visible change)';
			minorbox.checked = true;
		}
		watchbox.checked = false;
		diffbutton.click();
	    }
	/* deprecate templates */
	else if(wgCanonicalNamespace=='Template') {
		var langcode = wgTitle.replace(/^User ([^\s]+?)(?:-\d)?$/,'$1');
		var langlvl = wgTitle.replace(/^.*(\d)$/,'$1');
		if(langlvl == wgTitle) {
			langlvl = "N";
		}
	
		editbox.value = '{{deprecated babel|' + wgTitle + '|' + langcode + '|' + langlvl + '}}';
	
		summarybox.value = 'deprecated by {{[[template:user language|user language]]}}';
		savebutton.click();
	}
	/* standardize categories */
	else if(wgCanonicalNamespace=='Category') {
		// strip extra markup
		editbox.value = editbox.value.replace(/\s*\[\[\s*Category\s*:[^\]]+\]\]\s*/gi,'');
		if(editbox.value.match(/^[\s\n]*$/)) {
			var pageBlank = true;
		}
		else {
			var pageBlank = false;
		}
	
		// place template
		editbox.value = '{{user language category|' + wgTitle.replace(/user /i,'') + '|' + editbox.value + '}}';
		editbox.value = editbox.value.replace('|}}','}}');
	
		// submit
		summarybox.value = '+ standard description';
		if(pageBlank) {
			savebutton.click();
		}
	}
}
// </source>