User:DR/test.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
DR (talk | contribs)
No edit summary
Tag: Reverted
DR (talk | contribs)
restore
Tags: Manual revert Reverted
Line 4: Line 4:
$('#firstHeading').text('MassDelete');
$('#firstHeading').text('MassDelete');


var categoryInputField = new OO.ui.TextInputWidget({
var pagesTextarea = new OO.ui.MultilineTextInputWidget({
placeholder: 'Category name (optional)',
id: 'categoryInput',
style: 'margin: 10px 0; padding: 5px;'
}),
fetchCategoryButton = new OO.ui.ButtonWidget({
label: 'Fetch Pages from Category',
flags: ['progressive'],
style: 'margin: 10px 0; padding: 5px;'
}),
pagesTextarea = new OO.ui.MultilineTextInputWidget({
placeholder: 'Enter list of pages (one per line)',
placeholder: 'Enter list of pages (one per line)',
autosize: true,
autosize: true,
rows: 10,
rows: 10
style: 'margin-top: 10px;'
}),
}),
reasonInputField = new OO.ui.TextInputWidget({
reasonInputField = new OO.ui.TextInputWidget({
placeholder: 'Reason for deletion',
placeholder: 'Reason for deletion'
style: 'margin-top: 10px;'
}),
}),
deleteTalkPagesCheckbox = new OO.ui.CheckboxInputWidget({
deleteTalkPagesCheckbox = new OO.ui.CheckboxInputWidget({
Line 29: Line 17:
previewButton = new OO.ui.ButtonWidget({
previewButton = new OO.ui.ButtonWidget({
label: 'Preview Deletion',
label: 'Preview Deletion',
flags: ['primary'],
flags: ['primary']
style: 'margin-top: 10px;'
}),
}),
startButton = new OO.ui.ButtonWidget({
startButton = new OO.ui.ButtonWidget({
Line 36: Line 23:
icon: 'alert',
icon: 'alert',
flags: ['primary', 'progressive'],
flags: ['primary', 'progressive'],
disabled: true,
disabled: true
style: 'margin-top: 10px;'
}),
}),
cancelButton = new OO.ui.ButtonWidget({
cancelButton = new OO.ui.ButtonWidget({
label: 'Cancel',
label: 'Cancel',
flags: ['primary', 'destructive'],
flags: ['primary', 'destructive'],
href: 'https:' + mw.config.get('wgServer'),
href: 'https:' + mw.config.get('wgServer')
style: 'margin-top: 10px;'
}),
}),
logContainer = $("<div>").hide();
logContainer = $("<div>").hide();


var labels = {
var labels = {
categoryLabel: $('<p>').text('Category name (optional)').css('font-weight', 'bold'),
pagesLabel: $('<p>').text('Pages to Delete:').css('font-weight', 'bold'),
pagesLabel: $('<p>').text('Pages to Delete:').css('font-weight', 'bold'),
reasonLabel: $('<p>').text('Reason:').css('font-weight', 'bold'),
reasonLabel: $('<p>').text('Reason:').css('font-weight', 'bold'),
Line 55: Line 39:


$('#mw-content-text').append(
$('#mw-content-text').append(
labels.categoryLabel, categoryInputField.$element, fetchCategoryButton.$element,
labels.pagesLabel, pagesTextarea.$element,
labels.pagesLabel, pagesTextarea.$element,
labels.reasonLabel, reasonInputField.$element,
labels.reasonLabel, reasonInputField.$element,
Line 172: Line 155:


processNextPage();
processNextPage();
}

function fetchCategoryMembers() {
var category = categoryInputField.getValue().trim();

if (category === "") {
showAlert("Please enter a category name.");
return;
}

var params = {
action: 'query',
list: 'categorymembers',
cmtitle: 'Category:' + category,
cmlimit: 'max',
format: 'json'
};

$.ajax({
url: mw.util.wikiScript('api'),
data: params,
dataType: 'json',
success: function(data) {
if (data.query && data.query.categorymembers.length > 0) {
var pages = data.query.categorymembers.map(function(member) {
return member.title;
}).join("\n");
var existingPages = pagesTextarea.getValue().trim();
if (existingPages) {
pagesTextarea.setValue(existingPages + "\n" + pages);
} else {
pagesTextarea.setValue(pages);
}
showAlert("Fetched " + data.query.categorymembers.length + " pages from category: " + category);
} else {
showAlert("No pages found in the category: " + category);
}
},
error: function(xhr, status, error) {
showAlert("Error fetching category members: " + error);
}
});
}
}


Line 226: Line 167:
});
});


fetchCategoryButton.on('click', fetchCategoryMembers);
previewButton.on('click', previewDeleting);
previewButton.on('click', previewDeleting);
startButton.on('click', startDeleting);
startButton.on('click', startDeleting);

Revision as of 18:22, 30 March 2025

$(document).ready(function() {
    function initializeMassDelete() {
        $('#mw-content-text > p').remove();
        $('#firstHeading').text('MassDelete');

        var pagesTextarea = new OO.ui.MultilineTextInputWidget({
                placeholder: 'Enter list of pages (one per line)',
                autosize: true,
                rows: 10
            }),
            reasonInputField = new OO.ui.TextInputWidget({
                placeholder: 'Reason for deletion'
            }),
            deleteTalkPagesCheckbox = new OO.ui.CheckboxInputWidget({
                selected: false
            }),
            previewButton = new OO.ui.ButtonWidget({
                label: 'Preview Deletion',
                flags: ['primary']
            }),
            startButton = new OO.ui.ButtonWidget({
                label: 'Start Deletion',
                icon: 'alert',
                flags: ['primary', 'progressive'],
                disabled: true
            }),
            cancelButton = new OO.ui.ButtonWidget({
                label: 'Cancel',
                flags: ['primary', 'destructive'],
                href: 'https:' + mw.config.get('wgServer')
            }),
            logContainer = $("<div>").hide();

        var labels = {
            pagesLabel: $('<p>').text('Pages to Delete:').css('font-weight', 'bold'),
            reasonLabel: $('<p>').text('Reason:').css('font-weight', 'bold'),
            deleteTalkPagesLabel: $('<p>').text('Delete associated talk pages').css('font-weight', 'bold')
        };

        $('#mw-content-text').append(
            labels.pagesLabel, pagesTextarea.$element,
            labels.reasonLabel, reasonInputField.$element,
            labels.deleteTalkPagesLabel, deleteTalkPagesCheckbox.$element,
            '<br/>',
            previewButton.$element,
            startButton.$element,
            cancelButton.$element,
            '<br/>',
            logContainer
        );

        function deletePage(page, reason, callback) {
            (new mw.Api({
                ajax: {
                    headers: {
                        'Api-User-Agent': 'en:User:DreamRimmer/MassDelete.js'
                    }
                }
            })).postWithToken('csrf', {
                action: 'delete',
                title: page,
                reason: reason
            }, {
                async: false
            }).done(function(data) {
                callback(null, data, page);
            }).fail(function(code, data) {
                callback(code, data, page);
            });
        }

        function showAlert(message) {
            alert("Error: " + message);
        }

        function handleDeleteResponse(err, data, page) {
            var logList = $("<ul>").appendTo(logContainer);

            if (err) {
                logList.append("<li>Failed to delete page <b>" + page + "</b>: " + err + "</li>");
            } else {
                logList.append("<li><b>" + page + "</b> deleted successfully.</li>");
            }
        }

        function previewDeleting() {
            var pages = pagesTextarea.getValue().replace(/^\s*[\r\n]/gm, '').split("\n"),
                reason = reasonInputField.getValue().trim(),
                deleteTalkPages = deleteTalkPagesCheckbox.isSelected();

            if (pages[0].trim() === "" || reason === "") {
                showAlert("Please fill in all fields.");
                return;
            }

            logContainer.empty();
            $("<h1>").wrapInner("<span class='mw-headline'>Deletion preview</span>").appendTo(logContainer);
            logContainer.show();

            var logList = $("<ul>").appendTo(logContainer);

            pages.forEach(function(page) {
                page = page.trim();
                logList.append("<li><b>" + page + "</b> will be deleted for reason: <b>" + reason + "</b></li>");
                if (deleteTalkPages) {
                    logList.append("<li>Associated talk page <b>Talk:" + page + "</b> will also be deleted.</li>");
                }
            });

            startButton.setDisabled(false);
        }

        function startDeleting() {
            if (!confirm("Are you sure you want to delete the listed pages? This action cannot be undone.")) {
                return;
            }

            var pages = pagesTextarea.getValue().replace(/^\s*[\r\n]/gm, '').split("\n"),
                reason = reasonInputField.getValue().trim() + " (using [[User:DreamRimmer/MassDelete|MassDelete.js]])",
                deleteTalkPages = deleteTalkPagesCheckbox.isSelected();

            if (pages[0].trim() === "" || reason === "") {
                showAlert("Please fill in all fields.");
                return;
            }

            logContainer.empty();
            $("<h1>").wrapInner("<span class='mw-headline'>Deletion log</span>").appendTo(logContainer);
            logContainer.show();

            var currentIndex = 0;

            function processNextPage() {
                if (currentIndex >= pages.length) {
                    return;
                }

                var page = pages[currentIndex].trim();

                deletePage(page, reason, function(err, data) {
                    handleDeleteResponse(err, data, page);
                    if (deleteTalkPages) {
                        var talkPage = "Talk:" + page;
                        deletePage(talkPage, reason, function(err, data) {
                            handleDeleteResponse(err, data, talkPage);
                            currentIndex++;
                            setTimeout(processNextPage, 2000);
                        });
                    } else {
                        currentIndex++;
                        setTimeout(processNextPage, 2000);
                    }
                });
            }

            processNextPage();
        }

        pagesTextarea.on('change', function() {
            previewButton.setDisabled(pagesTextarea.getValue().trim() === '' || reasonInputField.getValue().trim() === '');
            startButton.setDisabled(true);
        });

        reasonInputField.on('change', function() {
            previewButton.setDisabled(pagesTextarea.getValue().trim() === '' || reasonInputField.getValue().trim() === '');
            startButton.setDisabled(true);
        });

        previewButton.on('click', previewDeleting);
        startButton.on('click', startDeleting);
    }

    $.when(mw.loader.using('mediawiki.util'), $.ready).then(function() {
        mw.util.addPortletLink(
            'p-tb',
            mw.util.getUrl('Special:BlankPage/MassDelete'),
            'MassDelete'
        );
    });

    if (mw.config.get('wgCanonicalSpecialPageName') === 'Blankpage' && mw.config.get('wgTitle').split('/', 2)[1] === 'MassDelete') {
        $.when(mw.loader.using('oojs-ui-core'), $.ready).then(function() {
            initializeMassDelete();
        });
    }
});