Skip to content

Return of Reckoning

Wiki

RoR Wiki

MediaWiki:Common.js

From RoR Wiki
Revision as of 15:19, 1 August 2026 by MaxHayman (talk | contribs) (Created page with "→Any JavaScript here will be loaded for all users on every page load.: →Return of Reckoning Wiki contributor scorecard: (function () { if (mw.config.get('wgPageName') !== 'Wiki_Contributor_Scorecard') { return; } var liveTarget = document.getElementById('ror-contributor-scorecard-live'); var staticFallback = document.getElementById('ror-contributor-scorecard-static'); if (!liveTarget) { return; } var api = new mw.Ap...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
/* Any JavaScript here will be loaded for all users on every page load. */

/* Return of Reckoning Wiki contributor scorecard */
(function () {
    if (mw.config.get('wgPageName') !== 'Wiki_Contributor_Scorecard') {
        return;
    }

    var liveTarget = document.getElementById('ror-contributor-scorecard-live');
    var staticFallback = document.getElementById('ror-contributor-scorecard-static');

    if (!liveTarget) {
        return;
    }

    var api = new mw.Api();
    var contributors = [];

    liveTarget.innerHTML = '<p>Loading current contributor statistics…</p>';

    function loadBatch(continueFrom) {
        var parameters = {
            action: 'query',
            list: 'allusers',
            auwitheditsonly: 1,
            auprop: 'editcount|registration',
            aulimit: 'max',
            formatversion: 2
        };

        if (continueFrom) {
            parameters.aufrom = continueFrom;
        }

        return api.get(parameters).then(function (data) {
            contributors = contributors.concat(data.query.allusers);

            if (data.continue && data.continue.aufrom) {
                return loadBatch(data.continue.aufrom);
            }
        });
    }

    function renderScorecard() {
        contributors = contributors.filter(function (user) {
            return (user.editcount || 0) >= 10;
        });

        contributors.sort(function (a, b) {
            return (b.editcount || 0) - (a.editcount || 0) ||
                a.name.localeCompare(b.name);
        });

        var totalEdits = contributors.reduce(function (total, user) {
            return total + (user.editcount || 0);
        }, 0);

        var summary = $('<div>')
            .css({
                border: '1px solid #c9a227',
                padding: '10px',
                margin: '12px 0',
                background: '#fff8dc',
                color: '#222'
            })
            .append($('<b>').text('Live statistics'))
            .append('<br>')
            .append(document.createTextNode(
                'Qualifying contributors: ' + contributors.length
            ))
            .append('<br>')
            .append(document.createTextNode(
                'Recorded edits: ' + totalEdits.toLocaleString()
            ))
            .append('<br>')
            .append(document.createTextNode(
                'Loaded: ' + new Date().toLocaleString()
            ));

        var table = $('<table>')
            .addClass('wikitable sortable darktable')
            .css('width', '100%');

        table.append(
            $('<thead>').append(
                $('<tr>')
                    .append($('<th>').text('Rank'))
                    .append($('<th>').text('Contributor'))
                    .append($('<th>').text('Total edits'))
                    .append($('<th>').text('Registered'))
            )
        );

        var body = $('<tbody>');

        contributors.forEach(function (user, index) {
            var registered = user.registration ?
                user.registration.slice(0, 10) : 'Unknown';
            var contributionsUrl = mw.util.getUrl(
                'Special:Contributions/' + user.name
            );

            body.append(
                $('<tr>')
                    .append($('<td>').css('text-align', 'center').text(index + 1))
                    .append(
                        $('<td>').append(
                            $('<a>').attr('href', contributionsUrl).text(user.name)
                        )
                    )
                    .append(
                        $('<td>')
                            .css('text-align', 'right')
                            .text((user.editcount || 0).toLocaleString())
                    )
                    .append($('<td>').text(registered))
            );
        });

        table.append(body);
        $(liveTarget).empty().append(summary, table);

        if (staticFallback) {
            staticFallback.style.display = 'none';
        }
    }

    loadBatch()
        .then(renderScorecard)
        .catch(function () {
            liveTarget.innerHTML =
                '<p><strong>Live statistics could not be loaded.</strong> ' +
                'The saved snapshot remains available below.</p>';
        });
}());