MediaWiki:Gadget-RequestSolver.js

From Test Wiki
Revision as of 09:41, 22 September 2024 by Bhairava7 (talk | contribs) (Restored revision 51796 by Kiteretsu (Restorer))
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Forked from https://pl.wikipedia.org/w/index.php?title=Wikipedysta:AramilFeraxa/ReportSolver.js */
// <nowiki>
$(function () {
    var RS = {};
    const RSConfig = window.RSConfig || {
        allowedPages: []
    };
    window.RequestSolver = RS;
    var wgPageName = mw.config.get('wgPageName');
    RS.summary = ' - with [[User:Kiteretsu/RequestSolver|RequestSolver]]';
    var MSG = {
        dialogTitle: 'Close with a comment',
        dialogCancel: 'Cancel',
        dialogConfirm: 'Submit',
        dialogMessage: 'Comment:',
        dialogInfo: 'Template and signature will be inserted automatically.'
    };
    RS.setup = function () {
        if (wgPageName === 'Test Wiki:Request for permissions') {
            $('span.mw-editsection-bracket:first-child').each(function () {
                try {
                    if (this.parentElement.childNodes.length > 1 && this.parentElement.childNodes[1].href) {
                        sectionNumber = this.parentElement.childNodes[1].href.match(/action=edit&section=(\d+)/)[1];
                    }
                    this.after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-close" data-section=' + sectionNumber + '>Close discussion</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-edit-redirected" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-mark-redirected" data-section=' + sectionNumber + '>redirected</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-edit-kept" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-mark-kept" data-section=' + sectionNumber + '>kept</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="-edit-deleted" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="-mark-deleted" data-section=' + sectionNumber + '>Mark as deleted</a>'));
                } catch (e) {
                }
            });
        }
        else if ((wgPageName === 'Test Wiki:Request for permissions') || wgPageName.startsWith('Test Wiki:Request for permissions/') || RSConfig.allowedPages.includes(wgPageName) || [1, 4, 5, 15, 11, 9].includes(mw.config.get('wgNamespaceNumber'))) {
            $('span.mw-editsection-bracket:first-child').each(function () {
                try {
                    if (this.parentElement.childNodes.length > 1 && this.parentElement.childNodes[1].href) {
                        sectionNumber = this.parentElement.childNodes[1].href.match(/action=edit&section=(\d+)/)[1];
                    }
                    this.after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-close" data-section=' + sectionNumber + '>Close discussion</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-edit-stale" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-mark-stale" data-section=' + sectionNumber + '>Withdraw</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-edit-on-hold" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-mark-on-hold" data-section=' + sectionNumber + '>on hold</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-edit-already-done" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-mark-already-done" data-section=' + sectionNumber + '>already done</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-edit-rejected" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-mark-rejected" data-section=' + sectionNumber + '>not done</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-edit-done" data-section="' + sectionNumber + '"> (C)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="RequestSolver-mark-done" data-section=' + sectionNumber + '>Mark as done</a>'));
                } catch (e) {

                }
            });
        }

        $('a.RequestSolver-mark-done').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{done}}', 'Marked as done', 'done');
        });

        $('a.RequestSolver-mark-rejected').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{not done}}', 'Marked as not done', 'not done');
        });

        $('a.RequestSolver-mark-already-done').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{already done}}', 'Marked as already done', 'already done');
        });

        $('a.RequestSolver-mark-on-hold').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{on hold}}', 'Marked as on hold', 'onhold');
        });

        $('a.RequestSolver-mark-stale').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{Withdraw}}', 'Marked as withdraw', 'Withdraw');
        });

        $('a.RequestSolver-close').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{section resolved|1=~~~~}}', 'Closed');
        });

        $('a.RequestSolver-mark-deleted').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{Icon|delete|Deleted}}', 'Deleted');
        });

        $('a.RequestSolver-mark-kept').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{Icon|keep|Kept}}', 'Kept');
        });

        $('a.RequestSolver-mark-redirected').click(function (e) {
            var sectionNumber = $(this).data('section');
            $(this).text("Processing...");
            RS.doEdit(sectionNumber, '{{Icon|redirect|Redirected}}', 'Redirected');
        });

        RS.handleEditButtonClick();
    };

    RS.handleEditButtonClick = function () {
        $(document).off('click', '[class^="ReportSolver-edit-"]').on('click', '[class^="ReportSolver-edit-"]', function (e) {
            var sectionNumber = $(this).data('section');
			var $editSectionLink = $('span.mw-editsection a[href$="&section=' + sectionNumber + '"]');
			var $mwEditSection = $editSectionLink.closest('.mw-editsection');
			var $headline = $mwEditSection.prevAll('.mw-headline').first();
			
			if ($headline.length === 0) {
			    $headline = $mwEditSection.prevAll('h1, h2, h3, h4, h5, h6').first();
			}
    		var sectionTitle = $headline.attr('id').replace(/_/g, ' ');
            var pageTitle = mw.config.get('wgPageName');
            var editSummary, template;

            var classList = $(this).attr('class').split(/\s+/);
            for (var i = 0; i < classList.length; i++) {
                if (classList[i].startsWith('RequestSolver-edit-')) {
                    var action = classList[i].substring('RequestSolver-edit-'.length);
                    if (action === 'done') {
                        template = '{{done}}. ';
                        editSummary = 'Marked as done';
                        statusTemplate = 'done';
                    } else if (action === 'rejected') {
                        template = '{{not done}}. ';
                        editSummary = 'Marked as not done';
                        statusTemplate = 'not done';
                    } else if (action === 'already-done') {
                        template = '{{already done}}. ';
                        editSummary = 'Marked as already done';
                        statusTemplate = 'already done';
                   } else if (action === 'on-hold') {
                        template = '{{on hold}} ';
                        editSummary = 'Marked as on hold';
                        statusTemplate = 'on hold';
                   
                    }
                    break;
                }
            }

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

            RequestSolverDialog.static.name = "RequestSolverDialog";
            RequestSolverDialog.static.title = MSG.dialogTitle;
            RequestSolverDialog.static.actions = [
                {
                    label: MSG.dialogCancel,
                    flags: "safe",
                },
                {
                    action: 'submit',
                    label: MSG.dialogConfirm,
                    flags: ["primary", "progressive"]
                }
            ];

            RequestSolverDialog.prototype.initialize = function () {
                RequestSolverDialog.super.prototype.initialize.call(this);

                var rootPanel = new OO.ui.PanelLayout({
                    padded: true,
                    expanded: false,
                });

                var commentInput = new OO.ui.MultilineTextInputWidget({
                    rows: 5,
                    value: '',
                    placeholder: 'Enter a comment...'
                });

                var selectedOptionLabel = new OO.ui.LabelWidget({
                    label: "Chosen option: ",
                    classes: ['centered-label']
                });

                var editSummaryElement = $("<span>", {
                    html: editSummary,
                    class: "bold-text"
                });

                var infoElement = $("<span>", {
                    html: MSG.dialogInfo,
                    class: "red-text"
                });

                rootPanel.$element.append(commentInput.$element, infoElement, selectedOptionLabel.$element, editSummaryElement);
                this.$body.append(rootPanel.$element);

                mw.util.addCSS(".centered-label { display: flex; align-items: center; justify-content: center; margin: 10px; }");
                mw.util.addCSS(".bold-text { display: flex; align-items: center; justify-content: center; font-weight: bold; margin: 10px }");
                mw.util.addCSS(".red-text { display: flex; color: red; align-items: center; justify-content: center; font-weight: bold; margin: 10px }");
            };

            RequestSolverDialog.prototype.getActionProcess = function (action) {
                var dialog = this;
                if (action === 'submit') {
                    var comment = this.$body.find('textarea').val();
                    var wikitext = template + comment;
                    RS.doEdit(sectionNumber, wikitext, editSummary, statusTemplate);
                    dialog.close();
                }
                return ReportSolverDialog.super.prototype.getActionProcess.call(this, action);
            };

            var reportSolverDialog = new RequestSolverDialog({
                size: "large",
            });

            var windowManager = new OO.ui.WindowManager();
            $(document.body).append(windowManager.$element);
            windowManager.addWindows([requestSolverDialog]);
            windowManager.openWindow(requestSolverDialog);
        });
    };

    RS.doEdit = function (sectionNumber, comment, editSummary, status) {
	var $editSectionLink = $('span.mw-editsection a[href$="&section=' + sectionNumber + '"]');
	var $mwEditSection = $editSectionLink.closest('.mw-editsection');
	var $headline = $mwEditSection.prevAll('.mw-headline').first();
	
	if ($headline.length === 0) {
	    $headline = $mwEditSection.prevAll('h1, h2, h3, h4, h5, h6').first();
	}
    var sectionTitle = $headline.attr('id').replace(/_/g, ' ');
    var pageTitle = mw.config.get('wgPageName');
        if (editSummary === 'Closed') {
            new mw.Api().postWithEditToken({
                action: 'parse',
                page: pageTitle,
                prop: 'wikitext',
                section: sectionNumber
            }).done(function (result) {
                var wikitext = result.parse.wikitext['*'];
                wikitext = wikitext + '\n' + comment;
                new mw.Api().postWithEditToken({
                    action: 'edit',
                    title: pageTitle,
                    section: sectionNumber,
                    text: wikitext,
                    summary: '/* ' + sectionTitle + ' */ ' + editSummary + RS.summary,
                    minor: true,
                    nocreate: true
                }).done(function (result) {
                    if (result && result.edit && result.edit.result && result.edit.result === 'Success') {
                        location.reload();
                    }
                });
            });
        } else if (editSummary === 'Done' || editSummary === 'Not done' || editSummary === 'Redirected') {
            new mw.Api().get({
                action: 'parse',
                page: pageTitle,
                prop: 'wikitext',
                section: sectionNumber
            }).done(function (result) {
                var wikitext = result.parse.wikitext['*'];
                comment = comment.trim();
				if (!comment.endsWith(".") && !comment.endsWith("!") && !comment.endsWith("?")) {
					comment += ".";
				}
                comment += ' ~~~~';
                wikitext = wikitext + '\n' + '----' + '\n' + comment;
                new mw.Api().postWithEditToken({
                    action: 'edit',
                    title: pageTitle,
                    section: sectionNumber,
                    text: wikitext,
                    summary: '/* ' + sectionTitle + ' */ ' + editSummary + RS.summary,
                    minor: true,
                    nocreate: true
                }).done(function (result) {
                    if (result && result.edit && result.edit.result && result.edit.result === 'Success') {
                        location.reload();
                    }
                });
            });
        }
        else {
            new mw.Api().get({
                action: 'parse',
                page: pageTitle,
                prop: 'wikitext',
                section: sectionNumber
            }).done(function (result) {
                var wikitext = result.parse.wikitext['*'];
                wikitext = wikitext.replace(/\{\{\s*Status\s*\|\s*[^\|\}]*\s*\}\}/g, '{{Status|' + status + '}}');
                var isCloseAction = (editSummary === 'Closed');
                comment = comment.trim();
                if (!comment.endsWith('.')) {
                    comment += '.';
                }
                if (!isCloseAction) {
                    comment += ' ~~~~';
                }
                var isSrRequestSection = wikitext.includes('{{userlinks') || wikitext.includes('{{SRUC') || wikitext.includes('{{interwiki request');
                if (isSrRequestSection) {
                    wikitext = wikitext.replace(/\|\s*status\s*=\s*[^\|]*\|/i, '|status = ' + status + '\n |');
                }
                wikitext = wikitext + '\n:' + comment;
                new mw.Api().postWithEditToken({
                    action: 'edit',
                    title: pageTitle,
                    section: sectionNumber,
                    text: wikitext,
                    summary: '/* ' + sectionTitle + ' */ ' + editSummary + RS.summary,
                    minor: true,
                    nocreate: true
                }).done(function (result) {
                    if (result && result.edit && result.edit.result && result.edit.result === 'Success') {
                        location.reload();
                    }
                });
            });
        }
    };
});

mw.loader.using('mediawiki.api', function () {
    $(document).ready(function () {
        window.RequestSolver.setup();
    });
});
// </nowiki>