לדלג לתוכן

משתמש:רובין בוט/הפניות כפולות.js – הבדלי גרסאות

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית
אין תקציר עריכה
אין תקציר עריכה
שורה 1: שורה 1:
# ניצור את הגרסה המלאה של הסקריפט עבור משתמש:רובין בוט/הפניות כפולות.js
final_script_userpage = """
final_script_userpage = """
(() => {
(() => {

גרסה מ־15:52, 23 ביולי 2025

final_script_userpage = """
(() => {
    const api = new mw.Api();

    const skipTitles = [
        "770",
        "פורטל:חסידים ואנשי מעשה",
        "פורטל:משיח וגאולה",
        "פורטל:\\"הקהל\\"",
        "פורטל:ימי חבד",
        "פורטל:ניגוני חבד",
        "פורטל:נשיאי חבד",
        "פורטל:חסידות חב\\\"ד",
        "פורטל:תורת חב\\\"ד",
        "שיחה:פורטל:\\"הקהל\\"",
        "פורטל:אישי חב\\\"ד",
        "פורטל:בית רבי",
        "שיחה:פורטל:אישי חב\\\"ד",
        "שיחה:פורטל:בית רבי"
    ];

    const getRedirectContent = async (title) => {
        try {
            const response = await api.get({
                prop: "revisions",
                titles: title,
                rvprop: "content",
                rvslots: "*",
                formatversion: "2",
            });
            const page = response.query?.pages?.[0];
            return page?.revisions?.[0]?.slots?.main?.content ?? null;
        } catch {
            return null;
        }
    };

    const getRedirectTarget = async (title) => {
        try {
            const { query } = await api.get({
                titles: title,
                redirects: true,
            });
            if (query?.pages) {
                const page = Object.values(query.pages).find((p) => !p.missing);
                return page?.title ?? null;
            }
            return null;
        } catch {
            return null;
        }
    };

    const extractAnchorFromContent = (content) => {
        const match = content.match(/#הפניה\\s*\\[\\[[^\\]]+?(?:#(.+?))?\\]\\]/);
        return match?.[1] ? "#" + match[1] : "";
    };

    const getNamespacePrefix = async (title) => {
        try {
            const { query } = await api.get({
                titles: title,
                prop: "info",
            });
            if (query?.pages) {
                const page = Object.values(query.pages)[0];
                const ns = page.ns;
                const prefixes = { 4: "חבדפדיה:", 6: "קובץ:", 10: "תבנית:", 12: "עזרה:" };
                return prefixes[ns] ?? "";
            }
            return "";
        } catch {
            return "";
        }
    };

    const createRedirect = async (title, target) => {
        try {
            await api.postWithEditToken({
                action: "edit",
                format: "json",
                title: title,
                text: "#הפניה [[" + target + "]]",
                summary: "בוט: מתקן הפניה כפולה ל- [[" + target + "]]",
            });
            mw.notify(title + ": ההפניה תוקנה ל- " + target, { type: 'success' });
        } catch (error) {
            mw.notify(title + ": שגיאה בעת תיקון ההפניה", { type: 'error' });
        }
    };

    const processRedirect = async (title) => {
        if (skipTitles.includes(title)) return;
        try {
            const content = await getRedirectContent(title);
            if (content === null) return;

            const anchor = extractAnchorFromContent(content);
            const finalTarget = await getRedirectTarget(title);
            if (!finalTarget) return;

            const namespace = await getNamespacePrefix(finalTarget);
            const fullTarget = namespace + finalTarget + anchor;

            if (title.replace(/_/g, " ") === fullTarget.replace(/_/g, " ")) return;

            await createRedirect(title, fullTarget);
        } catch { }
    };

    const init = async () => {
        const pageName = mw.config.get("wgPageName");
        if (pageName !== "מיוחד:הפניות_כפולות") return;

        const numInput = prompt("כמה הפניות לתקן?", "10");
        const num = parseInt(numInput, 10);
        if (isNaN(num) || num <= 0) return;

        try {
            const { query } = await api.get({
                list: "querypage",
                qppage: "DoubleRedirects",
                qplimit: num,
            });

            const redirects = query.querypage.results;
            if (!redirects.length) return;

            for (const redirect of redirects) {
                const title = redirect.title.replace(/_/g, " ");
                await processRedirect(title);
            }
        } catch { }
    };

    $(init);
})();
"""

script_userpage_path = "/mnt/data/הפניות_כפולות_למשתמש_רובין_בוט.js"
with open(script_userpage_path, "w", encoding="utf-8") as f:
    f.write(final_script_userpage)

script_userpage_path