MediaWiki:UpdateLanguageNameAndCode.js

/* jshint undef: true */ /* globals $, apiWrapper, mw */ //

{ "use strict";

const action = mw.config.get("wgAction"); const api = new mw.Api({	timeout: 30 * 1000, // Dirty hack to hopefully get rid of timeout errors. });

const updatePageWithTemplateExpansion = function (title, template, summary, changeTemplateExpansion) { return mw.loader.using("mediawiki.api", function {		return api.get({ action: "expandtemplates", title: title, text: template, prop: "wikitext", }).done(function (data) { var expanded = data.expandtemplates.wikitext; return api.edit(title, function {				return {					text: changeTemplateExpansion						? changeTemplateExpansion(expanded)						: expanded,					summary: summary,				};			}).done(function (data) {				if (data.nochange) {					mw.notify(title + " was up-to-date already.");				} else {					mw.notify("Updated " + title + ".");				}			}).fail(function(...args) {				mw.notify("Failed to post!");				console.log(...args);			}); });	}); };

const summary = "updated";

const updateLanguageData = function (title, moduleFunction) { return updatePageWithTemplateExpansion(title, "", summary).then(function {		return updatePageWithTemplateExpansion(title + ".json", "", summary);	}); };

const button = $(" ");

button .html("Update  and   and other data modules") .attr("id", "update-module");

button.on("click", function {	updateLanguageData("Module:languages/code to canonical name", "code_to_name").then(function { return updateLanguageData("Module:languages/canonical names", "name_to_code"); }).then(function { updatePageWithTemplateExpansion(			"Module:Hani-sortkey/data/serialized",			"",			summary,			function(expanded) {				return 'return "' + expanded + '"';			}		); }); });

// Color the button to help editors to notice it. // Add the following to Special:MyPage/common.js to switch back to the default styles: // window.plainModuleUpdateButton = true; if (!window.plainModuleUpdateButton) mw.util.addCSS("#update-module, #update-module code { background-color: orange; }");

const p = $(".mw-parser-output p:first-of-type"); p.before(button);

} // empty block scope

//