- Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathperm_clerk.rb
1138 lines (941 loc) · 42.5 KB
/
perm_clerk.rb
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
$LOAD_PATH << '..'
require'musikbot'
modulePermClerk
COMMENT_INDENT="\n:".freeze
COMMENT_PREFIX='{{comment|Automated comment}} '.freeze
SPLIT_KEY='====[[User:'.freeze
AWB_CHECKPAGE='Wikipedia:AutoWikiBrowser/CheckPageJSON'.freeze
defself.run
@mb=MusikBot::Session.new(inspect)
@denied_cache={}
@user_info_cache={}
@user_links_cache={}
@archive_changes={}
@errors={}
@total_user_count=0
@mb.config[:pages].keys.eachdo |permission|
@permission=permission.to_s
@edit_summaries=[]
@headers_removed={}
@users_count=0
begin
@flag_as_ran=false
process_permission
if@flag_as_ran
@total_user_count += @users_count
run_status[@permission]=@mb.now.to_s
end
rescue=>e
@mb.report_error("Failed to process #{permission}",e)
@errors[@permission]=@errors[@permission].to_a << {
group: 'fatal',
message: 'Failed for unknown reasons. Check the [[User:MusikBot/PermClerk/Error log|error log]] ' \
'and contact the [[User talk:MusikAnimal|bot operator]] if you are unable to resolve the issue.'
}
end
end
archive_requestsif@archive_changes.any?
generate_report
@mb.local_storage(run_status)
info("#{'~' * 25} Task complete #{'~' * 25}")
rescue=>e
@mb.report_error('Fatal error',e)
end
defself.process_permission(throttle=0)
info("Processing #{@permission}...")
page_name="Wikipedia:Requests for permissions/#{@permission}"
old_wikitext=page_props(page_name)
returnunlessold_wikitext.present? && formatting_check(old_wikitext)
last_run=@mb.parse_date(run_status[@permission])
ifprereqs# if prereqs enabled for this permission
has_prereq_data=old_wikitext.match(/\<!-- mb-\w*(?:Count|Age) --\>/)
should_check_prereq_data=has_prereq_data.present?
else
should_check_prereq_data=false
end
# only process if there's data to update, the page has changed since the last run, or 90 minutes has passed
if@mb.env == :production && !should_check_prereq_data && last_run > @last_edit && last_run + Rational(90,1440) > @mb.now
returninfo(' Less than 90 minutes since last run without changes, and no prerequisites to update')
else
@flag_as_ran=true
end
if@mb.config[:run][:autoformat]
info('Checking for extraneous headers')
old_wikitext=remove_headers(old_wikitext)
end
# first make fixes to confirm to what SPLIT_KEY looks for
old_wikitext.gsub!(/\=\=\=\=\s+\[\[User:/,'====[[User:')
@new_wikitext=[]
sections=old_wikitext.split(SPLIT_KEY)
@new_wikitext << sections.shift
@num_open_requests=0
@open_timestamps=[]
sections.eachdo |section|
process_section(section)
end
@new_wikitext=@new_wikitext.map{ |aa| aa.chomp('')}.join("\n\n")
admin_backlog
if@edit_summaries.any?
@mb.edit(page_name,
content: @new_wikitext,
summary: perm_edit_summary,
conflicts: true
)
else
info('Nothing to do this time around')
end
rescueMediaWiki::APIError=>e
ifthrottle > 3
@mb.report_error('Edit throttle hit',e)
elsife.code.to_s == 'editconflict'
process_permission(throttle + 1)
else
raise
end
end
defself.process_section(section)
@section=section
@request_changes=[]
username=@section.scan(/{{(?:template\:)?rfplinks\|1=(.*?)}}/i).flatten[0]
returnSPLIT_KEY + @sectionunlessusername
username[0]=username[0].capitalize
username.strip!
@username=username.descore
info("Checking section for User:#{@username}...")
timestamps=@section.scan(/(?<!\<!-- mbdate --\> )\b\d\d:\d\d, \d+ \w+ \d{4} \(UTC\)(?!\)\<!-- mb-temp-perm -->)/)
@newest_timestamp=@mb.parse_date(timestamps.min{ |a,b| @mb.parse_date(b) <=> @mb.parse_date(a)})
@request_timestamp=@mb.parse_date(timestamps.min{ |a,b| @mb.parse_date(a) <=> @mb.parse_date(b)})
overriden_resolution=if@section =~ %r{\{\{User:MusikBot/override\|d\}\}}
'done'
elsif@section =~ /\{\{User:MusikBot\/override\|nd\}\}/i
'notdone'
else
false
end
info(' Resolution override found')ifoverriden_resolution
done_regex=@mb.config[:archive_config][:done]
notdone_regex=@mb.config[:archive_config][:notdone]
revoked_regex=@mb.config[:archive_config][:revoked]
resolution=ifoverriden_resolution
overriden_resolution
elsif@section =~ /(?:#{revoked_regex})/i
'revoked'
elsif@section =~ /(?:#{done_regex})/i
'done'
elsif@section =~ /(?:#{notdone_regex})/i
'notdone'
else
false
end
@num_open_requests += 1unlessresolution
# archiving has precedence; e.g. if we are archiving, we don't do anything else for this section
returnifarchiving(resolution,overriden_resolution,@newest_timestamp)
# determine if there's any else to be done
ifresolution
info(" #{@username}'s request already responded to")
@new_wikitext << SPLIT_KEY + @section
return
end
@open_timestamps << timestamps.min{ |a,b| @mb.parse_date(a) <=> @mb.parse_date(b)}
@should_update_prereq_data=should_update_prereq_data
if@section.match(/\{\{comment\|Automated comment\}\}.*MusikBot/) && !@should_update_prereq_data
info(" MusikBot has already commented on #{username}'s request and no prerequisite data to update")
# We still want to do the autorespond task.
ifautorespond
returnqueue_changes
else
@new_wikitext << SPLIT_KEY + @section
return
end
end
# these tasks have already been ran if we're just updating prereq data
unless@should_update_prereq_data
# autoformat first, especially the case for Confirmed where they have a malformed report and are already autoconfirmed
autoformat
# 1) Checks if the right was already temporarily granted, and comments if so.
# 2) Otherwise, run the autorespond task to close the request if they already have the right.
if !check_temp_granted && autorespond
returnqueue_changes
end
fetch_declined
check_revoked
end
prerequisites
queue_changes
rescue=>e
@new_wikitext << SPLIT_KEY + @section
ife.message == 'unknown_user'
returnrecord_error(
group: 'fatal',
message: "[[User:#{@username}]] not found! There may be a typo or the account was renamed.",
log_message: 'User not found'
)
end
record_error(
group: 'fatal',
message: "Unknown error occurred when processing section for [[User:#{@username}]]. " \
'Check the [[User:MusikBot/PermClerk/Error log|error log]] and contact the ' \
'[[User talk:MusikAnimal|bot operator]] if you are unable to resolve the issue.'
)
end
# Core tasks
defself.fetch_declined
returnif !@mb.config[:run][:fetch_declined] || @permission == 'Confirmed'
info(" Searching for declined #{@permission} requests by #{@username}...")
# cache for a day
links=@mb.cache("mb-#{@username}-#{@permission}-declined",86_400)do
find_links
end
links=JSON.parse(links)iflinks.is_a?(String)
iflinks.any?
info(' Found previously declined requests')
links_message=links.map{ |l| "[#{l}]"}.join
@request_changes << {
type: :fetchdeclined,
numDeclined: links.length,
declinedLinks: links_message
}
@edit_summaries << :fetchdeclined
end
end
defself.find_links
target_date=@mb.today - @mb.config[:fetchdeclined_config][:offset]
links=[]
dates_to_fetch=(target_date..@mb.today).select{ |d| d.day == target_date.day || d.day == @mb.today.day}.uniq(&:month)
dates_to_fetch.eachdo |date|
key="#{Date::MONTHNAMES[date.month]}#{date.year}"
if@denied_cache[key]
info(" Cache hit for #{key}")
page=@denied_cache[key]
else
page=@mb.get("Wikipedia:Requests for permissions/Denied/#{key}")
@denied_cache[key]=page
end
nextunlesspage
decline_days=page.split(/==\s*\w+\s+/i)
decline_days.eachdo |decline_day|
day_number=decline_day.scan(/^(\d+)\s*==/).flatten[0].to_i
nextifday_number == 0
decline_day_date=@mb.parse_date("#{date.year}-#{date.month}-#{day_number}")
match=decline_day.scan(/\{\{Usercheck.*\|#{Regexp.escape(@username).descore}}}.*#{@permission}\]\].*(https?:\/\/.*)\s+link\]/i)[0]
links << match.flatten[0]ifdecline_day_date >= target_date && match
end
end
links
end
defself.autorespond
unless@mb.config[:run][:autorespond] && api_relevant_permission && !@section.match(/<\!-- (mb-autorespond|mb-temp-perm) -->/)
returnfalse
end
info(" User has permission #{@permission}")
# FIXME: the `@permission != 'Autopatrolled'` is a hack; see api_relevant_permission() for more.
if(sysop? && @permission != 'Autopatrolled') || @permission == 'AutoWikiBrowser'
# if sysop, no need to do other checks
# for AWB just say "already done" as it's too expensive to figure out when they were added
time_granted=@request_timestamp
elsifapi_relevant_permission == 'autoconfirmed'
# make time_granted earlier than request_timestamp to force marking as done
time_granted=@request_timestamp - 1
else
event=fetch_last_granted
time_granted=@mb.parse_date(event.attributes['timestamp'])
end
iftime_granted <= @request_timestamp
request_change={
type: :autorespond,
resolution: '{{already done}}'
}
# Check if the permission was granted within the past N hours.
elsif@mb.now > time_granted + Rational(@mb.config[:autorespond_config][:offset].to_i,24)
info(' Admin apparently forgot to respond to the request')
request_change={
type: :autorespond_admin_forgot,
resolution: '{{already done}}',
admin: event.attributes['user']
}
else
info(' Admin has not responded to request yet')
# return true to skip other checks, as they've already got the right
returntrue
end
@request_changes << {
permission: api_relevant_permission,
sysop: sysop?
}.merge(request_change)
@num_open_requests -= 1
@edit_summaries << :autorespond
true
end
defself.check_temp_granted
expiry=get_user_info(@username)[:userGroups][@mb.config[:pages][@permission.to_sym]]
returnfalseifexpiry.nil?
info(" User currently has the permission temporarily")
event=fetch_last_granted
log_timestamp=@mb.parse_date(event.attributes['timestamp'])
# offset by 1 second since it will show everything _before_ the given timestamp
time_granted=log_timestamp.strftime('%Y%m%d%H%M') + (log_timestamp.second + 1).to_s
@request_changes << {
type: :temp_granted,
permission: @permission.downcase,
admin: event.attributes['user'],
granted: time_granted,
expiry: expiry
}
@edit_summaries << :temp_granted
true
end
defself.autoformat
returnunless@mb.config[:run][:autoformat]
fragmented_regex=/\{\{rfplinks.*\}\}\n:(Reason for requesting (?:#{@permission.downcase}) (?:rights|access))(?m:(.*?)(?:\n\=\=|\z))/
fragmented_match=@section.scan(fragmented_regex)
unlessfragmented_match.present?
if@headers_removed[@username] && @headers_removed[@username].present?
@request_changes << {type: :autoformat}
@edit_summaries << :autoformat
end
return
end
info(" Found improperly formatted request for #{@username}, repairing")
actual_reason=fragmented_match.flatten[1]
ifactual_reason.empty? && @headers_removed[@username]
actual_reason=@headers_removed[@username]
else
@section.gsub!(actual_reason,'')
loopdo
frag_match=@section.match(fragmented_regex)
iffrag_match && frag_match[2] != '' && !(frag_match[2].include?('UTC') && !frag_match[2].include?(@username))
reason_part=frag_match[2]
actual_reason += "\n:#{reason_part}"
@section.gsub!(reason_part,'')
else
break
end
end
end
actual_reason=actual_reason.gsub(/^\s*\n+/,'').chomp('')
if@headers_removed[@username]
actual_reason="#{@headers_removed[@username].strip}: #{actual_reason.strip}"
end
@section.gsub!(fragmented_match.flatten[0],actual_reason)
duplicate_sig=@section.scan(/.*\(UTC\)(.*\(UTC\))/)
ifduplicate_sig.any?
info(' Duplicate signature found, repairing')
sig=duplicate_sig.flatten[0]
@section=@section.sub(sig,'')
end
@request_changes << {type: :autoformat}
@edit_summaries << :autoformat
end
defself.prerequisites
returnunless@mb.config[:run][:prerequisites] && prereqs.present? && @permission != 'Confirmed'# && !@username.downcase.match(/bot$/)
info(" Checking if #{@username} meets configured prerequisites...")
if@mb.redis_client.get("mb-#{@username}-#{@permission}-qualified")
returninfo(' Cache hit, user meets criteria')
end
updating_prereq=@section.match(/\<!-- mb-\w*(?:Count|Age) --\>/)
user_info=get_user_info(@username,prereqs.keys)
prereqs.eachdo |key,value|
pass=user_info[key] >= valuerescuenil
nextifpass.nil? && user_info && user_info[:editCount] > 50_000
ifpass.nil?
record_error(
group: 'prerequisites',
message: "Failed to fetch data {{mono|#{key}}} for User:#{@username}",
log_message: " failed to fetch prerequisite data: #{key}"
)
elsifpass
info(' User meets criteria')
@mb.redis_client.set("mb-#{@username}-#{@permission}-qualified",true)
elsifupdating_prereq
prereq_count_regex=@section.scan(/(\<!-- mb-#{key} --\>(.*)\<!-- mb-#{key}-end --\>)/)
prereq_text=prereq_count_regex.flatten[0]
prereq_count=prereq_text.nil? ? 0 : prereq_count_regex.flatten[1].to_i
if !user_info[key.to_sym].nil? && user_info[key.to_sym].to_i > prereq_count && prereq_count > 0
@section.gsub!(prereq_text,"<!-- mb-#{key} -->#{user_info[key.to_sym].to_i}<!-- mb-#{key}-end -->")
@section.gsub!(@prereq_signature,'~~~~')
info(' Prerequisite data updated')
@request_changes << {type: :prerequisitesUpdated}
@edit_summaries << :prerequisitesUpdated
else
info(' Update not needed')
end
elsif !pass
info(" Found unmet prerequisite: #{key}")
@request_changes << {type: key}.merge(user_info)
@edit_summaries << :prerequisites
end
end
end
defself.archiving(resolution,overriden_resolution,resolution_timestamp)
returnfalseunless@mb.config[:run][:archive] && resolution.present?
should_archive_now=@section.match(/\{\{(?:User:MusikBot\/)?archive\s?now\}\}/)
ifresolution_timestamp.nil?
record_error(
group: 'archive',
message: "User:#{@username} - Resolution template not dated",
log_message: " User:#{@username}: Resolution template not dated"
)
returntrue
end
# not time to archive
unlessshould_archive_now || @newest_timestamp + Rational(@mb.config[:archive_config][:offset].to_i,24) < @mb.now
returnfalse
end
ifshould_archive_now
info(' Found request for immediate archiving')
else
info(' Time to archive!')
end
# if we're archiving as done, check if they have the said permission and act accordingly (skip if overriding resolution)
ifresolution == 'done' && !overriden_resolution && !api_relevant_permission
if@section.include?('<!-- mbNoPerm -->')
warn(" MusikBot already reported that #{@username} does not have the permission #{@permission}")
@new_wikitext << SPLIT_KEY + @section
else
@request_changes << {
type: :noSaidPermission,
permission: @permission.downcase
}
@edit_summaries << :noSaidPermission
queue_changes
message=if@permission == 'AutoWikiBrowser'
"has not been added to the [[#{AWB_CHECKPAGE}|check page]]"
else
"does not have the permission #{@permission}"
end
record_error(
group: 'archive',
message: "User:#{@username}#{message}. " \
'Use <code><nowiki>{{subst:User:MusikBot/override|d}}</nowiki></code> to archive as approved or ' \
'<code><nowiki>{{subst:User:MusikBot/override|nd}}</nowiki></code> to archive as declined',
log_message: " #{@username}#{message}"
)
end
returntrue
elsifresolution == 'revoked' && !overriden_resolution && api_relevant_permission
if@section.include?('<!-- mbHasPerm -->')
warn(" MusikBot already reported that #{@username} still has the permission #{@permission}")
@new_wikitext << SPLIT_KEY + @section
else
@request_changes << {
type: :saidPermission,
permission: @permission.downcase
}
@edit_summaries << :saidPermission
queue_changes
message=if@permission == 'AutoWikiBrowser'
"has not been added to the [[#{AWB_CHECKPAGE}|check page]]"
else
"does not have the permission #{@permission}"
end
record_error(
group: 'archive',
message: "User:#{@username}#{message}. " \
'Use <code><nowiki>{{subst:User:MusikBot/override|d}}</nowiki></code> to archive as approved or ' \
'<code><nowiki>{{subst:User:MusikBot/override|nd}}</nowiki></code> to archive as declined',
log_message: " #{@username}#{message}"
)
end
returntrue
end
resolution_page_name=resolution == 'done' || resolution == 'revoked' ? 'Approved' : 'Denied'
info(" archiving as #{resolution_page_name.upcase}")
archive_key="#{resolution_page_name}/#{Date::MONTHNAMES[resolution_timestamp.month]}#{resolution_timestamp.year}"
archive_set=@archive_changes[archive_key].to_a << {
username: @username,
permission: @permission,
revision_id: @revision_id,
date: resolution_timestamp
}
@archive_changes[archive_key]=archive_set
@users_count += 1
@edit_summaries << "archive#{resolution_page_name}".to_sym
true
end
# Extensions to tasks
defself.queue_changes
if@request_changes.any?
info('***** Commentable data found *****')
@users_count += 1
@new_section=SPLIT_KEY + @section.chomp('')
@new_section += if@request_changes.index{ |obj| obj[:type] == :prerequisitesUpdated}
"\n"
else
message_compiler(@request_changes)
end
@new_wikitext << @new_section
else
info(' ~~ No commentable data found ~~')
@new_wikitext << SPLIT_KEY + @section
end
end
defself.archive_requests
num_requests=@archive_changes.values.flatten.length
info("***** Archiving #{num_requests} requests *****")
@archive_changes.keys.eachdo |key|
page_to_edit="Wikipedia:Requests for permissions/#{key}"
month_name=key.scan(/\/(\w+)/).flatten[0]
year=key.scan(/\d{4}/).flatten[0]
page_wikitext=@mb.get(page_to_edit) || ''
new_page=page_wikitext.blank?
edit_summary="Archiving #{@archive_changes[key].length} request#{'s'if@archive_changes[key].length > 1}:"
# ensure there's a newline at the end
page_wikitext=page_wikitext.chomp('') + "\n"
# convert sections as a hash of format {"Month day" => "content"}
sections=Hash[*page_wikitext.split(/\=\=\s*(\w+ \d+)\s*\=\=/).drop(1).flatten(1)]
@archive_changes[key].eachdo |request|
edit_summary += " #{request[:username]} (#{request[:permission].downcase});"
archive_page_name="Wikipedia:Requests for permissions/#{request[:permission]}"
link_markup="*{{Usercheck-short|#{request[:username]}}} [[#{archive_page_name}]] " \
"<sup>[https://en.wikipedia.org/wiki/Special:PermaLink/#{request[:revision_id]}#User:#{request[:username].score} link]</sup>"
# add link_markup to section
section_key="#{month_name}#{request[:date].day}"
sections[section_key]=sections[section_key].to_s.gsub(/^\n|\n$/,'') + "\n" + link_markup + "\n"
end
edit_summary.chomp!(';')
# construct back to single wikitext string, sorted by day
new_wikitext=''
sorted_keys=sections.keys.sort_by{ |k| k.scan(/\d+/)[0].to_i}
sorted_keys.eachdo |sort_key|
new_wikitext += "\n== " + sort_key + " ==\n" + sections[sort_key].gsub(/^\n/,'')
end
# we're done archiving for this month
# first see if it's a new page and if so add it to the log page
ifnew_page
log_page_name="Wikipedia:Requests for permissions/#{key.scan(/(.*)\//).flatten[0]}"
info(" Adding new page [[#{page_to_edit}]] to log [[#{log_page_name}]]")
log_page=@mb.get(log_page_name)
# convert to {"year" => "requests"}
year_sections=Hash[*log_page.split(/\=\=\=\s*(\d{4})\s*\=\=\=/).drop(1)]
year_sections[year]="\n*[[#{page_to_edit}]]" + year_sections[year].to_s
log_page_wikitext=''
year_sections.sort{ |a,b| b <=> a}.to_h.keys.eachdo |year_section_key|
log_page_wikitext += "\n=== " + year_section_key + " ===\n" + year_sections[year_section_key].gsub(/^\n/,'')
end
info(" Attempting to write to page [[#{log_page_name}]]")
log_page_wikitext=log_page.split('===')[0].gsub(/^\n/,'') + "\n" + log_page_wikitext.gsub(/^\n/,'')
@mb.edit(log_page_name,
content: log_page_wikitext,
summary: "Adding entry for [[#{page_to_edit}]]"
)
end
info(" Attempting to write to page [[#{page_to_edit}]]")
@mb.edit(page_to_edit,
content: new_wikitext,
summary: edit_summary
)
end
end
defself.check_revoked
returnunless@mb.config[:run][:checkrevoked]
info(" Checking revocations of #{@permission} for #{@username}...")
revoke_type=:checkrevoked
if@permission == 'AutoWikiBrowser'
awb_report_page='User:MusikBot_II/AWBListMan/Report/User'
auto_revoke_report=@mb.get_page_props(awb_report_page,full_response: true,no_conflict: true)
ifauto_revoke_report.elements['revisions/rev/slots/slot'].text =~ /\{\{no ping\|#{Regexp.escape(@username)}\}\}/
revision_id=auto_revoke_report.attributes['lastrevid']
revoke_type=:awb_autorevoked
revocations=["#{@mb.gateway.wiki_url.chomp('api.php')}index.php?title=#{awb_report_page}&oldid=#{revision_id}"]
else
revocations=check_revoked_awb || []
end
else
revocations=check_revoked_perm.flatten
end
returnunlessrevocations.any?
@request_changes << {
type: revoke_type,
permission: @permission.downcase,
revokedLinks: revocations.map{ |l| "[#{l}]"}.join
}
@edit_summaries << :checkrevoked
end
defself.check_revoked_perm
revocations=[]
logevents=@mb.gateway.custom_query(
list: 'logevents',
letype: 'rights',
letitle: "User:#{@username}",
leprop: 'timestamp|details'
).elements['logevents'].to_a
normalized_perm=@mb.config[:pages][@permission.to_sym]
logevents.eachdo |event|
old_events=event.elements['params/oldgroups']
new_events=event.elements['params/newgroups']
in_old=old_events && old_events.collect(&:text).grep(normalized_perm).any?
in_new=new_events && new_events.collect(&:text).grep(normalized_perm).any?
timestamp=@mb.parse_date(event.attributes['timestamp'])
nextunlessin_old && !in_new && timestamp > @mb.today - @mb.config[:checkrevoked_config][:offset]
# offset by 1 second since it will show everything _before_ the given timestamp
log_timestamp=timestamp.strftime('%Y%m%d%H%M') + (timestamp.second + 1).to_s
revocations << "#{@mb.gateway.wiki_url.chomp('api.php')}index.php?title=Special:Log&" \
"page=User:#{@username.score}&type=rights&offset=#{log_timestamp}&limit=1"
end
revocations
end
defself.check_revoked_awb
old_awb_content=@mb.get_revision_at_date(
AWB_CHECKPAGE,
@mb.today - @mb.config[:checkrevoked_config][:offset]
)rescuenil
ifold_awb_content && old_awb_content.include?("\"#{@username}\"") && !awb_checkpage_content.include?("\"#{@username}\"")
return["#{@mb.gateway.wiki_url.chomp('api.php')}index.php?title=#{AWB_CHECKPAGE}&action=history"]
else
return[]
end
end
defself.admin_backlog
# always update for Account creator
is_account_creator=@permission == 'Account creator'
returnunless@mb.config[:run][:admin_backlog]
oldest_timestamp=@open_timestamps.compact.min{ |a,b| @mb.parse_date(a) <=> @mb.parse_date(b)}
min_num_requests=is_account_creator ? 0 : @mb.config[:adminbacklog_config][:requests]
has_old_requests=oldest_timestamp ? @mb.parse_date(oldest_timestamp) <= @mb.today - @mb.config[:adminbacklog_config][:offset] : false
backlogged=@new_wikitext.include?('{{WP:PERM/Backlog}}')
if@num_open_requests > 0 && (@num_open_requests >= min_num_requests || has_old_requests)
returnifbacklogged# no change
@edit_summaries << :backlog
info('{{WP:PERM/Backlog}}')
@new_wikitext.sub!('{{WP:PERM/Backlog|none}}','{{WP:PERM/Backlog}}')
elsifbacklogged
@edit_summaries << :no_backlog
info('{{WP:PERM/Backlog|none}}')
@new_wikitext.sub!('{{WP:PERM/Backlog}}','{{WP:PERM/Backlog|none}}')
end
end
defself.generate_report
errors_digest=Digest::MD5.hexdigest(@errors.values.join)
expired=@total_user_count > 0 && @mb.parse_date(run_status['report']) < @mb.now - Rational(6,24)
returnunlessrun_status['report_errors'] != errors_digest || expired
if@errors.keys.any?
num_errors=@errors.values.flatten.length
content='{{hidden|style=display:inline-block;background:transparent|headerstyle=padding-right:3.5em|header=' \
"<span style='color:red;font-weight:bold'>#{num_errors} error#{'s'ifnum_errors > 1} as of ~~~~~</span>|content="
@errors.keys.eachdo |permission_group|
content += "\n;[[Wikipedia:Requests for permissions/#{permission_group}|#{permission_group}]]\n"
@errors[permission_group].eachdo |error|
group=error[:group] == 'fatal' ? 'FATAL' : error[:group].capitalize
content += "* '''#{group}''': #{error[:message]}\n"
end
end
content += '}}'
else
content="<span style='color:green; font-weight:bold'>No errors!</span> Report generated at ~~~~~"
end
run_status['report']=@mb.now.to_s
run_status['report_errors']=errors_digest
info('Updating report...')
@mb.edit('User:MusikBot/PermClerk/Report',
content: content,
summary: 'Updating [[User:MusikBot/PermClerk|PermClerk]] report'
)
end
# Helpers
defself.sysop?
get_user_info(@username)[:userGroups].keys.include?('sysop')
end
defself.should_update_prereq_data
if@section =~ /\<!-- mb-/
prereq_sig_regex=@section.scan(/(\<!-- mbsig --\>.*\<!-- mbdate --\> (\d\d:\d\d.*\d{4} \(UTC\)))/)
@prereq_signature=prereq_sig_regex.flatten[0]
@prereq_timestamp=prereq_sig_regex.flatten[1]
if@mb.now > @mb.parse_date(@prereq_timestamp) + Rational(@mb.config[:prerequisites_config][:offset],1440)
info(' Found expired prerequisite data')
returntrue
else
info(" Prerequisite data under #{@mb.config[:prerequisites_config][:offset]} minutes old")
end
end
false
end
defself.formatting_check(old_wikitext)
ret=true
split_key_match=old_wikitext.scan(/\n(.*)[^\n]#{Regexp.escape(SPLIT_KEY)}(.*)\n/).flatten
ifsplit_key_match.any?
error("A request heading is not on its own line: #{split_key_match[0]}")
@errors[@permission]=@errors[@permission].to_a << {
group: 'formatting',
message: "Unable to process page! A request heading is not on its own line:\n*:" \
"<code style='color:red'><nowiki>#{split_key_match[0]}</nowiki></code><code><nowiki>#{SPLIT_KEY}#{split_key_match[1]}</nowiki></code>"
}
ret=false
end
ret
end
defself.remove_headers(old_wikitext)
headers_match=old_wikitext.scan(/(^\=\=[^\=]*\=\=([^\=]*)(\=\=\=\=[^\=]*\=\=\=\=\n\*.*rfplinks\|1=(.*)\}\}\n))/)
ifheaders_match.any?
info('Extraneous headers detected')
headers_match.eachdo |match|
name=match[3]
nextunlessname
original_markup=match[0]
level_two_text=match[1].delete("\n")
rfp_links_part=match[2]
old_wikitext.sub!(original_markup,rfp_links_part)
header_text=original_markup.scan(/\=\=\s*([^\=]*)\s*\=\=/)[0][0]
@headers_removed[name]=iflevel_two_text.length > header_text.length
level_two_text.gsub(/^\n*/,'').gsub(/\n$/,'')
else
header_text
end
end
end
old_wikitext
end
defself.headers_removed?
@headers_removed.any?
end
defself.get_message(type,params={})
casetype
when:accountAge
"has had an account for <!-- mb-accountAge -->#{params[:accountAge]}<!-- mb-accountAge-end --> days"
when:articleCount
"has created roughly <!-- mb-articleCount -->#{params[:articleCount]}<!-- mb-articleCount-end --> [[WP:ARTICLE|article#{'s'ifparams[:articleCount] != 1}]]"
when:autoformat
'An extraneous header or other inappropriate text was removed from this request'
when:autorespond
"#{'is a sysop and 'if(params[:sysop] && params[:permission] != 'autoreviewer')}already has #{params[:permission] == 'AutoWikiBrowser' ? 'AutoWikiBrowser access' : "the \"#{params[:permission]}\" user right"}"
when:temp_granted
log_link="#{@mb.gateway.wiki_url.chomp('api.php')}index.php?title=Special:Log&" \
"page=User:#{@username.score}&type=rights&offset=#{params[:granted]}&limit=1"
"was [#{log_link} granted] temporary #{params[:permission]} rights by {{no ping|#{params[:admin]}}} (expires #{@mb.wiki_date(params[:expiry])})<!-- mb-temp-perm -->"
when:autorespond_admin_forgot
"by {{no ping|#{params[:admin]}}}"
when:checkrevoked
"has had this permission revoked in the past #{@mb.config[:checkrevoked_config][:offset]} days (#{params[:revokedLinks]})"
when:awb_autorevoked
"has had their access to AutoWikiBrowser automatically revoked (#{params[:revokedLinks]})"
when:editCount
"has <!-- mb-editCount -->#{params[:editCount]}<!-- mb-editCount-end --> total edits"
when:fetchdeclined
"has had #{params[:numDeclined]} request#{'s'ifparams[:numDeclined].to_i > 1} for #{@permission.downcase} " \
"declined in the past #{@mb.config[:fetchdeclined_config][:offset]} days (#{params[:declinedLinks]})"
when:mainSpaceCount
"has <!-- mb-mainSpaceCount -->#{params[:mainSpaceCount]}<!-- mb-mainSpaceCount-end --> " \
"edit#{'s'ifparams[:mainSpaceCount] != 1} in the [[WP:MAINSPACE|mainspace]]"
when:manualMainSpaceCount
"has approximately <!-- mb-manualMainSpaceCount -->#{params[:manualMainSpaceCount]}<!-- mb-manualMainSpaceCount-end --> " \
"[https://xtools.wmflabs.org/autoedits/en.wikipedia.org/#{URI.escape(params[:username].score)}" \
" non-automated edit#{'s'ifparams[:manualMainSpaceCount] != 1}] in the [[WP:MAINSPACE|mainspace]]"
when:moduleSpaceCount
"has <!-- mb-moduleSpaceCount -->#{params[:moduleSpaceCount]}<!-- mb-moduleSpaceCount-end --> " \
"edit#{'s'ifparams[:moduleSpaceCount] != 1} in the [[WP:LUA|module namespace]]"
when:noSaidPermission
ifparams[:permission] == 'autowikibrowser'
"does not appear to have been added to the [[#{AWB_CHECKPAGE}|CheckPage]]<!-- mbNoPerm -->"
else
"does not appear to have the permission {{mono|#{params[:permission]}}}<!-- mbNoPerm -->"
end
when:saidPermission
ifparams[:permission] == 'autowikibrowser'
"is still on the [[#{AWB_CHECKPAGE}|CheckPage]]<!-- mbHasPerm -->"
else
"still holds the {{mono|#{params[:permission]}}} right<!-- mbHasPerm -->"
end
when:templateSpaceCount
"has <!-- mb-templateSpaceCount -->#{params[:templateSpaceCount]}<!-- mb-templateSpaceCount-end --> " \
"edit#{'s'ifparams[:templateSpaceCount] != 1} in the [[WP:TMP|template namespace]]"
when:templateAndModuleSpaceCount
"has <!-- mb-templateAndModuleSpaceCount -->#{params[:templateAndModuleSpaceCount]}<!-- mb-templateAndModuleSpaceCount-end --> " \
"edit#{'s'ifparams[:templateAndModuleSpaceCount] != 1} in the [[WP:TMP|template]] and [[WP:LUA|module]] namespaces"
end
end
defself.message_compiler(request_data)
str=''
ifindex=request_data.index{ |obj| obj[:type] == :autoformat}
request_data.delete_at(index)
str="#{COMMENT_INDENT}<small>#{COMMENT_PREFIX}#{get_message(:autoformat)} ~~~~</small>\n"
returnstrifrequest_data.empty?
end
str += ifindex=request_data.index{ |obj| obj[:type] == :autorespond}
"#{COMMENT_INDENT}#{request_data[index][:resolution]}<!-- mb-autorespond --> (automated response): This user "
elsifindex=request_data.index{ |obj| obj[:type] == :autorespond_admin_forgot}
"#{COMMENT_INDENT}#{request_data[index][:resolution]}<!-- mb-autorespond --> (automated response) "
else
COMMENT_INDENT + COMMENT_PREFIX + 'This user '
end
request_data.each_with_indexdo |data,i|
type=data.delete(:type).to_sym
str=str.chomp(', ') + ' and 'ifi == request_data.length - 1 && request_data.length > 1
str += get_message(type,data) + ', '
end
str.chomp(', ') + ". ~~~~\n"
end
defself.perm_edit_summary
summaries=[]
# get approved/denied counts
approved=@edit_summaries.count(:archiveApproved)
denied=@edit_summaries.count(:archiveDenied)
ifapproved + denied > 0
archive_msg=[]
archive_msg << "#{approved} approved"ifapproved > 0
archive_msg << "#{denied} denied"ifdenied > 0
archive_msg=archive_msg.join(', ')
summaries << "archiving (#{archive_msg})"
end
plural=@users_count > 1
summaries << "marked request#{'s'ifplural} as already done"if@edit_summaries.include?(:autorespond)
summaries << "repaired malformed request#{'s'ifplural}"if@edit_summaries.include?(:autoformat)
summaries << 'prerequisite data updated'if@edit_summaries.include?(:prerequisitesUpdated)
summaries << 'unmet prerequisites'if@edit_summaries.include?(:prerequisites)
summaries << 'found previously declined requests'if@edit_summaries.include?(:fetchdeclined)
summaries << 'found previous revocations'if@edit_summaries.include?(:checkrevoked)
summaries << 'unable to archive one or more requests'if@edit_summaries.include?(:noSaidPermission) || @edit_summaries.include?(:saidPermission)
summaries << '{{WP:PERM/Backlog}}'if@edit_summaries.include?(:backlog)
summaries << '{{WP:PERM/Backlog|none}}'if@edit_summaries.include?(:no_backlog)
request_count_msg=if@num_open_requests > 0
"#{@num_open_requests} open request#{'s'if@num_open_requests > 1} remaining"
else
'0 open requests remaining'
end
"Bot clerking#{" on #{@users_count} requests"ifplural}: #{summaries.join(', ')} (#{request_count_msg})"
end
# Config-related
defself.run_status
@run_status ||= @mb.local_storage
end
defself.prereqs
@mb.config[:run][:prerequisites] ? @mb.config[:prerequisites_config][@permission.to_sym] : nil
end
# API-related
defself.api_relevant_permission
info(" checking if #{@username} has permission #{@permission}")
# FIXME: right-hand conditional is a hack; should instead check if user already has the user right(s) that are part of the requested group
return@permissionifsysop? && @permission != 'Autopatrolled'
if@permission == 'AutoWikiBrowser'
awb_checkpage_content.include?("\"#{@username}\"") ? 'AutoWikiBrowser' : nil
else
get_user_info(@username)[:userGroups].keys.grep(/#{@mb.config[:pages][@permission.to_sym]}/).first
end
end
defself.awb_checkpage_content
@awb_checkpage_content ||= @mb.get(AWB_CHECKPAGE)
end
defself.get_user_info(username, *data_attrs)
data_attrs=data_attrs.flatten
# return cache if there's nothing new to fetch
if@user_info_cache[username] && data_attrs.all?{ |da| @user_info_cache[username].keys.include?(da)}
info(" cache hit for #{username}")
return@user_info_cache[username]
end
data_fetch_str=data_attrs.join(', ')
info(" Fetching data for: #{data_fetch_str.present? ? data_fetch_str : 'basic info'}")