Jump to content

User:Utcursch/Migrate Infobox Indian Jurisdiction to Infobox settlement/main.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Utcursch (talk | contribs) at 10:25, 24 May 2012 (+). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/**
 * A quick-and-dirty solution to migrate the Infobox Indian Jurisdiction to Infobox settlement
 *
 * @author User:Utcursch
 */

/*
 * if auto_replace is true,
 *     the infobox will automatically be migrated whenever the user clicks on "Edit this page"
 * if auto_replace is false,
 *     a portlet link will be made available in the drop-down menu beside the "View history" tab
 */
migrateIIJ = {
	auto_replace: true
};


/**
 * Replaces Infobox Indian Jurisdiction (if found) with Infobox settlement
 */
migrateIIJ.migrateInfoboxIJ = function () {
	// regular expression to parse Infobox Indian Jurisdiction
	var regex = /{{Infobox Indian Jurisdiction[^{]*?({{[^}]*?}}[\s\S]*?)*}}/gmi

	// get the article content
	var orig_article_text = document.getElementById("wpTextbox1").value;

	// get the infobox text
	var arr_infobox_text = orig_article_text.match(regex);

	// Infobox Indian Jurisdiction not found
	if (arr_infobox_text == null) {
		return;
	}

	// get infobox text
	var infobox_text = arr_infobox_text[0];

	// parse original infobox
	orig_infobox_params = migrateIIJ.parseInfobox(infobox_text);

	// create Infobox settlement
	var new_infobox_text = migrateIIJ.prepareInfoboxSettlement(orig_infobox_params);

	// replace original infobox with the new one
	var new_article_text = orig_article_text.replace(regex, new_infobox_text);

	// Insert Infobox settlement
	document.getElementById("wpTextbox1").value = new_article_text;

	// Provide an edit summary
	var summary = "converted"
	if (document.getElementById("wpSummary")) {
		document.getElementById("wpSummary").value = "Replaced [[User:Utcursch/Migrate Infobox Indian Jurisdiction to Infobox settlement|Infobox Indian Jurisdiction with Infobox settlement]]";
	}
}


/**
 * Build the Infobox settlement string
 *
 * @param infobox_text the infobox string
 *     {{Infobox Indian Jurisdiction ...  }}
 *
 * @return an object contaning the infobox parameters and values
 *     orig_infobox_params["parameter"] = "value"
 */
migrateIIJ.parseInfobox = function(infobox_text) {
	// to store
	var temp_param_arr = [];

	var temp_arr = infobox_text.split("=");

	var temp_len_i = temp_arr.length;

	for(var i = 0; i < temp_len_i; i++) {
		var temp_str = temp_arr[i];
		var temp_str_arr = temp_str.split(/\|/gm);
		var temp_len = (temp_str_arr.length - 1);

		// second part
		var temp_str_2 = temp_str_arr[temp_len];

		// first part
		var temp_str_1 = temp_str_arr[0];
		for (var j = 1; j < temp_len; j++) {
			temp_str_1 += ("|" + temp_str_arr[j]);
		}

		if (i != 0 ) { // ignore "{{Infobox Indian Jurisdiction<!-- see blah -->"

			if (i == (temp_arr.length - 1)) {
				// remove "}}" at the end
				temp_str_1 = temp_str_1.replace("}}","");
			}

			// trimmed parameter
			temp_param_arr.push(temp_str_1.replace(/^\s+|\s+$/gm,""));
		}

		 if (i != (temp_arr.length - 1)) {
			// trimmed parameter value
			temp_param_arr.push(temp_str_2.replace(/^\s+|\s+$/gm,""));
		}
	}

	// create an object
	// orig_infobox_params["param_name"] = "param value"
	var orig_infobox_params = [];
	for(var i = 0; i < temp_param_arr.length; i = i + 2) {
		var param_name = temp_param_arr[i];
		var param_value = temp_param_arr[i + 1];
		orig_infobox_params[param_name] = param_value;
	}
	return orig_infobox_params;
}


/**
 * Build the Infobox settlement string
 *
 * @return an object contaning the Infobox settlement parameters and values
 *     (orig_infobox_params["parameter"] = "value"
 */
migrateIIJ.prepareInfoboxSettlement = function (orig_infobox_params) {
	// Location map
	var pushpin_map = "India";
	if (orig_infobox_params["state_name"]) {
		pushpin_map = "India " + orig_infobox_params["state_name"];
	}

	// Governing body
	var governing_body = "";
	if (orig_infobox_params["civic_agency"]) {
		governing_body = orig_infobox_params["civic_agency"];
	} else if (orig_infobox_params["planning_agency"]) {
		governing_body = orig_infobox_params["planning_agency"];
	}

	var new_infobox_str =
	"{{Infobox settlement"         + "\n" +
	"| name                    = " + wgPageName.replace("_", " ") + "\n" +
	"| native_name             = " + (orig_infobox_params["native_name"] ? orig_infobox_params["native_name"] : "") + "\n" +
	"| native_name_lang        = " + (orig_infobox_params["native_name_lang"] ? orig_infobox_params["native_name_lang"] : "") + "\n" +
	"| other_name              = " + (orig_infobox_params["other_name"] ? orig_infobox_params["other_name"] : "") + "\n" +
	"| settlement_type         = " + (orig_infobox_params["type"] ? orig_infobox_params["type"] : "") + "\n" +
	"| image_skyline           = " + (orig_infobox_params["skyline"] ? orig_infobox_params["skyline"] : "") + "\n" +
	"| image_alt               = " + (orig_infobox_params["skyline_alt"] ? orig_infobox_params["skyline_alt"] : "") + "\n" +
	"| image_caption           = " + (orig_infobox_params["skyline_caption"] ? orig_infobox_params["skyline_caption"] : "") + "\n" +
	"| image_seal              = " + (orig_infobox_params["seal"] ? orig_infobox_params["seal"] : "") + "\n" +
	"| seal_size               = " + (orig_infobox_params["seal_size"] ? orig_infobox_params["seal_size"] : "") + "\n" +
	"| seal_alt                = " + (orig_infobox_params["seal_alt"] ? orig_infobox_params["seal_alt"] : "") + "\n" +
	/*
	"| image_shield            = " + "\n" +
	"| shield_alt              = " + "\n" +
	*/
	"| nickname                = " + (orig_infobox_params["nickname"] ? orig_infobox_params["nickname"] : "") + "\n" +
	/*
	"| motto                   = " + "\n" +
	"| image_map               = " + "\n" +
	*/
	"| map_alt                 = " + (orig_infobox_params["base_map_alt"] ? orig_infobox_params["base_map_alt"] : "") + "\n" +
	"| map_caption             = " + "\n" +
	"| pushpin_map             = " + pushpin_map + "\n" +
	"| pushpin_label_position  = " + (orig_infobox_params["locator_position"] ? orig_infobox_params["locator_position"] : "") + "\n" +
	"| pushpin_map_alt         = " + "\n" +
	"| pushpin_map_caption     = " + (orig_infobox_params["map_caption"] ? orig_infobox_params["map_caption"] : "") + "\n" +
	"| latd                    = " + (orig_infobox_params["latd"] ? orig_infobox_params["latd"] : "") + "\n" +
	"| latm                    = " + (orig_infobox_params["latm"] ? orig_infobox_params["latm"] : "") + "\n" +
	"| lats                    = " + (orig_infobox_params["lats"] ? orig_infobox_params["lats"] : "") + "\n" +
	"| latNS                   = " + "N\n" +
	"| longd                   = " + (orig_infobox_params["longd"] ? orig_infobox_params["longd"] : "") + "\n" +
	"| longm                   = " + (orig_infobox_params["longm"] ? orig_infobox_params["longm"] : "") + "\n" +
	"| longs                   = " + (orig_infobox_params["longs"] ? orig_infobox_params["longs"] : "") + "\n" +
	"| longEW                  = " + "E\n" +
	/*
	"| coor_pinpoint           = " + "\n" +
	"| coordinates_type        = " + "\n" +
	*/
	"| coordinates_display     = inline,title" + "\n" +
	/*
	"| coordinates_footnotes   = " + "\n" +
	*/
	"| subdivision_type        = Country" + "\n" +
	"| subdivision_name        = " + "India\n" +
	"| subdivision_type1       = " + (orig_infobox_params["state_name"] ? "State" : "") + "\n" +
	"| subdivision_name1       = " + (orig_infobox_params["state_name"] ? orig_infobox_params["state_name"] : "") + "\n" +
	"| subdivision_type2       = " + (orig_infobox_params["jurisdiction_title_1"] ? orig_infobox_params["jurisdiction_title_1"] : (orig_infobox_params["region"] ? "Region" : "")) + "\n" +
	"| subdivision_name2       = " + (orig_infobox_params["jurisdiction_name_1"] ? orig_infobox_params["jurisdiction_name_1"] : (orig_infobox_params["region"] ? orig_infobox_params["region"] : "")) + "\n" +
	"| subdivision_type3       = " + (orig_infobox_params["jurisdiction_title_2"] ? orig_infobox_params["jurisdiction_title_2"] : (orig_infobox_params["division"] ? "Division" : "")) + "\n" +
	"| subdivision_name3       = " + (orig_infobox_params["jurisdiction_name_2"] ? orig_infobox_params["jurisdiction_name_2"] : (orig_infobox_params["division"] ? orig_infobox_params["division"] : "")) + "\n" +
	"| subdivision_type4       = " + (orig_infobox_params["jurisdiction_title_3"] ? orig_infobox_params["jurisdiction_title_3"] : (orig_infobox_params["district"] ? "District" : "")) + "\n" +
	"| subdivision_name4       = " + (orig_infobox_params["jurisdiction_name_3"] ? orig_infobox_params["jurisdiction_name_3"] : (orig_infobox_params["district"] ? orig_infobox_params["district"] : "")) + "\n" +
	"| subdivision_type5       = " + (orig_infobox_params["corp_zone"] ? "Zone" : "") + "\n" +
	"| subdivision_name5       = " + (orig_infobox_params["corp_zone"] ? orig_infobox_params["corp_zone"] : "") + "\n" +
	"| subdivision_type6       = " + (orig_infobox_params["state_name"] ? "Ward" : "") + "\n" +
	"| subdivision_name6       = " + (orig_infobox_params["corp_ward"] ? orig_infobox_params["corp_ward"] : "") + "\n" +
	"| established_title       = " + (orig_infobox_params["established_title"] ? orig_infobox_params["established_title"] : "") + "\n" +
	"| established_date        = " + (orig_infobox_params["established_date"] ? orig_infobox_params["established_date"] : "") + "\n" +
	/*
	"| founder                 = " + "\n" +
	"| seat_type               = " + "\n" +
	"| seat                    = " + "\n" +
	"| government_type	       = " + "\n" +
	*/
	"| governing_body          = " + governing_body + "\n" +
	/*
	"| government_footnotes    = " + "\n" +
	"| leader_party            = " + "\n" +
	*/
	"| leader_title            = " + (orig_infobox_params["leader_title_1"] ? orig_infobox_params["leader_title_1"] : "") + "\n" +
	"| leader_name             = " + (orig_infobox_params["leader_name_1"] ? orig_infobox_params["leader_name_1"] : "") + "\n" +
	"| leader_title1           = " + (orig_infobox_params["leader_title_2"] ? orig_infobox_params["leader_title_2"] : "") + "\n" +
	"| leader_name1            = " + (orig_infobox_params["leader_name_2"] ? orig_infobox_params["leader_name_2"] : "") + "\n" +
	"| leader_title2           = " + (orig_infobox_params["leader_title_3"] ? orig_infobox_params["leader_title_3"] : "") + "\n" +
	"| leader_name2            = " + (orig_infobox_params["leader_name_3"] ? orig_infobox_params["leader_name_3"] : "") + "\n" +
	"| unit_pref               = Metric" + "\n" +
	"| area_footnotes          = " + (orig_infobox_params["area_total_cite"] ? orig_infobox_params["area_total_cite"] : "") + "\n" +
	/*
	"| area_urban_footnotes    = " + "\n" +
	"| area_rural_footnotes    = " + "\n" +
	"| area_metro_footnotes    = " + "\n" +
	"| area_magnitude          = " + "\n" +
	"| area_water_percent      = " + "\n" +
	*/
	"| area_rank               = " + (orig_infobox_params["area_rank"] ? orig_infobox_params["area_rank"] : "") + "\n" +
	"| area_total_km2          = " + (orig_infobox_params["area_total"] ? orig_infobox_params["area_total"] : "") + "\n" + //TODO: not necessarily km2 in the original
	/*
	"| area_land_km2           = " + "\n" +
	"| area_water_km2          = " + "\n" +
	"| area_urban_km2          = " + "\n" +
	"| area_rural_km2          = " + "\n" +
	"| area_metro_km2          = " + "\n" +
	"| dimensions_footnotes    = " + "\n" +
	*/
	"| elevation_footnotes     = " + (orig_infobox_params["altitude_cite"] ? orig_infobox_params["altitude_cite"] : "") + "\n" +
	"| elevation_m             = " + (orig_infobox_params["altitude"] ? orig_infobox_params["altitude"] : "") + "\n" + // TODO: not necessarily m in the original
	"| population_footnotes    = " + "\n" +
	"| population_total        = " + (orig_infobox_params["population_total"] ? orig_infobox_params["population_total"] : "") + "\n" +
	"| population_as_of        = " + (orig_infobox_params["population_as_of"] ? orig_infobox_params["population_as_of"] : "") + "\n" +
	"| population_rank         = " + (orig_infobox_params["population_rank"] ? orig_infobox_params["population_rank"] : "") + "\n" +
	"| population_density_km2  = " + (orig_infobox_params["population_density"] ? orig_infobox_params["population_density"] : "auto") + "\n" + // TODO: not necessarily km2 in the original
	"| population_metro        = " + (orig_infobox_params["population_metro"] ? orig_infobox_params["population_metro"] : "") + "\n" +
	"| population_metro_footnotes = " + (orig_infobox_params["population_metro_cite"] ? orig_infobox_params["population_metro_cite"] : "") + "\n" +
	"| population_demonym      = " + "\n" +
	"| population_note         = " + (orig_infobox_params["population_total_cite"] ? orig_infobox_params["population_total_cite"] : "auto") + "\n" +
	"| demographics_type1      = " + "Languages\n" +
	"| demographics1_title1    = " + "Official\n" +
	"| demographics1_info1     = " + (orig_infobox_params["official_languages"] ? orig_infobox_params["official_languages"] : "") + "\n" +
	"| demographics1_title2    = " + "Regional\n" +
	"| demographics1_info2     = " + (orig_infobox_params["regional_languages"] ? orig_infobox_params["regional_languages"] : "") + "\n" +
	"| demographics1_title3    = " + "Other types\n" +
	"| demographics1_info3     = " + (orig_infobox_params["languages_type"] ? orig_infobox_params["languages_type"] : "") + "\n" +
	"| demographics1_title4    = " + "\n" +
	"| demographics1_info4     = " + (orig_infobox_params["languages"] ? orig_infobox_params["languages"] : "") + "\n" +
	"| demographics1_title5    = " + "\n" +
	"| demographics1_info5     = " + (orig_infobox_params["x"] ? orig_infobox_params["x"] : "") + "\n" +
	"| demographics_type2      = " + "Ethnicity\n" +
	"| demographics2_title1    = " + "Ethnic groups\n" +
	"| demographics2_info1     = " + (orig_infobox_params["ethnic_groups"] ? orig_infobox_params["ethnic_groups"] : "") + "\n" +
	"| demographics2_title2    = " + "Year of data\n" +
	"| demographics2_info2     = " + (orig_infobox_params["ethnic_groups_year"] ? orig_infobox_params["ethnic_groups_year"] : "") + "\n" +
	"| timezone1               = " + "[[Indian Standard Time|IST]]\n" +
	"| utc_offset1             = " + "+5:30\n" +
	"| postal_code_type        = " + "[[Postal Index Number|PIN]]\n" +
	"| postal_code             = " + (orig_infobox_params["postal_code"] ? orig_infobox_params["postal_code"] : "") + "\n" +
	"| area_code_type          = " + "Telephone code\n" +
	"| area_code               = " + (orig_infobox_params["area_telephone"] ? orig_infobox_params["area_telephone"] : "") + "\n" +
	"| iso_code                = " + (orig_infobox_params["abbreviation"] ? orig_infobox_params["abbreviation"] : "") + "\n" +
	"| registration_plate      = " + (orig_infobox_params["vehicle_code_range"] ? orig_infobox_params["vehicle_code_range"] : "") + "\n" +
	"| blank_name_sec1         = " + (orig_infobox_params["blank_title_1"] ? orig_infobox_params["blank_title_1"] : "") + "\n" +
	"| blank_info_sec1         = " + (orig_infobox_params["blank_value_1"] ? orig_infobox_params["blank_value_1"] : "") + "\n" +
	"| blank1_name_sec1        = " + (orig_infobox_params["blank_title_2"] ? orig_infobox_params["blank_title_2"] : "") + "\n" +
	"| blank1_info_sec1        = " + (orig_infobox_params["blank_value_2"] ? orig_infobox_params["blank_value_2"] : "") + "\n" +
	"| blank2_name_sec1        = " + (orig_infobox_params["blank_title_3"] ? orig_infobox_params["blank_title_3"] : "") + "\n" +
	"| blank2_info_sec1        = " + (orig_infobox_params["blank_value_3"] ? orig_infobox_params["blank_value_3"] : "") + "\n" +
	"| blank3_name_sec1        = " + (orig_infobox_params["blank_title_4"] ? orig_infobox_params["blank_title_4"] : "") + "\n" +
	"| blank3_info_sec1        = " + (orig_infobox_params["blank_value_4"] ? orig_infobox_params["blank_value_4"] : "") + "\n" +
	"| website                 = " + (orig_infobox_params["website"] ? orig_infobox_params["website"] : "") + "\n" +
	"| footnotes               = " + (orig_infobox_params["footnotes"] ? orig_infobox_params["footnotes"] : "") + "\n" +
	"}}";

	return new_infobox_str;
}


/**
 * initialize the program
 */
migrateIIJ.init = function() {
	if (migrateIIJ.auto_replace == true) {
		// migrate the infobox
		if (wgAction == "edit") {
			migrateIIJ.migrateInfoboxIJ();
		}

		return;
	}

	// the user has to click on a portlet link to migrate the infobox
	var portletLink = mw.util.addPortletLink(
		"p-cactions",
		"#",
		"Infobox Indian Jurisdiction → settlement",
		"ca-infobox-settlement",
		"Click here to migrate the Infobox Indian Jurisdiction to Infobox settlement"
	);
	$(portletLink).click(function (e) {
		e.preventDefault();
		migrateIIJ.migrateInfoboxIJ();
	});
};

// bind to page load
addOnloadHook(migrateIIJ.init);