MediaWiki:UpdateLanguageNameAndCode.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

Updates both Module:languages/code to canonical name and Module:languages/canonical names when you click a button at the top of one of their documentation pages.

It also updates their JSON variants Module:languages/code to canonical name.json and Module:languages/canonical names.json.


/* jshint undef: true */
/* globals $, apiWrapper, mw */
// <nowiki>

{
"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 = "[[MediaWiki:UpdateLanguageNameAndCode.js|updated]]";

const updateLanguageData = function (title, moduleFunction) {
	return updatePageWithTemplateExpansion(title, "{{#invoke:languages/print|" + moduleFunction + "|plain}}", summary).then(function() {
		return updatePageWithTemplateExpansion(title + ".json", "{{#invoke:languages/print|" + moduleFunction + "|json}}", summary);
	});
};

const button = $("<button>");

button
	.html("Update <code>Module:languages/code to canonical name</code> and <code>Module:languages/canonical names</code> 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",
			"{{#invoke:Hani-sortkey/data/serializer|main}}",
			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

// </nowiki>