משתמש:רובין בוט/הפניות כפולות.js: הבדלים בין גרסאות בדף
קפיצה לניווט
קפיצה לחיפוש
אין תקציר עריכה |
אין תקציר עריכה |
||
| (3 גרסאות ביניים של אותו משתמש אינן מוצגות) | |||
| שורה 39: | שורה 39: | ||
if (query?.pages) { | if (query?.pages) { | ||
const page = Object.values(query.pages)[0]; | const page = Object.values(query.pages)[0]; | ||
if (page.ns | if (page.ns) { | ||
const namespacePrefixes = { | const namespacePrefixes = { | ||
4: | 4: "חב"דציטוט:", | ||
6: "קובץ:", | 6: "קובץ:", | ||
10: "תבנית:", | 10: "תבנית:", | ||
12: "עזרה:", | 12: "עזרה:", | ||
}; | }; | ||
return namespacePrefixes[page.ns] ?? ""; | return namespacePrefixes[page.ns] ?? ""; | ||
| שורה 58: | שורה 57: | ||
action: "edit", | action: "edit", | ||
format: "json", | format: "json", | ||
tags: "doubleredirect-bot", | |||
bot: true, | bot: true, | ||
title: title, | title: title, | ||
text: `#הפניה [[${target}]]`, | text: `#הפניה [[${target}]]`, | ||
}); | }); | ||
mw.notify(`\nstatus:${title} | mw.notify(`\nstatus:${title} succes`); | ||
} catch (error) { | } catch (error) { | ||
console.error(error); | console.error(error); | ||
גרסה אחרונה מ־17:03, 23 בנובמבר 2025
(() => {
const BOT_USERS = [31];
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);
})();