مدیاویکی:Gadget-Contributions-report.js

از ویکی‌گفتاورد

نکته: پس از انتشار ممکن است برای دیدن تغییرات نیاز باشد که حافظهٔ نهانی مرورگر خود را پاک کنید.

  • فایرفاکس / سافاری: کلید Shift را نگه دارید و روی دکمهٔ Reload کلیک کنید، یا کلید‌های Ctrl-F5 یا Ctrl-R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-R)
  • گوگل کروم: کلیدهای Ctrl+Shift+R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-Shift-R)
  • اینترنت اکسپلورر/ Edge: کلید Ctrl را نگه‌دارید و روی دکمهٔ Refresh کلیک کنید، یا کلید‌های Ctrl-F5 را با هم فشار دهید
  • اپرا: Ctrl-F5 را بفشارید.
/**
 * Description: Quick prototype for a contribution report
 * Maintainer: Jeblad
 */
(function(undefined) {
    // bail out the window if we're in the wrong room
    if (mw.config.get('wgNamespaceNumber') != 2) return;
    if (/\//.test(mw.config.get('wgTitle'))) return;
 
    // counter for the initial data requests
    var pending = 2;
    // nice to have this one, but note that we get it twice
    var totaledits;
    var days = [ 28, 91 ];
    var edits = [ 0, 0 ];
    // this is the build after we have collected our data
    function build() {
        // the params for the parse request
        var parse = {
            'format': 'json',
            'action': 'parse',
            'text': '{{Mediawiki:Contributions-report'
            + '|short edit=' + edits[0]
            + '|short norm=' + (edits[0]/days[0])
            + '|short days=' + days[0]
            + '|long edit=' + (edits[1]-edits[0])
            + '|long norm=' + ((edits[1]-edits[0])/(days[1]-days[0]))
            + '|long days=' + (days[1]-days[0])
            + '|factor=' + 4
            + '|total=' + totaledits
            + '|user=' + mw.config.get('wgTitle')
            + '}}',
            'prop': 'text',
            'disablepp' : 1,
            'maxage': 5*60,
            'smaxage': 15*60
        };
        $.getJSON(
            mw.util.wikiScript( 'api' ),
            parse,
            function ( data, txt ) {
                mw.notify((data && data.parse) ? data.parse.text['*'] : txt);
            }
        );
    };
    // initial collect of two data sets
    function collect() {
        // the params for the contribs request
        var contribs = {
            'action' : 'userdailycontribs',
            'user' : mw.config.get('wgTitle'),
            'format' : 'json',
            'maxage': 5*60,
            'smaxage': 15*60
        }
        for (var i = 0, l = days.length; i<l; i++) {
            $.getJSON(
                mw.util.wikiScript( 'api' ),
                jQuery.extend({ 'daysago' : days[i], 'requestid' : i }, contribs),
                function ( data, txt ) {
                    // the number of edits within this timeframe
                    edits[parseInt(data.requestid)] = parseInt(data.userdailycontribs.timeFrameEdits);
                    // total edits, note that it will be set twice
                    totaledits = parseInt(data.userdailycontribs.totalEdits);
                    // when we got our two datsets then we build
                    if (--pending == 0) build();
                }
            );
        }
        // get rid of the pesky hash
        return false;
    }
    // our worker to set up the menu item
    $( function() {
        // usual portlet link
        mw.util.addPortletLink(
            "p-cactions",
            '#',
            "تعداد مشارکت‌های اخیر این کاربر",
            "t-contrib-trend",
            "نمایش آمار تعداد مشارکت‌های اخیر این کاربر",
            null,
            null
        );
        // and set a click handler
        $('#t-contrib-trend').click(collect);
    });
})();