User:MPGuy2824/scripts/constituencyCleaner.js

// // Structure borrowed Novem Linguae's DraftCleaner script // jshint esversion: 8, esnext:false $(async function {

/* - Adds "Run ConstCleaner" link to the left sidebar - Uses: - Use the template

async function getWikicode(title) { let pageIsDeleted = ! mw.config.get('wgCurRevisionId'); if ( pageIsDeleted ) { return ''; }

var wikicode = ''; title = encodeURIComponent(title); await $.ajax({		url: mw.config.get( 'wgServer') + mw.config.get( 'wgScriptPath') +			'/api.php?action=parse&page=' + title +			'&prop=wikitext&formatversion=2&format=json',		success: function (result) {			wikicode = result.parse.wikitext;		},		dataType: "json",	}); return wikicode; }

function goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, editSummary) { let titleEncoded = encodeURIComponent(titleWithNamespaceAndUnderscores); let wgServer = mw.config.get('wgServer'); let wgScriptPath = mw.config.get('wgScriptPath'); let baseURL = wgServer + wgScriptPath + '/'; // https://stackoverflow.com/a/12464290/3480193 $(``) .append($('').val(wikicode)) .append($('').val(editSummary)) .append($('').val('preview')) .append($('').val('Show changes')) .append($('').val('1')) .appendTo($(document.body)) //it has to be added somewhere into the .trigger('submit'); }

/** returns the pagename, including the namespace name, but with spaces replaced by underscores */ function getArticleName { return mw.config.get('wgPageName'); }

// don't run when not viewing articles let action = mw.config.get('wgAction'); let isNotViewing = action != 'view'; if ( isNotViewing ) { return; }

// don't run when viewing diffs let isDiff = mw.config.get('wgDiffNewId'); if ( isDiff ) { return; }

// Don't run in virtual namespaces let isVirtualNamespace = mw.config.get('wgNamespaceNumber') < 0; if ( isVirtualNamespace ) { return; }

let titleWithNamespaceAndUnderscores = getArticleName; let namespaceNumber = mw.config.get('wgNamespaceNumber');

let running = false;

// Add DraftCleaner to the toolbar mw.loader.using(['mediawiki.util'], function {	mw.util.addPortletLink('p-navigation', '#', 'Run ConstCleaner', 'ConstCleanerLink');	$('#ConstCleanerLink').on('click', async function { // prevent running the script while script is already in progress if ( running ) { return; }		running = true;

mw.notify('Parsing page content...'); // get page wikicode let titleWithNamespaceAndSpaces = titleWithNamespaceAndUnderscores.replace(/_/g, ' '); let originalWikicode = await getWikicode(titleWithNamespaceAndUnderscores); let wikicode = originalWikicode;

let dc = new ConstCleaner; wikicode = dc.cleanConst(wikicode, namespaceNumber, titleWithNamespaceAndSpaces);

let needsChanges = wikicode != originalWikicode; if ( needsChanges ) { let summary = 'clean up (via constituencyCleaner script)'; await goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, summary); } else { mw.notify('No changes needed!'); }	}); });

class ConstCleaner { cleanConst(wikicode, namespaceNumber, titleWithNamespaceAndSpaces) { // run before other stuff wikicode = this.fixListSection(wikicode); wikicode = this.addFullPartyNameWithColor(wikicode); wikicode = this.cleanInfobox(wikicode); wikicode = this.swapBracketRefPeriodWithRefBracket(wikicode); wikicode = this.cleanInfobox(wikicode); return wikicode; }	// 	addFullPartyNameWithColor(wikicode) { // 		wikicode = wikicode.replace(/ bgcolor=\"/gm, ' style=\"background-color:'); wikicode = wikicode.replace(/background\:/gm, 'background-color:');

wikicode = wikicode.replace(/\|\s*(width=\"[0-9]+px\"\s)?\s*style=\"?background-color:\s*\{\{party color\|([A-Za-z\-\|\'\ \(\)]+)\}\}\;?\"?\s*\|\s*\n\|\|?\s*\[\[([A-Za-z\'\-\ \(\)]+)(\|[A-Za-z\-\'\ \(\)]+)?\]\]\s*/gm, '|\n'); wikicode = wikicode.replace(/\|\s*(width=\"[0-9]+px\"\s)?\s*rowspan\s*=\s*\"?([0-9]+)\"?\s*(width=\"[0-9]+px\"\s)?\s+style=\"?background-color:\s*\{\{party color\|([A-Za-z\-\|\'\ \(\)]+)\}\}\;?\"?\s*\|\|?\s*\n\|\|?\s*rowspan=\"?([0-9]+)\"?\s*\|\s*\[\[([A-Za-z\-\'\ \(\)]+)(\|[A-Za-z\-\ \(\)]+)?\]\]\s*/gm, '| \n'); wikicode = wikicode.replace(/\|\s*\[\[([A-Za-z\-\'\ \(\)]+)(\|[A-Za-z\-\ \(\)]+)?\]\]\s*\|\s*(width=\"[0-9]+px\"\s)?\s*style=\"?background-color:\s*\{\{party color\|([A-Za-z\-\|\'\ \(\)]+)\}\}\;?\"?\s*\|\s*/gm, '| \n'); wikicode = wikicode.replace(/\|(\s*rowspan=\"?([0-9]+)\|)?\s*\[\[([A-Za-z\-\'\ \(\)]+)(\|[A-Za-z\-\ \(\)]+)?\]\]\s*\|(\s*rowspan=\"?([0-9]+))?\s*\{\{party color cell\|([A-Za-z\-\|\'\ \(\)]+)\}\}\s*/gm, '| \n'); wikicode = wikicode.replace(/\|rowspan=1}}/gm, '}}'); wikicode = wikicode.replace(/{{Party name with color\|/gm, '{{Full party name with color|'); wikicode = wikicode.replace(/{{Full party name with color\|Independent(\|\})/gm, '{{Full party name with color|Independent (politician)$1'); return wikicode; }

// 	fixListSection(wikicode) { wikicode = wikicode.replace(/==\s*Members? of( the)? (Legislative Assembly|Vidhana? Sabha)\s*==\s*/gm, '== Members of the Legislative Assembly ==\n'); wikicode = wikicode.replace(/==\s*Members? of( the)? ((P|p)arliament|Lok Sabha)\s*==\s*/gm, '== Members of Parliament ==\n'); wikicode = wikicode.replace(/\"sortable\"/gm, '\"wikitable sortable\"'); wikicode = wikicode.replace(/\"wikitable\"/gm, '\"wikitable sortable\"'); wikicode = wikicode.replace(/\"wikitable sortable\"\s*width=\"[0-9]+px\"/gm, '\"wikitable sortable\"'); return wikicode; }	//	swapBracketRefPeriodWithRefBracket(wikicode) { //		wikicode = wikicode.replace(/\}\}((?:]*?>[^>]*?<\/ref>){1,})/gm, '$1}}'); //		wikicode = wikicode.replace(/((?:]*?>[^>]*?<\/ref>){1,})\.\n/gm, ".$1\n"); return wikicode; }

cleanInfobox(wikicode) { //		wikicode = wikicode.replace(/\{\{Infobox Indian state legislative assembly constituency/gm, '{{Infobox constituency'); wikicode = wikicode.replace(/(\s*|\s*established\s*=\s*[0-9]+)\s*([\-–])\s*present/gm, '$1'); wikicode = wikicode.replace(/([\ \t]*\|\s*(parl_name|most_successful_party|image_size)\s*=\s*([\w\s\[\]]+)?)\s*/gm, ''); wikicode = wikicode.replace(/([\ \t]*\|\s*Reservation\s*=\s*)/gm, '| reservation = '); wikicode = wikicode.replace(/([\ \t]*\|\s*image\s*=\s*)/gm, '| map_image = '); wikicode = wikicode.replace(/([\ \t]*\|\s*constituency\s*=\s*)/gm, '| loksabha_cons = '); return wikicode; }

_replaceAll(haystack, needle, replacement) { let regex = new RegExp(this._escapeRegEx(needle), 'g'); haystack = haystack.replace(regex, replacement); return haystack; } } }); //