/
CheckErrorAlgorithm083.java
172 lines (155 loc) · 5.81 KB
/
CheckErrorAlgorithm083.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
/*
* WPCleaner: A tool to help on Wikipedia maintenance tasks.
* Copyright (C) 2013 Nicolas Vervelle
*
* See README.txt file for licensing information.
*/
package org.wikipediacleaner.api.check.algorithm.a0xx.a08x.a083;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.wikipediacleaner.api.check.CheckErrorResult;
import org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithmBase;
import org.wikipediacleaner.api.data.Namespace;
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.PageElementTitle;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.tag.WikiTagType;
import org.wikipediacleaner.api.data.contents.title.TitleBuilder;
/**
* Algorithm for analyzing error 83 of check wikipedia project.
* Error 83: Headlines start with three "=" and later with level two
*/
public class CheckErrorAlgorithm083 extends CheckErrorAlgorithmBase {
public CheckErrorAlgorithm083() {
super("Headlines start with three \"=\" and later with level two");
}
/**
* Analyze a page to check if errors are present.
*
* @param analysis Page analysis.
* @param errors Errors found in the page.
* @param onlyAutomatic True if analysis could be restricted to errors automatically fixed.
* @return Flag indicating if the error was found.
*/
@Override
public boolean analyze(
PageAnalysis analysis,
Collection<CheckErrorResult> errors, boolean onlyAutomatic) {
if (analysis == null) {
return false;
}
// Check first title
List<PageElementTitle> titles = analysis.getTitles();
if (titles.size() == 0) {
return false;
}
PageElementTitle firstTitle = titles.get(0);
if (firstTitle.getLevel() < 3) {
return false;
}
// Check every title
boolean result = false;
for (int titleIndex = 1; titleIndex < titles.size(); titleIndex++) {
PageElementTitle title = titles.get(titleIndex);
if (title.getLevel() < firstTitle.getLevel()) {
if (errors == null) {
return true;
}
result = true;
if (titleIndex == 1) {
CheckErrorResult errorResult = createCheckErrorResult(analysis, firstTitle.getBeginIndex(), firstTitle.getEndIndex());
errorResult.addReplacement(
TitleBuilder
.from(title.getLevel(), firstTitle.getTitle())
.withAfter(firstTitle.getAfterTitle()).toString(),
firstTitle.isCoherent() && title.isCoherent());
errors.add(errorResult);
break;
}
CheckErrorResult errorResult = createCheckErrorResult(
analysis,
title.getBeginIndex(), title.getEndIndex());
errorResult.addEditTocAction(title);
errors.add(errorResult);
break;
}
}
return result;
}
private final static Set<Integer> AUTOMATIC_NAMESPACES = Stream
.of(Namespace.MAIN, Namespace.IMAGE)
.collect(Collectors.toCollection(HashSet::new));
/**
* Automatic fixing of all the errors in the page.
*
* @param analysis Page analysis.
* @return Page contents after fix.
*/
@Override
protected String internalAutomaticFix(PageAnalysis analysis) {
String defaultContents = analysis.getContents();
Integer namespace = analysis.getPage().getNamespace();
if ((namespace == null) || !AUTOMATIC_NAMESPACES.contains(namespace)) {
return defaultContents;
}
// Check first title
List<PageElementTitle> titles = analysis.getTitles();
if (titles.size() < 2) {
return defaultContents;
}
PageElementTitle firstTitle = titles.get(0);
if ((firstTitle.getLevel() < 3) || !firstTitle.isCoherent()) {
return defaultContents;
}
// Find first title with smaller level
int correctTitleIndex = -1;
int delta = 0;
for (int titleIndex = 1; (titleIndex < titles.size()) && (correctTitleIndex < 0); titleIndex++) {
PageElementTitle title = titles.get(titleIndex);
if (title.getLevel() < firstTitle.getLevel()) {
if (!title.isCoherent()) {
return defaultContents;
}
correctTitleIndex = titleIndex;
delta = firstTitle.getLevel() - title.getLevel();
}
}
if (correctTitleIndex < 0) {
return defaultContents;
}
// Do not fix pages with include tags
List<PageElementTag> exclusionTags = analysis.getTags(WikiTagType.INCLUDEONLY);
if ((exclusionTags != null) && !exclusionTags.isEmpty()) {
return defaultContents;
}
exclusionTags = analysis.getTags(WikiTagType.NOINCLUDE);
if ((exclusionTags != null) && !exclusionTags.isEmpty()) {
return defaultContents;
}
exclusionTags = analysis.getTags(WikiTagType.ONLYINCLUDE);
if ((exclusionTags != null) && !exclusionTags.isEmpty()) {
return defaultContents;
}
// Construct result
int lastIndex = 0;
StringBuilder newContents = new StringBuilder();
for (int titleIndex = 0; titleIndex < correctTitleIndex; titleIndex++) {
PageElementTitle title = titles.get(titleIndex);
if (lastIndex < title.getBeginIndex()) {
newContents.append(defaultContents.substring(lastIndex, title.getBeginIndex()));
}
newContents.append(TitleBuilder
.from(title.getLevel() - delta, title.getTitle())
.withAfter(title.getAfterTitle()).toString());
lastIndex = title.getEndIndex();
}
if (lastIndex < defaultContents.length()) {
newContents.append(defaultContents.substring(lastIndex));
}
return newContents.toString();
}
}