/
CheckErrorAlgorithm564.java
466 lines (429 loc) · 17.3 KB
/
CheckErrorAlgorithm564.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
/*
* 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.a56x.a564;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.wikipediacleaner.api.API;
import org.wikipediacleaner.api.APIException;
import org.wikipediacleaner.api.APIFactory;
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.check.algorithm.a5xx.TemplateConfigurationGroup;
import org.wikipediacleaner.api.check.algorithm.a5xx.TemplateParameterSuggestion;
import org.wikipediacleaner.api.configuration.WPCConfiguration;
import org.wikipediacleaner.api.configuration.WPCConfigurationStringList;
import org.wikipediacleaner.api.constants.EnumWikipedia;
import org.wikipediacleaner.api.data.DataManager;
import org.wikipediacleaner.api.data.Page;
import org.wikipediacleaner.api.data.PageElementTemplate;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.i18n.GT;
/**
* Algorithm for analyzing error 564 of check wikipedia project.
* Error 564: Unknown parameters
*/
public class CheckErrorAlgorithm564 extends CheckErrorAlgorithmBase {
public CheckErrorAlgorithm564() {
super("Unknown parameters");
}
/**
* 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;
}
// Preliminary checks
if (configurationByTemplateName.isEmpty()) {
return false;
}
List<PageElementTemplate> templates = analysis.getTemplates();
if ((templates == null) || templates.isEmpty()) {
return false;
}
// Check each template
boolean result = false;
for (PageElementTemplate template : templates) {
result |= analyzeTemplate(analysis, errors, template);
}
return result;
}
/**
* Analyze a template to see if it has unknown parameters.
*
* @param analysis Page analysis.
* @param errors Errors found in the page.
* @param template Template to analyze.
* @return True if the template has unknown parameters.
*/
private boolean analyzeTemplate(
PageAnalysis analysis,
Collection<CheckErrorResult> errors,
PageElementTemplate template) {
if ((analysis == null) || (template == null)) {
return false;
}
TemplateConfiguration templateConfiguration = configurationByTemplateName.get(template.getTemplateName());
if (templateConfiguration == null) {
return false;
}
boolean result = false;
for (int paramNum = 0; paramNum < template.getParameterCount(); paramNum++) {
result |= analyzeTemplateParameter(analysis, errors, template, paramNum, templateConfiguration);
}
return result;
}
/**
* Analyze a template parameter to see if it is unknown.
*
* @param analysis Page analysis.
* @param errors Errors found in the page.
* @param templateParameter Parameter to analyze.
* @param templateConfiguration Configuration for the template.
* @return
*/
private boolean analyzeTemplateParameter(
PageAnalysis analysis,
Collection<CheckErrorResult> errors,
PageElementTemplate template,
int paramNum,
TemplateConfiguration templateConfiguration) {
// Check if there's an error
Optional<List<TemplateParameterSuggestion>> suggestions = templateConfiguration.analyzeParam(analysis, template, paramNum);
if (!suggestions.isPresent()) {
return false;
}
if (errors == null) {
return true;
}
// Report error
PageElementTemplate.Parameter templateParam = template.getParameter(paramNum);
int beginIndex = templateParam.getBeginIndex();
int endIndex = templateParam.getEndIndex();
CheckErrorResult errorResult = createCheckErrorResult(analysis, beginIndex, endIndex);
errorResult.addText(GT._T("Template {0}", template.getTemplateName()));
for (TemplateParameterSuggestion suggestion : suggestions.get()) {
boolean automatic = suggestion.isAutomatic();
automatic &= (suggestion.getParamName() == null) || (template.getParameterValue(suggestion.getParamName()) == null);
errorResult.addReplacement(suggestion.getReplacement(), automatic);
}
errors.add(errorResult);
return true;
}
/**
* @return True if the error has a special list of pages.
*/
@Override
public boolean hasSpecialList() {
return !dumpAnalyses.isEmpty();
}
/**
* Retrieve the list of pages in error.
*
* @param wiki Wiki.
* @param limit Maximum number of pages to retrieve.
* @return List of pages in error.
*/
@Override
public List<Page> getSpecialList(EnumWikipedia wiki, int limit) {
List<Page> result = new ArrayList<>();
// Use internal links
for (String dumpAnalysis : dumpAnalyses) {
API api = APIFactory.getAPI();
Page page = DataManager.createSimplePage(wiki, dumpAnalysis, null, null, null);
try {
api.retrieveLinks(wiki, page, null, null, false, false);
if (page.getLinks() != null) {
result.addAll(page.getLinks());
}
} catch (APIException e) {
//
}
}
Collections.sort(result);
// Limit result size
while (result.size() > limit) {
result.remove(result.size() - 1);
}
return result;
}
/**
* 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 */
/* ====================================================================== */
/** Page containing a dump analysis of the error */
private static final String PARAMETER_DUMP_ANALYSIS = "dump_analysis";
/** Template groups */
private static final String PARAMETER_TEMPLATE_GROUPS = "template_groups";
/** Templates and parameters that are checked */
private static final String PARAMETER_TEMPLATES = "templates";
/** Parameters that can be safely deleted */
private static final String PARAMETER_DELETE_PARAMETERS = "delete_parameters";
/** Parameters for which unnamed parameters can be safely deleted when values are equal */
private static final String PARAMETER_DELETE_UNNAMED_PARAMETERS = "delete_unnamed_parameters";
/** Values that can be safely deleted */
private static final String PARAMETER_DELETE_VALUES = "delete_values";
/** Parameters that can be safely commented */
private static final String PARAMETER_COMMENT_PARAMETERS = "comment_parameters";
/** Parameters that can be safely replaced */
private static final String PARAMETER_REPLACE_PARAMETERS = "replace_parameters";
/** Values that can be safely replaced */
private static final String PARAMETER_REPLACE_VALUES = "replace_values";
/** Prefixes for parameter names that are ok */
private static final String PARAMETER_PARAMETERS_OK = "parameters_ok";
/**
* Initialize settings for the algorithm.
*
* @see org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithmBase#initializeSettings()
*/
@Override
protected void initializeSettings() {
String tmp = getSpecificProperty(PARAMETER_TEMPLATE_GROUPS, true, true, false);
TemplateConfigurationGroup group = new TemplateConfigurationGroup();
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
group.addGroups(tmpList);
}
List<String[]> generalList = getWPCConfiguration().getStringArrayList(WPCConfigurationStringList.TEMPLATE_GROUPS);
if (generalList != null) {
group.addGroups(generalList);
}
tmp = getSpecificProperty(PARAMETER_TEMPLATES, true, true, false);
configurationByTemplateName.clear();
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addKnownParameters(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_DELETE_PARAMETERS, true, true, false);
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addParametersToDelete(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_DELETE_UNNAMED_PARAMETERS, true, true, false);
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addUnnamedParametersToDelete(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_DELETE_VALUES, true, true, false);
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addValuesToDelete(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_COMMENT_PARAMETERS, true, true, false);
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addParametersToComment(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_REPLACE_PARAMETERS, true, true, false);
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addParametersToReplace(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_REPLACE_VALUES, true, true, false);
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addValuesToReplace(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_PARAMETERS_OK, true, true, false);
if (tmp != null) {
List<String[]> tmpList = WPCConfiguration.convertPropertyToStringArrayList(tmp);
TemplateConfiguration.addParametersOk(tmpList, configurationByTemplateName, group);
}
tmp = getSpecificProperty(PARAMETER_DUMP_ANALYSIS, true, true, false);
dumpAnalyses.clear();
if (tmp != null) {
List<String> tmpList = WPCConfiguration.convertPropertyToStringList(tmp);
dumpAnalyses.addAll(tmpList);
}
}
/** Page containing a dump analysis */
private List<String> dumpAnalyses = new ArrayList<>();
/** Templates and parameters that are checked */
private final Map<String, TemplateConfiguration> configurationByTemplateName = new HashMap<>();
/**
* Build the list of parameters for this algorithm.
*/
@Override
protected void addParameters() {
super.addParameters();
addParameter(new AlgorithmParameter(
PARAMETER_DUMP_ANALYSIS,
GT._T("A page containing a dump analysis for this error."),
new AlgorithmParameterElement(
"page name",
GT._T("A page containing a dump analysis for this error."))));
addParameter(new AlgorithmParameter(
PARAMETER_TEMPLATE_GROUPS,
GT._T("Groups of templates"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"group",
GT._T("Name of the group")),
new AlgorithmParameterElement(
"template",
GT._T("Name of a template in the group"),
false,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_TEMPLATES,
GT._T("Templates for which unknown parameters should be verified"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"param",
GT._T("Name of the parameter"),
true,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_DELETE_PARAMETERS,
GT._T("Template parameters which can be safely deleted"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"param",
GT._T("Name of the parameter"),
true,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_DELETE_UNNAMED_PARAMETERS,
GT._T("Template parameters for which unnamed parameters can be safely deleted when values are equal"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"param",
GT._T("Name of parameter"),
true,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_DELETE_VALUES,
GT._T("Template parameters which can be safely deleted if they have a given value"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"value",
GT._T("Value of the parameter")),
new AlgorithmParameterElement(
"param",
GT._T("Name of the parameter"),
true,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_COMMENT_PARAMETERS,
GT._T("Template parameters which can be safely commented"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"param",
GT._T("Name of the parameter"),
true,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_DELETE_PARAMETERS,
GT._T("Template parameters which can be safely deleted"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"param",
GT._T("Name of the parameter"),
true,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_REPLACE_PARAMETERS,
GT._T("Template parameters which can be safely replaced"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"initial param",
GT._T("Initial name of the parameter"),
true,
true),
new AlgorithmParameterElement(
"target param",
GT._T("Target name of the parameter"),
true,
true)
},
true));
addParameter(new AlgorithmParameter(
PARAMETER_REPLACE_VALUES,
GT._T("Template parameters which can be safely replaced if they have a given value"),
new AlgorithmParameterElement[] {
new AlgorithmParameterElement(
"template",
GT._T("Name of the template")),
new AlgorithmParameterElement(
"initial param",
GT._T("Initial name of the parameter")),
new AlgorithmParameterElement(
"initial value",
GT._T("Initial value of the parameter")),
new AlgorithmParameterElement(
"target param",
GT._T("Target name of the parameter")),
new AlgorithmParameterElement(
"target value",
GT._T("Target value of the parameter"))
},
true));
}
}