-
Notifications
You must be signed in to change notification settings - Fork 3
/
FFUHelper.js
411 lines (354 loc) · 12.9 KB
/
FFUHelper.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*** FFU Helper ***/
// User script to close and respond to FFU request
// Documentation at [[en:w:User:BrandonXLF/FFUHelper]]
// By [[en:w:User:BrandonXLF]]
// <syntaxhighlight lang=javascript>
mw.hook('wikipage.content').add(function(content) {
if (mw.config.get('wgPageName') !== 'Wikipedia:Files_for_upload') return;
var currentInterface,
// Template data (TEMPLATE, DESCRIPTION, USER TALK MESSAGE TYPE, PARAMETER 1, PARAMETER 2)
templates = {
accept: [
['{{su' + 'bst:ffu|a|file=$1}} $2', 'If the file has been uploaded.', '{{su' + 'bst:ffu talk|file=$1|section=SECTION}}', 'File', 'Comment']
],
decline: [
['{{su' + 'bst:ffu|d}} $1', 'If the request has been declined.', 'd', 'Comment'],
['{{su' + 'bst:ffu|blankreq}}', 'If the requesting editor did not fill in the necessary fields.', 'd'],
['{{su' + 'bst:ffu|permission}}', 'If permission has not been granted to use the file.', 'd'],
['{{su' + 'bst:ffu|copyrighted}}', 'If the file is copyrighted, and not available under the given license.', 'd'],
['{{su' + 'bst:ffu|corrupt}}', 'If the file is not loading properly or is malformed.', 'd'],
['{{su' + 'bst:ffu|blank}}', 'If the file is blank.', 'd'],
['{{su' + 'bst:ffu|quality}}', 'If the file is too low-quality to use in an article.', 'd'],
['{{su' + 'bst:ffu|redundant|file=$1}}', 'If a very similar file already exists.', 'd', 'File'],
['{{su' + 'bst:ffu|useless}}', 'If there is no reason to have such an file on Wikipedia.', 'd'],
['{{su' + 'bst:ffu|nonsense}}', 'If the file is too strange to be used. This is not the same as corrupt above.', 'd'],
['{{su' + 'bst:ffu|blp}}', 'If the file is a BLP violation.', 'd'],
['{{su' + 'bst:ffu|advert}}', 'If the file is spam or advertising.', 'd'],
['{{su' + 'bst:ffu|van}}', 'Plain, pure vandalism files. This is not the same as nonsense, because vandal requests are usually made in bad faith.', 'd'],
['{{su' + 'bst:ffu|afce}}', 'For submissions that were on hold pending a draft\'s review at AfCwhere the draft has not been reviewed due to the backlog. This decline should only be used after a 7-day hold.', 'd'],
['{{su' + 'bst:ffu|afcd|page=$1}}', 'For submissions that were on hold pending a draft\'s acceptance at AfC which was declined.', 'd', 'Draft'],
['{{su' + 'bst:ffu|afdd|page=$1|afd location=$2}}', 'For submissions that were on hold pending an AfD outcome, which was delete.', 'd', 'Page', 'AFD location'],
['{{su' + 'bst:ffu|csdd|page=$1}}', 'For submissions where the associated article has been deleted under CSD.', 'd', 'Page']
],
comment: [
['{{su' + 'bst:ffu|c}} $1', 'Add a comment.', 'c', 'Comment'],
['{{su' + 'bst:ffu|l}}', 'If no (link to a) license has been given.', 'c'],
['{{su' + 'bst:ffu|flickr}}', 'For copyrighted Flickr files, where it could be assumed, that the request was made by the owner of the Flickr account.', 'c'],
['{{su' + 'bst:ffu|commons}}', 'Asks the user to upload the file to Wikimedia Commons.', 'c'],
['{{su' + 'bst:ffu|rat}}', 'Asks the user to complete a non-free use rationale for the file, if none has been provided, and the file meets the Non-free content guideline.', 'c'],
['{{su' + 'bst:ffu|nourl}}', 'If the request lacks an url to the file, but it indicates a presence of an offline source.', 'h'],
['{{su' + 'bst:ffu|fixurl}}', 'If the request contains a url that does not work.', 'h'],
['{{su' + 'bst:ffu|h}}', 'Places a request on hold while a reviewer awaits a permission confirmation.', 'h'],
['{{su' + 'bst:ffu|afd|page=$1|afd location=$2}}', 'Places a request on hold if a non-free image is to be used in an article, which is at AfD.', 'h', 'Page', 'AfD Location'],
['{{su' + 'bst:ffu|afc|page=$1}}', 'Places a request on hold if a non-free image is to be used in an article, which is awaiting review at AFC.', 'afc', 'Page']
],
};
function createParameter(key, desc) {
return $('<span>')
.attr('id', 'ffu-parameter-' + key)
.css({
marginLeft: '1em',
display: 'block'
})
.append(key + ': ')
.append($('<input>')
.attr('placeholder', desc)
.css({
border: 'none',
borderBottom: '1px solid #555',
padding: '2px'
})
);
}
function createOption(data, select) {
var label = $('<label>'),
code = data[0].replace(/\|[a-z0-9A-Z _-]+=\$\d/g, '').replace(/ \$\d/g, '');
function selectClicked() {
$('#ffu-parameter-1, #ffu-parameter-2').remove();
$('#ffu-selected')
.removeAttr('id')
.find('input')
.prop('checked', false);
label.attr('id', 'ffu-selected');
if (data[4])
label.after(createParameter('2', data[4]));
if (data[3])
label.after(createParameter('1', data[3]));
}
setTimeout(function() {
if (select)
selectClicked();
});
label
.data('ffu', data)
.css('display', 'block')
.append($('<input>')
.css({
verticalAlign: 'middle',
marginRight: '4px'
})
.attr('type', 'radio')
.prop('checked', select)
.click(selectClicked)
)
.append($('<span>')
.css('vertical-align', 'middle')
.text(code + ' - ')
.append($('<i>').text(data[1]))
);
return label;
}
function getUser(sectionElement) {
var user = '',
element = sectionElement.next();
while (user === '' && element[0]) {
if (!element.is(currentInterface)) {
user = element.find('.userlink').attr('href') || '';
}
element = element.next();
}
user = decodeURIComponent(user).match(/.*(?:[Uu]ser:|\/)([^?&]+)/);
user = user ? user[1] : '';
return user;
}
function openInterface(type, section, sectionElement) {
var idElement = sectionElement.find('.mw-headline');
if (!idElement.length) {
idElement = sectionElement.find(':header');
}
var sectionName = idElement.attr('id').replace(/_/g, ' '),
user = getUser(sectionElement),
addHoldInput = $('<input>'),
notifyUserInput = $('<input>'),
userInput = $('<input>'),
options = [];
for (var i = 0; i < templates[type].length; i++) {
options.push(createOption(templates[type][i], i === 0));
}
var actionButtons;
function onSave() {
actionButtons.prop('disabled', true);
var selectedOption = $('#ffu-selected'),
shouldNotifyUser = notifyUserInput.prop('checked');
if (!selectedOption.length) {
actionButtons.prop('disabled', false);
return;
}
if (shouldNotifyUser && !userInput.val()) {
mw.notify('No username given to notify.', {
type: 'error'
});
actionButtons.prop('disabled', false);
return;
}
var data = selectedOption.data('ffu'),
api = new mw.Api();
mw.notify('Fetching page text...');
$.get(mw.config.get('wgScript'), {
title: mw.config.get('wgPageName'),
action: 'raw',
section: section
}).then(function(text) {
mw.notify('Updating FFU request...');
// Substitute parameters
var code = data[0]
.replace(/\$1/, $('#ffu-parameter-1 input').val() || '')
.replace(/\$2/, $('#ffu-parameter-2 input').val() || '');
text += '\n:' + code + ' ~~' + '~~';
// Add hold notice for comments
if (type === 'comment' && addHoldInput.prop('checked')) {
text = text.replace(/{{(?:Template:|)[Ff]fu h\|.*?}}/g, '');
text = text.replace(/-->\n/, '-->');
text = text.replace(/-->\n+/, '-->');
text = text.replace(/\n+<!--/, '<!--');
text = text.replace(/(==.*==)\n*/, '$1\n{{su' + 'bst:ffu h}}\n');
}
// Add top and bottom for accept/decline
if (type !== 'comment') {
text = text.replace(/(==.*==)\n*/, '$1\n{{su' + 'bst:ffu ' + type[0] + '}}\n');
text = text.replace(/(<!-- \[\[User:DoNotArchiveUntil\]\].*[\n\r]*)/, '');
text += '\n{{su' + 'bst:ffu b}}';
}
var editSummary = {
'accept': 'Accepted',
'decline': 'Declined',
'comment': 'Commented on'
}[type] + ' request using [[en:w:User:BrandonXLF/FFUHelper|FFU Helper]]';
return api.postWithEditToken({
action: 'edit',
section: section,
text: text,
title: mw.config.get('wgPageName'),
summary: editSummary
}).then(function() {
mw.notify('Finished editing FFU request.');
});
}).then(function() {
if (!shouldNotifyUser) return;
mw.notify('Posting on user talk page...');
return api.postWithEditToken({
action: 'edit',
appendtext: '\n\n{{su' + 'bst:ffu talk|' + data[2] + '|section=' + sectionName + '}} ~~' + '~~',
title: 'User talk:' + userInput.val(),
summary: 'Notifying about [[WP:FFU|FFU]] request using [[en:w:User:BrandonXLF/FFUHelper|FFU Helper]]'
}).then(function() {
mw.notify('Posted notice on user talk page.');
});
}).then(function() {
mw.notify('Reloading page...');
$.get(mw.config.get('wgScriptPath') + '/api.php', {
action: 'parse',
page: mw.config.get('wgPageName'),
prop: 'text|categorieshtml',
format: 'json'
}).done(function(res) {
var contentText = $('#mw-content-text'),
catLinks = $('#catlinks');
contentText.find('.mw-parser-output').replaceWith(res.parse.text['*']);
mw.hook('wikipage.content').fire(contentText);
catLinks.replaceWith(res.parse.categorieshtml['*']);
mw.hook('wikipage.categories').fire(catLinks);
mw.notify('Page reloaded.');
});
}).fail(function(_, data) {
mw.notify(new mw.Api().getErrorMessage(data), {type: 'error'});
actionButtons.prop('disabled', false);
});
}
if (currentInterface)
currentInterface.remove();
actionButtons = $()
.add($('<button>')
.css('margin', '4px')
.text('Save')
.click(onSave)
)
.add($('<button>')
.css('margin', '4px')
.text('Cancel')
.click(function() {
currentInterface.remove();
})
);
currentInterface = $('<p>')
.css({
border: '1px solid ' + {
comment: 'grey',
decline: 'red',
accept: 'green'
}[type],
borderRadius: '4px',
padding: '4px 0.5em'
})
.append($('<span>')
.css({
fontStyle: '125%',
fontWeight: 'bold',
marginBottom: '1em'
})
.text('FFU Helper - ' + type.charAt(0).toUpperCase() + type.slice(1))
)
.append($('<div>')
.css('margin-left', '5px')
.append(options)
)
.append($('<div>')
.css({
margin: '0.5em 0',
borderBottom: '1px solid #555'
})
)
.append(type !== 'comment' ? '' : $('<div>')
.css('margin-left', '5px')
.append($('<label>')
.append(addHoldInput
.css({
verticalAlign: 'middle',
marginRight: '4px'
})
.attr('type', 'checkbox')
.prop('checked', true)
)
.append($('<span>')
.css('vertical-align', 'middle')
.text('Add 7 day hold notice')
)
)
)
.append($('<div>')
.css('margin-left', '5px')
.append($('<label>')
.append(notifyUserInput
.css({
verticalAlign: 'middle',
marginRight: '4px'
})
.attr('type', 'checkbox')
.prop('checked', true)
)
.append($('<span>')
.css('vertical-align', 'middle')
.text('Notify user:')
)
)
.append(userInput
.css({
border: 'none',
borderBottom: '1px solid #555',
padding: '2px'
})
.attr('placeholder', 'User')
.val(user)
)
)
.append($('<div>')
.css('margin', '4px 0 0 -4px')
.append(actionButtons)
)
.append(
'<div style="margin-top:4px;">[' +
'<a href="https://en.wikipedia.org/wiki/Wikipedia:Files_for_upload/Reviewer_instructions">Reviewer instructions</a>' +
' • <a href="https://commons.wikimedia.org/wiki/Special:UploadWizard">Commons</a>' +
' (<a href="https://commons.wikimedia.org/wiki/Special:Upload">plain</a>)' +
' • <a href="' + mw.config.get('wgScript') + '?title=Wikipedia:File_Upload_Wizard&withJS=MediaWiki:FileUploadWizard.js">Local</a>' +
' (<a href="' + mw.config.get('wgScript') + '?title=Special:Upload">plain</a>)' +
' • <a href="https://en.wikipedia.org/wiki/User:BrandonXLF/FFUHelper">FFU Helper</a>' +
']</span>'
);
sectionElement.after(currentInterface);
}
// Add links to sections
content.find('.mw-parser-output h2').each(function() {
var heading = $(this);
if (heading.closest('.mw-heading').length)
heading = heading.closest('.mw-heading');
var editLink = heading.find('.mw-editsection a[href*="title="][href*="section="]').first();
if (!editLink.length) return;
var section = /[?&]v?e?section=(T?-?\d*)/.exec(editLink.attr('href'))[1];
if (heading.next().hasClass('mw-collapsible')) {
editLink.siblings().last().after('<span class="mw-editsection" style="background:#dfdfdf;">[closed]</span>');
return;
}
$.each({
accept: '#a0ffa0',
decline: '#ffcece',
comment: '#ededed'
}, function(type, color) {
editLink.siblings().last().after($('<span>')
.css('background', color)
.addClass('mw-editsection')
.append('[')
.append($('<a>')
.text(type)
.attr('href', '#')
.click(function(e) {
e.preventDefault();
openInterface(type, section, heading);
})
)
.append(']')
);
});
});
});
// </syntaxhighlight>