User:Rhododendrites/drsort.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// Check if we are on a deletion request page
if (mw.config.get('wgPageName').indexOf('Commons:Deletion_requests/') === 0) {
    $(document).ready(function() {
        // Create a style element to hold the CSS
        var style = $('<style>\
            #customDropdowns, #customDropdowns label, #customDropdowns select, #customDropdowns button { \
                font-size: 10px; \
            } \
        </style>');

        // Append the style to the head
        $('head').append(style);

        // Create the dropdowns and button, then append to the page
        var categoryDropdown = $('<select id="categoryDropdown"><option value="">Subject-based categories</option></select>');
        var countryDropdown = $('<select id="countryDropdown"><option value="">Country-based categories</option></select>');
        var sourceDropdown = $('<select id="sourceDropdown"><option value="">Source-based categories</option></select>');
        var propertiesDropdown = $('<select id="propertiesDropdown"><option value="">Properties-based categories</option></select>');
        var statusDropdown = $('<select id="statusDropdown"><option value="">Select Status</option></select>');
        var applyButton = $('<button id="applyButton">Apply</button>');

        // Add options to subject-based category dropdown (you can modify this list)
        var categories = [
            'AI-generation related deletion requests',
            'Album cover related deletion requests',
            'Book cover related deletion requests',
            'CCTV related deletion discussions',
            'Costume deletion requests',
            'Currency related deletion requests',
            'Fictitious symbol related deletion requests',
            'Film poster related deletion requests',
            'Graffiti deletion requests',
            'Hoax-related deletion requests',
            'Internet meme-related deletion requests',
            'INUSE files deletion requests',
            'LGBT related deletion requests',
            'Mathematics related deletion requests',
            'Deletion requests for works created by minors',
            'Mobile phone-related deletion requests',
            'Nudity and sexuality-related deletion requests',
            'Numismatics related deletion requests',
            'Philately related deletion requests',
            'Podcast related deletion requests',
            'Polandball-related deletion requests',
            'Poster related deletion requests',
            'Privacy-related deletion requests',
            'Quality-related deletion requests',
            'Racial theories deletion requests',
            'Scanned works related deletion discussions',
            'Scientific racism deletion requests',
            'Scope-related deletion requests',
            'Screenshot-related deletion requests',
            'Seizure risk-related deletion requests',
            'Signature-related deletion requests',
            'Stained glass related deletion requests',
            'Tablet computer-related deletion requests',
            'Taxidermy related deletion requests',
            'Technical and scientific drawings deletion requests',
            'Trophy-related deletion requests',
            'Usage restriction deletion requests',
            'Video game trailer related deletion requests',
            'Wax figure deletion requests',
            'YouTube video related deletion requests'
            // Add more categories here
        ];
        categories.forEach(function(category) {
            categoryDropdown.append($('<option></option>').val(category).text(category));
        });

        // Add options to country-based category dropdown (you can modify this list)
        var countries = [
            'NOT AVAILABLE YET'
            // Add more categories here
        ];
        countries.forEach(function(country) {
            countryDropdown.append($('<option></option>').val(country).text(country));
        });

        // Add options to source-based category dropdown (you can modify this list)
        var sources = [
            '20minutos.es related deletion requests',
            '500px.com related deletion requests',
            'Agência Brasil related deletion requests',
            'Bollywoodhungama.com related deletion requests',
            'Brasil2016.gov.br related deletion requests',
            'Copa2014.gov.br related deletion requests',
            'Flickr "Commons" related deletion requests',
            'IA mirror related deletion requests',
            'Mynewsdesk-related deletion requests',
            'NASA-related deletion requests',
            'National Police of Colombia related deletion requests',
            'Naval Postgraduate School deletion requests',
            'Pexels related deletion requests',
            'Philippine News Agency-related deletion requests',
            'Pixabay.com related deletion requests',
            'Tasnim News Agency related deletion requests',
            'Tistory.com related deletion requests',
            'Touhou Project related deletion requests',
            'TV Brasil related deletion requests',
            'Unsplash related deletion requests',
            'VOA Persian related deletion requests',
            'WikiMapia related deletion requests',
            'World66.com related deletion requests'
            // Add more categories here
        ];
        sources.forEach(function(source) {
            sourceDropdown.append($('<option></option>').val(source).text(source));
        });

        // Add options to properties-based category dropdown (you can modify this list)
        var properties = [
            'Disney related deletion requests',
            'Genshin Impact related deletion requests',
            'Marvel Comics characters deletion requests',
            'Minecraft related deletion requests',
            'Neon Genesis Evangelion related deletion requests',
            'Nickelodeon related deletion requests',
            'Nintendo related deletion requests',
            'Pokémon related deletion requests',
            'Roblox related deletion requests',
            'Warner Bros. characters deletion requests'
            // Add more categories here
        ];
        properties.forEach(function(property) {
            propertiesDropdown.append($('<option></option>').val(property).text(property));
        });

        // Add options to status dropdown
        var statuses = ['kept', 'deleted', 'pending', 'undeleted'];
        statuses.forEach(function(status) {
            statusDropdown.append($('<option></option>').val(status).text(status));
        });

        // Append dropdowns and button to the page
        $('#content').prepend('<div id="customDropdowns" style="margin-bottom: 10px;"></div>');
        $('#customDropdowns').append('<label for="categoryDropdown">Subject: </label>');
        $('#customDropdowns').append(categoryDropdown);
        $('#customDropdowns').append('<label for="countryDropdown" style="margin-left: 10px;">Country: </label>');
        $('#customDropdowns').append(countryDropdown);
        $('#customDropdowns').append('<label for="sourceDropdown" style="margin-left: 10px;">Source: </label>');
        $('#customDropdowns').append(sourceDropdown);
        $('#customDropdowns').append('<label for="propertiesDropdown" style="margin-left: 10px;">Property: </label>');
        $('#customDropdowns').append(propertiesDropdown);
        $('#customDropdowns').append('<label for="statusDropdown" style="margin-left: 10px;">Status: </label>');
        $('#customDropdowns').append(statusDropdown);
        $('#customDropdowns').append('<span style="margin-left: 10px;"></span>');
        $('#customDropdowns').append(applyButton);

        // Define the category prefix and suffix variables
        var categoryPrefix = '[[Category:';
        var categorySuffix = ']]';

        // Function to update or append category in edit mode
        function updateCategoryInEditMode(selectedCategory, selectedStatus) {
            var categoryText = categoryPrefix + selectedCategory + '/' + selectedStatus + categorySuffix;
            var textareaContent = $('#wpTextbox1').val();
            var categoryRegex = new RegExp('\\[\\[Category:' + selectedCategory + '/(kept|deleted|pending|undeleted)\\]\\]', 'i');
            if (categoryRegex.test(textareaContent)) {
                // If category already exists, replace it with the new status
                $('#wpTextbox1').val(textareaContent.replace(categoryRegex, categoryText));
            } else {
                // If category does not exist, append it to the content
                $('#wpTextbox1').val(textareaContent + '\n' + categoryText);
            }
        }

        // Function to change status of all existing categories
        function changeAllStatuses(selectedStatus) {
            var textareaContent = $('#wpTextbox1').val();
            var categoryRegex = /\[\[Category:([^\]]+\/)(kept|deleted|pending|undeleted)\]\]/gi;
            textareaContent = textareaContent.replace(categoryRegex, categoryPrefix + '$1' + selectedStatus + categorySuffix);
            $('#wpTextbox1').val(textareaContent);
        }

        // Function to handle apply button click
        function handleApplyClick() {
            var selectedCategory = $('#categoryDropdown').val();
            var selectedCountry = $('#countryDropdown').val();
            var selectedSource = $('#sourceDropdown').val();
            var selectedProperty = $('#propertiesDropdown').val();
            var selectedStatus = $('#statusDropdown').val();

            if (!selectedStatus) {
                alert('Please select a status.');
                return;
            }

            if ((selectedCategory && selectedCountry) || (selectedCategory && selectedSource) || (selectedCategory && selectedProperty) ||
                (selectedCountry && selectedSource) || (selectedCountry && selectedProperty) || (selectedSource && selectedProperty)) {
                alert('Please select only one category (subject-based, country-based, source-based, or properties-based).');
                return;
            }

            if (mw.config.get('wgAction') === 'edit') {
                // If already in edit mode, update the category directly or change all statuses
                if (selectedCategory) {
                    updateCategoryInEditMode(selectedCategory, selectedStatus);
                } else if (selectedCountry) {
                    updateCategoryInEditMode(selectedCountry, selectedStatus);
                } else if (selectedSource) {
                    updateCategoryInEditMode(selectedSource, selectedStatus);
                } else if (selectedProperty) {
                    updateCategoryInEditMode(selectedProperty, selectedStatus);
                } else {
                    changeAllStatuses(selectedStatus);
                }
            } else {
                // If not in edit mode, open the edit page with pre-loaded content
                var editUrl = mw.config.get('wgScript') + '?title=' + mw.config.get('wgPageName') + '&action=edit';
                $.get(editUrl, function(data) {
                    var editText = $(data).find('#wpTextbox1').val();
                    var categoryText = '';
                    if (selectedCategory) {
                        categoryText = categoryPrefix + selectedCategory + '/' + selectedStatus + categorySuffix;
                        var categoryRegex = new RegExp('\\[\\[Category:' + selectedCategory + '/(kept|deleted|pending|undeleted)\\]\\]', 'i');
                        if (categoryRegex.test(editText)) {
                            // If category already exists, replace it with the new status
                            editText = editText.replace(categoryRegex, categoryText);
                        } else {
                            // If category does not exist, append it to the content
                            editText += '\n' + categoryText;
                        }
                    } else if (selectedCountry) {
                        categoryText = categoryPrefix + selectedCountry + '/' + selectedStatus + categorySuffix;
                        var countryRegex = new RegExp('\\[\\[Category:' + selectedCountry + '/(kept|deleted|pending|undeleted)\\]\\]', 'i');
                        if (countryRegex.test(editText)) {
                            // If category already exists, replace it with the new status
                            editText = editText.replace(countryRegex, categoryText);
                        } else {
                            // If category does not exist, append it to the content
                            editText += '\n' + categoryText;
                        }
                    } else if (selectedSource) {
                        categoryText = categoryPrefix + selectedSource + '/' + selectedStatus + categorySuffix;
                        var sourceRegex = new RegExp('\\[\\[Category:' + selectedSource + '/(kept|deleted|pending|undeleted)\\]\\]', 'i');
                        if (sourceRegex.test(editText)) {
                            // If category already exists, replace it with the new status
                            editText = editText.replace(sourceRegex, categoryText);
                        } else {
                            // If category does not exist, append it to the content
                            editText += '\n' + categoryText;
                        }
                    } else if (selectedProperty) {
                        categoryText = categoryPrefix + selectedProperty + '/' + selectedStatus + categorySuffix;
                        var propertyRegex = new RegExp('\\[\\[Category:' + selectedProperty + '/(kept|deleted|pending|undeleted)\\]\\]', 'i');
                        if (propertyRegex.test(editText)) {
                            // If category already exists, replace it with the new status
                            editText = editText.replace(propertyRegex, categoryText);
                        } else {
                            // If category does not exist, append it to the content
                            editText += '\n' + categoryText;
                        }
                    } else {
                        // Change all existing categories to the selected status
                        var allCategoryRegex = /\[\[Category:([^\]]+\/)(kept|deleted|pending|undeleted)\]\]/gi;
                        editText = editText.replace(allCategoryRegex, categoryPrefix + '$1' + selectedStatus + categorySuffix);
                    }
                    var form = $('<form action="' + editUrl + '" method="post"></form>');
                    form.append($('<textarea name="wpTextbox1" style="display:none;"></textarea>').val(editText));
                    form.append($('<input type="hidden" name="wpSummary" value="Updating category status"/>'));
                    form.append($('<input type="hidden" name="wpSave" value="Save changes"/>'));
                    $('body').append(form);
                    form.submit();
                });
            }
        }

        // Add event listener to the apply button
        $('#applyButton').click(handleApplyClick);
    });
}