MediaWiki:UpdateLanguageNameAndCode.js

Từ điển mở Wiktionary

Chú ý: Sau khi lưu trang này, phải xóa bộ nhớ đệm (cache) của trình duyệt để những thay đổi hiện ra

  • Firefox / Safari: Nhấn giữ phím Shift trong khi nhấn Tải lại (Reload), hoặc nhấn tổ hợp Ctrl-F5 hay Ctrl-R (⌘R trên Mac)
  • Google Chrome: Nhấn tổ hợp Ctrl-Shift-R (⇧⌘R trên Mac)
  • Internet Explorer / Edge: Nhấn giữ phím Ctrl trong khi nhấn Làm tươi (Refresh), hoặc nhấn tổ hợp Ctrl-F5
  • Opera: Nhấn tổ hợp Ctrl-F5.
//From [[en:MediaWiki:UpdateLanguageNameAndCode.js]]
/* 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|cập nhật]]";

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>