משתמש:רובין בוט/הפניות כפולות.js: הבדלים בין גרסאות בדף
קפיצה לניווט
קפיצה לחיפוש
אין תקציר עריכה |
אין תקציר עריכה |
||
| שורה 1: | שורה 1: | ||
const BOT_USERS = [31]; | |||
const userId = mw.config.get("wgUserId"); | |||
if (!BOT_USERS.includes(userId)) { | |||
console.log("Not authorized bot user:", userId); | |||
return; | |||
} | |||
const api = new mw.Api(); | |||
const getRedirectContent = async (title) => { | |||
const { query } = await api.get({ | |||
prop: "revisions", | |||
titles: title, | |||
rvprop: "content", | |||
rvslots: "*", | |||
formatversion: "2", | |||
}); | }); | ||
return query; | |||
}; | |||
const getRedirectTarget = async (title) => { | |||
const { query } = await api.get({ | const { query } = await api.get({ | ||
titles: title, | |||
redirects: true, | |||
}); | }); | ||
if (query?.pages) { | if (query?.pages) { | ||
const page = Object.values(query.pages).find(p => !p.missing); | |||
return page?.title ?? null; | |||
} | } | ||
return null; | return null; | ||
}; | |||
const extractAnchorFromContent = (content) => { | |||
const match = content.match(/\[\[.*?(?:#(.+?))?\]\]/); | const match = content.match(/\[\[.*?(?:#(.+?))?\]\]/); | ||
return match?.[1] ? `#${match[1]}` : ""; | return match?.[1] ? `#${match[1]}` : ""; | ||
}; | |||
const getNamespacePrefix = async (title) => { | |||
const { query } = await api.get({ titles: title, prop: "info" }); | |||
if (query?.pages) { | if (query?.pages) { | ||
const page = Object.values(query.pages)[0]; | |||
const nsPrefixes = {4:"חב\"דפדיה:",6:"קובץ:",10:"תבנית:",12:"עזרה:"}; | |||
const | return nsPrefixes[page.ns] ?? ""; | ||
return | |||
} | } | ||
return ""; | return ""; | ||
}; | |||
const createRedirect = async (title, target) => { | |||
try { | try { | ||
await api.postWithEditToken({ | |||
action: "edit", | |||
title, | |||
text: `#הפניה [[${target}]]`, | |||
tags: "doubleredirect-bot", | |||
bot: true, | |||
}); | |||
mw.notify(`Redirect created: ${title}`); | |||
} catch(e) { | |||
} catch ( | console.error(e); | ||
} | } | ||
}; | |||
const processRedirect = async (title) => { | |||
try { | try { | ||
const contentData = await getRedirectContent(title); | |||
const content = contentData.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) { console.log("Circular redirect:", title); return; } | |||
console.log("Creating redirect to:", fullTarget); | |||
await createRedirect(title, fullTarget); | |||
} catch ( | } catch(e) { | ||
console.error(e); | |||
} | } | ||
}; | |||
const init = async () => { | |||
const pageName = mw.config.get("wgPageName"); | const pageName = mw.config.get("wgPageName"); | ||
if (pageName !== "מיוחד:הפניות_כפולות") return; | if (pageName !== "מיוחד:הפניות_כפולות") return; | ||
const num = prompt(" | const num = prompt("כמה הפניות כפולות להציג?", 0); | ||
if (!num) return; | if (!num) return; | ||
try { | try { | ||
const { query } = await api.get({ list:"querypage", qppage:"DoubleRedirects", qplimit:num }); | |||
const redirects = query.querypage.results; | |||
for (const r of redirects) { | |||
await processRedirect(r.title.replace(/_/g," ")); | |||
} | |||
} catch(e) { console.error(e); } | |||
}; | |||
} catch ( | |||
$(init); | |||
גרסה מ־16:59, 23 בנובמבר 2025
const BOT_USERS = [31];
const userId = mw.config.get("wgUserId");
if (!BOT_USERS.includes(userId)) {
console.log("Not authorized bot user:", userId);
return;
}
const api = new mw.Api();
const getRedirectContent = async (title) => {
const { query } = await api.get({
prop: "revisions",
titles: title,
rvprop: "content",
rvslots: "*",
formatversion: "2",
});
return query;
};
const getRedirectTarget = async (title) => {
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;
};
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];
const nsPrefixes = {4:"חב\"דפדיה:",6:"קובץ:",10:"תבנית:",12:"עזרה:"};
return nsPrefixes[page.ns] ?? "";
}
return "";
};
const createRedirect = async (title, target) => {
try {
await api.postWithEditToken({
action: "edit",
title,
text: `#הפניה [[${target}]]`,
tags: "doubleredirect-bot",
bot: true,
});
mw.notify(`Redirect created: ${title}`);
} catch(e) {
console.error(e);
}
};
const processRedirect = async (title) => {
try {
const contentData = await getRedirectContent(title);
const content = contentData.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) { console.log("Circular redirect:", title); return; }
console.log("Creating redirect to:", fullTarget);
await createRedirect(title, fullTarget);
} catch(e) {
console.error(e);
}
};
const init = async () => {
const pageName = mw.config.get("wgPageName");
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 r of redirects) {
await processRedirect(r.title.replace(/_/g," "));
}
} catch(e) { console.error(e); }
};
$(init);