User:GregorB/FixCroIS.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:GregorB/FixCroIS. |
var debug = 1;
var myContent = document.editform.wpTextbox1;
if (wgNamespaceNumber != -1 && myContent) $(onLoad);
function onLoad() {
var portletLink = mw.util.addPortletLink('p-tb', '#', 'Fix Croatian Infobox settlement', 't-fixcrois');
$(portletLink).click(wpFixCroIS);
}
var parseData;
function GetValue(paramName) {
var paramIndex = parseData.names.indexOf(paramName);
return paramIndex != -1 ? parseData.values[paramIndex] : "";
}
function GetName() {
if (GetValue("name") === "" && GetValue("official_name") !== "")
return GetValue("official_name");
else
return GetValue("name");
}
function GetOfficialName() {
if (GetValue("name") === "" && GetValue("official_name") !== "")
return "";
else
return GetValue("official_name");
}
function GetCountyName() {
for (var i = 0; i < wgCategories.length; i++) {
if (wgCategories[i].match(/^Populated places in .* (County|City of Zagreb)$/) !== null) {
return '[[' + wgCategories[i].replace(/Populated places in (the )?/, '') + ']]';
}
}
return "";
}
function GetWebsite() {
return GetValue("website").replace(/.*(http:\/\/\S+)\b.*/, "{{URL|$1}}");
}
var dfltParams = [
['name', GetName],
['official_name', GetOfficialName],
['settlement_type'],
['<!-- images, nickname, motto -->'],
['image_skyline'],
['image_alt'],
['image_caption'],
['image_flag'],
['flag_size'],
['flag_alt'],
['image_seal'],
['seal_alt'],
['image_shield'],
['shield_size'],
['shield_alt'],
['nickname'],
['<!-- maps and coordinates -->'],
['image_map'],
['mapsize'],
['map_alt'],
['map_caption'],
['pushpin_map'],
['pushpin_label_position'],
['pushpin_map_alt'],
['pushpin_map_caption'],
['latd'],
['latm'],
['lats'],
['latNS'],
['longd'],
['longm'],
['longs'],
['longEW'],
['coor_pinpoint'],
['coordinates_type'],
['coordinates_display', 'inline,title'],
['coordinates_format', 'dms'],
['coordinates_footnotes'],
['coordinates_region', 'HR'],
['<!-- location -->'],
['subdivision_type', 'Country'],
['subdivision_name', '{{flag|Croatia}}'],
['subdivision_type1', '[[Counties of Croatia|County]]'],
['subdivision_name1', GetCountyName],
['subdivision_type2'],
['subdivision_name2'],
['<!-- established -->'],
['established_title'],
['established_date'],
['established_title1'],
['established_date1'],
['established_title2'],
['established_date2'],
['established_title3'],
['established_date3'],
['established_title4'],
['established_date4'],
['<!-- seat, smaller parts -->'],
['seat_type'],
['seat'],
['<!-- government type, leaders -->'],
['government_footnotes'],
['leader_party'],
['leader_title'],
['leader_name'],
['<!-- display settings -->'],
['unit_pref', 'Metric'],
['<!-- area -->'],
['area_footnotes'],
['area_urban_footnotes'],
['area_metro_footnotes'],
['<!-- square kilometers -->'],
['area_total_km2'],
['area_urban_km2'],
['area_metro_km2'],
['<!-- elevation -->'],
['elevation_footnotes'],
['elevation_m'],
['<!-- population -->'],
['population_as_of'],
['population_footnotes'],
['population_total'],
['population_density_km2', 'auto'],
['population_urban_footnotes'],
['population_urban'],
['population_density_urban_km2', 'auto'],
['population_metro_footnotes'],
['population_metro'],
['population_density_metro_km2', 'auto'],
['population_note'],
['<!-- time zone(s) -->'],
['timezone', '[[Central European Time|CET]]'],
['utc_offset', '+1'],
['timezone_DST', '[[Central European Summer Time|CEST]]'],
['utc_offset_DST', '+2'],
['<!-- postal codes, area code -->'],
['postal_code_type'],
['postal_code'],
['area_code_type'],
['area_code'],
['registration_plate'],
['<!-- website, footnotes -->'],
['website', GetWebsite],
['footnotes']
];
function ParseTemplateCall(wikiText, templateName) {
function next(substring) {
return wikiText.indexOf(substring, pos) == -1 ? Infinity : wikiText.indexOf(substring, pos);
}
var parseData = { names:[], values:[], templateBegin:0, templateEnd:0 };
var templateNestingLevel = 0;
var pos = wikiText.search('{{\\s*' + templateName + '\\b');
parseData.templateBegin = pos;
pos = wikiText.indexOf('|', pos) + 1;
while (templateNestingLevel >= 0) {
var paramNamePos = pos;
var paramValuePos = wikiText.indexOf('=', pos) + 1;
var paramName = wikiText.substring(paramNamePos, paramValuePos - 1).trim();
parseData.names.push(paramName);
if (debug >= 2) alert(paramName);
pos = paramValuePos;
while (true) {
var firstTemplateBegin = next('{{');
var firstTemplateEnd = next('}}');
var firstLinkBegin = next('[[');
var firstLinkEnd = next(']]');
var firstPipe = next('|');
if (templateNestingLevel > 0) {
if (firstTemplateBegin < firstTemplateEnd)
templateNestingLevel++;
else
templateNestingLevel--;
pos = Math.min(firstTemplateBegin, firstTemplateEnd) + 2;
} else {
var first = Math.min(firstTemplateBegin, firstTemplateEnd, firstLinkBegin, firstLinkEnd, firstPipe);
if (first == firstTemplateBegin) {
templateNestingLevel = 1;
pos = first + 2;
}
if (first == firstTemplateEnd) {
templateNestingLevel = -1;
pos = first + 2;
}
if (first == firstLinkBegin) {
pos = firstLinkEnd + 2;
}
if (first == firstPipe) {
pos = first + 1;
}
if (first == firstTemplateEnd || first == firstPipe) {
paramValue = wikiText.substring(paramValuePos, first).replace(/<!--.*-->/, '').trim();
parseData.values.push(paramValue);
if (debug >= 2) alert(paramValue);
break;
}
}
}
}
parseData.templateEnd = pos;
return parseData;
}
function wpFixCroIS() {
parseData = ParseTemplateCall(myContent.value, '[Ii]nfobox settlement');
var templateText = '{{Infobox settlement\n';
for (var i = 0; i < parseData.names.length; i++) {
templateText += '| ' + parseData.names[i] + " ".repeat(40 - parseData.names[i].length) + ' = ' + parseData.values[i] + '\n';
if (parseData.values[i] !== "") {
var found = false;
for (var j = 0; j < dfltParams.length; j++) {
if (parseData.names[i] == dfltParams[j][0]) {
found = true;
break;
}
}
if (!found) {
alert('Missing param: ' + parseData.names[i] + ' = ' + parseData.values[i]);
}
}
}
templateText += '}}';
if (debug >= 1) {
alert(myContent.value.substring(parseData.templateBegin, parseData.templateEnd));
alert(templateText);
}
templateText = '{{Infobox settlement\n';
for (i = 0; i < dfltParams.length; i++) {
var paramName = dfltParams[i][0];
if (paramName.startsWith('<!--')) {
templateText += paramName + '\n';
}
else {
var paramValue;
switch (typeof dfltParams[i][1]) {
case "undefined":
paramValue = GetValue(paramName);
break;
case "string":
paramValue = dfltParams[i][1];
break;
case "function":
paramValue = dfltParams[i][1]();
break;
}
templateText += '| ' + paramName + " ".repeat(40 - paramName.length) + ' = ' + paramValue + '\n';
}
}
templateText += '}}';
if (debug >= 1) {
alert(templateText);
}
}