User:Frietjes/politicalparty.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Frietjes/politicalparty. |
/// 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 + ' → ' + 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 );
}