跳转到内容

User:1F616EMO/MoveDiscussion.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
 * Discussion mover (zhwiki)
 * 
 * Use the following line to load this script:
 * mw.loader.load( "https://zh.wikipedia.org/w/index.php?title=User:1F616EMO/MoveDiscussion.js&action=raw&ctype=text/javascript" ); // [[:w:zh:U:1F616EMO/MoveDiscussion]]
 * 
 * This script is made by 1F616EMO on zhwiki, licensed under CC BY-SA 4.0.
 */
// <nowiki>

$.when(mw.loader.using([
    'mediawiki.api',
    'mediawiki.jqueryMsg',
    'oojs-ui-core',
    'oojs-ui-windows',
    'ext.gadget.HanAssist', // adoption note: zhwiki specific
]), $.ready).then((require) => {
    window.MoveDiscussion = window.MoveDiscussion || {};

    const api = new mw.Api();
    const { batchConv } = require('ext.gadget.HanAssist');

    mw.messages.set(batchConv({
        // 'move-discussion-window-title': {
        //     hans: '移动讨论',
        //     hant: '移動討論',
        // },
        'move-discussion-button': {
            hans: '移动',
            hant: '移動'
        },
        'move-discussion-target-page': {
            hans: '目标页面',
            hant: '目標頁面'
        },
        'move-discussion-reason': {
            hans: '移动理由',
            hant: '移動理由'
        },
        'move-discussion-confirm': {
            hans: '目标页面:$1;移动理由:$2。确定?',
            hant: '目標頁面$1;移動理由:$2。確定?'
        },

        'move-discussion-source-summary-no-reason': {
            hans: '/* $1 */ 移动至[[$2]] // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]',
            hant: '/* $1 */ 移動至[[$2]] // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]'
        },
        'move-discussion-source-summary-with-reason': {
            hans: '/* $1 */ 移动至[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]',
            hant: '/* $1 */ 移動至[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]'
        },

        'move-discussion-target-summary-no-reason': {
            hans: '/* $1 */ 移动自[[$2]] // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]',
            hant: '/* $1 */ 移動自[[$2]] // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]'
        },
        'move-discussion-target-summary-with-reason': {
            hans: '/* $1 */ 移动自[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]',
            hant: '/* $1 */ 移動自[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion.js|MoveDiscussion]]'
        },
        // 'move-discussion-rfc-bio': {
        //     hans: '传记',
        //     hant: '傳記'
        // },
        // 'move-discussion-rfc-econ': {
        //     hans: '经济、贸易与公司',
        //     hant: '經濟、貿易與公司'
        // },
        // 'move-discussion-rfc-hist': {
        //     hans: '历史与地理',
        //     hant: '歷史與地理'
        // },
        // 'move-discussion-rfc-lang': {
        //     hans: '语言与语言学',
        //     hant: '語言與語言學'
        // },
        // 'move-discussion-rfc-sci': {
        //     hans: '数学、科学与科技',
        //     hant: '數學、科學與科技'
        // },
        // 'move-discussion-rfc-media': {
        //     hans: '媒体、艺术与建筑',
        //     hant: '媒體、藝術與建築'
        // },
        // 'move-discussion-rfc-pol': {
        //     hans: '政治、政府与法律',
        //     hant: '政治、政府與法律'
        // },
        // 'move-discussion-rfc-reli': {
        //     hans: '宗教与哲学',
        //     hant: '宗教與哲學'
        // },
        // 'move-discussion-rfc-soc': {
        //     hans: '社会、体育运动与文化',
        //     hant: '社會、體育運動與文化'
        // },
        // 'move-discussion-rfc-style': {
        //     hans: '维基百科格式与命名',
        //     hant: '維基百科格式與命名'
        // },
        // 'move-discussion-rfc-policy': {
        //     hans: '维基百科方针与指引',
        //     hant: '維基百科方針與指引'
        // },
        // 'move-discussion-rfc-proj': {
        //     hans: '维基专题与协作',
        //     hant: '維基專題與協作'
        // },
        // 'move-discussion-rfc-tech': {
        //     hans: '维基百科技术议题与模板',
        //     hant: '維基百科技術議題與模板'
        // },
        // 'move-discussion-rfc-prop': {
        //     hans: '维基百科提议',
        //     hant: '維基百科提議'
        // }
    }))

    window.MoveDiscussion = window.MoveDiscussion || {}

    window.MoveDiscussion.settings = window.MoveDiscussion.settings || {
        ArchiveToTemplates: /^ *{{ *(?:[Tt](?:[Ee][Mm][Pp][Ll][Aa][Tt][Ee])?:)?(?:存[檔档][至|到]|[Ss]ave ?to|[Aa]rchive|[Nn]osave|[Aa]rchive ?to|保存至).*}} *\n/gm,
        // SavedFromTemplates: '^ *{{ *(?:[Tt](?:[Ee][Mm][Pp][Ll][Aa][Tt][Ee])?:)?(?:移[動动]自|[Mm]ovedfrom|[Ss]avedfrom|[Aa]rchievedfrom|[Mm]ove(?:d )?from|[Ss]avefrom|[Mm]OVEDFROM|[Ss]witchfrom|[Mm]oved discussion from) *(?:\| *([^\|]*)[^\|]*).*}} *$',
        // RfcTemplates: '^ *{{ *(?:[Rr]fc(?: subpage)?|[征徵]求意[见見]).*}} *$',

        MovedToNoReasonFormat: '{{移動至|1=$1|sign=~~~~}}',
        MovedToWithReasonFormat: '{{移動至|1=$1|sign=~~~~|reason=$2}}',

        MovedFromNoReasonFormat: '{{移動自|1=$1|sign=~~~~}}',
        MovedFromWithReasonFormat: '{{移動自|1=$1|sign=~~~~|reason=$2}}',
    };

    window.MoveDiscussion.move = function (sourcesection, targetpage, reason) {
        const pageName = mw.config.get("wgPageName");
        const pageID = mw.config.get("wgArticleId");
        api.get({
            action: 'query',
            prop: 'revisions',
            pageids: pageID,
            rvprop: 'ids|content',
            rvlimit: 1,
            rvslots: 'main',
            rvsection: sourcesection,
        }).done((data) => {
            const text = data['query']['pages'][pageID]['revisions'][0]['slots']['main']['*'];
            const revid = data['query']['pages'][pageID]['revisions'][0]['revid'];
            const lines = text.split("\n");
            const titleline = lines.shift();

            let body = lines.join("\n");
            body = body.replace(window.MoveDiscussion.settings.ArchiveToTemplates, '');

            const titleresult = titleline.match(/^== *(.*?) *== *$/);
            const sectiontitle = titleresult[1];

            if (!sectiontitle) {
                alert("MoveDiscussion failed: Title not found.");
                return;
            }

            let newpagebody, oldpagebody, newpagesummary, oldpagesummary;

            if (reason !== "") {
                newpagebody = window.MoveDiscussion.settings.MovedFromWithReasonFormat
                    .replace("$1", pageName)
                    .replace("$2", reason) + "\n" + body;
                oldpagebody = window.MoveDiscussion.settings.MovedToWithReasonFormat
                    .replace("$1", targetpage)
                    .replace("$2", reason);
                newpagesummary = mw.message('move-discussion-target-summary-with-reason', sectiontitle, pageName, reason).plain()
                oldpagesummary = mw.message('move-discussion-source-summary-with-reason', sectiontitle, targetpage, reason).plain()
            } else {
                newpagebody = window.MoveDiscussion.settings.MovedFromNoReasonFormat
                    .replace("$1", pageName) + "\n" + body;
                oldpagebody = window.MoveDiscussion.settings.MovedToNoReasonFormat
                    .replace("$1", targetpage);
                newpagesummary = mw.message('move-discussion-target-summary-no-reason', sectiontitle, pageName).plain()
                oldpagesummary = mw.message('move-discussion-source-summary-no-reason', sectiontitle, targetpage).plain()
            }

            api.postWithToken('csrf', {
                action: 'edit',
                pageid: pageID,
                section: sourcesection,
                text: titleline + "\n" + oldpagebody,
                summary: oldpagesummary,
                baserevid: revid,
                nocreate: true,
            }).done(() => {
                api.postWithToken('csrf', {
                    action: 'edit',
                    title: targetpage,
                    section: 'new',
                    sectiontitle: sectiontitle,
                    text: newpagebody,
                    summary: newpagesummary,
                }).done(() => {
                    window.open(mw.util.getUrl(targetpage));
                    window.location.reload();
                });
            });
        });
    };

    window.MoveDiscussion.askmove = function (sourcesection) {
        const targetpage = prompt(mw.message('move-discussion-target-page').plain());
        if (targetpage === null || targetpage.trim() === '')
            return;
        const reason = prompt(mw.message('move-discussion-reason').plain());
        if (reason === null)
            return;
        if (confirm(mw.message('move-discussion-confirm', targetpage, reason || '').plain())) {
            window.MoveDiscussion.move(sourcesection, targetpage.trim(), reason ? reason.trim() : '');
        }
    };

    mw.util.addCSS(`
        .mw-editsection .movediscussion-editsection-link-group::before {
            content: ' | ';
        }
    `);

    $(".mw-heading2 .mw-editsection").each(function () {
        const $this = $(this);
        const editLink = $this.find('a').first();

        const editURL = new URL(editLink.attr("href"));
        const urlParams = new URLSearchParams(editURL.search);

        if (urlParams.has("section")) {
            const section = parseInt(urlParams.get("section"));
            if (!section)
                return;
            $('<span>')
                .addClass("movediscussion-editsection-link-group")
                .append($("<a>")
                    .text(mw.message('move-discussion-button').plain())
                    .attr("href", "javascript:window.MoveDiscussion.askmove(" + section + ")"))
                .insertBefore($this.find(".mw-editsection-bracket").last());
        }
    });
});

// </nowiki>