Jump to content

User:Frietjes/politicalparty.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/// Check political party data modules
/// Adapted from [[User:Timotheus_Canens/massedit.js]]
/// Once installed, go to Special:PoliticalParty

function getModulePageText(title){
	var req = new XMLHttpRequest();
	req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(title), false);
	req.send();
	var response = JSON.parse(req.responseText);
	pageid = response.query.pageids[0];
	if (pageid == "-1") {
		return '';
	}
	var pagetext = response.query.pages[pageid].revisions[0]['*'];
	return pagetext;
}

function doListPoliticalParty() {
	document.getElementById("wpListPoliticalPartySubmit").disabled = true;
	var subpages = "1ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	const subpagelist = subpages.split("");
	var fparties = [];
	var aparties = [];
	for(var k=0; k < subpagelist.length; k++) {
		var txt = getModulePageText("Module:Political_party/" + subpagelist[k]);
		var full = txt.match(/[\r\n][\t ]*\[['"]([^\r\n]*?)["']\][\t ]*=[\t ]*\{/g);
		var redir = txt.match(/[\r\n][\t ]*(\[['"][^\r\n]*["']\][\t ]*=[\t ]*['"][^\r\n]*)/g);
		if(full) {
			for (var j=0; j < full.length; j++) {
				var t = full[j].replace(/[\r\n][\t ]*\[['"]([^\r\n]*?)['"]\][\t ]*=[\t ]*\{[\t ]*$/, '$1');
				fparties.push(t);
			}
		}
		if(redir) {
			for (var i=0; i < redir.length; i++) {
				var a = redir[i].replace(/[\r\n][\t ]*\[['"]([^\r\n]*?)['"]\][\t ]*=[\t ]*['"]([^\r\n]*?)['"](?:[,\t ]*|[,\t ]*\-\-[^\r\n]*)$/, '$1');
				var r = redir[i].replace(/[\r\n][\t ]*\[['"]([^\r\n]*?)['"]\][\t ]*=[\t ]*['"]([^\r\n]*?)['"](?:[,\t ]*|[,\t ]*\-\-[^\r\n]*)$/, '$2');
				aparties.push(a + '===' + r);
			}
		}
		document.getElementById("wpListPoliticalPartySubmit").value = "(" + (k+1) + ")";
	}
	// look for duplicates and broken redirects
	var dups = [];
	var broke = [];
	// look for duplicates in full entries
	var phash = {};
	for (var i=0; i < fparties.length; i++) {
		var t = fparties[i]
		if ( t in phash ) {
			dups.push(t);
		} else {
			phash[t] = 1;
		}
	}
	var ahash = {};
	// look for duplicates in alternate names
	for (var i=0; i < aparties.length; i++) {
		var a = aparties[i].replace(/^(.*)===(.*)$/, '$1');
		var r = aparties[i].replace(/^(.*)===(.*)$/, '$2');
		if (a in phash ) {
			dups.push(a);
		}
		if ( a in ahash ) {
			dups.push(a);
		} else {
			ahash[a] = 1;
		}
		if ( !(r in phash) ) {
			broke.push(a + ' &rarr; ' + r);
		}
	}
	var res = '<table class="wikitable"><tr><th>Duplicates:</th><th>Broken redirects:</th></tr><tr style="vertical-align:top"><td>' + (dups.join('<br>')) + '</td><td>' + (broke.join('<br>')) + '</td></tr></table>';
	
	document.getElementById("wpPoliticalPartyList").innerHTML = res;
}

function listPoliticalPartyform() {
	var bodyContent = 'bodyContent';
	switch (mw.config.get('skin')) {
		case 'modern':
			bodyContent = 'mw_contentholder';
			break;
		case 'cologneblue':
			bodyContent = 'article';
			break;
		case 'vector':
		case 'monobook':
		case 'vector':
		default:
			bodyContent = 'bodyContent';
			break;
	}
	document.getElementsByTagName("h1")[0].textContent = "Check Module:Political party";
	document.title = "Check Module:Political party - Wikipedia, the free encyclopedia";
	document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">From Wikipedia, the free encyclopedia</h3><br /><br />'
		+ '<form id="wpListPoliticalParty" name="wpPoliticalParty">'
		+ '<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>'
		+ '<p id="wpPoliticalPartyProcessing"></p>'
		+ '<input type="button" id="wpListPoliticalPartySubmit" name="wpListPoliticalPartySubmit" value="Query" onclick="doListPoliticalParty()" />'
		+ '</form>'
		+ '<div id="wpPoliticalPartyList"></div>';
}
if(mw.config.get('wgNamespaceNumber') === -1 && (mw.config.get('wgPageName') === "Special:PoliticalParty" || mw.config.get('wgPageName') === "Special:PoliticalPartyList")
) {
	$.when( $.ready, mw.loader.using(['mediawiki.util'])).done( listPoliticalPartyform );
}