forked from wikimedia-gadgets/twinkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twinklebatchprotect.js
342 lines (301 loc) · 10.2 KB
/
twinklebatchprotect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// <nowiki>
(function($) {
/*
****************************************
*** twinklebatchprotect.js: Batch protect module (sysops only)
****************************************
* Mode of invocation: Tab ("P-batch")
* Active on: Existing project pages and user pages; existing and
* non-existing categories; Special:PrefixIndex
*/
Twinkle.batchprotect = function twinklebatchprotect() {
if (Morebits.userIsSysop && ((mw.config.get('wgArticleId') > 0 && (mw.config.get('wgNamespaceNumber') === 2 ||
mw.config.get('wgNamespaceNumber') === 4)) || mw.config.get('wgNamespaceNumber') === 14 ||
mw.config.get('wgCanonicalSpecialPageName') === 'Prefixindex')) {
Twinkle.addPortletLink(Twinkle.batchprotect.callback, 'P-batch', 'tw-pbatch', 'Protect pages linked from this page');
}
};
Twinkle.batchprotect.unlinkCache = {};
Twinkle.batchprotect.callback = function twinklebatchprotectCallback() {
const Window = new Morebits.simpleWindow(600, 400);
Window.setTitle('Batch protection');
Window.setScriptName('Twinkle');
Window.addFooterLink('Protection policy', 'WP:PROT');
Window.addFooterLink('Twinkle help', 'WP:TW/DOC#protect');
Window.addFooterLink('Give feedback', 'WT:TW');
const form = new Morebits.quickForm(Twinkle.batchprotect.callback.evaluate);
form.append({
type: 'checkbox',
event: Twinkle.protect.formevents.editmodify,
list: [
{
label: 'Modify edit protection',
value: 'editmodify',
name: 'editmodify',
tooltip: 'Only for existing pages.',
checked: true
}
]
});
form.append({
type: 'select',
name: 'editlevel',
label: 'Edit protection:',
event: Twinkle.protect.formevents.editlevel,
list: Twinkle.protect.protectionLevels
});
form.append({
type: 'select',
name: 'editexpiry',
label: 'Expires:',
event: function(e) {
if (e.target.value === 'custom') {
Twinkle.protect.doCustomExpiry(e.target);
}
},
list: Twinkle.protect.protectionLengths // Default (2 days) set after render
});
form.append({
type: 'checkbox',
event: Twinkle.protect.formevents.movemodify,
list: [
{
label: 'Modify move protection',
value: 'movemodify',
name: 'movemodify',
tooltip: 'Only for existing pages.',
checked: true
}
]
});
form.append({
type: 'select',
name: 'movelevel',
label: 'Move protection:',
event: Twinkle.protect.formevents.movelevel,
// Autoconfirmed is required for a move, redundant
list: Twinkle.protect.protectionLevels.filter((level) => level.value !== 'autoconfirmed')
});
form.append({
type: 'select',
name: 'moveexpiry',
label: 'Expires:',
event: function(e) {
if (e.target.value === 'custom') {
Twinkle.protect.doCustomExpiry(e.target);
}
},
list: Twinkle.protect.protectionLengths // Default (2 days) set after render
});
form.append({
type: 'checkbox',
event: function twinklebatchprotectFormCreatemodifyEvent(e) {
e.target.form.createlevel.disabled = !e.target.checked;
e.target.form.createexpiry.disabled = !e.target.checked || (e.target.form.createlevel.value === 'all');
e.target.form.createlevel.style.color = e.target.form.createexpiry.style.color = e.target.checked ? '' : 'transparent';
},
list: [
{
label: 'Modify create protection',
value: 'createmodify',
name: 'createmodify',
tooltip: 'Only for pages that do not exist.',
checked: true
}
]
});
form.append({
type: 'select',
name: 'createlevel',
label: 'Create protection:',
event: Twinkle.protect.formevents.createlevel,
list: Twinkle.protect.protectionLevels
});
form.append({
type: 'select',
name: 'createexpiry',
label: 'Expires:',
event: function(e) {
if (e.target.value === 'custom') {
Twinkle.protect.doCustomExpiry(e.target);
}
},
list: Twinkle.protect.protectionLengths // Default (indefinite) set after render
});
form.append({
type: 'header',
label: '' // horizontal rule
});
form.append({
type: 'input',
name: 'reason',
label: 'Reason:',
size: 60,
tooltip: 'For the protection log and page history.'
});
const query = {
action: 'query',
prop: 'revisions|info|imageinfo',
rvprop: 'size|user',
inprop: 'protection',
format: 'json'
};
if (mw.config.get('wgNamespaceNumber') === 14) { // categories
query.generator = 'categorymembers';
query.gcmtitle = mw.config.get('wgPageName');
query.gcmlimit = Twinkle.getPref('batchMax');
} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Prefixindex') {
query.generator = 'allpages';
query.gapnamespace = mw.util.getParamValue('namespace') || $('select[name=namespace]').val();
query.gapprefix = mw.util.getParamValue('prefix') || $('input[name=prefix]').val();
query.gaplimit = Twinkle.getPref('batchMax');
} else {
query.generator = 'links';
query.titles = mw.config.get('wgPageName');
query.gpllimit = Twinkle.getPref('batchMax');
}
const statusdiv = document.createElement('div');
statusdiv.style.padding = '15px'; // just so it doesn't look broken
Window.setContent(statusdiv);
Morebits.status.init(statusdiv);
Window.display();
const statelem = new Morebits.status('Grabbing list of pages');
const wikipedia_api = new Morebits.wiki.api('loading...', query, ((apiobj) => {
const response = apiobj.getResponse();
const pages = (response.query && response.query.pages) || [];
const list = [];
pages.sort(Twinkle.sortByNamespace);
pages.forEach((page) => {
const metadata = [];
let missing = !!page.missing, editProt;
if (missing) {
metadata.push('page does not exist');
editProt = page.protection.filter((pr) => pr.type === 'create' && pr.level === 'sysop').pop();
} else {
if (page.redirect) {
metadata.push('redirect');
}
if (page.ns === 6) {
metadata.push('uploader: ' + page.imageinfo[0].user);
metadata.push('last edit from: ' + page.revisions[0].user);
} else {
metadata.push(mw.language.convertNumber(page.revisions[0].size) + ' bytes');
}
editProt = page.protection
.filter((pr) => pr.type === 'edit' && pr.level === 'sysop')
.pop();
}
if (editProt) {
metadata.push('fully' + (missing ? ' create' : '') + ' protected' +
(editProt.expiry === 'infinity' ? ' indefinitely' : ', expires ' + new Morebits.date(editProt.expiry).calendar('utc') + ' (UTC)'));
}
const title = page.title;
list.push({ label: title + (metadata.length ? ' (' + metadata.join('; ') + ')' : ''), value: title, checked: true, style: editProt ? 'color:red' : '' });
});
form.append({ type: 'header', label: 'Pages to protect' });
form.append({
type: 'button',
label: 'Select All',
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, 'pages')).prop('checked', true);
}
});
form.append({
type: 'button',
label: 'Deselect All',
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, 'pages')).prop('checked', false);
}
});
form.append({
type: 'checkbox',
name: 'pages',
shiftClickSupport: true,
list: list
});
form.append({ type: 'submit' });
const result = form.render();
Window.setContent(result);
// Set defaults
result.editexpiry.value = '2 days';
result.moveexpiry.value = '2 days';
result.createexpiry.value = 'infinity';
Morebits.quickForm.getElements(result, 'pages').forEach(Twinkle.generateArrowLinks);
}), statelem);
wikipedia_api.post();
};
Twinkle.batchprotect.currentProtectCounter = 0;
Twinkle.batchprotect.currentprotector = 0;
Twinkle.batchprotect.callback.evaluate = function twinklebatchprotectCallbackEvaluate(event) {
Morebits.wiki.actionCompleted.notice = 'Batch protection is now complete';
const form = event.target;
const numProtected = $(Morebits.quickForm.getElements(form, 'pages'))
.filter((index, element) => element.checked && element.nextElementSibling.style.color === 'red')
.length;
if (numProtected > 0 && !confirm('You are about to act on ' + mw.language.convertNumber(numProtected) + ' fully protected page(s). Are you sure?')) {
return;
}
const input = Morebits.quickForm.getInputData(form);
if (!input.reason) {
alert("You've got to give a reason, you rouge admin!");
return;
}
Morebits.simpleWindow.setButtonsEnabled(false);
Morebits.status.init(form);
if (input.pages.length === 0) {
Morebits.status.error('Error', 'Nothing to protect, aborting');
return;
}
const batchOperation = new Morebits.batchOperation('Applying protection settings');
batchOperation.setOption('chunkSize', Twinkle.getPref('batchChunks'));
batchOperation.setOption('preserveIndividualStatusLines', true);
batchOperation.setPageList(input.pages);
batchOperation.run((pageName) => {
const query = {
action: 'query',
titles: pageName,
format: 'json'
};
const wikipedia_api = new Morebits.wiki.api('Checking if page ' + pageName + ' exists', query,
Twinkle.batchprotect.callbacks.main, null, batchOperation.workerFailure);
wikipedia_api.params = $.extend({
page: pageName,
batchOperation: batchOperation
}, input);
wikipedia_api.post();
});
};
Twinkle.batchprotect.callbacks = {
main: function(apiobj) {
const response = apiobj.getResponse();
if (response.query.normalized) {
apiobj.params.page = response.query.normalized[0].to;
}
const exists = !response.query.pages[0].missing;
const page = new Morebits.wiki.page(apiobj.params.page, 'Protecting ' + apiobj.params.page);
let takenAction = false;
if (exists && apiobj.params.editmodify) {
page.setEditProtection(apiobj.params.editlevel, apiobj.params.editexpiry);
takenAction = true;
}
if (exists && apiobj.params.movemodify) {
page.setMoveProtection(apiobj.params.movelevel, apiobj.params.moveexpiry);
takenAction = true;
}
if (!exists && apiobj.params.createmodify) {
page.setCreateProtection(apiobj.params.createlevel, apiobj.params.createexpiry);
takenAction = true;
}
if (!takenAction) {
Morebits.status.warn('Protecting ' + apiobj.params.page, 'page ' + (exists ? 'exists' : 'does not exist') + '; nothing to do, skipping');
apiobj.params.batchOperation.workerFailure(apiobj);
return;
}
page.setEditSummary(apiobj.params.reason);
page.setChangeTags(Twinkle.changeTags);
page.protect(apiobj.params.batchOperation.workerSuccess, apiobj.params.batchOperation.workerFailure);
}
};
Twinkle.addInitCallback(Twinkle.batchprotect, 'batchprotect');
}(jQuery));
// </nowiki>