User:Hamish/unblock-zh-comb.js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// <nowiki>
/* globals UnblockZh:true */
/**
* 使用方法:
* 1. 前往[[Special:Userrights/使用者名稱]]
* 2. 在右上角「更多」選單中選取「處理Unblock-zh的IPBE請求」/「處理Unblock-zh的註冊請求」
* 3. 按照提示操作
* 原作者:User:Xiplus
*/
(function() {
if (typeof (UnblockZh) == 'undefined')
UnblockZh = {};
if (typeof (UnblockZh.enableOn) != 'function') {
UnblockZh.enableOn = function() {
if (mw.config.get('wgCanonicalSpecialPageName') == 'Userrights') {
return true;
}
return false;
}
}
if (typeof (UnblockZh.summarySuffix) != 'string') {
UnblockZh.summarySuffix = ' via [[User:Hamish/unblock-zh-comb.js|unblock-zh-comb]]';
}
if (typeof (UnblockZh.duration) != 'object') {
UnblockZh.duration = ['3 months', '6 months'];
}
UnblockZh.duration.push('infinite');
function AskUnblockZhUrl() {
var url;
var msgprefix = '';
while (true) { /* eslint-disable-line no-constant-condition */
url = prompt(msgprefix + '請輸入 unblock-zh 郵件網址\n形如 https://lists.wikimedia.org/mailman/private/unblock-zh/2018-November/012345.html');
if (url === null) {
return false;
}
var m;
if ((m = url.match(/(https?:\/\/lists\.wikimedia\.org\/mailman\/private\/(unblock-zh\/\d+-.+\/\d+)\.html)/)) !== null) {
return { 'long': m[1], 'short': m[2] };
}
msgprefix = '格式錯誤!請重新輸入\n';
}
}
function AskDuration() {
var duration;
var msgprefix = '';
var msg = '授權期限?';
for (var i = 0; i < UnblockZh.duration.length; i++) {
msg += '\n' + (i + 1) + '. ' + UnblockZh.duration[i];
}
while (true) { /* eslint-disable-line no-constant-condition */
duration = prompt(msgprefix + msg);
if (duration === null) {
return false;
}
duration = duration.trim() - 1;
if (UnblockZh.duration[duration] !== undefined) {
return UnblockZh.duration[duration];
}
msgprefix = '輸入不在列表內,請重新輸入';
}
}
async function RegisterAccount(username, email, urllong) {
var isRegistered = false;
var params = {
action: 'query',
meta: 'tokens',
type: 'createaccount',
format: 'json'
},
api = new mw.Api();
await api.get(params).done( function (data) {
var token = data.query.tokens.createaccounttoken,
params1 = {
action: 'createaccount',
username: username,
mailpassword: "true",
email: email,
reason: urllong,
createreturnurl: 'http:' + mw.config.get( 'wgServer' ),
createtoken: token,
format: 'json'
}
api.post(params1).done(function fun(data) {
if (data['createaccount']['status'] === 'FAIL') {
mw.notify('錯誤:' + data['createaccount']['message']);
} else {
mw.notify('成功建立賬戶 ' + username + '並以電子郵件通知密碼至 ' + email);
isRegistered = true;
}
}
);
});
return isRegistered;
}
function GrantIpbe(username, duration, urlshort) {
new mw.Api().postWithToken('userrights', {
action: 'userrights',
user: username,
add: 'ipblock-exempt',
expiry: duration,
reason: urlshort + UnblockZh.summarySuffix
}).then(function() {
mw.notify('成功授予 ' + username + ' IPBE ' + duration);
}, function(e) {
mw.notify('未知錯誤:' + e);
});
}
function NotifyUser(username, duration) {
var message = '{{subst:Ipexemptgranted|temp=1}}';
if (duration == 'infinite') {
message = '{{subst:Ipexemptgranted}}';
}
var usertalk = 'User talk:' + username;
new mw.Api().get({
action: 'query',
prop: 'info',
titles: usertalk
}).done(function(data) {
var page = Object.values(data.query.pages)[0];
if (page.missing !== undefined) {
new mw.Api().create(
usertalk,
{ summary: '授予IP封禁例外權通知' + UnblockZh.summarySuffix },
message
).then(function() {
mw.notify('成功發送通知給 ' + username);
}, function(e) {
mw.notify('未知錯誤:' + e);
});
} else if (page.contentmodel == 'flow-board') {
new mw.Api().postWithEditToken({
action: 'flow',
page: usertalk,
submodule: 'new-topic',
nttopic: '授予IP封禁例外權通知',
ntcontent: message,
ntformat: 'wikitext',
}).then(function() {
mw.notify('成功發送通知給 ' + username);
}, function(e) {
mw.notify('未知錯誤:' + e);
});
} else {
new mw.Api().edit(usertalk, function(revision) {
return {
text: (revision.content + '\n\n' + message).trim(),
summary: '授予IP封禁例外權通知' + UnblockZh.summarySuffix
};
}).then(function() {
mw.notify('成功發送通知給 ' + username);
}, function(e) {
mw.notify('未知錯誤:' + e);
});
}
});
}
function Report(username, urllong) {
new mw.Api().edit('Wikipedia:權限申請/申請IP封禁例外權', function(revision) {
return {
text: revision.content + '\n\n{{subst:rfp|' + username + '|2=[' + urllong + ' unblock-zh]|status=+}}--~~~~',
summary: '授予 ' + username + ' IP封禁例外權備案' + UnblockZh.summarySuffix
};
}).then(function() {
mw.notify('成功為 ' + username + ' 備案');
}, function(e) {
mw.notify('未知錯誤:' + e);
});
}
if (UnblockZh.enableOn()) {
var clickLinkIpbe = mw.util.addPortletLink(
'p-cactions',
'#',
'處理Unblock-zh的IPBE請求'
);
var clickLinkRegister = mw.util.addPortletLink(
'p-cactions',
'#',
'處理Unblock-zh的注册請求'
);
$(clickLinkIpbe).on('click', function() {
var user = mw.config.get('wgRelevantUserName');
if (user === null) {
user = prompt('要處理的用戶?');
if (user === null) {
mw.notify('動作已取消');
return;
}
user = user.trim();
if (user == '') {
mw.notify('動作已取消');
return;
}
}
var action = prompt('要處理的動作?\n1. 授權\n2. 發送通知\n3. 在WP:RFIPBE備案\n若要全部執行請輸入 123', '123');
if (action === null) {
mw.notify('動作已取消');
return;
}
var act1 = (action.indexOf('1') !== -1)
, act2 = (action.indexOf('2') !== -1)
, act3 = (action.indexOf('3') !== -1);
if (act1 || act3) {
var url = AskUnblockZhUrl();
if (url === false) {
mw.notify('動作已取消');
return;
}
}
if (act1) {
var duration = AskDuration();
if (duration === false) {
mw.notify('動作已取消');
return;
}
} else if (act2) {
var duration = confirm('是永久權限嗎?');
if (duration) {
duration = 'infinite';
} else {
duration = 'not infinite';
}
}
if (action.indexOf('1') !== -1) {
GrantIpbe(user, duration, url.short);
}
if (action.indexOf('2') !== -1) {
NotifyUser(user, duration);
}
if (action.indexOf('3') !== -1) {
Report(user, url.long);
}
});
$(clickLinkRegister).on('click', function() {
var user = prompt('要注册的用戶名?');
if (user === null) {
mw.notify('動作已取消');
return;
}
user = user.trim();
if (user === '') {
mw.notify('動作已取消');
return;
}
var email = prompt('將隨機密碼傳送至哪個地址?')
if (email === null) {
mw.notify('動作已取消');
return;
}
email = email.trim();
if (email === '') {
mw.notify('動作已取消');
return;
}
var url = AskUnblockZhUrl();
if (url === false) {
mw.notify('動作已取消');
return;
}
var action = prompt('其餘要處理的動作?\n1. 授予IP封鎖豁免權限\n2. 發送通知\n3. 在WP:RFIPBE備案\n若要全部執行請輸入 123', '123');
var isreg = RegisterAccount(user, email, url.long)
if (!isreg && action !== null) {
mw.notify('授權作業已中止');
return;
}
var act1 = (action.indexOf('1') !== -1)
, act2 = (action.indexOf('2') !== -1)
, act3 = (action.indexOf('3') !== -1);
if (act1 || act3) {
if (url === null) {
url = AskUnblockZhUrl();
if (url === false) {
mw.notify('動作已取消');
return;
}
}
}
if (act1) {
var duration = AskDuration();
if (duration === false) {
mw.notify('動作已取消');
return;
}
} else if (act2) {
var duration = confirm('是永久權限嗎?');
if (duration) {
duration = 'infinite';
} else {
duration = 'not infinite';
}
}
if (action.indexOf('1') !== -1) {
GrantIpbe(user, duration, url.short);
}
if (action.indexOf('2') !== -1) {
NotifyUser(user, duration);
}
if (action.indexOf('3') !== -1) {
Report(user, url.long);
}
});
}
})();
// </nowiki>