MediaWiki:UpdateLanguageNameAndCode.js

Ji Wîkîferhengê

Zanibe: Piştî weşandinê, ji bo dîtina guhartinan dibe ku hewce be "cache"ya geroka xwe paqij bikî.

  • Firefox / Safari: Pê li Shift û Reload bike an jî Ctrl-F5 an Ctrl-R bike (ji bo Mac: ⌘-R)
  • Google Chrome: Pê li Ctrl-Shift-R (ji bo Mac: ⌘-Shift-R) bike
  • Internet Explorer / Edge: Pê li Ctrl û Refresh bike, an jî Ctrl-F5 bike
  • Opera: Pê li Ctrl-F5 bike.
/* [[en:MediaWiki:UpdateLanguageNameAndCode.js]]
/* jshint esversion: 6, undef: true */
/* globals $, apiWrapper, mw */
// <nowiki>

{
"use strict";

const action = mw.config.get("wgAction");
const update = function (pageType) {
    const title = pageType === "code_to_name"
        ? "Modul:ziman/ji kod li nav"
        : pageType === "name_to_code"
            ? "Modul:ziman/navên zimanan"
            : (function() { throw new TypeError("Expected 'code_to_name' or 'name_to_code'")})();
    const template = "{{#invoke:languages/print|" + pageType + "|plain}}";
    const summary = "[[MediaWiki:UpdateLanguageNameAndCode.js|rojane kir]]";

    return mw.loader.using("mediawiki.api", function () {
        return new mw.Api().get({
            action: "expandtemplates",
            title: title,
            text: template,
            prop: "wikitext",
        }).done(function (data) {
            var expanded = data.expandtemplates.wikitext;
            return new mw.Api().edit(title, function () {
                return {
                    text: expanded,
                    summary: summary,
                };
            }).done(function (data) {
                if (data.nochange) {
                    mw.notify(title + " jixwe rojane ye.");
                } else {
                    mw.notify(title + " hat rojanekirin.");
                }
            }).fail(function(...args) {
                mw.notify("Pirsgirêkek derket!");
                console.log(...args);
            });
        });
    }); // getScript
};

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

button
    .html("<code>Rojane bike</code>: Modul:ziman/ji kod li nav û Modul:ziman/navên zimanan")
    .attr("id", "update-module");

button.on("click", function () {
    update("code_to_name").then(function() {
        update("name_to_code").then(function() {
            setTimeout(function() {
                updateJson();
            }, 5000); // Wait for 1 second (1000 milliseconds) before running updateJson
        });
    });
});

const updateJson = function () {
    const jsonPageTitle = "MediaWiki:Gadget-translation editor.js/ziman.json";
    const luaModuleTitle = "Modul:ziman/ji_kod_li_nav";
    
    return mw.loader.using(["mediawiki.api", "mediawiki.user"]).then(function () {
        // Fetch CSRF token
        return new mw.Api().get({
            action: "query",
            meta: "tokens",
            format: "json"
        }).then(function (data) {
            const csrfToken = data.query.tokens.csrftoken;

            // Fetch Lua module content
            return new mw.Api().get({
                action: "query",
                prop: "revisions",
                rvprop: "content",
                titles: luaModuleTitle,
                formatversion: "2",
                format: "json"
            }).then(function (data) {
                const luaModuleContent = data.query.pages[0].revisions[0].content;

                // Convert Lua module content to JSON-like format
                const jsonContent = luaModuleContent
                    .replace(/^return\s*/, "") // Remove "return"
                    .replace(/\,\s*\}/, "}") // Remove trailing comma if present
                    .replace(/\[\"([^\"]+)\"\] = \"([^\"]+)\"/g, '"$1": "$2"');

                const summary = "[[MediaWiki:UpdateLanguageNameAndCode.js|rojane kir]]";
				console.log (jsonContent);
                // Update the JSON page using CSRF token
                return new mw.Api().post({
                    action: "edit",
                    title: jsonPageTitle,
                    text: jsonContent,
                    summary: summary,
                    token: csrfToken, // Include the CSRF token
                    format: "json"
                }).then(function (data) {
                    if (data.edit && data.edit.result === "Success") {
                        mw.notify(jsonPageTitle + " was updated successfully.");
                    } else {
                        mw.notify("Failed to update " + jsonPageTitle);
                    }
                }).catch(function (error) {
                    mw.notify("An error occurred while updating " + jsonPageTitle);
                    console.log(error);
                });
            });
        });
    });
};

// 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>