/
CheckErrorAlgorithm547.java
272 lines (249 loc) · 8.84 KB
/
CheckErrorAlgorithm547.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
/*
* 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.a5xx.a54x.a547;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.wikipediacleaner.api.algorithm.AlgorithmParameter;
import org.wikipediacleaner.api.algorithm.AlgorithmParameterElement;
import org.wikipediacleaner.api.check.CheckErrorResult;
import org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithmBase;
import org.wikipediacleaner.api.configuration.WPCConfiguration;
import org.wikipediacleaner.api.data.Namespace;
import org.wikipediacleaner.api.data.Page;
import org.wikipediacleaner.api.data.PageElementListItem;
import org.wikipediacleaner.api.data.PageElementParagraph;
import org.wikipediacleaner.api.data.PageElementTemplate;
import org.wikipediacleaner.api.data.PageElementTemplate.Parameter;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.tag.WikiTagType;
import org.wikipediacleaner.i18n.GT;
import org.wikipediacleaner.utils.string.CharacterUtils;
import org.wikipediacleaner.api.data.PageElementTitle;
/**
* Algorithm for analyzing error 547 of check wikipedia project.
* Error 547: Empty list item.
*/
public class CheckErrorAlgorithm547 extends CheckErrorAlgorithmBase {
public CheckErrorAlgorithm547() {
super("Empty list item");
}
/**
* 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;
}
// Only in main name space
if ((analysis.getPage().getNamespace() == null) ||
(analysis.getPage().getNamespace().intValue() != Namespace.MAIN)) {
return false;
}
// Check if list items are present
List<PageElementListItem> listItems = analysis.getListItems();
if ((listItems == null) || (listItems.isEmpty())) {
return false;
}
// Check each list item
boolean result = false;
for (PageElementListItem listItem : listItems) {
result |= analyzeListItem(analysis, errors, listItem);
}
return result;
}
/**
* Analyze a list item to check if errors are present.
*
* @param analysis Page analysis.
* @param errors Errors found in the page.
* @param listItem List item to be analyzed.
* @return Flag indicating if the error was found.
*/
private boolean analyzeListItem(
PageAnalysis analysis,
Collection<CheckErrorResult> errors,
PageElementListItem listItem) {
// Check if list item has text
int index = listItem.getBeginIndex() + listItem.getDepth();
String contents = analysis.getContents();
while (index < listItem.getEndIndex()) {
if (!CharacterUtils.isWhitespace(contents.charAt(index))) {
return false;
}
index++;
}
// Filter special cases
if ((analysis.getSurroundingTag(WikiTagType.NOWIKI, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.SCORE, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.SOURCE, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.SYNTAXHIGHLIGHT, index) != null)) {
return false;
}
if (analysis.comments().isAt(index)) {
return false;
}
if (!templates.isEmpty()) {
PageElementTemplate template = analysis.isInTemplate(index);
if (template != null) {
for (String[] ignoredTemplate : templates) {
if (Page.areSameTitle(template.getTemplateName(), ignoredTemplate[0])) {
if (ignoredTemplate.length > 1) {
PageElementTemplate.Parameter param = template.getParameterAtIndex(index);
if (param != null) {
for (int paramNum = 1; paramNum < ignoredTemplate.length; paramNum++) {
if (param.getComputedName().equals(ignoredTemplate[paramNum])) {
return false;
}
}
}
} else {
return false;
}
}
}
}
}
// Report error
if (errors == null) {
return true;
}
// Determine boundaries
boolean automatic = false;
int begin = listItem.getBeginIndex();
int end = listItem.getEndIndex();
boolean extended = false;
if (end + 1 < contents.length()) {
char nextChar = contents.charAt(end + 1);
if (nextChar == '\n') {
automatic = true;
end++;
extended = true;
} else if (PageElementListItem.isListIndicator(nextChar)) {
end++;
extended = true;
} else {
automatic = true;
}
} else {
automatic = true;
}
if (begin > 1) {
char previousChar = contents.charAt(begin - 1);
if (previousChar == '\n') {
char previousChar2 = contents.charAt(begin - 2);
if (previousChar2 == '\n') {
if (!extended) {
begin--;
extended = true;
}
automatic = true;
} else if (previousChar2 == '=') {
PageElementTitle title = analysis.isInTitle(begin - 2);
if (title != null) {
automatic = true;
}
} else {
PageElementParagraph paragraph = analysis.isInParagraph(begin - 2);
if (paragraph != null) {
automatic = true;
}
}
}
} else {
automatic = true;
}
// Specific check if fix can be automatic
if (automatic &&
(analysis.isInImage(index) != null)) {
automatic = false;
}
if (automatic) {
// Note: due to badly written templates that requires a parameter not to be empty...
PageElementTemplate template = analysis.isInTemplate(index);
if (template != null) {
Parameter param = template.getParameterAtIndex(index);
if (param != null) {
// TODO: be less restrictive, only if list item is alone?
automatic = false;
}
}
}
// Report error
CheckErrorResult errorResult = createCheckErrorResult(analysis, begin, end);
errorResult.addReplacement("", automatic);
errors.add(errorResult);
return true;
}
/**
* Automatic fixing of all the errors in the page.
*
* @param analysis Page analysis.
* @return Page contents after fix.
*/
@Override
protected String internalAutomaticFix(PageAnalysis analysis) {
if (!analysis.getPage().isArticle() ||
!analysis.getPage().isInMainNamespace()) {
return analysis.getContents();
}
return fixUsingAutomaticReplacement(analysis);
}
/* ====================================================================== */
/* PARAMETERS */
/* ====================================================================== */
/** Templates in which empty list items should be ignored */
private static final String PARAMETER_TEMPLATES = "templates";
/**
* Initialize settings for the algorithm.
*
* @see org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithmBase#initializeSettings()
*/
@Override
protected void initializeSettings() {
String tmp = getSpecificProperty(PARAMETER_TEMPLATES, true, true, false);
templates.clear();
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
for (String[] tmpElement : tmpList) {
if (tmpElement.length > 0) {
templates.add(tmpElement);
}
}
}
}
/** Templates in which empty list items should be ignored */
private final List<String[]> templates = new ArrayList<>();
/**
* Build the list of parameters for this algorithm.
*/
@Override
protected void addParameters() {
super.addParameters();
addParameter(new AlgorithmParameter(
PARAMETER_TEMPLATES,
GT._T("Templates in which empty list items should be ignored"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template name",
GT._T("Template in which empty list items should be ignored")),
new AlgorithmParameterElement(
"parameter name",
GT._T("Template parameter in which empty list items should be ignored"),
true, true)
},
true));
}
}