Jump to content

User:Doğu/Adiutor-PRD.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* 
 * Adiutor: A gadget to assist various user actions
 * Author: Vikipolimer
 * Translation: Kadı
 * Licencing and attribution: [[WP:Adiutor#Licencing and attribution]]
 * Module: Proposed deletion
 */
/* <nowiki> */
$.when(mw.loader.using(["mediawiki.user", "oojs-ui-core", "oojs-ui-windows", ]), $.ready).then(function() {
    var mwConfig = mw.config.get(["wgAction", "wgPageName", "wgTitle", "wgUserGroups", "wgUserName", "wgCanonicalNamespace", "wgNamespaceNumber"]);
    var api = new mw.Api();
    var prdSendMessageToCreator = localStorage.getItem("prdSendMessageToCreator") == "true";
    var PRDText, PRDSummary;

    function ProposedDeletionDialog(config) {
        ProposedDeletionDialog.super.call(this, config);
    }
    OO.inheritClass(ProposedDeletionDialog, OO.ui.ProcessDialog);
    ProposedDeletionDialog.static.name = 'ProposedDeletionDialog';
    ProposedDeletionDialog.static.title = 'Adiutor (Beta) - Proposed deletion (PROD)';
    ProposedDeletionDialog.static.actions = [{
        action: 'save',
        label: 'Propose',
        flags: ['primary', 'progressive']
    }, {
        label: 'Cancel',
        flags: 'safe'
    }];
    ProposedDeletionDialog.prototype.initialize = function() {
        ProposedDeletionDialog.super.prototype.initialize.apply(this, arguments);
        var headerTitle = new OO.ui.MessageWidget({
            type: 'notice',
            inline: true,
            label: new OO.ui.HtmlSnippet('<strong>Proposed deletion (PROD)</strong><br><small>PROD process is to suggest that the item can  be deleted without a nomination page, although it does not meet the stricter criterias required for speedy deletion.</small>')
        });
        ProposeOptions = new OO.ui.FieldsetLayout({
            label: 'Type of proposed deletion'
        });
        ProposeOptions.addItems([
            new OO.ui.FieldLayout(new OO.ui.CheckboxInputWidget({
                selected: false,
                value: 'standardPropose'
            }), {
                label: 'PROD (Proposed deletion)',
                help: 'Proposing deletion in accordance with [[WP:PROD]]',
                align: 'inline'
            }),
            new OO.ui.FieldLayout(new OO.ui.CheckboxInputWidget({
                selected: false,
                value: 'LivingPersonPropose'
            }), {
                label: 'BLP PROD (unsourced biographies of living people\'s PROD process)',
                help: 'Proposed deletion of new and completely unsourced living human biographies in accordance with [[WP:BLP#Proposed_deletion_of_biographies_of_living_people]]',
                align: 'inline'
            }), rationaleField = new OO.ui.FieldLayout(rationaleInput = new OO.ui.MultilineTextInputWidget({
                placeholder: 'Rationale for proposed deletion',
                indicator: 'required',
                value: '',
            }), {
                label: 'Rationale',
                align: 'inline',
            }),
            new OO.ui.FieldLayout(new OO.ui.ToggleSwitchWidget({
                value: prdSendMessageToCreator,
                data: 'informCreator'
            }), {
                label: 'Notify the creator',
                align: 'top',
                help: 'If selected, a notification template will be placed on the talk page of the user who created the page.',
            })
        ]);
        rationaleInput.on('change', function() {
            if (rationaleInput.value != "") {
                InputFilled = false;
            } else {
                InputFilled = true;
            }
        });
        this.content = new OO.ui.PanelLayout({
            padded: true,
            expanded: false
        });
        this.content.$element.append(headerTitle.$element, '<br><hr><br>', ProposeOptions.$element);
        this.$body.append(this.content.$element);
    };
    ProposedDeletionDialog.prototype.getActionProcess = function(action) {
        var dialog = this;
        if (action) {
            return new OO.ui.Process(function() {
                var date = new Date();
                var Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                var PRDoptions = [];
                ProposeOptions.items.forEach(function(Option) {
                    if (Option.fieldWidget.selected) {
                        PRDoptions.push({
                            value: Option.fieldWidget.value,
                            selected: Option.fieldWidget.selected
                        });
                    }
                    if (Option.fieldWidget.value === true) {
                        PRDoptions.push({
                            value: Option.fieldWidget.value,
                            data: Option.fieldWidget.data
                        });
                    }
                });
                PRDoptions.forEach(function(Option) {
                    if (Option.value === "standardPropose") {
                        PRDText = '{{Proposed deletion/dated |concern = ' + rationaleInput.value + ' |timestamp = ' + new Date().valueOf() + ' |nom = ' + mwConfig.wgUserName + ' |help = off }}';
                        PRDSummary = 'Proposing article for deletion per [[WP:PROD]]';
                    }
                    if (Option.value === "LivingPersonPropose") {
                        PRDText = '{{Prod blp/dated | concern = | user = | timestamp = ' + new Date().valueOf() + ' | help = off }}';
                        PRDText = '{{subst:Proposed deletion notify|Living person biography with no references.|help=close}}';
                        PRDSummary = 'Proposing article for deletion per [[WP:BLPPROD]]';
                    }
                    if (Option.data === "informCreator") {
                        getCreator().then(function(data) {
                            var Author = data.query.pages[mw.config.get('wgArticleId')].revisions[0].user;
                            if (!mw.util.isIPAddress(Author)) {
                                var message = '{{subst:Proposed deletion notify|' + mwConfig.wgPageName.replace(/_/g, " ") + '|concern=' + rationaleInput.value + '|nowelcome=yes}}';
                                sendMessageToAuthor(Author, message);
                            }
                        });
                    }
                });
                putPRDTemplate(PRDText);
                dialog.close({
                    action: action
                });
            });
        }
        return ProposedDeletionDialog.super.prototype.getActionProcess.call(this, action);
    };
    var windowManager = new OO.ui.WindowManager();
    $(document.body).append(windowManager.$element);
    var dialog = new ProposedDeletionDialog();
    windowManager.addWindows([dialog]);
    windowManager.openWindow(dialog);

    function putPRDTemplate(PRDText) {
        api.postWithToken('csrf', {
            action: 'edit',
            title: mwConfig.wgPageName,
            prependtext: PRDText + "\n",
            summary: PRDSummary,
            //tags: 'Adiutor',
            format: 'json'
        }).done(function() {
            location.reload();
        });
    }

    function getCreator() {
        return api.get({
            action: 'query',
            prop: 'revisions',
            rvlimit: 1,
            rvprop: ['user'],
            rvdir: 'newer',
            titles: mwConfig.wgPageName
        });
    }

    function sendMessageToAuthor(Author, message) {
        api.postWithToken('csrf', {
            action: 'edit',
            title: 'User_talk:' + Author,
            appendtext: '\n' + message,
            summary: 'Notification: proposed deletion of [[' + mwConfig.wgPageName.replace(/_/g, " ") + ']].',
            tags: 'Adiutor',
            format: 'json'
        });
    }
});
/* </nowiki> */