User:1F616EMO/MoveDiscussion.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ 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-button': {
hans: '移动',
hant: '移動'
},
'move-discussion-target-page': {
hans: '目标页面',
hant: '目標頁面'
},
'move-discussion-reason': {
hans: '移动理由',
hant: '移動理由'
},
'move-discussion-additional-reason': {
hans: '附加理由',
hant: '附加理由',
},
'move-discussion-confirm': {
hans: '目标页面:$1;移动理由:$2。确定?',
hant: '目標頁面$1;移動理由:$2。確定?'
},
'move-discussion-source-summary-no-reason': {
hans: '/* $1 */ 移动至[[$2]] // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]',
hant: '/* $1 */ 移動至[[$2]] // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]'
},
'move-discussion-source-summary-with-reason': {
hans: '/* $1 */ 移动至[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]',
hant: '/* $1 */ 移動至[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]'
},
'move-discussion-target-summary-no-reason': {
hans: '/* $1 */ 移动自[[$2]] // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]',
hant: '/* $1 */ 移動自[[$2]] // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]'
},
'move-discussion-target-summary-with-reason': {
hans: '/* $1 */ 移动自[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]',
hant: '/* $1 */ 移動自[[$2]]:$3 // [[U:1F616EMO/MoveDiscussion|MoveDiscussion]]'
},
}))
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,
MovedToNoReasonFormat: '{{移動至|1=$1|sign=~~~~ <small>[[U:1F616EMO/MoveDiscussion|MoveDiscussion]]</small>}}',
MovedToWithReasonFormat: '{{移動至|1=$1|sign=~~~~ <small>[[U:1F616EMO/MoveDiscussion|MoveDiscussion]]</small>|reason=$2}}',
MovedFromNoReasonFormat: '{{移動自|1=$1|sign=~~~~ <small>[[U:1F616EMO/MoveDiscussion|MoveDiscussion]]</small>}}',
MovedFromWithReasonFormat: '{{移動自|1=$1|sign=~~~~ <small>[[U:1F616EMO/MoveDiscussion|MoveDiscussion]]</small>|reason=$2}}',
};
window.MoveDiscussion.move = async function (sourcesection, targetpage, reason) {
const pageName = mw.config.get("wgPageName");
const pageID = mw.config.get("wgArticleId");
try {
// 1. Get the section content
const data = await api.get({
action: 'query',
prop: 'revisions',
pageids: pageID,
rvprop: 'ids|content',
rvlimit: 1,
rvslots: 'main',
rvsection: sourcesection,
});
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(/^== *(.*?) *== *$/);
// Handle case where title match fails
const sectiontitle = titleresult ? titleresult[1] : null;
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();
}
// 2. Edit the source page (remove content/leave link)
await api.postWithToken('csrf', {
action: 'edit',
pageid: pageID,
section: sourcesection,
text: titleline + "\n" + oldpagebody,
summary: oldpagesummary,
baserevid: revid,
nocreate: true,
});
// 3. Edit the target page (add content)
await api.postWithToken('csrf', {
action: 'edit',
title: targetpage,
section: 'new',
sectiontitle: sectiontitle,
text: newpagebody,
summary: newpagesummary,
});
// 4. Cleanup and Redirect
window.open(mw.util.getUrl(targetpage));
window.location.reload();
} catch (error) {
console.error("MoveDiscussion encountered an error:", error);
alert("An error occurred while moving the discussion. Please check the console for details.");
}
};
window.MoveDiscussion.askmove = async function (sourcesection) {
let 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;
let realReason = window.MoveDiscussion.presets && window.MoveDiscussion.presets[reason] || reason;
if (realReason != reason) {
const additionalReason = prompt(mw.message('move-discussion-additional-reason').plain());
if (additionalReason !== null && additionalReason.trim() !== '')
realReason += ',' + additionalReason.trim();
}
// Get real title of targetpage
const redirect_req_data = await api.get({
action: 'query',
titles: targetpage,
redirects: true,
converttitles: true,
format: 'json',
});
const data_pages_first = Object.values(redirect_req_data.query.pages)[0];
targetpage = data_pages_first.title || targetpage;
const data_redirects = redirect_req_data.redirects;
if (data_redirects && data_redirects[0].from === targetpage)
targetpage = data_redirects[0].to;
if (confirm(mw.message('move-discussion-confirm', targetpage, realReason || '').plain())) {
window.MoveDiscussion.move(sourcesection, targetpage.trim(), realReason ? realReason.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 editHref = editLink.attr("href");
const section = mw.util.getParamValue("section", editHref);
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>