MediaWiki:Gadget-MassRollback.js

From Test Wiki
Revision as of 18:57, 6 October 2024 by Justarandomamerican (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//Authored by [[User:BZPN]]
$(document).ready(function () {
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'Contributions') {
        return;
    }

    // Dodaj nowy rozwijany box
    if ($('#rollback-box').length === 0) {
        var $rollbackBox = $(`
            <div id="rollback-box" class="mw-htmlform-ooui-wrapper oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-padded oo-ui-panelLayout-framed">
                <form id="rollback-form" class="mw-htmlform mw-htmlform-ooui oo-ui-layout oo-ui-formLayout">
                    <fieldset class="oo-ui-layout oo-ui-labelElement oo-ui-fieldsetLayout mw-collapsibleFieldsetLayout mw-collapsible mw-made-collapsible">
                        <legend role="button" class="oo-ui-fieldsetLayout-header mw-collapsible-toggle mw-collapsible-toggle-expanded" aria-expanded="true" tabindex="0">
                            <span class="oo-ui-iconElement-icon oo-ui-iconElement-noIcon"></span>
                            <span class="oo-ui-labelElement-label">Contributions rollback</span>
                            <span class="oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-expand oo-ui-iconElement oo-ui-labelElement-invisible oo-ui-iconWidget">Expand</span>
                            <span class="oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-collapse oo-ui-iconElement oo-ui-labelElement-invisible oo-ui-iconWidget">Collapse</span>
                        </legend>
                        <div class="oo-ui-fieldsetLayout-group mw-collapsible-content" style="display: block;">
                            <div class="oo-ui-widget oo-ui-widget-enabled">
                                <label for="rollback-reason" class="oo-ui-labelElement-label">Reason for reverting edits (optional):</label>
                                <select id="rollback-reason" class="oo-ui-inputWidget-input oo-ui-dropdownWidget">
                                    <option value="">-- Select a reason --</option>
                                    <option value="Test">Test</option>
                                    <option value="Vandalism">Vandalism</option>
                                    <option value="Spam">Spam</option>
                                    <option value="Revert">Revert</option>
                                </select>
                                <input type="text" id="rollback-summary" placeholder="Reason (optional)" class="oo-ui-inputWidget-input oo-ui-textInputWidget-input" style="margin-top: 10px; padding: 5px; width: 100%;"/>
                            </div>
                            <div class="oo-ui-widget oo-ui-widget-enabled mw-htmlform-submit-buttons">
                                <button id="rollback-selected-button" class="oo-ui-inputWidget-input oo-ui-buttonElement-button oo-ui-buttonElement-framed oo-ui-flaggedElement-progressive" style="padding: 5px 10px; background-color: #007bff; color: #fff; border: none; cursor: pointer; border-radius: 4px;">Revert selected edits</button>
                                <button id="rollback-all-button" class="oo-ui-inputWidget-input oo-ui-buttonElement-button oo-ui-buttonElement-framed oo-ui-flaggedElement-destructive" style="padding: 5px 10px; background-color: #ff4136; color: #fff; border: none; cursor: pointer; border-radius: 4px;">Revert all edits</button>
                                <button id="cancel-rollback" class="oo-ui-inputWidget-input oo-ui-buttonElement-button oo-ui-buttonElement-framed" style="padding: 5px 10px; background-color: #aaa; color: #fff; border: none; cursor: pointer; border-radius: 4px;">Cancel</button>
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
        `);

        $('#mw-content-text').prepend($rollbackBox);

        // Synchronize dropdown with input field
        $('#rollback-reason').change(function () {
            var selectedReason = $(this).val();
            var currentSummary = $('#rollback-summary').val();
            if (!currentSummary) {
                $('#rollback-summary').val(selectedReason);
            }
        });

        // 1
        $('#rollback-box .mw-collapsible-toggle').click(function () {
            var $content = $(this).closest('fieldset').find('.mw-collapsible-content');
            var isExpanded = $(this).attr('aria-expanded') === 'true';

            if (isExpanded) {
                $content.slideUp();
                $(this).attr('aria-expanded', 'false');
                $(this).find('.oo-ui-icon-collapse').hide();
                $(this).find('.oo-ui-icon-expand').show();
            } else {
                $content.slideDown();
                $(this).attr('aria-expanded', 'true');
                $(this).find('.oo-ui-icon-expand').hide();
                $(this).find('.oo-ui-icon-collapse').show();
            }
        });

        // Rollback selected
        $('#rollback-selected-button').click(function () {
            var userName = mw.config.get('wgRelevantUserName');
            var summary = $('#rollback-summary').val();
            var api = new mw.Api();

            var selectedRevisions = $('input[type="checkbox"].rollback-checkbox:checked').map(function () {
                return {
                    title: $(this).data('title'),
                    revid: $(this).data('revid'),
                    parentid: $(this).data('parentid')
                };
            }).get();

            if (selectedRevisions.length === 0) {
                alert('Nie zaznaczono żadnych edycji do cofnięcia.');
                return;
            }

            var rollbackPromises = selectedRevisions.map(function (contrib) {
                var formattedSummary = `Reverted edit by [[User:${userName}|${userName}]] using [[Gadget:MassRollback|gadget]]` + (summary ? `: ${summary}` : '');

                return api.postWithToken('csrf', {
                    action: 'edit',
                    undoafter: contrib.parentid,
                    undo: contrib.revid,
                    title: contrib.title,
                    summary: formattedSummary
                });
            });

            Promise.all(rollbackPromises).then(function () {
                alert('Zaznaczone edycje zostały wycofane!');
                location.reload();
            }).catch(function (error) {
                console.error('An error occured when reverting changes:', error);
                alert('Wystąpił błąd podczas wycofywania edycji.');
            });
        });

        // Rollback all
        $('#rollback-all-button').click(function () {
            var userName = mw.config.get('wgRelevantUserName');
            var summary = $('#rollback-summary').val();
            var api = new mw.Api();

            api.get({
                action: 'query',
                list: 'usercontribs',
                ucuser: userName,
                uclimit: 'max',
                ucprop: 'ids|title',
                format: 'json'
            }).done(function (data) {
                var contributions = data.query.usercontribs;

                if (contributions.length === 0) {
                    alert('No edits to revert.');
                    return;
                }

                var rollbackPromises = contributions.map(function (contrib) {
                    var formattedSummary = `Reverted edit by [[User:${userName}|${userName}]] using [[Gadget:MassRollback|gadget]]` + (summary ? `: ${summary}` : '');

                    return api.postWithToken('csrf', {
                        action: 'edit',
                        undoafter: contrib.parentid,
                        undo: contrib.revid,
                        title: contrib.title,
                        summary: formattedSummary
                    });
                });

                Promise.all(rollbackPromises).then(function () {
                    alert('All edits were reverted!');
                    location.reload();
                }).catch(function (error) {
                    console.error('An error occured when reverting:', error);
                    alert('An error occured while reverting.');
                });
            }).fail(function (error) {
                console.error('Error with downloading contributions:', error);
                alert('Error with downloading contributions.');
            });
        });

        $('#cancel-rollback').click(function () {
            $('#rollback-box').hide();
        });

        // Dodaj checkboxy przy każdej edycji
        $('li[data-mw-revid]').each(function () {
            var $this = $(this);
            var revid = $this.data('mw-revid');
            var parentid = $this.data('mw-prev-revid');
            var title = $this.find('.mw-contributions-title').text();

            var $checkbox = $('<input type="checkbox" class="rollback-checkbox" style="margin-right: 5px;">')
                .data('revid', revid)
                .data('parentid', parentid)
                .data('title', title);

            $this.prepend($checkbox);
        });
    }
});