-
Notifications
You must be signed in to change notification settings - Fork 12
/
UpdateWarningTools.java
1716 lines (1573 loc) · 57.8 KB
/
UpdateWarningTools.java
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
/*
* WPCleaner: A tool to help on Wikipedia maintenance tasks.
* Copyright (C) 2014 Nicolas Vervelle
*
* See README.txt file for licensing information.
*/
package org.wikipediacleaner.gui.swing.worker;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wikipediacleaner.api.API;
import org.wikipediacleaner.api.APIException;
import org.wikipediacleaner.api.APIFactory;
import org.wikipediacleaner.api.MediaWiki;
import org.wikipediacleaner.api.configuration.WPCConfiguration;
import org.wikipediacleaner.api.configuration.WPCConfigurationBoolean;
import org.wikipediacleaner.api.configuration.WPCConfigurationString;
import org.wikipediacleaner.api.configuration.WPCConfigurationStringList;
import org.wikipediacleaner.api.constants.EnumWikipedia;
import org.wikipediacleaner.api.data.DataManager;
import org.wikipediacleaner.api.data.Namespace;
import org.wikipediacleaner.api.data.Page;
import org.wikipediacleaner.api.data.PageElementTemplate;
import org.wikipediacleaner.api.data.QueryResult;
import org.wikipediacleaner.api.data.Section;
import org.wikipediacleaner.api.data.User;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.comment.CommentBuilder;
import org.wikipediacleaner.api.data.contents.template.TemplateBuilder;
import org.wikipediacleaner.gui.swing.basic.BasicWindow;
import org.wikipediacleaner.gui.swing.basic.BasicWorker;
import org.wikipediacleaner.gui.swing.basic.Utilities;
import org.wikipediacleaner.i18n.GT;
import org.wikipediacleaner.utils.Configuration;
import org.wikipediacleaner.utils.ConfigurationValueString;
/**
* Tools for updating warnings.
*/
public abstract class UpdateWarningTools {
private final static Logger log = LoggerFactory.getLogger(UpdateWarningTools.class);
/** Wiki. */
protected final EnumWikipedia wiki;
/** Wiki configuration. */
protected final WPCConfiguration configuration;
/** Worker. */
protected final BasicWorker worker;
/** Window. */
protected final BasicWindow window;
/** True for allowing warning creation. */
protected final boolean createWarning;
/** True if this is an automatic edit. */
protected final boolean automaticEdit;
/** Force use of section 0 in the talk page. */
protected final boolean section0;
/** MediaWiki API. */
protected final API api;
/** True if contents is already available in pages. */
private boolean contentsAvailable;
/** True if purge page cache should be attempted when errors are not found. */
private boolean usePurge;
/** True if this is a simulation. */
private boolean simulation;
/** Map for errors. */
private Map<String, List<String>> errorsMap;
/** List of articles titles supposed to have the error. */
private Set<String> articles;
/**
* @param wiki Wiki.
* @param worker Worker.
* @param window Window.
* @param createWarning Create warning if necessary.
* @param automaticEdit True if the edits are automatic.
*/
protected UpdateWarningTools(
EnumWikipedia wiki,
BasicWorker worker, BasicWindow window,
boolean createWarning, boolean automaticEdit) {
this.wiki = wiki;
this.configuration = wiki.getConfiguration();
this.worker = worker;
this.window = window;
this.createWarning = createWarning;
this.automaticEdit = automaticEdit;
this.usePurge = false;
this.section0 = useSection0();
this.api = APIFactory.getAPI();
}
/**
* @param available True if contents is already available in pages.
*/
public void setContentsAvailable(boolean available) {
this.contentsAvailable = available;
}
/**
* @return True if contents is already available in pages.
*/
public boolean getContentsAvailable() {
return contentsAvailable;
}
/**
* @param purge True if purge page cache should be attempted.
*/
public void setUsePurge(boolean purge) {
this.usePurge = purge;
}
/**
* @param simulation True if this is a simulation.
*/
public void setSimulation(boolean simulation) {
this.simulation = simulation;
}
/**
* Initialize the errors map.
*/
public void prepareErrorsMap() {
this.errorsMap = new HashMap<>();
}
/**
* @return Errors map.
*/
public Map<String, List<String>> getErrorsMap() {
return errorsMap;
}
// ==========================================================================
// Warning management
// ==========================================================================
/**
* Update warning for a list of pages.
*
* @param pages List of pages.
* @param creators For each page title, user who has created the page.
* @param modifiers For each page title, users who have modified the page.
* @param stats Statistics.
* @throws APIException Exception thrown by the API.
*/
public void updateWarning(
List<Page> pages,
Map<String, String> creators,
Map<String, List<String>> modifiers,
Stats stats) throws APIException {
if ((pages == null) || (pages.isEmpty())) {
return;
}
// Retrieve information in the pages
if (!retrievePageInformation(pages)) {
return;
}
// Deal with non encyclopedic pages
manageNonEncyclopedicPages(pages);
// Load talk pages and "To do" sub pages
Map<Page, Page> mapTalkPages = new HashMap<>();
Map<Page, Page> mapTodoSubpages = new HashMap<>();
for (Page page : pages) {
Page talkPage = page.getTalkPage();
mapTalkPages.put(page, talkPage);
String todoSubpageAttr = configuration.getString(WPCConfigurationString.TODO_SUBPAGE);
if (todoSubpageAttr != null) {
Page todoSubpage = talkPage.getSubPage(todoSubpageAttr);
mapTodoSubpages.put(page, todoSubpage);
}
}
if (canUpdateWarning()) {
MediaWiki mw = MediaWiki.getMediaWikiAccess(worker);
if (section0) {
mw.retrieveSectionContents(wiki, mapTalkPages.values(), 0, false);
} else {
mw.retrieveContents(wiki, mapTalkPages.values(), false, false, false, false);
}
mw.retrieveContents(wiki, mapTodoSubpages.values(), true, false, false, false);
if (mw.shouldStop()) {
return;
}
}
// Update warning
for (Page page : pages) {
PageAnalysis pageAnalysis = page.getAnalysis(page.getContents(), true);
boolean updated = updateWarning(
pageAnalysis, page.getRevisionId(),
mapTalkPages.get(page),
mapTodoSubpages.get(page),
(creators != null) ? creators.get(page.getTitle()) : null,
(modifiers != null) ? modifiers.get(page.getTitle()) : null,
stats);
if (updated) {
// log.debug("Page " + page.getTitle() + " has been updated.");
}
if (stats != null) {
stats.addAnalyzedPage(page);
if (updated) {
stats.addUpdatedPage(page);
}
}
}
return;
}
/**
* Manage talk pages present in the list.
*
* @param pages List of pages.
* @throws APIException Exception thrown by the API.
*/
private void manageNonEncyclopedicPages(List<Page> pages)
throws APIException {
if (pages == null) {
return;
}
Iterator<Page> itPage = pages.iterator();
List<Integer> encyclopedicNamespaces = configuration.getEncyclopedicNamespaces();
while (itPage.hasNext()) {
Page page = itPage.next();
if (!page.isArticle() ||
!encyclopedicNamespaces.contains(page.getNamespace())) {
itPage.remove();
if (!simulation) {
PageAnalysis analysis = page.getAnalysis(page.getContents(), true);
Collection<String> elements = constructWarningElements(analysis, null, null);
if ((elements == null) || (elements.isEmpty())) {
purgePage(page);
}
}
}
}
}
/**
* @return true if warnings can be updated.
*/
public boolean canUpdateWarning() {
List<String> todoTemplates = configuration.getStringList(WPCConfigurationStringList.TODO_TEMPLATES);
if ((todoTemplates == null) ||
(todoTemplates.isEmpty())) {
return false;
}
String warningTemplate = configuration.getString(getWarningTemplate());
if ((warningTemplate == null) || (warningTemplate.trim().length() == 0)) {
return false;
}
return true;
}
/**
* Update warning for a page.
*
* @param pageAnalysis Page analysis (must have enough information to compute the elements for the warning).
* @param pageRevId Page revision id.
* @param talkPage (Optional) Talk page with contents of section 0.
* @param todoSubpage (Optional) To do sub-page with contents.
* @param creator User who has created the page.
* @param modifiers Users who have modified the page.
* @param stats Statistics.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
public boolean updateWarning(
PageAnalysis pageAnalysis, Integer pageRevId,
Page talkPage, Page todoSubpage,
String creator, List<String> modifiers,
Stats stats) throws APIException {
if ((pageAnalysis == null) ||
(pageAnalysis.getPage() == null) ||
!pageAnalysis.getPage().isArticle()) {
return false;
}
List<String> todoTemplates = configuration.getStringList(WPCConfigurationStringList.TODO_TEMPLATES);
if ((todoTemplates == null) ||
(todoTemplates.isEmpty())) {
return false;
}
String warningTemplate = configuration.getString(getWarningTemplate());
if ((warningTemplate == null) || (warningTemplate.trim().length() == 0)) {
if (!simulation) {
return false;
}
}
Page page = pageAnalysis.getPage();
// Retrieving talk page contents
if (talkPage == null) {
talkPage = page.getTalkPage();
setText(GT._T("Retrieving page contents - {0}", talkPage.getTitle()));
if (section0) {
api.retrieveSectionContents(wiki, talkPage, 0);
} else {
api.retrieveContents(wiki, Collections.singletonList(talkPage), false, false);
}
}
// "To do" sub-page
String todoSubpageAttr = configuration.getString(WPCConfigurationString.TODO_SUBPAGE);
if (todoSubpageAttr != null) {
// Retrieving "To do" sub-page contents
if (todoSubpage == null) {
todoSubpage = talkPage.getSubPage(todoSubpageAttr);
setText(GT._T("Retrieving page contents - {0}", todoSubpage.getTitle()));
api.retrieveContents(wiki, Collections.singletonList(todoSubpage), false, false);
}
// If we force the use of "To do" sub-page, the warning must be on it
if ((page.getNamespace() != null) &&
(page.getNamespace().intValue() == Namespace.MAIN)) {
if (configuration.getBoolean(WPCConfigurationBoolean.TODO_SUBPAGE_FORCE)) {
return manageWarningOnTodoSubpage(
pageAnalysis, pageRevId, todoSubpage, talkPage,
creator, modifiers, stats);
}
} else if (configuration.getBoolean(WPCConfigurationBoolean.TODO_SUBPAGE_FORCE_OTHER)) {
return manageWarningOnTodoSubpage(
pageAnalysis, pageRevId, todoSubpage, talkPage,
creator, modifiers, stats);
}
// If "To do" sub-page exists, the warning must be on it
if (Boolean.TRUE.equals(todoSubpage.isExisting())) {
return manageWarningOnTodoSubpage(
pageAnalysis, pageRevId, todoSubpage, talkPage,
creator, modifiers, stats);
}
// If talk page has a template linking to the "To do" sub-page,
// the warning must be on the "To do" sub-page
PageElementTemplate templateTodoLink = getExistingTemplateTodoLink(talkPage, talkPage.getContents());
if (templateTodoLink != null) {
return manageWarningOnTodoSubpage(
pageAnalysis, pageRevId, todoSubpage, talkPage,
creator, modifiers, stats);
}
// If talk page has a link to the "To do" sub-page,
// the warning must be on the "To do" sub-page
/*api.retrieveLinks(wikipedia, talkPage, talkPage.getNamespace());
if (talkPage.getLinks() != null) {
for (Page link : talkPage.getLinks()) {
if (Page.areSameTitle(link.getTitle(), todoSubpage.getTitle())) {
return manageWarningOnTodoSubpage(pageAnalysis, pageRevId, todoSubpage, talkPage);
}
}
}*/
}
return manageWarningOnTalkPage(
pageAnalysis, pageRevId, talkPage,
creator, modifiers, stats);
}
/**
* Update warning on the "To do" sub-page.
*
* @param pageAnalysis Page analysis (must have enough information to compute the elements for the warning).
* @param pageRevId Page revision id.
* @param todoSubpage "To do" sub-page.
* @param talkPage Talk page.
* @param creator User who has created the page.
* @param modifiers Users who have modified the page.
* @param stats Statistics.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
private boolean manageWarningOnTodoSubpage(
PageAnalysis pageAnalysis, Integer pageRevId,
Page todoSubpage, Page talkPage,
String creator, List<String> modifiers,
Stats stats) throws APIException {
Collection<String> elements = constructWarningElements(pageAnalysis, talkPage, todoSubpage);
boolean result = false;
if ((elements == null) || (elements.isEmpty())) {
if (!simulation) {
boolean resultTodo = removeWarningOnTodoSubpage(todoSubpage);
boolean resultTalk = removeWarningOnTalkPage(talkPage);
result = resultTodo || resultTalk;
if (result) {
if (!resultTalk) {
purgePage(talkPage);
}
if (stats != null) {
stats.addRemovedWarning(pageAnalysis.getPage());
}
} else {
purgeArticle(pageAnalysis.getPage());
}
}
} else {
if (!simulation) {
result |= updateWarningOnTodoSubpage(
pageRevId, todoSubpage, elements, creator, modifiers);
if (createWarning) {
result |= cleanWarningOnTalkPage(talkPage, elements);
}
}
if (stats != null) {
stats.addLinks(pageAnalysis.getPage(), elements.size());
}
}
return result;
}
/**
* Update warning on the talk page.
*
* @param pageAnalysis Page analysis (must have enough information to compute the elements for the warning).
* @param pageRevId Page revision id.
* @param talkPage Talk page.
* @param creator User who has created the page.
* @param modifiers Users who have modified the page.
* @param stats Statistics.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
private boolean manageWarningOnTalkPage(
PageAnalysis pageAnalysis, Integer pageRevId, Page talkPage,
String creator, List<String> modifiers,
Stats stats) throws APIException {
Collection<String> elements = constructWarningElements(pageAnalysis, talkPage, null);
boolean result = false;
if ((elements == null) || (elements.isEmpty())) {
if (!simulation) {
result = removeWarningOnTalkPage(talkPage);
if (result) {
if (stats != null) {
stats.addRemovedWarning(pageAnalysis.getPage());
}
} else {
purgeArticle(pageAnalysis.getPage());
}
}
} else {
if (!simulation) {
result |= updateWarningOnTalkPage(
pageAnalysis, pageRevId, talkPage, elements, creator, modifiers);
}
if (stats != null) {
stats.addLinks(pageAnalysis.getPage(), elements.size());
}
}
return result;
}
/**
* Create/update warning on the "To do" sub-page.
*
* @param pageRevId Page revision id.
* @param todoSubpage "To do" sub-page.
* @param elements Elements for the warning.
* @param creator User who has created the page.
* @param modifiers Users who have modified the page.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
private boolean updateWarningOnTodoSubpage(
Integer pageRevId, Page todoSubpage, Collection<String> elements,
String creator, List<String> modifiers) throws APIException {
if ((todoSubpage == null) || (elements == null)) {
return false;
}
// Search warning in the "To do" sub-page
String contents = todoSubpage.getContents();
if (contents == null) {
contents = "";
}
PageAnalysis analysis = todoSubpage.getAnalysis(contents, true);
PageElementTemplate templateWarning = getFirstWarningTemplate(analysis);
// If warning is missing, add it
if (templateWarning == null) {
if (!createWarning) {
return false;
}
setText(getMessageUpdateWarning(todoSubpage.getTitle()));
StringBuilder tmp = new StringBuilder(contents);
if ((tmp.length() > 0) && (tmp.charAt(tmp.length() - 1) != '\n')) {
tmp.append('\n');
}
tmp.append("* ");
addWarning(tmp, pageRevId, elements);
tmp.append('\n');
updatePage(
todoSubpage, tmp.toString(),
getWarningComment(elements),
!Boolean.TRUE.equals(todoSubpage.isExisting()), false);
// Inform creator and modifiers of the page
informContributors(analysis, elements, creator, modifiers);
return true;
}
// Check if modifications are needed
if (isModified(elements, templateWarning)) {
StringBuilder tmp = new StringBuilder();
int index = templateWarning.getBeginIndex();
while ((index > 0) && (contents.charAt(index) != '\n')) {
index--;
}
if (index > 0) {
tmp.append(contents.substring(0, index));
tmp.append('\n');
}
tmp.append("* ");
addWarning(tmp, pageRevId, elements);
tmp.append('\n');
index = templateWarning.getEndIndex();
while ((index < contents.length()) && (contents.charAt(index) != '\n')) {
index++;
}
index++;
if (index < contents.length()) {
tmp.append(contents.substring(index));
}
api.updatePage(
wiki, todoSubpage, tmp.toString(),
getWarningComment(elements),
false, true, automaticEdit, false);
return true;
}
return false;
}
/**
* Create/update warning on the talk page.
*
* @param analysis Page analysis.
* @param pageRevId Page revision id.
* @param talkPage Talk page.
* @param elements Elements for the warning.
* @param creator User who has created the page.
* @param modifiers Users who have modified the page.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
private boolean updateWarningOnTalkPage(
PageAnalysis analysis, Integer pageRevId,
Page talkPage, Collection<String> elements,
String creator, List<String> modifiers) throws APIException {
if ((talkPage == null) || (elements == null)) {
return false;
}
// Search "To do" template in the talk page
String contents = talkPage.getContents();
if (contents == null) {
contents = "";
}
PageAnalysis talkAnalysis = talkPage.getAnalysis(contents, true);
PageElementTemplate templateTodo = null;
List<String> todoTemplates = configuration.getStringList(WPCConfigurationStringList.TODO_TEMPLATES);
if ((todoTemplates == null) ||
(todoTemplates.isEmpty())) {
return false;
}
for (String todoTemplate : todoTemplates) {
PageElementTemplate templateTmp = talkAnalysis.hasTemplate(todoTemplate);
if (templateTmp != null) {
if ((templateTodo == null) || (templateTmp.getBeginIndex() < templateTodo.getBeginIndex())) {
templateTodo = templateTmp;
}
}
}
// If "To do" template is missing, add it
if (templateTodo == null) {
if (!createWarning) {
return false;
}
// Search where to add "To do" template
PageElementTemplate templatePrevious = null;
List<String> warningAfterTemplates = configuration.getStringList(
WPCConfigurationStringList.WARNING_AFTER_TEMPLATES);
if (warningAfterTemplates != null) {
for (String previousTemplate : warningAfterTemplates) {
Collection<PageElementTemplate> templates = talkAnalysis.getTemplates(previousTemplate);
for (PageElementTemplate templateTmp : templates) {
if ((templatePrevious == null) ||
(templateTmp.getEndIndex() > templatePrevious.getEndIndex())) {
templatePrevious = templateTmp;
}
}
}
}
int indexStart = (templatePrevious != null) ? templatePrevious.getEndIndex() : 0;
if ((indexStart == 0) && (talkPage.getRedirects().isRedirect())) {
indexStart = contents.length();
}
// Add warning
setText(getMessageUpdateWarning(talkPage.getTitle()));
StringBuilder tmp = new StringBuilder();
if (indexStart > 0) {
tmp.append(contents.substring(0, indexStart));
if (tmp.charAt(tmp.length() - 1) != '\n') {
tmp.append("\n");
}
}
tmp.append("{{");
tmp.append(todoTemplates.get(0));
tmp.append("|\n* ");
addWarning(tmp, pageRevId, elements);
tmp.append("}}");
if (indexStart < contents.length()) {
if (contents.charAt(indexStart) != '\n') {
tmp.append("\n");
}
tmp.append(contents.substring(indexStart));
}
String comment = getWarningComment(elements);
updateTalkPage(talkPage, tmp.toString(), comment, false);
// Inform creator and modifiers of the page
informContributors(analysis, elements, creator, modifiers);
return true;
}
// Search warning in the "To do" parameter
String parameter = templateTodo.getParameterValue("1");
if (parameter == null) {
parameter = "";
}
PageAnalysis parameterAnalysis = talkPage.getAnalysis(parameter, false);
PageElementTemplate templateWarning = getFirstWarningTemplate(parameterAnalysis);
if (templateWarning == null) {
StringBuilder tmp = new StringBuilder();
int indexStart = templateTodo.getBeginIndex();
if (indexStart > 0) {
tmp.append(contents.substring(0, indexStart));
if (tmp.charAt(tmp.length() - 1) != '\n') {
tmp.append("\n");
}
}
StringBuilder tmpParameter = new StringBuilder(parameter);
if ((tmpParameter.length() == 0) ||
(tmpParameter.charAt(tmpParameter.length() - 1) != '\n')) {
tmpParameter.append("\n");
}
tmpParameter.append("* ");
addWarning(tmpParameter, pageRevId, elements);
tmpParameter.append("\n");
tmp.append(templateTodo.getParameterReplacement("1", tmpParameter.toString(), null));
int indexEnd = templateTodo.getEndIndex();
if (indexEnd < contents.length()) {
if ((tmp.charAt(tmp.length() - 1) != '\n') &&
(contents.charAt(indexEnd) != '\n')) {
tmp.append("\n");
}
tmp.append(contents.substring(indexEnd));
}
String comment = getWarningComment(elements);
updateTalkPage(talkPage, tmp.toString(), comment, false);
return true;
}
// Update warning if necessary
if (isModified(elements, templateWarning)) {
StringBuilder tmp = new StringBuilder();
tmp.append(contents.substring(0, templateTodo.getBeginIndex()));
StringBuilder tmpParameter = new StringBuilder();
if (templateWarning.getBeginIndex() > 0) {
tmpParameter.append(parameter.substring(0, templateWarning.getBeginIndex()));
}
addWarning(tmpParameter, pageRevId, elements);
int endIndex = parameter.indexOf('\n', templateWarning.getEndIndex());
if ((endIndex >= 0) && (endIndex < parameter.length())) {
tmpParameter.append(parameter.substring(endIndex));
}
tmp.append(templateTodo.getParameterReplacement("1", tmpParameter.toString(), null));
if (templateTodo.getEndIndex() < contents.length()) {
tmp.append(contents.substring(templateTodo.getEndIndex()));
}
String comment = getWarningComment(elements);
updateTalkPage(talkPage, tmp.toString(), comment, false);
return true;
}
return false;
}
/**
* Remove warning on the "To do" sub-page.
*
* @param todoSubpage "To do" sub-page.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
private boolean removeWarningOnTodoSubpage(Page todoSubpage) throws APIException {
// Check if page is already empty
if ((todoSubpage == null) || (Boolean.FALSE.equals(todoSubpage.isExisting()))) {
return false;
}
String contents = todoSubpage.getContents();
if ((contents == null) || (contents.trim().equals(""))) {
return false;
}
PageAnalysis analysis = todoSubpage.getAnalysis(contents, true);
// Search warning in the "To do" sub-page
PageElementTemplate template = getFirstWarningTemplate(analysis);
if (template == null) {
return false;
}
// Analyze text to remove the warning
setText(getMessageRemoveWarning(todoSubpage.getTitle()));
StringBuilder tmp = new StringBuilder();
int index = template.getBeginIndex();
while ((index > 0) && (contents.charAt(index) != '\n')) {
index--;
}
if (index > 0) {
tmp.append(contents.substring(0, index));
}
index = template.getEndIndex();
while ((index < contents.length()) && (contents.charAt(index) != '\n')) {
index++;
}
if (index < contents.length()) {
if (tmp.length() > 0) {
tmp.append('\n');
}
tmp.append(contents.substring(index));
}
// Remove the warning
String newContents = tmp.toString();
String reason = getWarningCommentDone();
if ((newContents.trim().length() == 0) &&
(wiki.getConnection().getUser() != null) &&
(wiki.getConnection().getUser().hasRight(User.RIGHT_DELETE))) {
api.deletePage(wiki, todoSubpage, reason, automaticEdit);
} else {
if (newContents.trim().length() == 0) {
String delete = configuration.getString(WPCConfigurationString.TODO_SUBPAGE_DELETE);
if ((delete != null) && (delete.trim().length() > 0)) {
newContents = delete;
}
}
updatePage(todoSubpage, newContents, reason, true, false);
}
return true;
}
/**
* Remove warning on the talk page.
*
* @param talkPage Talk page.
* @param elements Elements for the warning.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
private boolean cleanWarningOnTalkPage(
Page talkPage, Collection<String> elements) throws APIException {
// Check if page exists
if (talkPage == null) {
return false;
}
List<String> todoTemplates = configuration.getStringList(WPCConfigurationStringList.TODO_TEMPLATES);
if ((todoTemplates == null) || (todoTemplates.isEmpty())) {
return false;
}
if (Boolean.FALSE.equals(talkPage.isExisting())) {
String comment = getWarningComment(elements);
String newContents = TemplateBuilder.from(todoTemplates.get(0)).toString();
updateTalkPage(talkPage, newContents, comment, false);
return true;
}
String contents = talkPage.getContents();
if (contents == null) {
return false;
}
PageAnalysis analysis = talkPage.getAnalysis(contents, true);
// Search "To do" in the talk page
PageElementTemplate templateTodo = null;
for (String templateName : todoTemplates) {
PageElementTemplate template = analysis.hasTemplate(templateName);
if (template != null) {
templateTodo = template;
}
}
// If template is missing, verify that a link to the "To do" sub-page exists
if (templateTodo == null) {
// If link exists, nothing more to do
PageElementTemplate templateTodoLink = getExistingTemplateTodoLink(talkPage, contents);
if (templateTodoLink != null) {
return false;
}
// Search where to add "To do" template
PageElementTemplate templatePrevious = null;
List<String> warningAfterTemplates = configuration.getStringList(
WPCConfigurationStringList.WARNING_AFTER_TEMPLATES);
if (warningAfterTemplates != null) {
for (String previousTemplate : warningAfterTemplates) {
Collection<PageElementTemplate> templates = analysis.getTemplates(previousTemplate);
for (PageElementTemplate templateTmp : templates) {
if ((templatePrevious == null) ||
(templateTmp.getEndIndex() > templatePrevious.getEndIndex())) {
templatePrevious = templateTmp;
}
}
}
}
// Add warning
setText(getMessageUpdateWarning(talkPage.getTitle()));
StringBuilder tmp = new StringBuilder();
int indexStart = (templatePrevious != null) ? templatePrevious.getEndIndex() : 0;
if (indexStart > 0) {
tmp.append(contents.substring(0, indexStart));
if (tmp.charAt(tmp.length() - 1) != '\n') {
tmp.append("\n");
}
}
tmp.append(TemplateBuilder.from(todoTemplates.get(0)).toString());
if (indexStart < contents.length()) {
if (contents.charAt(indexStart) != '\n') {
tmp.append("\n");
}
tmp.append(contents.substring(indexStart));
}
String comment = getWarningComment(elements);
updateTalkPage(talkPage, tmp.toString(), comment, false);
return true;
}
if (templateTodo.getParameterValue("1") == null) {
return false;
}
// Search warning in the "To do" parameter
String parameter = templateTodo.getParameterValue("1");
PageAnalysis parameterAnalysis = talkPage.getAnalysis(parameter, false);
PageElementTemplate templateWarning = getFirstWarningTemplate(parameterAnalysis);
if (templateWarning != null) {
setText(getMessageRemoveWarning(talkPage.getTitle()));
StringBuilder tmp = new StringBuilder();
if (templateTodo.getBeginIndex() > 0) {
tmp.append(contents.substring(0, templateTodo.getBeginIndex()));
}
String tmpParameter = "";
int index = templateWarning.getBeginIndex();
while ((index > 0) && (parameter.charAt(index) != '\n')) {
index--;
}
if (index > 0) {
tmpParameter += parameter.substring(0, index);
}
index = templateWarning.getEndIndex();
while ((index < parameter.length()) && (parameter.charAt(index) != '\n')) {
index++;
}
if (index < parameter.length()) {
if (tmpParameter.length() > 0) {
tmpParameter += "\n";
}
tmpParameter += parameter.substring(index);
}
if (tmpParameter.length() > 0) {
if ((tmp.length() > 0) && (tmp.charAt(tmp.length() - 1) != '\n')) {
tmp.append('\n');
}
tmp.append(templateTodo.getParameterReplacement("1", tmpParameter, null));
} else {
// Search "To do" link
PageElementTemplate templateTodoLink = null;
List<String> todoLinkTemplates = configuration.getStringList(WPCConfigurationStringList.TODO_LINK_TEMPLATES);
if (todoLinkTemplates != null) {
for (String templateName : todoLinkTemplates) {
List<PageElementTemplate> tmpTemplates = analysis.getTemplates(templateName);
if ((tmpTemplates != null) && (tmpTemplates.size() > 0)) {
templateTodoLink = tmpTemplates.get(0);
}
}
}
if (templateTodoLink == null) {
if ((tmp.length() > 0) && (tmp.charAt(tmp.length() - 1) != '\n')) {
tmp.append('\n');
}
tmp.append(templateTodo.getParameterReplacement("1", null, null));
}
}
if (templateTodo.getEndIndex() < contents.length()) {
if ((tmp.length() > 0) && (tmp.charAt(tmp.length() - 1) != '\n')) {
if (contents.charAt(templateTodo.getEndIndex()) != '\n') {
tmp.append('\n');
}
}
tmp.append(contents.substring(templateTodo.getEndIndex()));
}
String comment = getWarningComment(elements);
updateTalkPage(talkPage, tmp.toString(), comment, false);
return true;
}
return false;
}
/**
* Remove warning on the talk page.
*
* @param talkPage Talk page.
* @return True if the warning has been updated.
* @throws APIException Exception thrown by the API.
*/
private boolean removeWarningOnTalkPage(
Page talkPage) throws APIException {
// Check if page is already empty
if ((talkPage == null) || (Boolean.FALSE.equals(talkPage.isExisting()))) {
return false;
}
String contents = talkPage.getContents();
if (contents == null) {
return false;
}
PageAnalysis analysis = talkPage.getAnalysis(contents, true);
// Search "To do" in the talk page
PageElementTemplate templateTodo = null;
List<String> todoTemplates = configuration.getStringList(WPCConfigurationStringList.TODO_TEMPLATES);
if (todoTemplates != null) {
for (String templateName : todoTemplates) {
PageElementTemplate template = analysis.hasTemplate(templateName);
if (template != null) {
templateTodo = template;
}
}
}
if ((templateTodo != null) && (templateTodo.getParameterValue("1") != null)) {
// Search warning in the "To do" parameter
int parameterIndex = templateTodo.getParameterIndex("1");
String parameter = templateTodo.getParameterValue(parameterIndex);
int parameterStartIndex = templateTodo.getParameterValueStartIndex(parameterIndex);
PageElementTemplate templateWarning = getFirstWarningTemplate(analysis);
if (templateWarning != null) {
setText(getMessageRemoveWarning(talkPage.getTitle()));
StringBuilder tmp = new StringBuilder();
if (templateTodo.getBeginIndex() > 0) {
tmp.append(contents.substring(0, templateTodo.getBeginIndex()));
}
String tmpParameter = "";
int index = templateWarning.getBeginIndex() - parameterStartIndex;
while ((index > 0) && (parameter.charAt(index) != '\n')) {
index--;
}
if (index > 0) {
tmpParameter += parameter.substring(0, index);
}
index = templateWarning.getEndIndex() - parameterStartIndex;
while ((index < parameter.length()) && (parameter.charAt(index) != '\n')) {
index++;