לדלג לתוכן

מדיה ויקי:Gadget-QuickRCDiff.js

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

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

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
if ($.inArray(mw.config.get('wgCanonicalSpecialPageName'), ['Watchlist', 'Recentchanges']) != -1) (function () {
    var lastRequset, $diffDialog, $diffContent, patrolBtn, thanksBtn, diffId, diffTitle;

    function qRcDiffHandler(content) {
        mw.loader.load(['jquery.spinner', 'mediawiki.diff.styles']);
        $('.mw-changeslist-edit', content).not('qRcDiff').addClass('qRcDiff').click(function (e) {
            if (e.target !== this || !$(this).find('a:contains(היסטוריה)').length) {
                return;
            }

            if (!$diffContent) $diffContent = $('<div>');
            if (!$diffDialog) {
                var buttons = {
                    '!': function () {
                        if (!diffId) {
                            return;
                        }
                        var api = new mw.Api();
                        api.postWithToken('patrol', {
                            action: 'patrol',
                            revid: diffId
                        }).done(function (data) {
                            if (data && data.patrol) {
                                patrolBtn.hide();
                                mw.notify('העריכה סומנה כבדוקה');
                            } else {
                                var desc = (data && data.error && data.error.info) || '';
                                mw.notify('שגיאה:' + desc);
                            }
                        });
                    },
                    '♥': function () {
                        if (!diffId) {
                            return;
                        }
                        var api = new mw.Api();
                        api.postWithToken('csrf', {
                            action: 'thank',
                            source: 'diff',
                            rev: diffId
                        }).done(function (data) {
                            mw.notify('תודה נשלחה לעורך');
                        });
                    }
                };
                $diffDialog = $('<div>').append($diffContent).dialog({
                    width: '100%',
                    height: '300',
                    draggable: false,
                    resizable: false,
                    buttons: buttons
                });
                $diffDialog.dialog('widget').css({
                    position: 'fixed',
                    bottom: '0',
                    top: 'auto'
                });
                patrolBtn = $(":button:contains('!')").prop('title', 'סימון כבדוק'),
                thanksBtn = $(":button:contains('♥')").prop('title', 'תודה');
            }

            var self = this,
                diffHref = $(this).find('a[href*=oldid]')
                    .filter(function () {
                        return !$(this).hasClass('WLM-icon');
                    })
                    .prop('href'),
                unpatrolled = $(this).find('.unpatrolled').length;
            diffTitle = $(this).find('.mw-changeslist-title').text();
            patrolBtn.hide();
            if (self == lastRequset) {
                return;
            }
            $diffContent.html($.createSpinner());
            var param = {
                action: 'query',
                prop: 'revisions',
            };
            var isWikidata = false;
            if (diffHref) {
                diffId = diffHref.match(/&diff=(\d+)&oldid=/)[1];
                isWikidata = diffHref.match(/www\.wikidata\.org/);
                param.rvprop = 'timestamp';
                param.rvdiffto = 'prev';
                param.revids = diffId;
            } else {
                diffId = 0;
                param.rvprop = 'content';
                param.titles = diffTitle;
            }
            var api = isWikidata ? new mw.ForeignApi('https://www.wikidata.org/w/api.php') : new mw.Api();

            api.get(param).done(function (res) {
                if (unpatrolled) patrolBtn.show();

                $(self).addClass('changeslist-entry-active');
                if (lastRequset) {
                    $(lastRequset).addClass('changeslist-entry-visited');
                    $(lastRequset).removeClass('changeslist-entry-active');
                }
                lastRequset = self;
                if (res === null) {
                    $diffContent.html('Error');
                    return false;
                }
                var revision = res.query.pages[Object.keys(res.query.pages)[0]].revisions[0];
                var diffString = diffHref ? revision.diff["*"] : revision['*'];
                if (diffString === null) {
                    $diffContent.html('Error');
                    return false;
                }

                var regexProblematicContent = /{{תמונה חילופית\||{{סינון\//;
                if (regexProblematicContent.test(diffString)) {
                    $diffContent.html('תצוגת ההבדלים כוללת תוכן בעייתי!');
                    return;
                }

                var trTemp = $('<tr><td class="diff-marker">&nbsp;</td><td class="diff-context"></td></tr>');
                var newTable = $('<table class="diff"></table>')
                    .html('<colgroup><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content"></colgroup>');
                if (!diffHref) {
                    newTable.append($('<tr><td colspan="4" class="diff-lineno">דף חדש</td></tr>'));
                    var rows = diffString.split('\n');
                    for (var tr in rows) {
                        var curTr = trTemp.clone();
                        curTr.find('.diff-context').first().text(rows[tr]);
                        newTable.append(curTr);
                    }
                } else {
                    newTable.append(diffString);
                }
                $diffDialog.dialog('option', 'title', diffTitle);
                $diffContent.html(newTable);
                $diffDialog.dialog('isOpen') || $diffDialog.dialog('open').dialog('widget').css({ 'height': '300px', 'top': 'auto' });
            });
        });
    }

    mw.hook('wikipage.content').add(qRcDiffHandler);
})();