|
|
| Line 1: |
Line 1: |
| /* jshint undef: true */ | | // This gadget contains code that will run unconditionally for everyone - both on mobile and desktop. |
| /* globals $, apiWrapper, mw, OO */ | | // Consider instead adding your code to [[MediaWiki:Common.js]], [[MediaWiki:Mobile.js]], or a gadget. |
| // <nowiki> | | // <nowiki> |
| mw.loader.using("oojs-ui-core").then(() => {
| |
| "use strict";
| |
|
| |
|
| const api = new mw.Api({
| | const pageName = mw.config.get("wgPageName"); |
| timeout: 30 * 1000, // Dirty hack to hopefully get rid of timeout errors.
| | const ns = mw.config.get("wgNamespaceNumber"); |
| });
| |
|
| |
|
| const updatePageWithTemplateExpansion = async (
| | /* ***** Page-specific code ***** */ |
| title,
| | switch (pageName) { |
| template,
| | case "MediaWiki:Gadgets-definition": // [[MediaWiki:Gadgets-definition]] |
| summary,
| | importScript("MediaWiki:BetterGadgetsDefinition.js"); // [[MediaWiki:BetterGadgetsDefinition.js]] |
| changeTemplateExpansion
| | break; |
| ) => { | | case "Special:Search": // [[Special:Search]] |
| await mw.loader.using("mediawiki.api"); | | importScript("MediaWiki:FindTrans.js"); // [[MediaWiki:FindTrans.js]] |
| try { | | break; |
| const data = await api.get({
| | case "Wiktionary:Fonts/list": // [[Wiktionary:Fonts/list]] |
| action: "expandtemplates",
| | if (mw.config.get("wgAction") === "view") |
| title,
| | importScript("MediaWiki:InteractiveFontList.js"); // [[MediaWiki:InteractiveFontList.js]] |
| text: template,
| | break; |
| prop: "wikitext",
| | } |
| });
| |
| const expanded = data.expandtemplates.wikitext;
| |
| const editResponse = await api.edit(title, () => ({
| |
| text: changeTemplateExpansion
| |
| ? changeTemplateExpansion(expanded)
| |
| : expanded,
| |
| summary,
| |
| }));
| |
| if (editResponse.nochange) {
| |
| mw.notify(`${title} was up-to-date already.`);
| |
| } else {
| |
| mw.notify(`Updated ${title}.`);
| |
| }
| |
| } catch (error) {
| |
| mw.notify("Failed to post!"); | |
| console.error(error);
| |
| } | |
| };
| |
|
| |
|
| const summary = "[[MediaWiki:UpdateLanguageNameAndCode.js|updated]]"; | | if ( |
| | document.querySelector(".mw-parser-output p:first-of-type") && [ |
| | "Module:languages/code_to_canonical_name", |
| | "Module:languages/canonical_names", |
| | "Module:etymology_languages/code_to_canonical_name", |
| | "Module:etymology_languages/canonical_names", |
| | "Module:families/code_to_canonical_name", |
| | "Module:families/canonical_names", |
| | "Module:scripts/code_to_canonical_name", |
| | "Module:scripts/canonical_names", |
| | ].some(title => pageName.startsWith(title)) |
| | ) |
| | importScript("MediaWiki:UpdateLanguageNameAndCode.js"); // [[MediaWiki:UpdateLanguageNameAndCode.js]] |
|
| |
|
| const updateLanguageData = async (title, moduleFunction, moduleType) => {
| | /* ***** Clean up category pages showing "0 c" or "0 e", e.g. [[Category:Pages with entries]] ***** */ |
| await updatePageWithTemplateExpansion(
| | for (const elem of document.querySelectorAll(".CategoryTreeItem bdi + span")) |
| title,
| | elem.textContent = elem.textContent |
| `{{#invoke:languages/print|${moduleFunction}|plain|${moduleType}}}`,
| | .replace(/\b0 c, /, "") |
| summary
| | .replace(/, 0 [ep]\b/, "") |
| );
| | .replace(/\(0 c\)/, "(0 e)"); |
| return updatePageWithTemplateExpansion(
| |
| `${title}.json`,
| |
| `{{#invoke:languages/print|${moduleFunction}|json|${moduleType}}}`,
| |
| summary
| |
| );
| |
| }; | |
|
| |
| const updateButton = new OO.ui.ButtonWidget({
| |
| label: "Update language, etymology language, language family and script modules, and Hani-sortkey serialized data module",
| |
| flags: [ | |
| 'primary',
| |
| 'progressive'
| |
| ] | |
| });
| |
| updateButton.$element.attr("id", "update-module");
| |
|
| |
|
| updateButton.on("click", async () => {
| | /* ***** Template gadget for [[Module:UnitTests]]: e.g. [[Module:vi/testcases]] ***** */ |
| updateButton.setDisabled(true);
| | for (const corner of document.querySelectorAll(".unit-tests .unit-tests-img-corner")) |
| try {
| | corner.addEventListener("click", () => corner.closest(".unit-tests").classList.toggle("unit-tests-hide-passing")); |
| await Promise.all([
| |
| updateLanguageData(
| |
| "Module:languages/code to canonical name",
| |
| "code_to_name",
| |
| "language"
| |
| ),
| |
| updateLanguageData(
| |
| "Module:languages/canonical names",
| |
| "name_to_code",
| |
| "language"
| |
| ),
| |
| updateLanguageData(
| |
| "Module:etymology languages/code to canonical name",
| |
| "code_to_name",
| |
| "etymology"
| |
| ),
| |
| updateLanguageData(
| |
| "Module:etymology languages/canonical names",
| |
| "name_to_code",
| |
| "etymology"
| |
| ),
| |
| updateLanguageData(
| |
| "Module:families/code to canonical name",
| |
| "code_to_name",
| |
| "family"
| |
| ),
| |
| updateLanguageData(
| |
| "Module:families/canonical names",
| |
| "name_to_code",
| |
| "family"
| |
| ),
| |
| updateLanguageData(
| |
| "Module:scripts/code to canonical name",
| |
| "code_to_name",
| |
| "script"
| |
| ),
| |
| updateLanguageData(
| |
| "Module:scripts/canonical names",
| |
| "name_to_code",
| |
| "script"
| |
| ),
| |
| updatePageWithTemplateExpansion(
| |
| "Module:Hani-sortkey/data/serialized",
| |
| "{{#invoke:Hani-sortkey/data/serializer|main}}",
| |
| summary,
| |
| (expanded) => `return "${expanded}"`
| |
| ),
| |
| ]);
| |
| } catch (error) {
| |
| console.error(error);
| |
| } finally {
| |
| updateButton.setDisabled(false);
| |
| }
| |
| });
| |
|
| |
|
| // Insert the button before the first paragraph in .mw-parser-output.
| |
| $(".mw-parser-output p:first-of-type").before(updateButton.$element);
| |
| });
| |
| // </nowiki> | | // </nowiki> |