Jump to content

User:GregorB/FixCroIS.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
//<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>