বিষয়বস্তুতে চলুন

ব্যবহারকারী:Yahya/common.js

উইকিবই থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন।
  • অপেরা: Ctrl-F5 টিপুন।
$(function () {
    "use strict";

    function isGlobalSysopWiki() {
        var api = new mw.Api();
        var deferred = $.Deferred();

        api.get({
            action: 'query',
            list: 'wikisets',
            wsfrom: 'Opted-out of global sysop wikis',
            wsprop: 'wikisincluded',
            wslimit: 1
        }).done(function (data) {
            var isGSWiki = false;
            var wikisincluded = data.query.wikisets[0].wikisincluded;
            var wiki;

            for (wiki in wikisincluded) {
                if (wikisincluded[wiki] === mw.config.get('wgDBname')) {
                    isGSWiki = true;
                    break;
                }
            }

            deferred.resolve(isGSWiki);
        }).fail(function () {
            deferred.reject(false);
        });

        return deferred.promise();
    }

    function addToToolbar() {
        // Add Block link
        var blockLink = mw.util.addPortletLink(
            'p-tb',
            '#',
            'Block',
            't-block-action',
            'Click to block a user'
        );
        $(blockLink).click(function (e) {
            e.preventDefault();
            openAdminActionsDialog('block');
        });

        // Add Delete link
        var deleteLink = mw.util.addPortletLink(
            'p-tb',
            '#',
            'Delete',
            't-delete-action',
            'Click to delete a page'
        );
        $(deleteLink).click(function (e) {
            e.preventDefault();
            openAdminActionsDialog('delete');
        });

        // Add Protect link
        var protectLink = mw.util.addPortletLink(
            'p-tb',
            '#',
            'Protect',
            't-protect-action',
            'Click to protect a page'
        );
        $(protectLink).click(function (e) {
            e.preventDefault();
            openAdminActionsDialog('protect');
        });
    }

    const reasons = {
        delete: [
            "Spam",
            "Vandalism",
            "No useful content",
            "Nonsense",
            "Test page",
            "Not written in this project's language",
            "Requested by the author",
            "Outside of this project's scope",
            "Empty",
            "Broken redirect"
        ],
        protect: [
            "Persistent vandalism",
            "Persistent spamming",
            "Persistent copyright violation"
        ],
        block: [
            "Spam",
            "Vandalism",
            "Intimidating behaviour/harassment",
            "Cross-wiki vandalism",
            "Spambot",
            "Spam-only account",
            "Open proxy"
        ]
    };

    const protectionLevels = {
        sysop: 'Sysop',
        autoconfirmed: 'Autoconfirmed',
    };

    const protectionDurations = {
        'infinite': 'Infinite',
        '24 hours': '24 hours',
        '1 week': '1 week',
        '1 month': '1 month',
        '3 month': '3 month',
        'custom': 'Custom'
    };

    const blockDurations = {
        '24 hours': '24 hours',
        '1 week': '1 week',
        '1 month': '1 month',
        '3 month': '3 month',
        'indefinite': 'indefinite',
        'custom': 'Custom'
    };

    function AdminActionsDialog(config) {
        AdminActionsDialog.super.call(this, config);
    }
    OO.inheritClass(AdminActionsDialog, OO.ui.ProcessDialog);

    AdminActionsDialog.static.name = 'adminActionsDialog';
    AdminActionsDialog.static.title = 'Admin Actions';
    AdminActionsDialog.static.actions = [
        { action: 'submit', label: 'Submit', flags: ['primary', 'progressive'] },
        { label: 'Cancel', flags: 'safe' }
    ];

    AdminActionsDialog.prototype.initialize = function () {
        AdminActionsDialog.super.prototype.initialize.apply(this, arguments);
        var fieldset = new OO.ui.FieldsetLayout({
            label: 'Choose an action and reason'
        });

        var reasonSelectField = new OO.ui.DropdownWidget({
            disabled: false
        });

        var protectionLevelField = new OO.ui.DropdownWidget({
            disabled: true
        });

        var protectionDurationField = new OO.ui.DropdownWidget({
            disabled: true
        });

        var blockDurationField = new OO.ui.DropdownWidget({
            disabled: true
        });

        var customProtectionLevelInput = new OO.ui.TextInputWidget({
            disabled: true
        });

        var customDurationInput = new OO.ui.TextInputWidget({
            disabled: true
        });

        fieldset.addItems([
            reasonSelectField,
            protectionLevelField,
            customProtectionLevelInput,
            protectionDurationField,
            blockDurationField,
            customDurationInput
        ]);
        
        this.content = new OO.ui.PanelLayout({
            padded: true,
            expanded: false
        });
        this.content.$element.append(fieldset.$element);
        this.$body.append(this.content.$element);
        this.reasonSelectField = reasonSelectField;
        this.protectionLevelField = protectionLevelField;
        this.protectionDurationField = protectionDurationField;
        this.blockDurationField = blockDurationField;
        this.customProtectionLevelInput = customProtectionLevelInput;
        this.customDurationInput = customDurationInput;
    };

    AdminActionsDialog.prototype.getActionProcess = function (action) {
        if (action === 'submit') {
            var selectedReason = this.reasonSelectField.getMenu().findSelectedItem().getData();
            var selectedProtectionLevel = this.protectionLevelField.getMenu().findSelectedItem()?.getData();
            var selectedProtectionDuration = this.protectionDurationField.getMenu().findSelectedItem()?.getData();
            var selectedBlockDuration = this.blockDurationField.getMenu().findSelectedItem()?.getData();
            var customProtectionLevel = this.customProtectionLevelInput.getValue();
            var customDuration = this.customDurationInput.getValue();

            var protectionLevel = selectedProtectionLevel === 'custom' ? customProtectionLevel : selectedProtectionLevel;
            var protectionDuration = selectedProtectionDuration === 'custom' ? customDuration : selectedProtectionDuration;
            var blockDuration = selectedBlockDuration === 'custom' ? customDuration : selectedBlockDuration;

            return new OO.ui.Process(function () {
                performAction(this.actionType, selectedReason, protectionLevel, protectionDuration, blockDuration);
                this.close();
            }, this);
        }
        return AdminActionsDialog.super.prototype.getActionProcess.call(this, action);
    };

    function performAction(action, reason, protectionLevel, protectionDuration, blockDuration) {
        var api = new mw.Api();
        var title = mw.config.get('wgPageName');

        if (action === 'delete') {
            api.postWithToken('csrf', {
                action: 'delete',
                title: title,
                reason: reason
            }).done(function () {
                mw.notify('Page deleted for reason: ' + reason, { type: 'success' });
            }).fail(function (code, error) {
                mw.notify('Failed to delete page: ' + error.info, { type: 'error' });
            });
        } else if (action === 'protect') {
            var protections = 'edit=' + protectionLevel + '|move=' + protectionLevel;
            api.postWithToken('csrf', {
                action: 'protect',
                title: title,
                protections: protections,
                expiry: protectionDuration,
                reason: reason
            }).done(function (data) {
                if (data && data.protect) {
                    mw.notify('Page protected for reason: ' + reason, { type: 'success' });
                } else {
                    mw.notify('Unexpected response from API. Protection may have failed.', { type: 'warn' });
                    console.log('API response:', data);
                }
            }).fail(function (code, error) {
                mw.notify('Failed to protect page: ' + error.info, { type: 'error' });
                console.error('Protection error:', code, error);
            });
        } else if (action === 'block') {
            api.postWithToken('csrf', {
                action: 'block',
                user: title.replace(/User:/, ''),
                expiry: blockDuration,
                reason: reason,
                autoblock: true
            }).done(function () {
                mw.notify('User blocked for reason: ' + reason, { type: 'success' });
            }).fail(function (code, error) {
                mw.notify('Failed to block user: ' + error.info, { type: 'error' });
            });
        }
    }

    function openAdminActionsDialog(actionType) {
        var windowManager = new OO.ui.WindowManager();
        $('body').append(windowManager.$element);
        var dialog = new AdminActionsDialog();
        dialog.actionType = actionType;

        // Pre-select reasons and options based on the action type
        dialog.reasonSelectField.getMenu().addItems(reasons[actionType].map(function (reason) {
            return new OO.ui.MenuOptionWidget({ data: reason, label: reason });
        }));

        if (actionType === 'protect') {
            dialog.protectionLevelField.setDisabled(false);
            dialog.protectionLevelField.getMenu().addItems(Object.keys(protectionLevels).map(function (level) {
                return new OO.ui.MenuOptionWidget({ data: level, label: protectionLevels[level] });
            }));
            dialog.protectionDurationField.setDisabled(false);
            dialog.protectionDurationField.getMenu().addItems(Object.keys(protectionDurations).map(function (duration) {
                return new OO.ui.MenuOptionWidget({ data: duration, label: protectionDurations[duration] });
            }));
        } else if (actionType === 'block') {
            dialog.blockDurationField.setDisabled(false);
            dialog.blockDurationField.getMenu().addItems(Object.keys(blockDurations).map(function (duration) {
                return new OO.ui.MenuOptionWidget({ data: duration, label: blockDurations[duration] });
            }));
        }

        windowManager.addWindows([dialog]);
        windowManager.openWindow(dialog);
    }

    mw.loader.using('oojs-ui-windows').then(function () {
        isGlobalSysopWiki().then(function (isGSWiki) {
            if (isGSWiki) {
                addToToolbar();
            } else {
                console.log('This script is only meant to run on global sysop wikis.');
            }
        });
    });
});