Jump to content

User:GregorB/FixCroIS.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by GregorB (talk | contribs) at 11:28, 1 January 2015 (FixLocationMap). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
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 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";
}

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', GetSettlementType],
	['<!-- 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'],
	['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');
	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 FixLocationMap() {
	myContent = myContent.replace(/{{Location map.*\|\s*caption\s*=(.*)\s*\|\s*lat\s*=(.*)\s*\|\s*long\s*=(.*)\s*}}/, 
	"{{Infobox settlement | pushpin_map = Croatia | pushpin_map_caption = $1 | latd = $2 | longd = $3}}"
	);
}
 
function wpFixCroIS() {
	FixLocationMap();
	parseData = ParseTemplateCall(myContent.value, '[Ii]nfobox settlement');
	
	if (parseData === null) return;
	
	var templateText = '{{Infobox settlement\n';
	for (var i = 0; i < parseData.names.length; i++) {
		templateText += '| ' + parseData.names[i] + " ".repeat(Math.max(0, 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);
	}
}