# MediaWiki:Gadget-Contributions-report.js

From faraghaib
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press 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.util.jsMessage((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);
        });
    })();

Retrieved from "https://en.faraghaib.ir/w/index.php?title=MediaWiki:Gadget-Contributions-report.js&oldid=100"