forked from GeneralNotability/spihelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspihelper.js
2852 lines (2657 loc) · 110 KB
/
spihelper.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// <nowiki>
// @ts-check
// GeneralNotability's rewrite of Tim's SPI helper script
// v2.6.0 "Userboxitis"
/* global mw, $, importStylesheet, importScript, displayMessage, spiHelperCustomOpts */
// Adapted from [[User:Mr.Z-man/closeAFD]]
importStylesheet('User:GeneralNotability/spihelper.css')
importScript('User:Timotheus Canens/displaymessage.js')
// Typedefs
/**
* @typedef SelectOption
* @type {Object}
* @property {string} label Text to display in the drop-down
* @property {string} value Value to return if this option is selected
* @property {boolean} selected Whether this item should be selected by default
* @property {boolean=} disabled Whether this item should be disabled
*/
/**
* @typedef BlockEntry
* @type {Object}
* @property {string} username Username to block
* @property {string} duration Duration of block
* @property {boolean} acb If set, account creation is blocked
* @property {boolean} ab Whether autoblock is enabled (for registered users)/
* logged-in users are blocked (for IPs)
* @property {boolean} ntp If set, talk page access is blocked
* @property {boolean} nem If set, email access is blocked
* @property {string} tpn Type of talk page notice to apply on block
*/
/**
* @typedef TagEntry
* @type {Object}
* @property {string} username Username to tag
* @property {string} tag Tag to apply to user
* @property {string} altmasterTag Altmaster tag to apply to user, if relevant
* @property {boolean} blocking Whether this account is marked for block as well
*/
/**
* @typedef ParsedArchiveNotice
* @type {Object}
* @property {string} username Case username
* @property {boolean} xwiki Whether the crosswiki flag is set
* @property {boolean} deny Whether the deny flag is set
*/
// Globals
// User-configurable settings, these are the defaults but will be updated by
// spiHelper_loadSettings()
const spiHelperSettings = {
// Choices are 'watch' (unconditionally add to watchlist), 'preferences'
// (follow default preferences), 'nochange' (don't change the watchlist
// status of the page), and 'unwatch' (unconditionally remove)
watchCase: 'preferences',
watchCaseExpiry: 'indefinite',
watchArchive: 'nochange',
watchArchiveExpiry: 'indefinite',
watchTaggedUser: 'preferences',
watchTaggedUserExpiry: 'indefinite',
watchNewCats: 'nochange',
watchNewCatsExpiry: 'indefinite',
watchBlockedUser: true,
watchBlockedUserExpiry: 'indefinite',
// Lets people disable clerk options if they're not a clerk
clerk: true,
// Log all actions to Special:MyPage/spihelper_log
log: false,
// Reverse said log, so that the newest actions are at the top.
reversed_log: false,
// Enable the "move section" button
iUnderstandSectionMoves: false,
// These are for debugging to view as other roles. If you're picking apart the code and
// decide to set these (especially the CU option), it is YOUR responsibility to make sure
// you don't do something that violates policy
debugForceCheckuserState: null,
debugForceAdminState: null
}
/** @type {string} Name of the SPI page in wiki title form
* (e.g. Wikipedia:Sockpuppet investigations/Test) */
let spiHelperPageName = mw.config.get('wgPageName').replace(/_/g, ' ')
/** @type {number} The main page's ID - used to check if the page
* has been edited since we opened it to prevent edit conflicts
*/
let spiHelperStartingRevID = mw.config.get('wgCurRevisionId')
// Just the username part of the case
let spiHelperCaseName = spiHelperPageName.replace(/Wikipedia:Sockpuppet investigations\//g, '')
/** list of section IDs + names corresponding to separate investigations */
let spiHelperCaseSections = []
/** @type {?number} Selected section, "null" means that we're opearting on the entire page */
let spiHelperSectionId = null
/** @type {?string} Selected section's name (e.g. "10 June 2020") */
let spiHelperSectionName = null
/** @type {ParsedArchiveNotice} */
let spiHelperArchiveNoticeParams
/** Map of top-level actions the user has selected */
const spiHelperActionsSelected = {
Case_act: false,
Block: false,
Note: false,
Close: false,
Rename: false,
Archive: false,
SpiMgmt: false
}
/** @type {BlockEntry[]} Requested blocks */
const spiHelperBlocks = []
/** @type {TagEntry[]} Requested tags */
const spiHelperTags = []
/** @type {string[]} Requested global locks */
const spiHelperGlobalLocks = []
// Count of unique users in the case (anything with a checkuser, checkip, user, ip, or vandal template on the page)
let spiHelperUserCount = 0
const spiHelperSectionRegex = /^(?:===[^=]*===|=====[^=]*=====)\s*$/m
/** @type {SelectOption[]} List of possible selections for tagging a user in the block/tag interface
*/
const spiHelperTagOptions = [
{ label: 'None', selected: true, value: '' },
{ label: 'Suspected sock', value: 'blocked', selected: false },
{ label: 'Proven sock', value: 'proven', selected: false },
{ label: 'CU confirmed sock', value: 'confirmed', selected: false },
{ label: 'Blocked master', value: 'master', selected: false },
{ label: 'CU confirmed master', value: 'sockmasterchecked', selected: false },
{ label: '3X banned master', value: 'bannedmaster', selected: false }
]
/** @type {SelectOption[]} List of possible selections for tagging a user's altmaster in the block/tag interface */
const spiHelperAltMasterTagOptions = [
{ label: 'None', selected: true, value: '' },
{ label: 'Suspected alt master', value: 'suspected', selected: false },
{ label: 'Proven alt master', value: 'proven', selected: false }
]
/** @type {SelectOption[]} List of templates that CUs might insert */
const spiHelperCUTemplates = [
{ label: 'CU templates', selected: true, value: '', disabled: true },
{ label: 'Confirmed', selected: false, value: '{{confirmed}}' },
{ label: 'Confirmed/No Comment', selected: false, value: '{{confirmed-nc}}' },
{ label: 'Indistinguishable', selected: false, value: '{{tallyho}}' },
{ label: 'Likely', selected: false, value: '{{likely}}' },
{ label: 'Possilikely', selected: false, value: '{{possilikely}}' },
{ label: 'Possible', selected: false, value: '{{possible}}' },
{ label: 'Unlikely', selected: false, value: '{{unlikely}}' },
{ label: 'Unrelated', selected: false, value: '{{unrelated}}' },
{ label: 'Inconclusive', selected: false, value: '{{inconclusive}}' },
{ label: 'Need behavioral eval', selected: false, value: '{{behav}}' },
{ label: 'No sleepers', selected: false, value: '{{nosleepers}}' },
{ label: 'Stale', selected: false, value: '{{IPstale}}' },
{ label: 'No comment (IP)', selected: false, value: '{{ncip}}' }
]
/** @type {SelectOption[]} Templates that a clerk or admin might insert */
const spiHelperAdminTemplates = [
{ label: 'Admin/clerk templates', selected: true, value: '', disabled: true },
{ label: 'Duck', selected: false, value: '{{duck}}' },
{ label: 'Megaphone Duck', selected: false, value: '{{megaphone duck}}' },
{ label: 'Blocked and tagged', selected: false, value: '{{bnt}}' },
{ label: 'Blocked, no tags', selected: false, value: '{{bwt}}' },
{ label: 'Blocked, awaiting tags', selected: false, value: '{{sblock}}' },
{ label: 'Blocked, tagged, closed', selected: false, value: '{{btc}}' },
{ label: 'Diffs needed', selected: false, value: '{{DiffsNeeded|moreinfo}}' },
{ label: 'Locks requested', selected: false, value: '{{GlobalLocksRequested}}' }
]
// Regex to match the case status, group 1 is the actual status
const spiHelperCaseStatusRegex = /{{\s*SPI case status\s*\|?\s*(\S*?)\s*}}/i
// Regex to match closed case statuses (close or closed)
const spiHelperCaseClosedRegex = /^closed?$/i
const spiHelperClerkStatusRegex = /{{(CURequest|awaitingadmin|clerk ?request|(?:self|requestand|cu)?endorse|inprogress|decline(?:-ip)?|moreinfo|relisted|onhold)}}/i
const spiHelperSockSectionWithNewlineRegex = /====\s*Suspected sockpuppets\s*====\n*/i
const spiHelperAdminSectionWithPrecedingNewlinesRegex = /\n*\s*====\s*<big>Clerk, CheckUser, and\/or patrolling admin comments<\/big>\s*====\s*/i
const spiHelperCUBlockRegex = /{{(checkuserblock(-account|-wide)?|checkuser block)}}/i
const spiHelperArchiveNoticeRegex = /{{\s*SPI\s*archive notice\|(?:1=)?([^|]*?)(\|.*)?}}/i
const spiHelperPriorCasesRegex = /{{spipriorcases}}/i
// regex to remove hidden characters from form inputs - they mess up some things,
// especially mw.util.isIP
const spiHelperHiddenCharNormRegex = /\u200E/
const spihelperAdvert = ' (using [[:w:en:User:GeneralNotability/spihelper|spihelper.js]])'
// The current wiki's interwiki prefix
const spiHelperInterwikiPrefix = spiHelperGetInterwikiPrefix()
// Map of active operations (used as a "dirty" flag for beforeunload)
// Values are strings representing the state - acceptable values are 'running', 'success', 'failed'
const spiHelperActiveOperations = new Map()
// Actually put the portlets in place if needed
if (mw.config.get('wgPageName').includes('Wikipedia:Sockpuppet_investigations/') &&
!mw.config.get('wgPageName').includes('Wikipedia:Sockpuppet_investigations/SPI/') &&
!mw.config.get('wgPageName').includes('/Archive')) {
mw.loader.load('mediawiki.user')
$(spiHelperAddLink)
}
// Main functions - do the meat of the processing and UI work
const spiHelperTopViewHTML = `
<div id="spiHelper_topViewDiv">
<h3>Handling SPI case</h3>
<select id="spiHelper_sectionSelect"/>
<h4 id="spiHelper_warning" class="spiHelper-errortext" hidden></h4>
<ul>
<li id="spiHelper_actionLine" class="spiHelper_singleCaseOnly">
<input type="checkbox" name="spiHelper_Case_Action" id="spiHelper_Case_Action" />
<label for="spiHelper_Case_Action">Change case status</label>
</li>
<li id="spiHelper_spiMgmtLine" class="spiHelper_allCasesOnly">
<input type="checkbox" id="spiHelper_SpiMgmt" />
<label for="spiHelper_SpiMgmt">Change SPI options</label>
</li>
<li id="spiHelper_blockLine" class="spiHelper_adminClerkClass">
<input type="checkbox" name="spiHelper_BlockTag" id="spiHelper_BlockTag" />
<label for="spiHelper_BlockTag">Block/tag socks</label>
</li>
<li id="spiHelper_commentLine" class="spiHelper_singleCaseOnly">
<input type="checkbox" name="spiHelper_Comment" id="spiHelper_Comment" />
<label for="spiHelper_Comment">Note/comment</label>
</li>
<li id="spiHelper_closeLine" class="spiHelper_adminClerkClass spiHelper_singleCaseOnly">
<input type="checkbox" name="spiHelper_Close" id="spiHelper_Close")" />
<label for="spiHelper_Close">Close case</label>
</li>
<li id="spiHelper_moveLine" class="spiHelper_clerkClass">
<input type="checkbox" name="spiHelper_Move" id="spiHelper_Move" />
<label for="spiHelper_Move" id="spiHelper_moveLabel">Move/merge full case (Clerk only)</label>
</li>
<li id="spiHelper_archiveLine" class="spiHelper_clerkClass">
<input type="checkbox" name="spiHelper_Archive" id="spiHelper_Archive"/>
<label for="spiHelper_Archive">Archive case (Clerk only)</label>
</li>
</ul>
<input type="button" id="spiHelper_GenerateForm" name="spiHelper_GenerateForm" value="Continue" onclick="spiHelperGenerateForm()" />
</div>
`
/**
* Initialization functions for spiHelper, displays the top-level menu
*/
async function spiHelperInit () {
'use strict'
spiHelperCaseSections = await spiHelperGetInvestigationSectionIDs()
// Load archivenotice params
spiHelperArchiveNoticeParams = await spiHelperParseArchiveNotice(spiHelperPageName)
// First, insert the template text
displayMessage(spiHelperTopViewHTML)
// Narrow search scope
const $topView = $('#spiHelper_topViewDiv', document)
// Next, modify what's displayed
// Set the block selection label based on whether or not the user is an admin
$('#spiHelper_blockLabel', $topView).text(spiHelperIsAdmin() ? 'Block/tag socks' : 'Tag socks')
// Wire up a couple of onclick handlers
$('#spiHelper_Move', $topView).on('click', function () {
spiHelperUpdateArchive()
})
$('#spiHelper_Archive', $topView).on('click', function () {
spiHelperUpdateMove()
})
// Generate the section selector
const $sectionSelect = $('#spiHelper_sectionSelect', $topView)
$sectionSelect.on('change', () => {
spiHelperSetCheckboxesBySection()
})
// Add the dates to the selector
for (let i = 0; i < spiHelperCaseSections.length; i++) {
const s = spiHelperCaseSections[i]
$('<option>').val(s.index).text(s.line).appendTo($sectionSelect)
}
// All-sections selector...deliberately at the bottom, the default should be the first section
$('<option>').val('all').text('All Sections').appendTo($sectionSelect)
// Hide block and close from non-admin non-clerks
if (!(spiHelperIsAdmin() || spiHelperIsClerk())) {
$('.spiHelper_adminClerkClass', $topView).hide()
}
// Hide move and archive from non-clerks
if (!spiHelperIsClerk()) {
$('.spiHelper_clerkClass', $topView).hide()
}
// Set the checkboxes to their default states
spiHelperSetCheckboxesBySection()
}
const spiHelperActionViewHTML = `
<div id="spiHelper_actionViewDiv">
<small><a id="spiHelper_backLink">Back to top menu</a></small>
<br />
<h3>Handling SPI case</h3>
<div id="spiHelper_actionView">
<h4>Changing case status</h4>
<label for="spiHelper_CaseAction">New status:</label>
<select id="spiHelper_CaseAction"/>
</div>
<div id="spiHelper_spiMgmtView">
<h4>Changing SPI settings</h4>
<ul>
<li>
<input type="checkbox" id="spiHelper_spiMgmt_crosswiki" />
<label for="spiHelper_Case_Action">Case is crosswiki</label>
</li>
<li>
<input type="checkbox" id="spiHelper_spiMgmt_deny" />
<label for="spiHelper_Case_Action">Socks should not be tagged per DENY</label>
</li>
</ul>
</div>
<div id="spiHelper_blockTagView">
<h4 id="spiHelper_blockTagHeader">Blocking and tagging socks</h4>
<ul>
<li class="spiHelper_adminClass">
<input type="checkbox" name="spiHelper_noblock" id="spiHelper_noblock" />
<label for="spiHelper_noblock">Do not make any blocks (this overrides the individual "Blk" boxes below)</label>
</li>
<li class="spiHelper_adminClass">
<input type="checkbox" name="spiHelper_override" id="spiHelper_override" />
<label for="spiHelper_override">Override any existing blocks</label>
</li>
<li class="spiHelper_cuClass">
<input type="checkbox" name="spiHelper_cublock" id="spiHelper_cublock" />
<label for="spiHelper_cublock">Mark blocks as Checkuser blocks.</label>
</li>
<li class="spiHelper_cuClass">
<input type="checkbox" name="spiHelper_cublockonly" id="spiHelper_cublockonly" />
<label for="spiHelper_cublockonly">
Suppress the usual block summary and only use {{checkuserblock-account}} and {{checkuserblock}} (no effect if "mark blocks as CU blocks" is not checked).
</label>
</li>
<li class="spiHelper_adminClass">
<input type="checkbox" checked="checked" name="spiHelper_blocknoticemaster" id="spiHelper_blocknoticemaster" />
<label for="spiHelper_blocknoticemaster">Add talk page notice when (re)blocking the sockmaster.</label>
</li>
<li class="spiHelper_adminClass">
<input type="checkbox" checked="checked" name="spiHelper_blocknoticesocks" id="spiHelper_blocknoticesocks" />
<label for="spiHelper_blocknoticesocks">Add talk page notice when blocking socks.</label>
</li>
<li class="spiHelper_adminClass">
<input type="checkbox" name="spiHelper_blanktalk" id="spiHelper_blanktalk" />
<label for="spiHelper_blanktalk">Blank the talk page when adding talk notices.</label>
</li>
<li>
<input type="checkbox" name="spiHelper_hidelocknames" id="spiHelper_hidelocknames" />
<label for="spiHelper_hidelocknames">Hide usernames when requesting global locks.</label>
</li>
</ul>
<table id="spiHelper_blockTable" style="border-collapse:collapse;">
<tr>
<th>Username</th>
<th class="spiHelper_adminClass"><span title="Block user" class="rt-commentedText spihelper-hovertext">Blk?</span></th>
<th class="spiHelper_adminClass"><span title="Block duration" class="rt-commentedText spihelper-hovertext">Duration</span></th>
<th class="spiHelper_adminClass"><span title="Account creation blocked" class="rt-commentedText spihelper-hovertext">ACB</span></th>
<th class="spiHelper_adminClass"><span title="Autoblock (for logged-in users)/Anonymous-only (for IPs)" class="rt-commentedText spihelper-hovertext">AB/AO</span></th>
<th class="spiHelper_adminClass"><span title="Disable talk page access" class="rt-commentedText spihelper-hovertext">NTP</span></th>
<th class="spiHelper_adminClass"><span title="Disable email" class="rt-commentedText spihelper-hovertext">NEM</span></th>
<th>Tag</th>
<th><span title="Tag the user with a suspected alternate master" class="rt-commentedText spihelper-hovertext">Alt Master</span></th>
<th><span title="Request a global lock at Meta:SRG" class="rt-commentedText spihelper-hovertext">Req Lock?</span></th>
</tr>
<tr style="border-bottom:2px solid black">
<td style="text-align:center;">(All users)</td>
<td class="spiHelper_adminClass"><input type="checkbox" id="spiHelper_block_doblock"/></td>
<td class="spiHelper_adminClass"></td>
<td class="spiHelper_adminClass"><input type="checkbox" id="spiHelper_block_acb" checked="checked"/></td>
<td class="spiHelper_adminClass"><input type="checkbox" id="spiHelper_block_ab" checked="checked"/></td>
<td class="spiHelper_adminClass"><input type="checkbox" id="spiHelper_block_tp"/></td>
<td class="spiHelper_adminClass"><input type="checkbox" id="spiHelper_block_email"/></td>
<td><select id="spiHelper_block_tag"/></td>
<td><select id="spiHelper_block_tag_altmaster"/></td>
<td><input type="checkbox" name="spiHelper_block_lock_all" id="spiHelper_block_lock"/></td>
</tr>
</table>
<span><input type="button" id="moreSerks" value="Add Row" onclick="spiHelperAddBlankUserLine();"/></span>
</div>
<div id="spiHelper_closeView">
<h4>Marking case as closed</h4>
<input type="checkbox" checked="checked" id="spiHelper_CloseCase" />
<label for="spiHelper_CloseCase">Close this SPI case</label>
</div>
<div id="spiHelper_moveView">
<h4 id="spiHelper_moveHeader">Move section</h4>
<label for="spiHelper_moveTarget">New sockmaster username: </label>
<input type="text" name="spiHelper_moveTarget" id="spiHelper_moveTarget" />
</div>
<div id="spiHelper_archiveView">
<h4>Archiving case</h4>
<input type="checkbox" checked="checked" name="spiHelper_ArchiveCase" id="spiHelper_ArchiveCase" />
<label for="spiHelper_ArchiveCase">Archive this SPI case</label>
</div>
<div id="spiHelper_commentView">
<h4>Comments</h4>
<span>
<select id="spiHelper_noteSelect"/>
<select class="spiHelper_adminClerkClass" id="spiHelper_adminSelect"/>
<select class="spiHelper_cuClass" id="spiHelper_cuSelect"/>
</span>
<div>
<label for="spiHelper_CommentText">Comment:</label>
<textarea rows="3" cols="80" id="spiHelper_CommentText">*</textarea>
<div><a id="spiHelper_previewLink">Preview</a></div>
</div>
<div class="spihelper-previewbox" id="spiHelper_previewBox" hidden/>
</div>
<input type="button" id="spiHelper_performActions" value="Done" />
</div>
`
/**
* Big function to generate the SPI form from the top-level menu selections
*
* Would fail ESlint no-unused-vars due to only being
* referenced in an onclick event
*
* @return {Promise<void>}
*/
// eslint-disable-next-line no-unused-vars
async function spiHelperGenerateForm () {
'use strict'
spiHelperUserCount = 0
const $topView = $('#spiHelper_topViewDiv', document)
spiHelperActionsSelected.Case_act = $('#spiHelper_Case_Action', $topView).prop('checked')
spiHelperActionsSelected.Block = $('#spiHelper_BlockTag', $topView).prop('checked')
spiHelperActionsSelected.Note = $('#spiHelper_Comment', $topView).prop('checked')
spiHelperActionsSelected.Close = $('#spiHelper_Close', $topView).prop('checked')
spiHelperActionsSelected.Rename = $('#spiHelper_Move', $topView).prop('checked')
spiHelperActionsSelected.Archive = $('#spiHelper_Archive', $topView).prop('checked')
spiHelperActionsSelected.SpiMgmt = $('#spiHelper_SpiMgmt', $topView).prop('checked')
const pagetext = await spiHelperGetPageText(spiHelperPageName, false, spiHelperSectionId)
if (!(spiHelperActionsSelected.Case_act ||
spiHelperActionsSelected.Note || spiHelperActionsSelected.Close ||
spiHelperActionsSelected.Archive || spiHelperActionsSelected.Block ||
spiHelperActionsSelected.Rename || spiHelperActionsSelected.SpiMgmt)) {
displayMessage('')
return
}
displayMessage(spiHelperActionViewHTML)
// Reduce the scope that jquery operates on
const $actionView = $('#spiHelper_actionViewDiv', document)
// Wire up the action view
$('#spiHelper_backLink', $actionView).on('click', () => {
spiHelperInit()
})
if (spiHelperActionsSelected.Case_act) {
const result = spiHelperCaseStatusRegex.exec(pagetext)
let casestatus = ''
if (result) {
casestatus = result[1]
}
const canAddCURequest = (casestatus === '' || /^(?:admin|moreinfo|cumoreinfo|hold|cuhold|clerk|open)$/i.test(casestatus))
const cuRequested = /^(?:CU|checkuser|CUrequest|request|cumoreinfo)$/i.test(casestatus)
const cuEndorsed = /^(?:endorse(d)?)$/i.test(casestatus)
const cuCompleted = /^(?:inprogress|checking|relist(ed)?|checked|completed|declined?|cudeclin(ed)?)$/i.test(casestatus)
/** @type {SelectOption[]} Generated array of values for the case status select box */
const selectOpts = [
{ label: 'No action', value: 'noaction', selected: true }
]
if (spiHelperCaseClosedRegex.test(casestatus)) {
selectOpts.push({ label: 'Reopen', value: 'reopen', selected: false })
} else if (spiHelperIsClerk() && casestatus === 'clerk') {
// Allow clerks to change the status from clerk to open.
// Used when clerk assistance has been given and the case previously had the status 'open'.
selectOpts.push({ label: 'Mark as open', value: 'open', selected: false })
} else if (spiHelperIsAdmin() && casestatus === 'admin') {
// Allow admins to change the status to open from admin
// Used when admin assistance has been given to the non-admin clerk and the case previously had the status 'open'.
selectOpts.push({ label: 'Mark as open', value: 'open', selected: false })
}
if (spiHelperIsCheckuser()) {
selectOpts.push({ label: 'Mark as in progress', value: 'inprogress', selected: false })
}
if (spiHelperIsClerk() || spiHelperIsAdmin()) {
selectOpts.push({ label: 'Request more information', value: 'moreinfo', selected: false })
}
if (canAddCURequest) {
// Statuses only available if the case could be moved to "CU requested"
selectOpts.push({ label: 'Request CU', value: 'CUrequest', selected: false })
if (spiHelperIsClerk()) {
selectOpts.push({ label: 'Request CU and self-endorse', value: 'selfendorse', selected: false })
}
}
// CU already requested
if (cuRequested && spiHelperIsClerk()) {
// Statuses only available if CU has been requested, only clerks + CUs should use these
selectOpts.push({ label: 'Endorse for CU attention', value: 'endorse', selected: false })
// Switch the decline option depending on whether the user is a checkuser
if (spiHelperIsCheckuser()) {
selectOpts.push({ label: 'Endorse CU as a CheckUser', value: 'cuendorse', selected: false })
}
if (spiHelperIsCheckuser()) {
selectOpts.push({ label: 'Decline CU', value: 'cudecline', selected: false })
} else {
selectOpts.push({ label: 'Decline CU', value: 'decline', selected: false })
}
selectOpts.push({ label: 'Request more information for CU', value: 'cumoreinfo', selected: false })
} else if (cuEndorsed && spiHelperIsCheckuser()) {
// Let checkusers decline endorsed cases
if (spiHelperIsCheckuser()) {
selectOpts.push({ label: 'Decline CU', value: 'cudecline', selected: false })
}
selectOpts.push({ label: 'Request more information for CU', value: 'cumoreinfo', selected: false })
}
// This is mostly a CU function, but let's let clerks and admins set it
// in case the CU forgot (or in case we're un-closing))
if (spiHelperIsAdmin() || spiHelperIsClerk()) {
selectOpts.push({ label: 'Mark as checked', value: 'checked', selected: false })
}
if (spiHelperIsClerk() && cuCompleted) {
selectOpts.push({ label: 'Relist for another check', value: 'relist', selected: false })
}
if (spiHelperIsCheckuser()) {
selectOpts.push({ label: 'Place case on CU hold', value: 'cuhold', selected: false })
} else { // I guess it's okay for anyone to have this option
selectOpts.push({ label: 'Place case on hold', value: 'hold', selected: false })
}
selectOpts.push({ label: 'Request clerk action', value: 'clerk', selected: false })
// I think this is only useful for non-admin clerks to ask admins to do stuff
if (!spiHelperIsAdmin() && spiHelperIsClerk()) {
selectOpts.push({ label: 'Request admin action', value: 'admin', selected: false })
}
// Generate the case action options
spiHelperGenerateSelect('spiHelper_CaseAction', selectOpts)
// Add the onclick handler to the drop-down
$('#spiHelper_CaseAction', $actionView).on('change', function (e) {
spiHelperCaseActionUpdated($(e.target))
})
} else {
$('#spiHelper_actionView', $actionView).hide()
}
if (spiHelperActionsSelected.SpiMgmt) {
const $xwikiBox = $('#spiHelper_spiMgmt_crosswiki', $actionView)
const $denyBox = $('#spiHelper_spiMgmt_deny', $actionView)
$xwikiBox.prop('checked', spiHelperArchiveNoticeParams.xwiki)
$denyBox.prop('checked', spiHelperArchiveNoticeParams.deny)
} else {
$('#spiHelper_spiMgmtView', $actionView).hide()
}
if (spiHelperActionsSelected.Block) {
if (spiHelperIsAdmin()) {
$('#spiHelper_blockTagHeader', $actionView).text('Blocking and tagging socks')
} else {
$('#spiHelper_blockTagHeader', $actionView).text('Tagging socks')
}
// eslint-disable-next-line no-useless-escape
const checkuserRegex = /{{\s*check(?:user|ip)\s*\|\s*(?:1=)?\s*([^\|}]*?)\s*(?:\|master name\s*=\s*.*)?}}/gi
const results = pagetext.match(checkuserRegex)
const likelyusers = []
const likelyips = []
const possibleusers = []
const possibleips = []
likelyusers.push(spiHelperCaseName)
if (results) {
for (let i = 0; i < results.length; i++) {
const username = spiHelperNormalizeUsername(results[i].replace(checkuserRegex, '$1'))
const isIP = mw.util.isIPAddress(username, true)
if (!isIP && !likelyusers.includes(username)) {
likelyusers.push(username)
} else if (isIP && !likelyips.includes(username)) {
likelyips.push(username)
}
}
}
// eslint-disable-next-line no-useless-escape
const userRegex = /{{\s*(?:user|vandal|IP|noping|noping2)[^\|}{]*?\s*\|\s*(?:1=)?\s*([^\|}]*?)\s*}}/gi
const userresults = pagetext.match(userRegex)
if (userresults) {
for (let i = 0; i < userresults.length; i++) {
const username = spiHelperNormalizeUsername(userresults[i].replace(userRegex, '$1'))
if (mw.util.isIPAddress(username, true) && !possibleips.includes(username) &&
!likelyips.includes(username)) {
possibleips.push(username)
} else if (!possibleusers.includes(username) &&
!likelyusers.includes(username)) {
possibleusers.push(username)
}
}
}
// Wire up the "select all" options
$('#spiHelper_block_doblock', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
$('#spiHelper_block_acb', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
$('#spiHelper_block_ab', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
$('#spiHelper_block_tp', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
$('#spiHelper_block_email', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
$('#spiHelper_block_lock', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
$('#spiHelper_block_lock', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
spiHelperGenerateSelect('spiHelper_block_tag', spiHelperTagOptions)
$('#spiHelper_block_tag', $actionView).on('change', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
spiHelperGenerateSelect('spiHelper_block_tag_altmaster', spiHelperAltMasterTagOptions)
$('#spiHelper_block_tag_altmaster', $actionView).on('change', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
$('#spiHelper_block_lock', $actionView).on('click', function (e) {
spiHelperSetAllBlockOpts($(e.target))
})
for (let i = 0; i < likelyusers.length; i++) {
spiHelperUserCount++
spiHelperGenerateBlockTableLine(likelyusers[i], true, spiHelperUserCount)
}
for (let i = 0; i < likelyips.length; i++) {
spiHelperUserCount++
spiHelperGenerateBlockTableLine(likelyips[i], true, spiHelperUserCount)
}
for (let i = 0; i < possibleusers.length; i++) {
spiHelperUserCount++
spiHelperGenerateBlockTableLine(possibleusers[i], false, spiHelperUserCount)
}
for (let i = 0; i < possibleips.length; i++) {
spiHelperUserCount++
spiHelperGenerateBlockTableLine(possibleips[i], false, spiHelperUserCount)
}
} else {
$('#spiHelper_blockTagView', $actionView).hide()
}
if (!spiHelperActionsSelected.Close) {
$('#spiHelper_closeView', $actionView).hide()
}
if (spiHelperActionsSelected.Rename) {
if (spiHelperSectionId) {
$('#spiHelper_moveHeader', $actionView).text('Move section "' + spiHelperSectionName + '"')
} else {
$('#spiHelper_moveHeader', $actionView).text('Move/merge full case')
}
} else {
$('#spiHelper_moveView', $actionView).hide()
}
if (!spiHelperActionsSelected.Archive) {
$('#spiHelper_archiveView', $actionView).hide()
}
// Only give the option to comment if we selected a specific section
if (spiHelperSectionId) {
// generate the note prefixes
/** @type {SelectOption[]} */
const spiHelperNoteTemplates = [
{ label: 'Comment templates', selected: true, value: '', disabled: true }
]
if (spiHelperIsClerk()) {
spiHelperNoteTemplates.push({ label: 'Clerk note', selected: false, value: 'clerknote' })
}
if (spiHelperIsAdmin()) {
spiHelperNoteTemplates.push({ label: 'Administrator note', selected: false, value: 'adminnote' })
}
if (spiHelperIsCheckuser()) {
spiHelperNoteTemplates.push({ label: 'CU note', selected: false, value: 'cunote' })
}
spiHelperNoteTemplates.push({ label: 'Note', selected: false, value: 'takenote' })
// Wire up the select boxes
spiHelperGenerateSelect('spiHelper_noteSelect', spiHelperNoteTemplates)
$('#spiHelper_noteSelect', $actionView).on('change', function (e) {
spiHelperInsertNote($(e.target))
})
spiHelperGenerateSelect('spiHelper_adminSelect', spiHelperAdminTemplates)
$('#spiHelper_adminSelect', $actionView).on('change', function (e) {
spiHelperInsertTextFromSelect($(e.target))
})
spiHelperGenerateSelect('spiHelper_cuSelect', spiHelperCUTemplates)
$('#spiHelper_cuSelect', $actionView).on('change', function (e) {
spiHelperInsertTextFromSelect($(e.target))
})
$('#spiHelper_previewLink', $actionView).on('click', function () {
spiHelperPreviewText()
})
} else {
$('#spiHelper_commentView', $actionView).hide()
}
// Wire up the submit button
$('#spiHelper_performActions', $actionView).on('click', () => {
spiHelperPerformActions()
})
// Hide items based on role
if (!spiHelperIsCheckuser()) {
// Hide CU options from non-CUs
$('.spiHelper_cuClass', $actionView).hide()
}
if (!spiHelperIsAdmin()) {
// Hide block options from non-admins
$('.spiHelper_adminClass', $actionView).hide()
}
if (!(spiHelperIsAdmin() || spiHelperIsClerk())) {
$('.spiHelper_adminClerkClass', $actionView).hide()
}
}
/**
* Archives everything on the page that's eligible for archiving
*/
async function spiHelperOneClickArchive () {
'use strict'
spiHelperActiveOperations.set('oneClickArchive', 'running')
const pagetext = await spiHelperGetPageText(spiHelperPageName, false)
spiHelperCaseSections = await spiHelperGetInvestigationSectionIDs()
if (!spiHelperSectionRegex.test(pagetext)) {
alert('Looks like the page has been archived already.')
spiHelperActiveOperations.set('oneClickArchive', 'successful')
return
}
displayMessage('<ul id="spiHelper_status"/>')
await spiHelperArchiveCase()
await spiHelperPurgePage(spiHelperPageName)
const logMessage = '* [[' + spiHelperPageName + ']]: used one-click archiver ~~~~~'
if (spiHelperSettings.log) {
spiHelperLog(logMessage)
}
$('#spiHelper_status', document).append($('<li>').text('Done!'))
spiHelperActiveOperations.set('oneClickArchive', 'successful')
}
/**
* Another "meaty" function - goes through the action selections and executes them
*/
async function spiHelperPerformActions () {
'use strict'
spiHelperActiveOperations.set('mainActions', 'running')
// Again, reduce the search scope
const $actionView = $('#spiHelper_actionViewDiv', document)
// set up a few function-scoped vars
let comment = ''
let cuBlock = false
let cuBlockOnly = false
let newCaseStatus = 'noaction'
let renameTarget = ''
/** @type {boolean} */
const blankTalk = $('#spiHelper_blanktalk', $actionView).prop('checked')
/** @type {boolean} */
const overrideExisting = $('#spiHelper_override', $actionView).prop('checked')
/** @type {boolean} */
const hideLockNames = $('#spiHelper_hidelocknames', $actionView).prop('checked')
if (spiHelperActionsSelected.Case_act) {
newCaseStatus = $('#spiHelper_CaseAction', $actionView).val().toString()
}
if (spiHelperActionsSelected.SpiMgmt) {
spiHelperArchiveNoticeParams.deny = $('#spiHelper_spiMgmt_deny', $actionView).prop('checked')
spiHelperArchiveNoticeParams.xwiki = $('#spiHelper_spiMgmt_crosswiki', $actionView).prop('checked')
}
if (spiHelperSectionId) {
comment = $('#spiHelper_CommentText', $actionView).val().toString()
}
if (spiHelperActionsSelected.Block) {
if (spiHelperIsCheckuser()) {
cuBlock = $('#spiHelper_cublock', $actionView).prop('checked')
cuBlockOnly = $('#spiHelper_cublockonly', $actionView).prop('checked')
}
if (spiHelperIsAdmin() && !$('#spiHelper_noblock', $actionView).prop('checked')) {
const masterNotice = $('#spiHelper_blocknoticemaster', $actionView).prop('checked')
const sockNotice = $('#spiHelper_blocknoticesocks', $actionView).prop('checked')
for (let i = 1; i <= spiHelperUserCount; i++) {
if ($('#spiHelper_block_doblock' + i, $actionView).prop('checked')) {
if (!$('#spiHelper_block_username' + i, $actionView).val().toString()) {
// Skip blank usernames, empty string is falsey
continue
}
let noticetype = ''
if (masterNotice && $('#spiHelper_block_tag' + i, $actionView).val().toString().includes('master')) {
noticetype = 'master'
} else if (sockNotice) {
noticetype = 'sock'
}
/** @type {BlockEntry} */
const item = {
username: spiHelperNormalizeUsername($('#spiHelper_block_username' + i, $actionView).val().toString()),
duration: $('#spiHelper_block_duration' + i, $actionView).val().toString(),
acb: $('#spiHelper_block_acb' + i, $actionView).prop('checked'),
ab: $('#spiHelper_block_ab' + i, $actionView).prop('checked'),
ntp: $('#spiHelper_block_tp' + i, $actionView).prop('checked'),
nem: $('#spiHelper_block_email' + i, $actionView).prop('checked'),
tpn: noticetype
}
spiHelperBlocks.push(item)
}
if ($('#spiHelper_block_lock' + i, $actionView).prop('checked')) {
spiHelperGlobalLocks.push($('#spiHelper_block_username' + i, $actionView).val().toString())
}
if ($('#spiHelper_block_tag' + i).val() !== '') {
if (!$('#spiHelper_block_username' + i, $actionView).val().toString()) {
// Skip blank entries
continue
}
const item = {
username: spiHelperNormalizeUsername($('#spiHelper_block_username' + i, $actionView).val().toString()),
tag: $('#spiHelper_block_tag' + i, $actionView).val().toString(),
altmasterTag: $('#spiHelper_block_tag_altmaster' + i, $actionView).val().toString(),
blocking: $('#spiHelper_block_doblock' + i, $actionView).prop('checked')
}
spiHelperTags.push(item)
}
}
} else {
for (let i = 1; i <= spiHelperUserCount; i++) {
if (!$('#spiHelper_block_username' + i, $actionView).val().toString()) {
// Skip blank entries
continue
}
if ($('#spiHelper_block_tag' + i, $actionView).val() !== '') {
const item = {
username: spiHelperNormalizeUsername($('#spiHelper_block_username' + i, $actionView).val().toString()),
tag: $('#spiHelper_block_tag' + i, $actionView).val().toString(),
altmasterTag: $('#spiHelper_block_tag_altmaster' + i, $actionView).val().toString(),
blocking: false
}
spiHelperTags.push(item)
}
if ($('#spiHelper_block_lock' + i, $actionView).prop('checked')) {
spiHelperGlobalLocks.push(spiHelperNormalizeUsername($('#spiHelper_block_username' + i, $actionView).val().toString()))
}
}
}
}
if (spiHelperActionsSelected.Close) {
spiHelperActionsSelected.Close = $('#spiHelper_CloseCase', $actionView).prop('checked')
}
if (spiHelperActionsSelected.Rename) {
renameTarget = spiHelperNormalizeUsername($('#spiHelper_moveTarget', $actionView).val().toString())
}
if (spiHelperActionsSelected.Archive) {
spiHelperActionsSelected.Archive = $('#spiHelper_ArchiveCase', $actionView).prop('checked')
}
displayMessage('<ul id="spiHelper_status" />')
const $statusAnchor = $('#spiHelper_status', document)
let sectionText = await spiHelperGetPageText(spiHelperPageName, true, spiHelperSectionId)
let editsummary = ''
let logMessage = '* [[' + spiHelperPageName + ']]'
if (spiHelperSectionId) {
logMessage += ' (section ' + spiHelperSectionName + ')'
} else {
logMessage += ' (full case)'
}
logMessage += ' ~~~~~'
if (spiHelperSectionId !== null) {
let caseStatusResult = spiHelperCaseStatusRegex.exec(sectionText)
if (caseStatusResult === null) {
sectionText = sectionText.replace('===', '{{SPI case status|}}\n===')
caseStatusResult = spiHelperCaseStatusRegex.exec(sectionText)
}
const oldCaseStatus = caseStatusResult[1] || 'open'
if (newCaseStatus === 'noaction') {
newCaseStatus = oldCaseStatus
}
if (spiHelperActionsSelected.Case_act && newCaseStatus !== 'noaction') {
switch (newCaseStatus) {
case 'reopen':
newCaseStatus = 'open'
editsummary = 'Reopening'
break
case 'open':
editsummary = 'Marking request as open'
break
case 'CUrequest':
editsummary = 'Adding checkuser request'
break
case 'admin':
editsummary = 'Requesting admin action'
break
case 'clerk':
editsummary = 'Requesting clerk action'
break
case 'selfendorse':
newCaseStatus = 'endorse'
editsummary = 'Adding checkuser request (self-endorsed for checkuser attention)'
break
case 'checked':
editsummary = 'Marking request as checked'
break
case 'inprogress':
editsummary = 'Marking request in progress'
break
case 'decline':
editsummary = 'Declining checkuser'
break
case 'cudecline':
editsummary = 'CU declining checkuser'
break
case 'endorse':
editsummary = 'Endorsing for checkuser attention'
break
case 'cuendorse':
editsummary = 'CU endorsing for checkuser attention'
break
case 'moreinfo': // Intentional fallthrough
case 'cumoreinfo':
editsummary = 'Requesting additional information'
break
case 'relist':
editsummary = 'Relisting case for another check'
break
case 'hold':
editsummary = 'Putting case on hold'
break
case 'cuhold':
editsummary = 'Placing checkuser request on hold'
break
case 'noaction':
// Do nothing
break
default:
console.error('Unexpected case status value ' + newCaseStatus)
}
logMessage += '\n** changed case status from ' + oldCaseStatus + ' to ' + newCaseStatus
}
}
if (spiHelperActionsSelected.SpiMgmt) {
const newArchiveNotice = spiHelperMakeNewArchiveNotice(spiHelperCaseName, spiHelperArchiveNoticeParams)
sectionText = sectionText.replace(spiHelperArchiveNoticeRegex, newArchiveNotice)
if (editsummary) {
editsummary += ', update archivenotice'
} else {
editsummary = 'Update archivenotice'
}
logMessage += '\n** Updated archivenotice'
}
if (spiHelperActionsSelected.Block) {
let sockmaster = ''
let altmaster = ''
let needsAltmaster = false
spiHelperTags.forEach(async (tagEntry) => {
// we do not support tagging IPs
if (mw.util.isIPAddress(tagEntry.username, true)) {
// Skip, this is an IP
return
}
if (tagEntry.tag.includes('master')) {
sockmaster = tagEntry.username
}
if (tagEntry.altmasterTag !== '') {
needsAltmaster = true
}
})
if (sockmaster === '') {
sockmaster = prompt('Please enter the name of the sockmaster: ', spiHelperCaseName) || spiHelperCaseName