לדלג לתוכן

משתמש:גאון הירדן/נעילה לקריאה.js

מתוך המכלול, האנציקלופדיה היהודית

לתשומת ליבך: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.notification']).then(function() {
    const api = new mw.Api();
    const userName = mw.config.get('wgUserName');
    if (!userName) return; 

    const storagePage = 'User:' + userName + '/ערכים_חסומים.json';
    const currentPage = mw.config.get('wgPageName');

    function hideCurrentContent() {
        $('#firstHeading, #bodyContent, #p-cactions, #p-views').hide();
    }

    // 1. קריאה ובדיקה בטעינת הדף
    function checkAndHide() {
        api.get({
            action: 'query',
            prop: 'revisions',
            titles: storagePage,
            rvprop: 'content',
            formatversion: 2
        }).done(function(data) {
            let page = data.query.pages[0];
            if (page && !page.missing) {
                try {
                    let hiddenPages = JSON.parse(page.revisions[0].content);
                    if (Array.isArray(hiddenPages) && hiddenPages.includes(currentPage)) {
                        hideCurrentContent();
                    }
                } catch (e) { 
                    mw.notification.notify('שגיאה: מבנה דף החסימות פגום. לא ניתן לבצע הסתרה.', { type: 'error' });
                }
            }
        });
    }

    // 2. הוספה לרשימה עם עצירה במקרה של שגיאה
    function addPagesToList(pagesToAdd) {
        const notifyLoading = mw.notification.notify('בודק תקינות דף חסימות...', { tag: 'block-act', autoHide: false });

        return api.get({
            action: 'query',
            prop: 'revisions',
            titles: storagePage,
            rvprop: 'content',
            formatversion: 2
        }).then(function(data) {
            let content = "[]";
            let page = data.query.pages[0];
            let list;

            if (page && !page.missing) {
                content = page.revisions[0].content;
                try { 
                    list = JSON.parse(content); 
                    if (!Array.isArray(list)) throw new Error("Not an array");
                } catch(e) { 
                    // במקרה של שגיאת פענוח - עוצרים הכל ולא דורסים
                    notifyLoading.close();
                    mw.notification.notify('שגיאה קריטית: ה-JSON בדף החסימות פגום. השמירה בוטלה כדי למנוע אובדן נתונים.', { type: 'error', autoHide: false });
                    return $.Deferred().reject("Invalid JSON");
                }
            } else {
                list = []; // דף חדש לגמרי
            }

            let initialCount = list.length;
            pagesToAdd.forEach(p => {
                let cleanName = p.replace(/ /g, '_');
                if (!list.includes(cleanName)) list.push(cleanName);
            });

            if (list.length === initialCount) {
                mw.notification.notify('הדפים כבר קיימים ברשימה.', { type: 'warn', tag: 'block-act' });
                return;
            }

            return api.postWithToken('csrf', {
                action: 'edit',
                title: storagePage,
                text: JSON.stringify(list, null, 2),
                summary: 'עדכון רשימת חסומים: נוספו ' + (list.length - initialCount) + ' דפים',
                contentmodel: 'json', 
                formatversion: 2
            }).done(function(res) {
                if (res.edit && res.edit.result === "Success") {
                    mw.notification.notify('נשמר בהצלחה!', { type: 'success', tag: 'block-act' });
                    if (pagesToAdd.map(p => p.replace(/ /g, '_')).includes(currentPage)) {
                        hideCurrentContent();
                    }
                }
            });
        }).fail(function(err) {
            if (err !== "Invalid JSON") {
                mw.notification.notify('שגיאה בתקשורת עם השרת.', { type: 'error', tag: 'block-act' });
            }
        });
    }

    $(document).ready(function() {
        checkAndHide();
        const addBtn = mw.util.addPortletLink('p-tb', '#', 'חסום דף זה', 't-hide-page');
        $(addBtn).click(function(e) {
            e.preventDefault();
            addPagesToList([currentPage]);
        });

        if (mw.config.get('wgNamespaceNumber') === 14) {
            const catBtn = mw.util.addPortletLink('p-tb', '#', 'חסום את כל הקטגוריה', 't-hide-cat');
            $(catBtn).click(function(e) {
                e.preventDefault();
                let titles = [];
                $('.mw-category a').each(function() {
                    let t = $(this).attr('title');
                    if (t) titles.push(t);
                });
                if (titles.length > 0) addPagesToList(titles);
            });
        }
    });
});