לדלג לתוכן

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

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית
אין תקציר עריכה
אין תקציר עריכה
 
(9 גרסאות ביניים של אותו משתמש אינן מוצגות)
שורה 1: שורה 1:
# ניצור את הגרסה המלאה של הסקריפט עבור משתמש:רובין בוט/הפניות כפולות.js
(() => {
  const BOT_USERS = [8516];
  const api = new mw.Api();


final_script_userpage = """
  const getRedirectContent = async (title) => {
(() => {
     return api.get({
     const api = new mw.Api();
      prop: "revisions",
      titles: title,
      rvprop: "content",
      rvslots: "*",
      formatversion: "2",
    });
  };


     const skipTitles = [
  const getRedirectTarget = async (title) => {
        "770",
     const { query } = await api.get({
        "פורטל:חסידים ואנשי מעשה",
      titles: title,
        "פורטל:משיח וגאולה",
      redirects: true,
        "פורטל:\\"הקהל\\"",
     });
        "פורטל:ימי חבד",
        "פורטל:ניגוני חבד",
        "פורטל:נשיאי חבד",
        "פורטל:חסידות חב\\\"ד",
        "פורטל:תורת חב\\\"ד",
        "שיחה:פורטל:\\"הקהל\\"",
        "פורטל:אישי חב\\\"ד",
        "פורטל:בית רבי",
        "שיחה:פורטל:אישי חב\\\"ד",
        "שיחה:פורטל:בית רבי"
     ];


     const getRedirectContent = async (title) => {
     if (query?.pages) {
        try {
      const page = Object.values(query.pages).find((page) => !page.missing);
            const response = await api.get({
      return page?.title ?? null;
                prop: "revisions",
    }
                titles: title,
    return null;
                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) => {
  const extractAnchorFromContent = (content) => {
        try {
    const match = content.match(/\[\[.*?(?:#(.+?))?\]\]/);
            const { query } = await api.get({
    return match?.[1] ? `#${match[1]}` : "";
                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 getNamespacePrefix = async (title) => {
        const match = content.match(/#הפניה\\s*\\[\\[[^\\]]+?(?:#(.+?))?\\]\\]/);
    const { query } = await api.get({
        return match?.[1] ? "#" + match[1] : "";
      titles: title,
     };
      prop: "info",
     });


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


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


    const processRedirect = async (title) => {
  const processRedirect = async (title) => {
        if (skipTitles.includes(title)) return;
    try {
        try {
      const contentData = await getRedirectContent(title);
            const content = await getRedirectContent(title);
      const content =
            if (content === null) return;
        contentData.query.pages[0].revisions[0].slots.main.content;
      const anchor = extractAnchorFromContent(content);


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


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


            if (title.replace(/_/g, " ") === fullTarget.replace(/_/g, " ")) return;
      if (title === finalTarget) {
        alert("הפניה מעגלית");
        return;
      }


            await createRedirect(title, fullTarget);
      console.log("Creating redirect to:", fullTarget);
        } catch { }
      await createRedirect(title, fullTarget);
    };
    } catch (error) {
      console.error("Error processing redirect:", error);
    }
  };


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


        const numInput = prompt("כמה הפניות לתקן?", "10");
    if (!userGroups.includes("bot") && !BOT_USERS.includes(userId)) return;
        const num = parseInt(numInput, 10);
    if (pageName !== "מיוחד:הפניות_כפולות") return;
        if (isNaN(num) || num <= 0) return;


        try {
    const num = prompt("? כמה הפניות כפולות להציג", 0);
            const { query } = await api.get({
    if (!num) return;
                list: "querypage",
                qppage: "DoubleRedirects",
                qplimit: num,
            });


            const redirects = query.querypage.results;
    try {
            if (!redirects.length) return;
      const { query } = await api.get({
        list: "querypage",
        qppage: "DoubleRedirects",
        qplimit: num,
      });


            for (const redirect of redirects) {
      const redirects = query.querypage.results;
                const title = redirect.title.replace(/_/g, " ");
      for (const redirect of redirects) {
                await processRedirect(title);
        const title = redirect.title.replace(/_/g, " ");
            }
        await processRedirect(title);
        } catch { }
      }
    };
    } catch (error) {
      console.error("Error fetching redirects:", error);
    }
  };


    $(init);
  $(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

גרסה אחרונה מ־11:20, 27 בנובמבר 2025

(() => {
  const BOT_USERS = [8516];
  const api = new mw.Api();

  const getRedirectContent = async (title) => {
    return api.get({
      prop: "revisions",
      titles: title,
      rvprop: "content",
      rvslots: "*",
      formatversion: "2",
    });
  };

  const getRedirectTarget = async (title) => {
    const { query } = await api.get({
      titles: title,
      redirects: true,
    });

    if (query?.pages) {
      const page = Object.values(query.pages).find((page) => !page.missing);
      return page?.title ?? null;
    }
    return null;
  };

  const extractAnchorFromContent = (content) => {
    const match = content.match(/\[\[.*?(?:#(.+?))?\]\]/);
    return match?.[1] ? `#${match[1]}` : "";
  };

  const getNamespacePrefix = async (title) => {
    const { query } = await api.get({
      titles: title,
      prop: "info",
    });

    if (query?.pages) {
      const page = Object.values(query.pages)[0];
      if (page.ns) {
        const namespacePrefixes = {
          4: 'חב\"דפדיה:',
          6: "קובץ:",
          10: "תבנית:",
          12: "עזרה:",
        };
        return namespacePrefixes[page.ns] ?? "";
      }
    }
    return "";
  };

  const createRedirect = async (title, target) => {
    try {
      await api.postWithEditToken({
        action: "edit",
        format: "json",
        tags: "doubleredirect-bot",
        bot: true,
        title: title,
        text: `#הפניה [[${target}]]`,
      });
      mw.notify(`\nstatus:${title} succes`);
    } catch (error) {
      console.error(error);
    }
  };

  const processRedirect = async (title) => {
    try {
      const contentData = await getRedirectContent(title);
      const content =
        contentData.query.pages[0].revisions[0].slots.main.content;
      const anchor = extractAnchorFromContent(content);

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

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

      if (title === finalTarget) {
        alert("הפניה מעגלית");
        return;
      }

      console.log("Creating redirect to:", fullTarget);
      await createRedirect(title, fullTarget);
    } catch (error) {
      console.error("Error processing redirect:", error);
    }
  };

  const init = async () => {
    const userGroups = mw.config.get("wgUserGroups");
    const userId = mw.config.get("wgUserId");
    const pageName = mw.config.get("wgPageName");

    if (!userGroups.includes("bot") && !BOT_USERS.includes(userId)) return;
    if (pageName !== "מיוחד:הפניות_כפולות") return;

    const num = prompt("? כמה הפניות כפולות להציג", 0);
    if (!num) return;

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

      const redirects = query.querypage.results;
      for (const redirect of redirects) {
        const title = redirect.title.replace(/_/g, " ");
        await processRedirect(title);
      }
    } catch (error) {
      console.error("Error fetching redirects:", error);
    }
  };

  $(init);
})();