User:WhitePhosphorus/js/massrd.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

(function($, mw) {
mw.loader.using(['jquery.ui', 'mediawiki.util'], function() {

'use strict';

// 不是用户贡献页或者看不到删除修订版本的链接(比如权限不够),直接返回
if (mw.config.get('wgCanonicalSpecialPageName') != 'Contributions' || !$('.mw-revdelundel-link').length) {
	return;
}

const rdReasons = {
	RD1: '[[Project:RD1|RD1]]:明显侵犯版权',
	RD2: '[[Project:RD2|RD2]]:针对个人、团体或组织的严重侮辱、贬低或攻击性材料',
	RD3: '[[Project:RD3|RD3]]:纯粹的扰乱性内容',
	RD4: '[[Project:RD4|RD4]]:非公开的私人信息',
	RD5: '[[Project:RD5|RD5]]:删除守则下的有效删除,使用RevisionDelete执行',
	RD6: '[[Project:RD6|RD6]]:版本删除校正',
};

let msg = {};
let dl = null;
let config = {'checkboxes': {}, 'others': {}};
// key 为页面名,value 为版本号的列表
let revs = {};

// 根据用户语言设定加载消息文字
switch (mw.config.get('wgUserLanguage')) {
	case 'zh-cn':
	case 'zh-hans':
	case 'zh-my':
	case 'zh-sg':
		msg = {
			err_no_revision_provided: '您没有选择需隐藏的版本!',
			err_no_item_provided: '您没有选择需隐藏的项目!',
			warn_no_reason_provided: '您没有输入任何理由!确定要继续吗?',
			show_hide_disc: '若不想改变某项目的可见性,则请勿勾选该项。',
			show_items: '需显示的项目:',
			hide_items: '需隐藏的项目:',
			hide_content: '编辑内容',
			hide_username: '编辑者用户名/IP地址',
			hide_summary: '编辑摘要',
			hide_reason: '理据:',
			hide_reason_rd1: '侵犯版权',
			hide_reason_rd2: '针对个人、团体或组织的严重侮辱、贬低或攻击性材料',
			hide_reason_rd3: '扰乱性内容',
			hide_reason_rd4: '非公开的私人信息',
			hide_reason_rd5: '删除守则里的有效删除,使用修订版本删除执行',
			hide_reason_rd6: '版本删除矫正',
			hide_reason_other: '仅使用下方的附加理由',
			other_reasons: '附加理由(可选)',
			dialog_title: '批量修订版本删除',
			dialog_button_submit: '删除',
			dialog_button_cancel: '取消'
		};
		break;
	default:
		msg = {
			err_no_revision_provided: '您沒有選擇需隱藏的版本!',
			err_no_item_provided: '您沒有選擇需隱藏的項目!',
			warn_no_reason_provided: '您沒有輸入任何理由!確定要繼續嗎?',
			show_hide_disc: '若不想改變某項目的可見性,則請勿勾選該項。',
			show_items: '需顯示的項目',
			hide_items: '需隱藏的項目:',
			hide_content: '編輯內容',
			hide_username: '編輯者用戶名/IP位址',
			hide_summary: '編輯摘要',
			hide_reason: '理據:',
			hide_reason_rd1: '侵犯版權',
			hide_reason_rd2: '針對個人、團體或組織的嚴重侮辱、貶低或攻擊性材料',
			hide_reason_rd3: '擾亂性內容',
			hide_reason_rd4: '非公開的私人信息',
			hide_reason_rd5: '刪除守則下的有效刪除,使用修訂版本刪除執行',
			hide_reason_rd6: '版本刪除校正',
			hide_reason_other: '僅使用下方的附加理由',
			other_reasons: '附加理由(可選)',
			dialog_title: '批量修訂版本刪除',
			dialog_button_submit: '刪除',
			dialog_button_cancel: '取消'
		};
}

// 插入复选框,name 的格式为`页面名[版本号]`,以便后续处理
$('.mw-contributions-list > li').each(function() {
	let revid = $(this).attr('data-mw-revid');
	let title = $('.mw-contributions-title', this).attr('title').replace(' ', '_');
	$('.mw-revdelundel-link', this).before(
		$(`<input name=${title}[${revid}] class="P4js-revdelundel-check" type="checkbox">`)
	);
});

// TODO: 重构对话框部分代码
// 再次打开对话框时加载用户上次填写的设定
const updateConfig = function() {
	config.checkboxes.rrdHideContent = $('#rrdHideContent').prop("checked");
	config.checkboxes.rrdHideUsername = $('#rrdHideUsername').prop("checked");
	config.checkboxes.rrdHideSummary = $('#rrdHideSummary').prop("checked");
	config.checkboxes.rrdShowContent = $('#rrdShowContent').prop("checked");
	config.checkboxes.rrdShowUsername = $('#rrdShowUsername').prop("checked");
	config.checkboxes.rrdShowSummary = $('#rrdShowSummary').prop("checked");
	config.others.rrdReason = $('#rrdReason').val();
	config.others.rrdOtherReasons = $('#rrdOtherReasons').val();
};

const loadConfig = function() {
	for (let k in config.others)
		if (config.others.hasOwnProperty(k))
			$('#' + k).val(config.others[k]);
	for (let k in config.checkboxes)
		if (config.checkboxes.hasOwnProperty(k))
			$('#' + k).prop('checked', config.checkboxes[k]);
};

// FIXME: 同一页面下版本数大于 50
const submit = function(toHide, toShow, reason, otherReasons) {
	for (let [title, ids] of Object.entries(revs)) {
		$.ajax({
			url: mw.util.wikiScript('api'),
			dataType: 'json',
			type: 'POST',
			data: {
				action: 'revisiondelete',
				type: 'revision',
				ids: ids.join('|'),
				hide: toHide,
				show: toShow,
				reason: reason + otherReasons,
				token: mw.user.tokens.get('csrfToken'),
				format: 'json'
			}
		}).done(function(data) {
			// TODO: 输出结果给用户
			console.log(data);
		}).fail(function(jqXHR, textStatus, errorThrown) {
			console.log('Error when deleting revision: ' + errorThrown);
			console.log(`title: ${title}; ids: ${ids}`);
		});
	}
};

const showDialog = function() {
	// 对话框主体
	let html =
		'<div id="rrdConfig">' +
		msg.hide_items + '<br />' +
		'<div style="float:left;padding:0 5px;">' +
		'<input name="content" id="rrdHideContent" type="checkbox" value="content">' +
		'<label for="rrdHideContent" id="rrd-content">' + msg.hide_content + '</label>' +
		'</div><div style="float:left;padding:0 5px;">' +
		'<input name="username" id="rrdHideUsername" type="checkbox" value="username">' +
		'<label for="rrdHideUsername" id="rrd-username">' + msg.hide_username + '</label>' +
		'</div><div style="float:left;padding:0 5px;">' +
		'<input name="summary" id="rrdHideSummary" type="checkbox" value="summary">' +
		'<label for="rrdHideSummary" id="rrd-summary">' + msg.hide_summary + '</label>' +
		'</div><br /><br />' + msg.show_items + '<br />' +
		'<div style="float:left;padding:0 5px;">' +
		'<input name="content" id="rrdShowContent" type="checkbox" value="content">' +
		'<label for="rrdShowContent" id="rrd-content-s">' + msg.hide_content + '</label>' +
		'</div><div style="float:left;padding:0 5px;">' +
		'<input name="username" id="rrdShowUsername" type="checkbox" value="username">' +
		'<label for="rrdShowUsername" id="rrd-username-s">' + msg.hide_username + '</label>' +
		'</div><div style="float:left;padding:0 5px;">' +
		'<input name="summary" id="rrdShowSummary" type="checkbox" value="summary">' +
		'<label for="rrdShowSummary" id="rrd-summary-s">' + msg.hide_summary + '</label>' +
		'</div><br /><br />' + msg.show_hide_disc + '<br /><br />' +
		msg.hide_reason + '<br />' +
		'<select name="rrdReason" id="rrdReason">' +
		'<option value=' + msg.hide_reason_rd1 + '>' + 'RD1:' + msg.hide_reason_rd1 + '</option>' +
		'<option value=' + msg.hide_reason_rd2 + '>' + 'RD2:' + msg.hide_reason_rd2 + '</option>' +
		'<option value=' + msg.hide_reason_rd3 + '>' + 'RD3:' + msg.hide_reason_rd3 + '</option>' +
		'<option value=' + msg.hide_reason_rd4 + '>' + 'RD4:' + msg.hide_reason_rd4 + '</option>' +
		'<option value=' + msg.hide_reason_rd5 + '>' + 'RD5:' + msg.hide_reason_rd5 + '</option>' +
		'<option value=' + msg.hide_reason_rd6 + '>' + 'RD6:' + msg.hide_reason_rd6 + '</option>' +
		'<option value="">' + msg.hide_reason_other + '</option>' +
		'</select>' +
		'<br /><br />' +
		msg.other_reasons + '<br />' +
		'<textarea name="otherReasons" id="rrdOtherReasons" rows=4></textarea>' +
		'</div>';
	// 已经初始化过,直接打开对话框并加载上次设定
	if (dl) {
		dl.html(html).dialog("open");
		loadConfig();
	} else {
		// 否则初始化
		dl = $(html).dialog({
			title: msg.dialog_title,
			minWidth: 515,
			minHeight: 150,
			close: updateConfig,
			buttons: [
				{
					text: msg.dialog_button_submit,
					click: function() {
						$(this).dialog('close');
						var reason = config.others.rrdReason;
						var otherReasons = config.others.rrdOtherReasons;
						if (otherReasons && reason) otherReasons = ',' + otherReasons;
						var toHide = [], toShow = [];
						if (config.checkboxes['rrdHideContent'])
							toHide.push('content');
						if (config.checkboxes['rrdHideUsername'])
							toHide.push('user');
						if (config.checkboxes['rrdHideSummary'])
							toHide.push('comment');
						if (config.checkboxes['rrdShowContent'])
							toShow.push('content');
						if (config.checkboxes['rrdShowUsername'])
							toShow.push('user');
						if (config.checkboxes['rrdShowSummary'])
							toShow.push('comment');
						var cont = true;
						if (toHide.length === 0 && toShow.length === 0) {
							alert(msg.err_no_item_provided);
							return null;
						}
						if (!reason && !otherReasons)
							cont = confirm(msg.warn_no_reason_provided);
						if (cont)
							submit(toHide.join('|'), toShow.join('|'), reason, otherReasons);
					}
				},
				{
					text: msg.dialog_button_cancel,
					click: function() { $(this).dialog('close'); }
				}
			]
		});
		$('#rrdHideContent').prop('checked', true);
	}
	for (let id of ['content', 'username', 'summary']) {
		$(`input:checkbox[name=${id}]`).click(function() {
			$(`input:checkbox[name=${id}]`).not(this).prop('checked', false);
		});
	}
};

$(mw.util.addPortletLink('p-cactions', '#', '批量隐藏修订版本')).click(function (e) {
	e.preventDefault();
	// 没有指定任何版本
	if (!$('.P4js-revdelundel-check:checked').length) {
		alert(msg.err_no_revision_provided);
		return;
	}
	// 将各版本号存入 revs 中,之后每次请求每个页面下的所有版本
	$('.P4js-revdelundel-check:checked').each(function() {
		let [, title, revid] = /(.*)\[(.*)\]/.exec($(this).attr('name'));
		revs[title] = revs[title] || [];
		revs[title].push(revid);
	});

	showDialog();
});

});
})(jQuery, mw);