MediaWiki:Common.js: Difference between revisions

From MDrivenWiki
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 414: Line 414:
// ==/UserScript==
// ==/UserScript==


mw.loader.using(['mediawiki.api', 'mediawiki.util', 'oojs-ui'], function () {
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'jquery'], function () {
     function addCleanDeleteLink() {
     function addCleanDeleteLink() {
         if (mw.config.get('wgUserGroups').indexOf('sysop') !== -1) {
         if (mw.config.get('wgUserGroups').indexOf('sysop') !== -1) {
Line 431: Line 431:
     function gatherAllDecisions(pageTitle) {
     function gatherAllDecisions(pageTitle) {
         var decisions = {};
         var decisions = {};
         var windowManager = new OO.ui.WindowManager();
         showModal('Handle Links and Redirects', '<div class="form-group">' +
        $('body').append(windowManager.$element);
            '<label>Specify how you would like to handle all incoming links and redirects:</label>' +
            '<select id="link-handling-select" class="form-control">' +
                '<option value="delete">Delete Links and Redirects</option>' +
                '<option value="change">Change Target of Links and Redirects</option>' +
            '</select>' +
        '</div>', function () {
            var choice = $('#link-handling-select').val();
            if (choice === 'change') {
                showModal('Specify New Target', '<div class="form-group">' +
                    '<label>Enter the new target page name to update all links and redirects:</label>' +
                    '<input type="text" id="new-target-input" class="form-control" placeholder="Enter new target">' +
                '</div>', function () {
                    decisions.links = $('#new-target-input').val();
                    confirmDeletion(pageTitle, decisions);
                });
            } else {
                decisions.links = '';
                confirmDeletion(pageTitle, decisions);
            }
        });
    }


        askForLinkHandling(pageTitle, decisions, windowManager, function () {
    function confirmDeletion(pageTitle, decisions) {
            confirmDeletion(pageTitle, decisions, windowManager, function () {
        showModal('Confirm Page Deletion', '<p>Are you sure you want to delete "' + pageTitle + '" after handling all links and redirects?</p>', function () {
                showProgress(windowManager, function (progressDialog, updateProgress) {
            decisions.deletion = true;
                    executeAllActions(pageTitle, decisions, windowManager, function () {
            showLoadingDialog('Performing Cleanup', function (loadingDialog, updateLoadingText, logDetail) {
                        progressDialog.close();
                executeAllActions(pageTitle, decisions, function () {
                    loadingDialog.close().closed.then(function() {
                         location.reload();
                         location.reload();
                     }, updateProgress);
                     });
                 });
                 }, updateLoadingText, logDetail);
             });
             });
         });
         });
     }
     }


     function askForLinkHandling(pageTitle, decisions, windowManager, callback) {
     function showModal(title, body, onConfirm) {
         function LinkHandlingDialog(config) {
         var modal = $(
             LinkHandlingDialog.super.call(this, config);
            '<div class="modal fade" tabindex="-1" role="dialog">' +
         }
                '<div class="modal-dialog" role="document">' +
        OO.inheritClass(LinkHandlingDialog, OO.ui.ProcessDialog);
                    '<div class="modal-content">' +
                        '<div class="modal-header">' +
                            '<h5 class="modal-title">' + title + '</h5>' +
                            '<button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
                                '<span aria-hidden="true">&times;</span>' +
                            '</button>' +
                        '</div>' +
                        '<div class="modal-body">' + body + '</div>' +
                        '<div class="modal-footer">' +
                            '<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>' +
                            '<button type="button" class="btn btn-primary">Confirm</button>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</div>'
        );
 
        modal.find('.btn-primary').click(function () {
             onConfirm();
            modal.modal('hide');
         });


         LinkHandlingDialog.static.name = 'linkHandlingDialog';
         modal.on('hidden.bs.modal', function () {
        LinkHandlingDialog.static.title = 'Handle Links and Redirects';
             modal.remove();
        LinkHandlingDialog.static.actions = [
         });
            { action: 'continue', label: 'Continue', flags: ['primary', 'progressive'] },
             { action: 'cancel', label: 'Cancel', flags: ['safe', 'close'] }
         ];


         LinkHandlingDialog.prototype.initialize = function () {
         $('body').append(modal);
            LinkHandlingDialog.super.prototype.initialize.apply(this, arguments);
        modal.modal('show');
    }


             this.radioSelect = new OO.ui.RadioSelectWidget({
    function showLoadingDialog(message, callback) {
                 items: [
        var modal = $(
                     new OO.ui.RadioOptionWidget({ data: 'delete', label: 'Delete Links and Redirects' }),
             '<div class="modal fade" tabindex="-1" role="dialog">' +
                    new OO.ui.RadioOptionWidget({ data: 'change', label: 'Change Target of Links and Redirects' })
                 '<div class="modal-dialog" role="document">' +
                 ]
                     '<div class="modal-content">' +
             });
                        '<div class="modal-header">' +
                            '<h5 class="modal-title">Performing Cleanup</h5>' +
                        '</div>' +
                        '<div class="modal-body">' +
                            '<p id="loading-text">' + message + '</p>' +
                            '<ul id="loading-log"></ul>' +
                        '</div>' +
                    '</div>' +
                 '</div>' +
             '</div>'
        );


            var fieldset = new OO.ui.FieldsetLayout({
        $('body').append(modal);
                items: [
        modal.modal('show');
                    new OO.ui.FieldLayout(this.radioSelect, {
                        label: 'Specify how you would like to handle all incoming links and redirects:',
                        align: 'top'
                    })
                ]
            });


             this.content = new OO.ui.PanelLayout({
        callback(modal, function (text) {
                padded: true,
             $('#loading-text').text(text);
                expanded: false
            $('#loading-log').append('<li>' + text + '</li>');
            });
        }, function (detail) {
            $('#loading-log').append('<li>' + detail + '</li>');
        });
    }


            this.content.$element.append(fieldset.$element);
    function executeAllActions(pageTitle, decisions, callback, updateLoadingText, logDetail) {
            this.$body.append(this.content.$element);
        var api = new mw.Api();
        };


         LinkHandlingDialog.prototype.getActionProcess = function (action) {
         function handleLinksAndRedirects(action, newTarget, next) {
             var dialog = this;
             updateLoadingText('Fetching backlinks...');
             var process = new OO.ui.Process(function () {
             api.get({
                 if (action === 'continue') {
                action: 'query',
                     var choice = dialog.radioSelect.findSelectedItem();
                list: 'backlinks',
                    if (choice && choice.data === 'change') {
                bltitle: pageTitle,
                        dialog.close().then(function () {
                bllimit: 'max',
                            askForNewTarget(pageTitle, decisions, windowManager, callback);
                blfilterredir: 'all' // Include both redirects and non-redirects
                        });
            }).done(function (data) {
                    } else {
                 logDetail('Backlinks fetched.');
                        decisions.links = '';
                if (data.query.backlinks) {
                        dialog.close().then(function () {
                     var promises = $.map(data.query.backlinks, function (link) {
                             callback();
                        return new $.Deferred(function (defer) {
                         });
                            api.get({
                     }
                                action: 'parse',
                } else if (action === 'cancel') {
                                page: link.title,
                     dialog.close().then(function () {
                                prop: 'wikitext'
                         windowManager.destroy();
                            }).done(function (linkData) {
                                var isRedirect = linkData.parse.wikitext['*'].trim().startsWith('#REDIRECT');
                                if (isRedirect) {
                                    if (action === 'update') {
                                        updateRedirectTarget(link.title, newTarget, api, defer.resolve, logDetail);
                                    } else {
                                        logDetail('Backlink ' + link.title + ' is a redirect. Deleting...');
                                        performDeletion(link.title, defer.resolve, logDetail);
                                    }
                                } else {
                                    if (action === 'update') {
                                        updateLinkInPage(link.title, pageTitle, newTarget, api, defer.resolve, logDetail);
                                    } else {
                                        removeLinkFromPage(link.title, pageTitle, api, defer.resolve, logDetail);
                                    }
                                }
                            }).fail(function (error) {
                                logDetail('Error checking if backlink ' + link.title + ' is a redirect: ' + error);
                                defer.resolve();
                             });
                         }).promise();
                     });
                     $.when.apply($, promises).then(function () {
                         updateLoadingText(action === 'update' ? 'All links updated.' : 'All links removed.');
                        next();
                     });
                     });
                } else {
                    next();
                 }
                 }
            }).fail(function (error) {
                updateLoadingText('Error fetching backlinks: ' + error);
                next();
             });
             });
            return process;
        };
        windowManager.addWindows([new LinkHandlingDialog()]);
        windowManager.openWindow('linkHandlingDialog');
    }
    function askForNewTarget(pageTitle, decisions, windowManager, callback) {
        function NewTargetDialog(config) {
            NewTargetDialog.super.call(this, config);
         }
         }
        OO.inheritClass(NewTargetDialog, OO.ui.ProcessDialog);


         NewTargetDialog.static.name = 'newTargetDialog';
         function updateLinkInPage(linkedPageTitle, originalPageTitle, newTarget, api, resolve, logDetail) {
        NewTargetDialog.static.title = 'Specify New Target';
            api.get({
        NewTargetDialog.static.actions = [
                action: 'parse',
            { action: 'continue', label: 'Continue', flags: ['primary', 'progressive'] },
                page: linkedPageTitle,
            { action: 'cancel', label: 'Cancel', flags: ['safe', 'close'] }
                prop: 'wikitext'
        ];
            }).then(function (data) {
 
                var wikitext = data.parse.wikitext['*'];
        NewTargetDialog.prototype.initialize = function () {
                var regex = new RegExp('\\[\\[' + originalPageTitle.replace(/[\[\]]/g, '\\$&') + '(\\|[^\\]]+)?\\]\\]', 'g');
            NewTargetDialog.super.prototype.initialize.apply(this, arguments);
                var newWikitext = wikitext.replace(regex, '[[' + newTarget + '$1]]');
 
                if (wikitext !== newWikitext) {
            this.input = new OO.ui.TextInputWidget({
                    logDetail('Updating link in ' + linkedPageTitle);
                value: '',
                } else {
                placeholder: 'Enter new target'
                    logDetail('No link found in ' + linkedPageTitle);
            });
                }
 
                return api.postWithToken('csrf', {
            var fieldset = new OO.ui.FieldsetLayout({
                    action: 'edit',
                items: [
                    title: linkedPageTitle,
                     new OO.ui.FieldLayout(this.input, {
                    text: newWikitext,
                        label: 'Enter the new target page name to update all links and redirects:',
                    summary: 'Updated link to [[' + newTarget + ']]'
                        align: 'top'
                }).then(function (response) {
                    })
                    console.log('API response from updating link: ', response);
                 ]
                    if (response.edit && response.edit.result === 'Success' && !response.edit.nochange) {
                        logDetail('Successfully updated link in ' + linkedPageTitle);
                    } else {
                        logDetail('No changes made to link in ' + linkedPageTitle);
                    }
                    resolve();
                }).fail(function (error) {
                    logDetail('Error updating link in ' + linkedPageTitle + ': ' + error);
                     resolve();
                });
            }).fail(function (error) {
                logDetail('Error fetching page content for ' + linkedPageTitle + ': ' + error);
                 resolve();
             });
             });
        }


            this.content = new OO.ui.PanelLayout({
        function updateRedirectTarget(redirectPageTitle, newTarget, api, resolve, logDetail) {
                padded: true,
             logDetail('Updating redirect target in ' + redirectPageTitle);
                expanded: false
             return api.postWithToken('csrf', {
            });
                action: 'edit',
 
                title: redirectPageTitle,
             this.content.$element.append(fieldset.$element);
                text: '#REDIRECT [[' + newTarget + ']]',
             this.$body.append(this.content.$element);
                 summary: 'Updated redirect to [[' + newTarget + ']]'
        };
            }).then(function (response) {
 
                console.log('API response from updating redirect: ', response);
        NewTargetDialog.prototype.getActionProcess = function (action) {
                 if (response.edit && response.edit.result === 'Success' && !response.edit.nochange) {
            var dialog = this;
                    logDetail('Successfully updated redirect in ' + redirectPageTitle);
            var process = new OO.ui.Process(function () {
                } else {
                 if (action === 'continue') {
                     logDetail('No changes made to redirect in ' + redirectPageTitle);
                    decisions.links = dialog.input.getValue();
                    dialog.close().then(function () {
                        callback();
                    });
                 } else if (action === 'cancel') {
                    dialog.close().then(function () {
                        windowManager.destroy();
                     });
                 }
                 }
                resolve();
            }).fail(function (error) {
                logDetail('Error updating redirect in ' + redirectPageTitle + ': ' + error);
                resolve();
             });
             });
            return process;
        };
        windowManager.addWindows([new NewTargetDialog()]);
        windowManager.openWindow('newTargetDialog');
    }
    function confirmDeletion(pageTitle, decisions, windowManager, callback) {
        var dialog = new OO.ui.MessageDialog();
        windowManager.addWindows([dialog]);
        windowManager.openWindow(dialog, {
            title: 'Confirm Page Deletion',
            message: 'Are you sure you want to delete "' + pageTitle + '" after handling all links and redirects?',
            actions: [
                { label: 'Confirm Deletion', action: 'confirm', flags: ['primary', 'destructive'] },
                { label: 'Cancel', action: 'cancel' }
            ]
        }).closed.then(function (data) {
            if (data.action === 'confirm') {
                decisions.deletion = true;
                callback();
            } else {
                windowManager.destroy();
            }
        });
    }
    function showProgress(windowManager, callback) {
        function ProgressDialog(config) {
            ProgressDialog.super.call(this, config);
         }
         }
        OO.inheritClass(ProgressDialog, OO.ui.ProcessDialog);


         ProgressDialog.static.name = 'progressDialog';
         function removeLinkFromPage(linkedPageTitle, originalPageTitle, api, resolve, logDetail) {
        ProgressDialog.static.title = 'Deleting Links and Redirects';
            api.get({
 
                action: 'parse',
        ProgressDialog.prototype.initialize = function () {
                page: linkedPageTitle,
            ProgressDialog.super.prototype.initialize.apply(this, arguments);
                prop: 'wikitext'
 
            }).then(function (data) {
            this.progressBar = new OO.ui.ProgressBarWidget();
                var wikitext = data.parse.wikitext['*'];
            this.progressLabel = new OO.ui.LabelWidget({ label: '' });
                var regex = new RegExp('\\[\\[' + originalPageTitle.replace(/[\[\]]/g, '\\$&') + '(\\|[^\\]]+)?\\]\\]', 'g');
 
                var newWikitext = wikitext.replace(regex, function(match, p1) {
            this.content = new OO.ui.PanelLayout({
                    return p1 ? p1.substring(1) : originalPageTitle;
                padded: true,
                });
                expanded: false
                if (wikitext !== newWikitext) {
            });
                    logDetail('Removing link from ' + linkedPageTitle);
 
                } else {
            this.content.$element.append(this.progressLabel.$element, this.progressBar.$element);
                    logDetail('No link found in ' + linkedPageTitle);
            this.$body.append(this.content.$element);
                }
        };
                return api.postWithToken('csrf', {
 
                    action: 'edit',
        ProgressDialog.prototype.getActionProcess = function (action) {
                    title: linkedPageTitle,
            return new OO.ui.Process();
                    text: newWikitext,
        };
                    summary: 'Removed link to [[' + originalPageTitle + ']]'
 
                }).then(function (response) {
        windowManager.addWindows([new ProgressDialog()]);
                    console.log('API response from removing link: ', response);
        windowManager.openWindow('progressDialog').then(function (opened) {
                    if (response.edit && response.edit.result === 'Success' && !response.edit.nochange) {
            opened.then(function (dialog) {
                        logDetail('Successfully removed link from ' + linkedPageTitle);
                 callback(dialog, function (progress, text) {
                    } else {
                     dialog.progressBar.setProgress(progress);
                        logDetail('No changes made to link in ' + linkedPageTitle);
                     dialog.progressLabel.setLabel(text);
                    }
                    resolve();
                 }).fail(function (error) {
                     logDetail('Error removing link from ' + linkedPageTitle + ': ' + error);
                     resolve();
                 });
                 });
            }).fail(function (error) {
                logDetail('Error fetching page content for ' + linkedPageTitle + ': ' + error);
                resolve();
             });
             });
        });
    }
    function executeAllActions(pageTitle, decisions, windowManager, callback, updateProgress) {
        var api = new mw.Api();
        var promises = [];
        var progress = 0;
        var totalSteps = 3; // Adjust based on the number of steps
        function incrementProgress(text) {
            progress += 1;
            updateProgress((progress / totalSteps) * 100, text);
         }
         }


         if (decisions.links !== undefined) {
         function handleRedirects(next) {
             if (decisions.links === '') {
             updateLoadingText('Fetching redirects...');
                 promises.push(new Promise(function (resolve) {
            api.get({
                    incrementProgress('Removing all links and redirects...');
                 action: 'query',
                     removeAllLinksAndRedirects(pageTitle, api, function () {
                titles: pageTitle,
                         incrementProgress('All links and redirects removed.');
                redirects: true
                         resolve();
            }).done(function (data) {
                logDetail('Redirects fetched.');
                var redirects = data.query.pages[Object.keys(data.query.pages)[0]].redirects;
                if (redirects) {
                     var promises = $.map(redirects, function (redirect) {
                         return new $.Deferred(function (defer) {
                            logDetail('Found redirect: ' + redirect.title);
                            performDeletion(redirect.title, defer.resolve, logDetail);
                         }).promise();
                     });
                     });
                }));
                     $.when.apply($, promises).then(function () {
            } else {
                         next();
                promises.push(new Promise(function (resolve) {
                     incrementProgress('Updating all links and redirects...');
                    updateAllLinksAndRedirects(pageTitle, decisions.links, api, function () {
                         incrementProgress('All links and redirects updated.');
                        resolve();
                     });
                     });
                 }));
                 } else {
             }
                    next();
                }
            }).fail(function (error) {
                updateLoadingText('Error fetching redirects: ' + error);
                next();
             });
         }
         }
         promises.push(new Promise(function (resolve) {
 
             incrementProgress('Deleting the page...');
         function performDeletion(pageTitle, resolve, logDetail) {
             performDeletion(pageTitle, api, function () {
             updateLoadingText('Deleting the page: ' + pageTitle);
                 incrementProgress('Page deleted.');
             api.postWithToken('csrf', {
                action: 'delete',
                title: pageTitle,
                reason: 'Automated clean delete by admin'
            }).done(function (response) {
                 console.log('API response from deleting page: ', response);
                if (response.delete && response.delete.title) {
                    logDetail('Successfully deleted page: ' + response.delete.title);
                } else {
                    logDetail('No deletion performed for ' + pageTitle);
                }
                resolve();
            }).fail(function (error) {
                logDetail('Error during clean deletion of ' + pageTitle + ': ' + error);
                 resolve();
                 resolve();
             });
             });
         }));
         }
        Promise.all(promises).then(callback);
    }


    function updateAllLinksAndRedirects(pageTitle, newTarget, api, callback) {
         var tasks = [
         handleLinks(pageTitle, newTarget, 'update', api, function () {
             function (resolve) {
             handleRedirects(pageTitle, newTarget, 'update', api, callback);
                if (decisions.links === '') {
        });
                    handleLinksAndRedirects('remove', '', resolve);
    }
                } else {
 
                    handleLinksAndRedirects('update', decisions.links, resolve);
    function removeAllLinksAndRedirects(pageTitle, api, callback) {
                }
        handleLinks(pageTitle, '', 'remove', api, function () {
             },
            handleRedirects(pageTitle, '', 'remove', api, callback);
             function (resolve) {
        });
                if (decisions.deletion) {
    }
                     handleRedirects(resolve);
 
                } else {
    function handleLinks(pageTitle, newTarget, action, api, callback) {
                    resolve();
        api.get({
                }
            action: 'query',
            },
            list: 'backlinks',
            function (resolve) {
            bltitle: pageTitle,
                 if (decisions.deletion) {
             blfilterredir: 'nonredirects',
                    performDeletion(pageTitle, resolve, logDetail);
             bllimit: 'max'
                } else {
        }).done(function (data) {
                    resolve();
            if (data.query.backlinks) {
                }
                var promises = data.query.backlinks.map(function (link) {
                     return new Promise(function (resolve) {
                        if (action === 'update') {
                            updateLink(link.title, newTarget, api).then(resolve);
                        } else {
                            removeLink(link.title, api).then(resolve);
                        }
                    });
                });
                 Promise.all(promises).then(callback);
            } else {
                callback();
             }
             }
         });
         ];
    }


    function handleRedirects(pageTitle, newTarget, action, api, callback) {
         (function executeTasks(i) {
         api.get({
             if (i < tasks.length) {
            action: 'query',
                 tasks[i](function () {
            list: 'backlinks',
                     executeTasks(i + 1);
            bltitle: pageTitle,
            blfilterredir: 'redirects',
            bllimit: 'max'
        }).done(function (data) {
             if (data.query.backlinks) {
                 var promises = data.query.backlinks.map(function (redirect) {
                     return new Promise(function (resolve) {
                        if (action === 'update') {
                            updateLink(redirect.title, newTarget, api).then(resolve);
                        } else {
                            removeLink(redirect.title, api).then(resolve);
                        }
                    });
                 });
                 });
                Promise.all(promises).then(callback);
             } else {
             } else {
                 callback();
                 callback();
             }
             }
         });
         })(0);
    }
 
    function updateLink(pageTitle, newTarget, api) {
        return api.postWithToken('csrf', {
            action: 'edit',
            title: pageTitle,
            text: function(text) {
                return text.replace(new RegExp('\\[\\[' + mw.util.escapeRegExp(pageTitle) + '(\\|[^\\]]+)?\\]\\]', 'g'), '[[' + newTarget + '$1]]');
            },
            summary: 'Updated redirect to new target [[' + newTarget + ']]',
        });
    }
 
    function removeLink(pageTitle, api) {
        return api.postWithToken('csrf', {
            action: 'edit',
            title: pageTitle,
            text: function(text) {
                return text.replace(new RegExp('\\[\\[' + mw.util.escapeRegExp(pageTitle) + '(\\|[^\\]]+)?\\]\\]', 'g'), '');
            },
            summary: 'Removed link to [[' + pageTitle + ']]',
        });
    }
 
    function performDeletion(pageTitle, api, callback) {
        api.postWithToken('csrf', {
            action: 'delete',
            title: pageTitle,
            reason: 'Automated clean delete by admin',
        }).done(function () {
            console.log('Page deleted:', pageTitle);
            callback();
        }).fail(function (error) {
            console.error('Error during clean deletion:', error);
            callback();
        });
     }
     }


     mw.hook('wikipage.content').add(addCleanDeleteLink);
     mw.hook('wikipage.content').add(addCleanDeleteLink);
});
});

Latest revision as of 11:13, 8 July 2024

/* Any JavaScript here will be loaded for all users on every page load. */
$(document).ready(function () {
    $.get(mw.util.wikiScript('api'), {
        action: 'query',
        meta: 'userinfo',
        format: 'json'
    }).done(function (data) {
        if (data.query.userinfo.id !== 0) { 
            var username = data.query.userinfo.name;
            var userLink = mw.util.getUrl('User:' + username);
            var logoutLink = mw.util.getUrl('Special:Logout');
            $('#user-info').html('<a href="' + userLink + '" class="text-white">' + username + '</a>' + 
                                 ' &nbsp;|&nbsp; <a href="' + logoutLink + '" class="text-white">Logout</a>');
        }
    });
});


document.getElementById('offcanvas-toggler').addEventListener('click', function() {
    var sidebar = document.getElementById('offcanvas-menu');
    if (sidebar.classList.contains('show')) {
        sidebar.classList.remove('show');
    } else {
        sidebar.classList.add('show');
    }
});




$(document).ready(function() {
    $('#offcanvas-close').on('click', function() {
        $('#offcanvas-menu').removeClass('show');
    });
});

document.addEventListener('DOMContentLoaded', function() {
    var form = document.querySelector('.namespace-search-form');
    if (form) {
        form.addEventListener('submit', function(e) {
            var input = form.querySelector('#bs-extendedsearch-input');
            if (input) {
                var namespace = mw.config.get('wgCanonicalNamespace');
                if (namespace && namespace.length > 0) {
                    input.value = namespace + ": " + input.value;
                }
            }
        });
    }
});

(function($) {
    'use strict';

    var css = [
        '#suggestion-container {',
        '    position: relative;',
        '    width: 100%;',
        '}',
        '#suggestion-box {',
        '    position: absolute;',
        '    top: 100%;', 
        '    left: 0;', 
        '    width: 100%;', 
        '    margin-top: 5px;', 
        '    background-color: #fff;',
        '    z-index: 1000;',
        '}',
        '.suggestion-item {',
        '    padding: 8px;',
        '    cursor: pointer;',
        '}',
        '.suggestion-item:hover {',
        '    background-color: #e0e0e0;',
        '}'
    ].join('\n');
    $('head').append('<style type="text/css">' + css + '</style>');

    $('#suggestion-container').append('<div id="suggestion-box"></div>');

    function showSuggestions() {
        var query = $(this).val();
        if (query.length > 0) { 
            var apiUrl = "https://wiki.mdriven.net/api.php";
            var requestData = {
                action: "bs-extendedsearch-autocomplete",
                format: "json",
                q: JSON.stringify({
                    query: {
                        bool: {
                            must: {
                                match: {
                                    ac_ngram: {
                                        query: query
                                    }
                                }
                            }
                        }
                    },
                    size: 8
                }),
                searchData: JSON.stringify({
                    namespace: 0,
                    value: query,
                    mainpage: ""
                })
            };
            $.ajax({
                url: apiUrl,
                data: requestData,
                dataType: "json",
                method: "GET",
                success: function(data) {
                    var suggestions = data.suggestions || [];
                    $('#suggestion-box').empty();
                    $.each(suggestions, function(index, suggestion) {
                        var item = $('<div class="suggestion-item"></div>').text(suggestion.basename);
                        $('#suggestion-box').append(item);
                    });
                },
                error: function(jqxhr, textStatus, error) {
                    console.error('Error fetching suggestions:', error);
                }
            });
        }
    }

   function hideSuggestions(event) {
    if (!$(event.target).closest('#suggestion-container').length) {
        $('#suggestion-box').empty();
    }
}

function selectSuggestion(event) {
    event.preventDefault();  // Prevent the mousedown event from triggering blur on the search input
    var selectedText = $(this).text();
    window.location.href = '/index.php?title=' + encodeURIComponent(selectedText);
}


   $('.search-input').on('input', showSuggestions);
$(document).on('click', hideSuggestions);  
$('#suggestion-box').on('mousedown', '.suggestion-item', selectSuggestion); 


})(jQuery);

$(document).ready(function() {
    // Function to toggle a section open or closed
    function toggleSection(element) {
        var submenu = $(element).next('.submenu');
        submenu.toggle();
    }

    // Function to open the submenu containing the current page link and scroll to it
    function openCurrentPageSubmenuAndScroll() {
        var currentPageLink = $('#navMenu .current-page');
        if (currentPageLink.length) {
            // Open the parent submenu(s) of the current page link
            currentPageLink.parents('.submenu').show();

            // Delay the scrolling to allow for any dynamic layout changes
            setTimeout(function() {
                // Calculate the position of the current page link
                var position = currentPageLink.offset().top - $('#navMenu').offset().top + $('#navMenu').scrollTop();

                // Scroll the menu to the active item
                $('#navMenu').animate({
                    scrollTop: position
                }, 500);
            }, 100); // Delay of 100 milliseconds
        }
    }

    // Event delegation for dynamically loaded content
    $('#navMenu').on('click', '.menu-header', function() {
        toggleSection(this);
    });

    // Open the submenu containing the current page link and scroll to it
    openCurrentPageSubmenuAndScroll();
});

document.getElementById('menu-toggle').addEventListener('click', function() {
    var navMenu = document.getElementById('navMenu');
    var bodyContent = document.getElementById('bodyContent');

    if (navMenu.style.display === 'none' || navMenu.style.display === '') {
        navMenu.style.display = 'block';
        bodyContent.style.display = 'none';  // Hide body content when nav menu is displayed
    } else {
        navMenu.style.display = 'none';
        bodyContent.style.display = 'block';  // Show body content when nav menu is hidden
    }
});

$(document).ready(function() {
    $('.video__navigation .navigation-item').click(function() {
        var videoID = $(this).data('video');
        var startTime = $(this).data('start');
        var newSrc = 'https://www.youtube.com/embed/' + videoID + '?start=' + startTime + '&autoplay=1';
        
        $('.video__wrapper iframe').attr('src', newSrc);
    });
});

$(document).ready(function() {
        $('.bs-extendedsearch-filter-button-button').each(function() {
            $(this).append('<span class="fa fa-chevron-down"></span>');
        });
});

document.addEventListener("scroll", function() {
    var sidebar = document.getElementById("navMenu");
    var footerHeight = 100;
    var windowHeight = window.innerHeight;

    var scrollBottomPosition = window.scrollY + windowHeight;
    var footerTopPosition = document.documentElement.offsetHeight - footerHeight;

    if (scrollBottomPosition <= footerTopPosition) {
        sidebar.style.height = "100vh";
    } else {
        sidebar.style.height = "calc(100vh - 110px)";
    }
});

mw.loader.using('jquery', function() {
    $(document).ready(function() {
        var $toc = $('#toc').clone();
        $('#toc').remove(); 
        $('#tocContainer').html($toc);

   
        $('#tocContainer').css({
            position: '-webkit-sticky', 
            position: 'sticky',
            top: '0', 
            padding: '15px',
            margin: '0 auto', 
            borderRadius: '5px', 
            maxWidth: '280px',
            maxHeight: 'max-content',
            overflowY: 'auto',
            height: '90vh',
            zIndex: 1000
        });

        $('#tocContainer .toctitle').css({
            fontSize: '16px',
            fontWeight: '600',
            color: '#555',
            borderBottom: '1px solid #ccc',
            paddingBottom: '10px',
            marginBottom: '10px'
        });

        $('#tocContainer ul').css({
            margin: 0,
            padding: '0 0 0 20px',
            listStyleType: 'none',
            fontSize: '14px',
            lineHeight: '1.6' 
        });

        $('#tocContainer li').css({
            marginBottom: '5px', 
        });

        $('#tocContainer a').css({
            color: '#555', 
            textDecoration: 'none',
        }).hover(
            function() { $(this).css({textDecoration: 'underline', color: '#000'}); },
            function() { $(this).css({textDecoration: 'none', color: '#555'}); }

        );
    });
});

$(document).ready(function() {
    var $inputElement = $('.oo-ui-textInputWidget-type-search .oo-ui-inputWidget-input');
    if ($inputElement.length) {
        $inputElement.on('keydown', function(event) {
            if (event.key === 'Enter') {
                event.preventDefault();
                $inputElement.addClass('search-input');
            }
        });
    }
});

$(document).ready(function() {
    var $inputElement = $('.oo-ui-textInputWidget-type-search .oo-ui-inputWidget-input');
    if ($inputElement.length) {
        $inputElement.addClass('search-input');
        console.log('Class "search-input" added to the input element.');
    } else {
        console.log('Input element not found.');
    }
});

$(document).ready(function() {
    function applyChanges() {
        console.log('applyChanges function called');
        var $searchInput = $('input[type="search"].oo-ui-inputWidget-input');
        var $searchWidgetResults = $('.oo-ui-searchWidget-results');
        if ($searchInput.length && $searchWidgetResults.length) {
            console.log('Input element found:', $searchInput);
            $searchInput.addClass('search-input-edit');
            console.log('Class "search-input" added to input element');

            if ($searchWidgetResults.next('#suggestion-container-edit').length === 0) {
                $('<div id="suggestion-container-edit"></div>').insertAfter($searchWidgetResults);
                console.log('Suggestion container added after .oo-ui-searchWidget-results element');
            }

            $searchInput.on('input', showSuggestions);
           $('#suggestion-container').css('width', $searchInput.outerWidth());
        } else {
            console.log('Input element or .oo-ui-searchWidget-results not found, retrying...');
            setTimeout(applyChanges, 100);
        }
    }

    function checkVeActiveClass() {
        console.log('Checking for ve-active class');
        var $htmlTag = $('html');
        if ($htmlTag.hasClass('ve-active')) {
            console.log('ve-active class detected on HTML tag');
            applyChanges();
        } else {
            console.log('ve-active class not yet detected, retrying...');
            setTimeout(checkVeActiveClass, 100);
        }
    }

    function showSuggestions() {
        var query = $(this).val();
        if (query.length > 0) {
            var apiUrl = "https://insider.mdriven.net/api/search";
            var requestData = {
                q: query
            };
            $.ajax({
                url: apiUrl,
                data: requestData,
                dataType: "json",
                method: "GET",
                success: function(data) {
                    var suggestions = data || [];
                    $('#suggestion-container-edit').empty();
                    $.each(suggestions, function(index, suggestion) {
                        var source = suggestion._source;
                        var displayText = source.namespace_text + ':' + source.basename;
                        var item = $('<div class="suggestion-item-edit"></div>').text(displayText);
                        item.on('click', function() {
                            $('input[type="search"].oo-ui-inputWidget-input').val(displayText);
                            $('#suggestion-container-edit').empty();
                        });
                        $('#suggestion-container-edit').append(item);
                    });
                },
                error: function(jqxhr, textStatus, error) {
                    console.error('Error fetching suggestions:', error);
                }
            });
        } else {
            $('#suggestion-container-edit').empty(); // Clear suggestions if query is empty
        }
    }

    console.log('Starting to check for ve-active class');
    checkVeActiveClass();
});

$('<style type="text/css">#suggestion-container-edit { position: static; border: 1px solid #ccc; background: white; z-index: 10000; width: 100%; overflow-y: auto; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } .suggestion-item-edit { padding: 8px; cursor: pointer; } .suggestion-item-edit:hover { background: #f0f0f0; }</style>').appendTo('head');

$(document).ready(function() {
        var signInLink = $('#user-info a');
        var currentUrl = encodeURIComponent(window.location.pathname + window.location.search + window.location.hash);
        signInLink.attr('href', signInLink.attr('href').replace('<CURRENT_URL>', currentUrl));
    });


document.addEventListener('DOMContentLoaded', function() {
    var images = document.querySelectorAll('.thumbimage');
    var overlay = document.createElement('div');
    overlay.className = 'image-overlay';
    document.body.appendChild(overlay);

    var overlayImage = document.createElement('img');
    overlay.appendChild(overlayImage);

    images.forEach(function(image) {
        image.addEventListener('click', function(event) {
            event.preventDefault();
            event.stopPropagation();

            overlayImage.src = image.src;
            overlay.classList.add('show');
        });
    });

    overlay.addEventListener('click', function() {
        overlay.classList.remove('show');
    });
});

// ==UserScript==
// @name        MediaWiki Clean Delete
// @namespace   MediaWikiScripts
// @description Adds a 'Clean Delete' action link to pages for admins to delete pages and clean up incoming links and redirects.
// ==/UserScript==

mw.loader.using(['mediawiki.api', 'mediawiki.util', 'jquery'], function () {
    function addCleanDeleteLink() {
        if (mw.config.get('wgUserGroups').indexOf('sysop') !== -1) {
            if ($('#ca-cleandelete').length === 0) {
                var $link = $('<div>').attr('id', 'ca-cleandelete').attr('class', 'mw-list-item').append(
                    $('<a>').attr('href', '#').attr('class', 'ca-cleandelete').text('Clean Delete').click(function (e) {
                        e.preventDefault();
                        gatherAllDecisions(mw.config.get('wgPageName'));
                    })
                );
                $('#p-actions .tab-group').append($link);
            }
        }
    }

    function gatherAllDecisions(pageTitle) {
        var decisions = {};
        showModal('Handle Links and Redirects', '<div class="form-group">' +
            '<label>Specify how you would like to handle all incoming links and redirects:</label>' +
            '<select id="link-handling-select" class="form-control">' +
                '<option value="delete">Delete Links and Redirects</option>' +
                '<option value="change">Change Target of Links and Redirects</option>' +
            '</select>' +
        '</div>', function () {
            var choice = $('#link-handling-select').val();
            if (choice === 'change') {
                showModal('Specify New Target', '<div class="form-group">' +
                    '<label>Enter the new target page name to update all links and redirects:</label>' +
                    '<input type="text" id="new-target-input" class="form-control" placeholder="Enter new target">' +
                '</div>', function () {
                    decisions.links = $('#new-target-input').val();
                    confirmDeletion(pageTitle, decisions);
                });
            } else {
                decisions.links = '';
                confirmDeletion(pageTitle, decisions);
            }
        });
    }

    function confirmDeletion(pageTitle, decisions) {
        showModal('Confirm Page Deletion', '<p>Are you sure you want to delete "' + pageTitle + '" after handling all links and redirects?</p>', function () {
            decisions.deletion = true;
            showLoadingDialog('Performing Cleanup', function (loadingDialog, updateLoadingText, logDetail) {
                executeAllActions(pageTitle, decisions, function () {
                    loadingDialog.close().closed.then(function() {
                        location.reload();
                    });
                }, updateLoadingText, logDetail);
            });
        });
    }

    function showModal(title, body, onConfirm) {
        var modal = $(
            '<div class="modal fade" tabindex="-1" role="dialog">' +
                '<div class="modal-dialog" role="document">' +
                    '<div class="modal-content">' +
                        '<div class="modal-header">' +
                            '<h5 class="modal-title">' + title + '</h5>' +
                            '<button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
                                '<span aria-hidden="true">&times;</span>' +
                            '</button>' +
                        '</div>' +
                        '<div class="modal-body">' + body + '</div>' +
                        '<div class="modal-footer">' +
                            '<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>' +
                            '<button type="button" class="btn btn-primary">Confirm</button>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</div>'
        );

        modal.find('.btn-primary').click(function () {
            onConfirm();
            modal.modal('hide');
        });

        modal.on('hidden.bs.modal', function () {
            modal.remove();
        });

        $('body').append(modal);
        modal.modal('show');
    }

    function showLoadingDialog(message, callback) {
        var modal = $(
            '<div class="modal fade" tabindex="-1" role="dialog">' +
                '<div class="modal-dialog" role="document">' +
                    '<div class="modal-content">' +
                        '<div class="modal-header">' +
                            '<h5 class="modal-title">Performing Cleanup</h5>' +
                        '</div>' +
                        '<div class="modal-body">' +
                            '<p id="loading-text">' + message + '</p>' +
                            '<ul id="loading-log"></ul>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</div>'
        );

        $('body').append(modal);
        modal.modal('show');

        callback(modal, function (text) {
            $('#loading-text').text(text);
            $('#loading-log').append('<li>' + text + '</li>');
        }, function (detail) {
            $('#loading-log').append('<li>' + detail + '</li>');
        });
    }

    function executeAllActions(pageTitle, decisions, callback, updateLoadingText, logDetail) {
        var api = new mw.Api();

        function handleLinksAndRedirects(action, newTarget, next) {
            updateLoadingText('Fetching backlinks...');
            api.get({
                action: 'query',
                list: 'backlinks',
                bltitle: pageTitle,
                bllimit: 'max',
                blfilterredir: 'all' // Include both redirects and non-redirects
            }).done(function (data) {
                logDetail('Backlinks fetched.');
                if (data.query.backlinks) {
                    var promises = $.map(data.query.backlinks, function (link) {
                        return new $.Deferred(function (defer) {
                            api.get({
                                action: 'parse',
                                page: link.title,
                                prop: 'wikitext'
                            }).done(function (linkData) {
                                var isRedirect = linkData.parse.wikitext['*'].trim().startsWith('#REDIRECT');
                                if (isRedirect) {
                                    if (action === 'update') {
                                        updateRedirectTarget(link.title, newTarget, api, defer.resolve, logDetail);
                                    } else {
                                        logDetail('Backlink ' + link.title + ' is a redirect. Deleting...');
                                        performDeletion(link.title, defer.resolve, logDetail);
                                    }
                                } else {
                                    if (action === 'update') {
                                        updateLinkInPage(link.title, pageTitle, newTarget, api, defer.resolve, logDetail);
                                    } else {
                                        removeLinkFromPage(link.title, pageTitle, api, defer.resolve, logDetail);
                                    }
                                }
                            }).fail(function (error) {
                                logDetail('Error checking if backlink ' + link.title + ' is a redirect: ' + error);
                                defer.resolve();
                            });
                        }).promise();
                    });
                    $.when.apply($, promises).then(function () {
                        updateLoadingText(action === 'update' ? 'All links updated.' : 'All links removed.');
                        next();
                    });
                } else {
                    next();
                }
            }).fail(function (error) {
                updateLoadingText('Error fetching backlinks: ' + error);
                next();
            });
        }

        function updateLinkInPage(linkedPageTitle, originalPageTitle, newTarget, api, resolve, logDetail) {
            api.get({
                action: 'parse',
                page: linkedPageTitle,
                prop: 'wikitext'
            }).then(function (data) {
                var wikitext = data.parse.wikitext['*'];
                var regex = new RegExp('\\[\\[' + originalPageTitle.replace(/[\[\]]/g, '\\$&') + '(\\|[^\\]]+)?\\]\\]', 'g');
                var newWikitext = wikitext.replace(regex, '[[' + newTarget + '$1]]');
                if (wikitext !== newWikitext) {
                    logDetail('Updating link in ' + linkedPageTitle);
                } else {
                    logDetail('No link found in ' + linkedPageTitle);
                }
                return api.postWithToken('csrf', {
                    action: 'edit',
                    title: linkedPageTitle,
                    text: newWikitext,
                    summary: 'Updated link to [[' + newTarget + ']]'
                }).then(function (response) {
                    console.log('API response from updating link: ', response);
                    if (response.edit && response.edit.result === 'Success' && !response.edit.nochange) {
                        logDetail('Successfully updated link in ' + linkedPageTitle);
                    } else {
                        logDetail('No changes made to link in ' + linkedPageTitle);
                    }
                    resolve();
                }).fail(function (error) {
                    logDetail('Error updating link in ' + linkedPageTitle + ': ' + error);
                    resolve();
                });
            }).fail(function (error) {
                logDetail('Error fetching page content for ' + linkedPageTitle + ': ' + error);
                resolve();
            });
        }

        function updateRedirectTarget(redirectPageTitle, newTarget, api, resolve, logDetail) {
            logDetail('Updating redirect target in ' + redirectPageTitle);
            return api.postWithToken('csrf', {
                action: 'edit',
                title: redirectPageTitle,
                text: '#REDIRECT [[' + newTarget + ']]',
                summary: 'Updated redirect to [[' + newTarget + ']]'
            }).then(function (response) {
                console.log('API response from updating redirect: ', response);
                if (response.edit && response.edit.result === 'Success' && !response.edit.nochange) {
                    logDetail('Successfully updated redirect in ' + redirectPageTitle);
                } else {
                    logDetail('No changes made to redirect in ' + redirectPageTitle);
                }
                resolve();
            }).fail(function (error) {
                logDetail('Error updating redirect in ' + redirectPageTitle + ': ' + error);
                resolve();
            });
        }

        function removeLinkFromPage(linkedPageTitle, originalPageTitle, api, resolve, logDetail) {
            api.get({
                action: 'parse',
                page: linkedPageTitle,
                prop: 'wikitext'
            }).then(function (data) {
                var wikitext = data.parse.wikitext['*'];
                var regex = new RegExp('\\[\\[' + originalPageTitle.replace(/[\[\]]/g, '\\$&') + '(\\|[^\\]]+)?\\]\\]', 'g');
                var newWikitext = wikitext.replace(regex, function(match, p1) {
                    return p1 ? p1.substring(1) : originalPageTitle;
                });
                if (wikitext !== newWikitext) {
                    logDetail('Removing link from ' + linkedPageTitle);
                } else {
                    logDetail('No link found in ' + linkedPageTitle);
                }
                return api.postWithToken('csrf', {
                    action: 'edit',
                    title: linkedPageTitle,
                    text: newWikitext,
                    summary: 'Removed link to [[' + originalPageTitle + ']]'
                }).then(function (response) {
                    console.log('API response from removing link: ', response);
                    if (response.edit && response.edit.result === 'Success' && !response.edit.nochange) {
                        logDetail('Successfully removed link from ' + linkedPageTitle);
                    } else {
                        logDetail('No changes made to link in ' + linkedPageTitle);
                    }
                    resolve();
                }).fail(function (error) {
                    logDetail('Error removing link from ' + linkedPageTitle + ': ' + error);
                    resolve();
                });
            }).fail(function (error) {
                logDetail('Error fetching page content for ' + linkedPageTitle + ': ' + error);
                resolve();
            });
        }

        function handleRedirects(next) {
            updateLoadingText('Fetching redirects...');
            api.get({
                action: 'query',
                titles: pageTitle,
                redirects: true
            }).done(function (data) {
                logDetail('Redirects fetched.');
                var redirects = data.query.pages[Object.keys(data.query.pages)[0]].redirects;
                if (redirects) {
                    var promises = $.map(redirects, function (redirect) {
                        return new $.Deferred(function (defer) {
                            logDetail('Found redirect: ' + redirect.title);
                            performDeletion(redirect.title, defer.resolve, logDetail);
                        }).promise();
                    });
                    $.when.apply($, promises).then(function () {
                        next();
                    });
                } else {
                    next();
                }
            }).fail(function (error) {
                updateLoadingText('Error fetching redirects: ' + error);
                next();
            });
        }

        function performDeletion(pageTitle, resolve, logDetail) {
            updateLoadingText('Deleting the page: ' + pageTitle);
            api.postWithToken('csrf', {
                action: 'delete',
                title: pageTitle,
                reason: 'Automated clean delete by admin'
            }).done(function (response) {
                console.log('API response from deleting page: ', response);
                if (response.delete && response.delete.title) {
                    logDetail('Successfully deleted page: ' + response.delete.title);
                } else {
                    logDetail('No deletion performed for ' + pageTitle);
                }
                resolve();
            }).fail(function (error) {
                logDetail('Error during clean deletion of ' + pageTitle + ': ' + error);
                resolve();
            });
        }

        var tasks = [
            function (resolve) {
                if (decisions.links === '') {
                    handleLinksAndRedirects('remove', '', resolve);
                } else {
                    handleLinksAndRedirects('update', decisions.links, resolve);
                }
            },
            function (resolve) {
                if (decisions.deletion) {
                    handleRedirects(resolve);
                } else {
                    resolve();
                }
            },
            function (resolve) {
                if (decisions.deletion) {
                    performDeletion(pageTitle, resolve, logDetail);
                } else {
                    resolve();
                }
            }
        ];

        (function executeTasks(i) {
            if (i < tasks.length) {
                tasks[i](function () {
                    executeTasks(i + 1);
                });
            } else {
                callback();
            }
        })(0);
    }

    mw.hook('wikipage.content').add(addCleanDeleteLink);
});
This page was edited 10 days ago on 07/08/2024. What links here