|
|
| שורה 1: |
שורה 1: |
| // קרדיט: המכלול (גרסה משופרת - מאפשרת הרצה לכל משתמש מחובר)
| |
| (() => { | | (() => { |
| const api = new mw.Api();
| | const BOT_USERS = [8516]; |
| | const api = new mw.Api(); |
|
| |
|
| /**
| | const getRedirectContent = async (title) => { |
| * מקבל את תוכן דף ההפניה.
| | return api.get({ |
| * @param {string} title - שם הדף.
| | prop: "revisions", |
| * @returns {Promise<string|null>} תוכן הדף או null אם הייתה שגיאה.
| | titles: title, |
| */
| | rvprop: "content", |
| const getRedirectContent = async (title) => {
| | rvslots: "*", |
| try {
| | formatversion: "2", |
| 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 (error) {
| |
| console.error("שגיאה בקבלת תוכן הפניה עבור " + title + ":", error);
| |
| return null;
| |
| }
| |
| };
| |
|
| |
|
| /**
| | const getRedirectTarget = async (title) => { |
| * מקבל את יעד ההפניה הסופי.
| | const { query } = await api.get({ |
| * @param {string} title - שם דף ההפניה.
| | titles: title, |
| * @returns {Promise<string|null>} יעד ההפניה או null אם לא נמצא.
| | redirects: true, |
| */
| | }); |
| const getRedirectTarget = async (title) => {
| |
| try {
| |
| const { query } = await api.get({
| |
| titles: title,
| |
| redirects: true, // מאפשר למדיה-וויקי לעקוב אחר הפניות
| |
| });
| |
|
| |
|
| if (query?.pages) {
| | if (query?.pages) { |
| // מצא את הדף שאינו חסר (היעד הסופי)
| | const page = Object.values(query.pages).find((page) => !page.missing); |
| const page = Object.values(query.pages).find((p) => !p.missing);
| | return page?.title ?? null; |
| return page?.title ?? null;
| | } |
| }
| | return null; |
| return null;
| | }; |
| } catch (error) {
| |
| console.error("שגיאה בקבלת יעד ההפניה עבור " + title + ":", error);
| |
| return null;
| |
| }
| |
| };
| |
|
| |
|
| /**
| | const extractAnchorFromContent = (content) => { |
| * מחלץ את העוגן (anchor) מתוך תוכן הפניה (לדוגמה: [[דף_יעד#עוגן]]).
| | const match = content.match(/\[\[.*?(?:#(.+?))?\]\]/); |
| * @param {string} content - תוכן דף ההפניה.
| | return match?.[1] ? `#${match[1]}` : ""; |
| * @returns {string} העוגן עם '#' אם קיים, אחרת מחרוזת ריקה.
| | }; |
| */
| |
| const extractAnchorFromContent = (content) => {
| |
| const match = content.match(/#הפניה\s*\[\[[^\]]+?(?:#(.+?))?\]\]/);
| |
| return match?.[1] ? "#" + match[1] : "";
| |
| };
| |
|
| |
|
| /**
| | const getNamespacePrefix = async (title) => { |
| * מקבל את קידומת מרחב השם של דף (לדוגמה: "מדיה ויקי:", "קובץ:").
| | const { query } = await api.get({ |
| * @param {string} title - שם הדף.
| | titles: title, |
| * @returns {Promise<string>} קידומת מרחב השם או מחרוזת ריקה.
| | prop: "info", |
| */
| | }); |
| const getNamespacePrefix = async (title) => {
| |
| try {
| |
| const { query } = await api.get({
| |
| titles: title,
| |
| prop: "info",
| |
| });
| |
|
| |
|
| if (query?.pages) {
| | if (query?.pages) { |
| const page = Object.values(query.pages)[0];
| | const page = Object.values(query.pages)[0]; |
| if (page.ns !== undefined) {
| | if (page.ns !== undefined) { |
| // יש להוסיף כאן מרחבי שמות נוספים במידת הצורך
| | const namespacePrefixes = { |
| const namespacePrefixes = {
| | 4: 'חב"דפדיה:', |
| 4: "חבדפדיה:", // Namespace 4
| | 6: "קובץ:", |
| 6: "קובץ:", // Namespace 6
| | 10: "תבנית:", |
| 10: "תבנית:", // Namespace 10
| | 12: "עזרה:", |
| 12: "עזרה:", // Namespace 12
| | 14: "קטגוריה:", |
| // ... ניתן להוסיף עוד מרחבי שמות לפי המספרים שלהם בוויקי
| | }; |
| };
| | return namespacePrefixes[page.ns] ?? ""; |
| return namespacePrefixes[page.ns] ?? "";
| | } |
| }
| | } |
| }
| | return ""; |
| return "";
| | }; |
| } catch (error) {
| |
| console.error("שגיאה בקבלת קידומת מרחב שם עבור " + title + ":", error);
| |
| return "";
| |
| }
| |
| };
| |
|
| |
|
| /**
| | const createRedirect = async (title, target) => { |
| * יוצר/מתקן הפניה.
| | try { |
| * @param {string} title - שם הדף שיהפוך להפניה.
| | await api.postWithEditToken({ |
| * @param {string} target - יעד ההפניה.
| | action: "edit", |
| */
| | format: "json", |
| const createRedirect = async (title, target) => {
| | bot: true, |
| try {
| | title: title, |
| await api.postWithEditToken({
| | text: `#הפניה [[${target}]]`, |
| action: "edit",
| | summary: "תיקון הפניה כפולה", |
| format: "json",
| | }); |
| // **השורות עם "tags" הוסרו לחלוטין כאן.**
| | mw.notify(`\nstatus:${title} success`); |
| title: title,
| | } catch (error) { |
| text: "#הפניה [[" + target + "]]",
| | console.error(error); |
| summary: "בוט: מתקן הפניה כפולה ל- [[" + target + "]]",
| | } |
| });
| | }; |
| mw.notify(title + ": ההפניה תוקנה בהצלחה ל- " + target, { type: 'success', title: 'הצלחה' });
| |
| } catch (error) {
| |
| console.error("שגיאה ביצירת הפניה עבור " + title + " ל- " + target + ":", error);
| |
| // בדיקה ספציפית לשגיאת badtags
| |
| if (error.code === 'badtags') {
| |
| mw.notify(title + ": שגיאה: אין הרשאה להשתמש בתגיות עריכה. אנא פנה למנהל המערכת.", { type: 'error', title: 'שגיאת הרשאה' });
| |
| } else {
| |
| mw.notify(title + ": נכשל תיקון ההפניה. פרטים בקונסול.", { type: 'error', title: 'שגיאה' });
| |
| }
| |
| }
| |
| };
| |
|
| |
|
| /**
| | const processRedirect = async (title) => { |
| * מעבד הפניה כפולה יחידה: מוצא את היעד הסופי ומתקן אותה.
| | try { |
| * @param {string} title - שם הדף של ההפניה הכפולה.
| | const contentData = await getRedirectContent(title); |
| */
| | const content = |
| const processRedirect = async (title) => {
| | contentData.query.pages[0].revisions[0].slots.main.content; |
| try {
| | const anchor = extractAnchorFromContent(content); |
| const content = await getRedirectContent(title);
| |
| if (content === null) {
| |
| mw.notify(title + ": לא ניתן לאחזר תוכן דף ההפניה.", { type: 'warn' });
| |
| return;
| |
| }
| |
|
| |
|
| const anchor = extractAnchorFromContent(content);
| | const finalTarget = await getRedirectTarget(title); |
| const finalTarget = await getRedirectTarget(title);
| | if (!finalTarget) return; |
|
| |
|
| if (!finalTarget) {
| | const namespace = await getNamespacePrefix(finalTarget); |
| mw.notify(title + ": לא נמצא יעד הפניה סופי.", { type: 'warn' });
| | const fullTarget = namespace + finalTarget + anchor; |
| return;
| |
| }
| |
|
| |
|
| const namespace = await getNamespacePrefix(finalTarget);
| | if (title === finalTarget) { |
| const fullTarget = namespace + finalTarget + anchor;
| | alert("הפניה מעגלית"); |
| | return; |
| | } |
|
| |
|
| // בדיקה למניעת הפניות מעגליות או תיקונים מיותרים
| | console.log("Creating redirect to:", fullTarget); |
| if (title.replace(/_/g, " ") === finalTarget.replace(/_/g, " ") && !anchor) {
| | await createRedirect(title, fullTarget); |
| mw.notify(title + ": ההפניה כבר תקינה. אין צורך בתיקון.", { type: 'info' });
| | } catch (error) { |
| return;
| | console.error("Error processing redirect:", error); |
| }
| | } |
| | }; |
|
| |
|
| if (title.replace(/_/g, " ") === fullTarget.replace(/_/g, " ")) {
| | const init = async () => { |
| mw.notify(title + ": הפניה מעגלית או תקינה. אין צורך בתיקון.", { type: 'info' });
| | const userGroups = mw.config.get("wgUserGroups"); |
| return;
| | const userId = mw.config.get("wgUserId"); |
| }
| | const pageName = mw.config.get("wgPageName"); |
|
| |
|
| console.log("יוצר הפניה חדשה: " + title + " --> " + fullTarget);
| | if (!userGroups.includes("bot") && !BOT_USERS.includes(userId)) return; |
| await createRedirect(title, fullTarget);
| | if (pageName !== "מיוחד:הפניות_כפולות") return; |
|
| |
|
| } catch (error) {
| | const num = prompt("? כמה הפניות כפולות להציג", 0); |
| console.error("שגיאה בעיבוד הפניה כפולה עבור " + title + ":", error);
| | if (!num) return; |
| mw.notify(title + ": אירעה שגיאה בעת עיבוד ההפניה. פרטים בקונסול.", { type: 'error', title: 'שגיאה' });
| |
| }
| |
| };
| |
|
| |
|
| /** | | try { |
| * הפונקציה הראשית שמפעילה את הסקריפט.
| | const { query } = await api.get({ |
| */
| | list: "querypage", |
| const init = async () => {
| | qppage: "DoubleRedirects", |
| const pageName = mw.config.get("wgPageName"); | | qplimit: num, |
| | }); |
|
| |
|
| // ודא שהסקריפט רץ רק בדף ההפניות הכפולות | | const redirects = query.querypage.results; |
| if (pageName !== "מיוחד:הפניות_כפולות") { | | for (const redirect of redirects) { |
| console.log("הסקריפט פועל רק בדף מיוחד:הפניות_כפולות.");
| | const title = redirect.title.replace(/_/g, " "); |
| return;
| | await processRedirect(title); |
| }
| | } |
| | } catch (error) { |
| | console.error("Error fetching redirects:", error); |
| | } |
| | }; |
|
| |
|
| const numInput = prompt("כמה הפניות כפולות להציג ולתקן? (מומלץ להתחיל עם מספר קטן, לדוגמה: 10)", "0");
| | $(init); |
| const num = parseInt(numInput, 10);
| |
| | |
| if (isNaN(num) || num <= 0) {
| |
| mw.notify("קלט לא חוקי. אנא הזן מספר שלם חיובי.", { type: 'error' });
| |
| return;
| |
| }
| |
| | |
| try {
| |
| mw.notify("מתחיל בדיקת " + num + " הפניות כפולות...", { type: 'info' });
| |
| | |
| // קבלת רשימת ההפניות הכפולות באמצעות API
| |
| const { query } = await api.get({
| |
| list: "querypage",
| |
| qppage: "DoubleRedirects",
| |
| qplimit: num,
| |
| });
| |
| | |
| const redirects = query.querypage.results;
| |
| if (redirects.length === 0) {
| |
| mw.notify("לא נמצאו הפניות כפולות לתיקון.", { type: 'info' });
| |
| return;
| |
| }
| |
| | |
| // לולאה על כל הפניה ותיקונה
| |
| for (const redirect of redirects) {
| |
| const title = redirect.title.replace(/_/g, " ");
| |
| await processRedirect(title);
| |
| }
| |
| mw.notify("סיום עיבוד ההפניות הכפולות.", { type: 'success', title: 'סיום' });
| |
| | |
| } catch (error) {
| |
| console.error("שגיאה באחזור הפניות כפולות:", error);
| |
| mw.notify("אירעה שגיאה באחזור רשימת ההפניות הכפולות.", { type: 'error', title: 'שגיאה כללית' });
| |
| }
| |
| };
| |
| | |
| // הפעל את הסקריפט כאשר הדף נטען
| |
| $(init);
| |
| })(); | | })(); |