לדלג לתוכן

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

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


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


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


    const getNamespacePrefix = async (title) => {
  const extractAnchorFromContent = (content) => {
        try {
    const match = content.match(/\[\[.*?(?:#(.+?))?\]\]/);
            const { query } = await api.get({
    return match?.[1] ? `#${match[1]}` : "";
                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) => {
  const getNamespacePrefix = async (title) => {
        try {
    const { query } = await api.get({
            await api.postWithEditToken({
      titles: title,
                action: "edit",
      prop: "info",
                format: "json",
    });
                title: title,
                text: "#הפניה [[" + target + "]]",
                summary: "בוט: מתקן הפניה כפולה ל- [[" + target + "]]"
            });
            mw.notify(title + ": ההפניה תוקנה ל- " + target, { type: 'success' });
        } catch {
            mw.notify(title + ": שגיאה בעת תיקון ההפניה", { type: 'error' });
        }
    };


     const processRedirect = async (title) => {
     if (query?.pages) {
        if (skipTitles.includes(title)) return;
      const page = Object.values(query.pages)[0];
        try {
      if (page.ns) {
            const content = await getRedirectContent(title);
        const namespacePrefixes = {
            if (content === null) return;
          4: 'חב\"דפדיה:',
            const anchor = extractAnchorFromContent(content);
          6: "קובץ:",
            const finalTarget = await getRedirectTarget(title);
          10: "תבנית:",
            if (!finalTarget) return;
          12: "עזרה:",
            const namespace = await getNamespacePrefix(finalTarget);
        };
            const fullTarget = namespace + finalTarget + anchor;
        return namespacePrefixes[page.ns] ?? "";
            if (title.replace(/_/g, " ") === fullTarget.replace(/_/g, " ")) return;
      }
            await createRedirect(title, fullTarget);
    }
        } catch {}
     return "";
     };
  };


    const init = async () => {
  const createRedirect = async (title, target) => {
        const pageName = mw.config.get("wgPageName");
    try {
         if (pageName !== "מיוחד:הפניות_כפולות") return;
      await api.postWithEditToken({
         const numInput = prompt("כמה הפניות לתקן?", "10");
        action: "edit",
         const num = parseInt(numInput, 10);
         format: "json",
         if (isNaN(num) || num <= 0) return;
         tags: "doubleredirect-bot",
         try {
         bot: true,
            const { query } = await api.get({
         title: title,
                list: "querypage",
         text: `#הפניה [[${target}]]`,
                qppage: "DoubleRedirects",
      });
                qplimit: num
      mw.notify(`\nstatus:${title} succes`);
            });
    } catch (error) {
            const redirects = query.querypage.results;
      console.error(error);
            if (!redirects.length) return;
    }
            for (const redirect of redirects) {
  };
                const title = redirect.title.replace(/_/g, " ");
                await processRedirect(title);
            }
            mw.notify("סיום תיקון ההפניות.", { type: 'success' });
        } catch {}
    };


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

גרסה אחרונה מ־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);
})();