forked from wikimedia-gadgets/twinkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twinklebatchundelete.js
209 lines (184 loc) · 6.91 KB
/
twinklebatchundelete.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
// <nowiki>
(function($) {
/*
****************************************
*** twinklebatchundelete.js: Batch undelete module
****************************************
* Mode of invocation: Tab ("Und-batch")
* Active on: Existing user and project pages
*/
Twinkle.batchundelete = function twinklebatchundelete() {
if (!Morebits.userIsSysop || !mw.config.get('wgArticleId') || (
mw.config.get('wgNamespaceNumber') !== mw.config.get('wgNamespaceIds').user &&
mw.config.get('wgNamespaceNumber') !== mw.config.get('wgNamespaceIds').project)) {
return;
}
Twinkle.addPortletLink(Twinkle.batchundelete.callback, 'Und-batch', 'tw-batch-undel', "Undelete 'em all");
};
Twinkle.batchundelete.callback = function twinklebatchundeleteCallback() {
const Window = new Morebits.simpleWindow(600, 400);
Window.setScriptName('Twinkle');
Window.setTitle('Batch undelete');
Window.addFooterLink('Twinkle help', 'WP:TW/DOC#batchundelete');
Window.addFooterLink('Give feedback', 'WT:TW');
const form = new Morebits.quickForm(Twinkle.batchundelete.callback.evaluate);
form.append({
type: 'checkbox',
list: [
{
label: 'Restore talk pages of undeleted pages if they existed',
name: 'undel_talk',
value: 'undel_talk',
checked: true
}
]
});
form.append({
type: 'input',
name: 'reason',
label: 'Reason:',
size: 60
});
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 query = {
action: 'query',
generator: 'links',
prop: 'info',
inprop: 'protection',
titles: mw.config.get('wgPageName'),
gpllimit: Twinkle.getPref('batchMax'),
format: 'json'
};
const statelem = new Morebits.status('Grabbing list of pages');
const wikipedia_api = new Morebits.wiki.api('loading...', query, ((apiobj) => {
const response = apiobj.getResponse();
let pages = (response.query && response.query.pages) || [];
pages = pages.filter((page) => page.missing);
const list = [];
pages.sort(Twinkle.sortByNamespace);
pages.forEach((page) => {
const editProt = page.protection.filter((pr) => pr.type === 'create' && pr.level === 'sysop').pop();
const title = page.title;
list.push({
label: title + (editProt ? ' (fully create protected' +
(editProt.expiry === 'infinity' ? ' indefinitely' : ', expires ' + new Morebits.date(editProt.expiry).calendar('utc') + ' (UTC)') + ')' : ''),
value: title,
checked: true,
style: editProt ? 'color:red' : ''
});
});
apiobj.params.form.append({ type: 'header', label: 'Pages to undelete' });
apiobj.params.form.append({
type: 'button',
label: 'Select All',
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, 'pages')).prop('checked', true);
}
});
apiobj.params.form.append({
type: 'button',
label: 'Deselect All',
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, 'pages')).prop('checked', false);
}
});
apiobj.params.form.append({
type: 'checkbox',
name: 'pages',
shiftClickSupport: true,
list: list
});
apiobj.params.form.append({ type: 'submit' });
const result = apiobj.params.form.render();
apiobj.params.Window.setContent(result);
Morebits.quickForm.getElements(result, 'pages').forEach(Twinkle.generateArrowLinks);
}), statelem);
wikipedia_api.params = { form: form, Window: Window };
wikipedia_api.post();
};
Twinkle.batchundelete.callback.evaluate = function(event) {
Morebits.wiki.actionCompleted.notice = 'Batch undeletion is now complete';
const numProtected = Morebits.quickForm.getElements(event.target, 'pages').filter((element) => element.checked && element.nextElementSibling.style.color === 'red').length;
if (numProtected > 0 && !confirm('You are about to undelete ' + numProtected + ' fully create protected page(s). Are you sure?')) {
return;
}
const input = Morebits.quickForm.getInputData(event.target);
if (!input.reason) {
alert('You need to give a reason, you cabal crony!');
return;
}
Morebits.simpleWindow.setButtonsEnabled(false);
Morebits.status.init(event.target);
if (!input.pages || !input.pages.length) {
Morebits.status.error('Error', 'nothing to undelete, aborting');
return;
}
const pageUndeleter = new Morebits.batchOperation('Undeleting pages');
pageUndeleter.setOption('chunkSize', Twinkle.getPref('batchChunks'));
pageUndeleter.setOption('preserveIndividualStatusLines', true);
pageUndeleter.setPageList(input.pages);
pageUndeleter.run((pageName) => {
const params = {
page: pageName,
undel_talk: input.undel_talk,
reason: input.reason,
pageUndeleter: pageUndeleter
};
const wikipedia_page = new Morebits.wiki.page(pageName, 'Undeleting page ' + pageName);
wikipedia_page.setCallbackParameters(params);
wikipedia_page.setEditSummary(input.reason);
wikipedia_page.setChangeTags(Twinkle.changeTags);
wikipedia_page.suppressProtectWarning();
wikipedia_page.setMaxRetries(3); // temporary increase from 2 to make batchundelete more likely to succeed [[phab:T222402]] #613
wikipedia_page.undeletePage(Twinkle.batchundelete.callbacks.doExtras, pageUndeleter.workerFailure);
});
};
Twinkle.batchundelete.callbacks = {
// this stupid parameter name is a temporary thing until I implement an overhaul
// of Morebits.wiki.* callback parameters
doExtras: function(thingWithParameters) {
const params = thingWithParameters.parent ? thingWithParameters.parent.getCallbackParameters() :
thingWithParameters.getCallbackParameters();
// the initial batch operation's job is to delete the page, and that has
// succeeded by now
params.pageUndeleter.workerSuccess(thingWithParameters);
let query, wikipedia_api;
if (params.undel_talk) {
const talkpagename = new mw.Title(params.page).getTalkPage().getPrefixedText();
if (talkpagename !== params.page) {
query = {
action: 'query',
prop: 'deletedrevisions',
drvprop: 'ids',
drvlimit: 1,
titles: talkpagename,
format: 'json'
};
wikipedia_api = new Morebits.wiki.api('Checking talk page for deleted revisions', query, Twinkle.batchundelete.callbacks.undeleteTalk);
wikipedia_api.params = params;
wikipedia_api.params.talkPage = talkpagename;
wikipedia_api.post();
}
}
},
undeleteTalk: function(apiobj) {
const page = apiobj.getResponse().query.pages[0];
const exists = !page.missing;
const delrevs = page.deletedrevisions && page.deletedrevisions[0].revid;
if (exists || !delrevs) {
// page exists or has no deleted revisions; forget about it
return;
}
const talkpage = new Morebits.wiki.page(apiobj.params.talkPage, 'Undeleting talk page of ' + apiobj.params.page);
talkpage.setEditSummary('Undeleting [[Help:Talk page|talk page]] of "' + apiobj.params.page + '"');
talkpage.setChangeTags(Twinkle.changeTags);
talkpage.undeletePage();
}
};
Twinkle.addInitCallback(Twinkle.batchundelete, 'batchundelete');
}(jQuery));
// </nowiki>