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. |
//<syntaxhighlight lang="JavaScript" line>
if(document.editform) {
var debug = 0;
var myContent = document.editform.wpTextbox1;
if (mw.config.get('wgNamespaceNumber') != -1 && myContent) $(onLoad);
function onLoad() {
var portletLink = mw.util.addPortletLink('p-tb', '#', 'Cleanup Croatian Infobox settlement', 't-fixcrois');
$(portletLink).click(wpFixCroIS);
}
var parseData;
function GetValue(paramName) {
if (parseData === null) return '';
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 GetSettlementType() {
if (GetValue('settlement_type') !== '')
return GetValue('settlement_type');
// else if (wgCategories.indexOf('Cities and towns in Croatia') != -1)
// return 'City/town'; //?
else if (wgCategories.indexOf('Municipalities of Croatia') != -1)
return 'Municipality';
else
return '';
}
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 GetValue('subdivision_name1');
}
function GetWebsite() {
return GetValue('website').replace(/.*(http:\/\/\S+)\b.*/, '{{URL|$1}}');
}
var dfltParams = [
['<!-- Name and transliteration -->'],
['name', GetName],
['native_name', false],
['native_name_lang', false],
['official_name', GetOfficialName],
['settlement_type', GetSettlementType],
['<!-- Images, nickname, motto -->'],
['image_skyline'],
['imagesize', false],
['image_alt'],
['image_caption'],
['image_flag', false],
['flag_size', false],
['flag_alt', false],
['image_seal', false],
['seal_size', false],
['seal_alt', false],
['image_shield', false],
['shield_size', false],
['shield_alt', false],
['nickname', false],
['<!-- Maps, coordinates -->'],
['image_map'],
['mapsize'],
['map_alt'],
['map_caption'],
['pushpin_map'],
['pushpin_label_position'],
['pushpin_map_alt'],
['pushpin_map_caption'],
['coordinates'],
['coor_pinpoint'],
['coordinates_footnotes'],
['<!-- 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', false],
['established_date1', false],
['established_title2', false],
['established_date2', false],
['established_title3', false],
['established_date3', false],
['established_title4', false],
['established_date4', false],
['<!-- Seat -->'],
['seat_type'],
['seat'],
['<!-- Government type, leaders -->'],
['government_footnotes'],
['leader_party'],
['leader_title'],
['leader_name'],
['leader_title1', false],
['leader_name1', false],
['<!-- Display settings -->'],
['unit_pref', 'Metric'],
['<!-- Area -->'],
['area_magnitude', null],
['area_footnotes'],
['area_urban_footnotes'],
['area_metro_footnotes'],
['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 code(s) & area code -->'],
['postal_code_type', '[[Postal codes in Croatia|Postal code]]'],
['postal_code'],
['area_code_type', '[[Telephone numbers in Croatia|Area code]]'],
['area_code'],
['registration_plate'],
['<!-- Website, footnotes -->'],
['website', GetWebsite],
['footnotes'],
['notes', null]
];
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');
if (pos == -1) return null;
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');
if (parseData === null) return;
for (var i = 0; i < parseData.names.length; i++) {
var found = false;
for (var j = 0; j < dfltParams.length; j++) {
if (parseData.names[i] == dfltParams[j][0]) {
found = true;
break;
}
}
if (!found) {
if (parseData.values[i] !== '') {
alert('Ignored param: ' + parseData.names[i] + ' = ' + parseData.values[i]);
}
else if (debug >= 1) {
alert('Ignored blank param: ' + parseData.names[i]);
}
}
}
var 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 'boolean':
paramValue = GetValue(paramName);
if (!dfltParams[i][1] && paramValue === '') {
paramValue = null;
}
break;
case 'object':
if (debug >= 1) {
alert('Removed obsolete param: ' + paramName);
}
continue;
case 'string':
paramValue = dfltParams[i][1];
if (debug >= 1 && GetValue(paramName) !== '' && GetValue(paramName) != paramValue) {
alert('Param value override: ' + paramName);
}
break;
case 'function':
paramValue = dfltParams[i][1]();
if (debug >= 1 && GetValue(paramName) !== '' && GetValue(paramName) != paramValue) {
alert('Param value override: ' + paramName);
}
break;
}
if (paramValue !== null) {
templateText += '| ' + paramName + ' '.repeat(28 - paramName.length) + ' = ' + paramValue + '\n';
}
}
}
templateText += '}}';
var oldTemplateText = myContent.value.substring(parseData.templateBegin, parseData.templateEnd);
if (debug >= 1) {
alert(oldTemplateText);
alert(templateText);
}
myContent.value = myContent.value.replace(oldTemplateText, templateText);
document.editform.wpSummary.value = 'Cleanup Infobox template using [[User:GregorB/FixCroIS.js|script]]';
document.editform.wpDiff.click(); // Auto-Show changes
}
}
//</syntaxhighlight>