Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CW#578, T333085: Detect templates in list item
- Loading branch information
Nicolas
authored and
Nicolas
committed
Mar 26, 2023
1 parent
ce9f4a3
commit 81ebeea
Showing
3 changed files
with
223 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
DoTasks _Common.txt | ||
Set Namespaces 0 6 14 | ||
Set Prefix [[Utilisateur:WikiCleanerBot#T3|Bot_T3]] | ||
ListCheckWiki frwiki-$-pages-articles.xml.bz2 wiki:Projet:Correction_syntaxique/Analyse_{0} 1 2 3 4 5 7 8 9 10 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 38 42 44 46 48 51 52 54 55 64 69 70 71 72 73 75 83 85 88 90 92 95 98 99 102 104 105 106 107 108 109 111 112 504 513 520 526 542 543 544 545 546 547 548 549 550 551 552 553 554 555 557 558 559 560 562 564 565 566 567 568 569 570 571 572 573 575 577 | ||
ListCheckWiki frwiki-$-pages-articles.xml.bz2 wiki:Projet:Correction_syntaxique/Analyse_{0} 1 2 3 4 5 7 8 9 10 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 38 42 44 46 48 51 52 54 55 64 69 70 71 72 73 75 83 85 88 90 92 95 98 99 102 104 105 106 107 108 109 111 112 504 513 520 526 542 543 544 545 546 547 548 549 550 551 552 553 554 555 557 558 559 560 562 564 565 566 567 568 569 570 571 572 573 575 577 578 |
169 changes: 169 additions & 0 deletions
169
...r/src/org/wikipediacleaner/api/check/algorithm/a5xx/a57x/a578/CheckErrorAlgorithm578.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* 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.a57x.a578; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
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.Page; | ||
import org.wikipediacleaner.api.data.PageElementListItem; | ||
import org.wikipediacleaner.api.data.PageElementTemplate; | ||
import org.wikipediacleaner.api.data.analysis.PageAnalysis; | ||
import org.wikipediacleaner.api.data.contents.ContentsUtil; | ||
import org.wikipediacleaner.i18n.GT; | ||
|
||
|
||
/** | ||
* Algorithm for analyzing error 578 of check wikipedia project. | ||
* <br> | ||
* Error 578: Template in list. | ||
*/ | ||
public class CheckErrorAlgorithm578 extends CheckErrorAlgorithmBase { | ||
|
||
@Nonnull private static final Logger log = LoggerFactory.getLogger(CheckErrorAlgorithm578.class); | ||
|
||
public CheckErrorAlgorithm578() { | ||
super("Template in list"); | ||
} | ||
|
||
/** | ||
* 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 each template | ||
boolean result = false; | ||
for (String templateName : templateNames) { | ||
List<PageElementTemplate> currentTemplates = analysis.getTemplates(templateName); | ||
for (PageElementTemplate template : currentTemplates) { | ||
result |= analyzeTemplate(analysis, errors, template); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Analyze a template to check if errors are present. | ||
* | ||
* @param analysis Page analysis. | ||
* @param errors Errors found in the page. | ||
* @param template Template. | ||
* @return Flag indicating if the error was found. | ||
*/ | ||
private boolean analyzeTemplate( | ||
PageAnalysis analysis, | ||
Collection<CheckErrorResult> errors, | ||
PageElementTemplate template) { | ||
|
||
// Check if template is in list item | ||
PageElementListItem listItem = analysis.isInListItem(template.getBeginIndex()); | ||
if (listItem == null) { | ||
return false; | ||
} | ||
|
||
// Report error | ||
if (errors == null) { | ||
return true; | ||
} | ||
int beginIndex = listItem.getBeginIndex(); | ||
int endIndex; | ||
if (template.getParameterCount() == 0) { | ||
endIndex = template.getEndIndex() - 2; | ||
} else { | ||
endIndex = template.getParameterPipeIndex(0); | ||
} | ||
CheckErrorResult errorResult = createCheckErrorResult(analysis, beginIndex, endIndex); | ||
String contents = analysis.getContents(); | ||
int tmpBeginIndex = ContentsUtil.moveIndexAfterWhitespace(contents, beginIndex + listItem.getDepth()); | ||
// TODO: Automatic fixing, be careful of not breaking list depths | ||
errorResult.addReplacement(contents.substring(tmpBeginIndex, endIndex), false); | ||
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()) { | ||
return analysis.getContents(); | ||
} | ||
return fixUsingAutomaticReplacement(analysis); | ||
} | ||
|
||
/* ====================================================================== */ | ||
/* PARAMETERS */ | ||
/* ====================================================================== */ | ||
|
||
/** Templates that shouldn't be used in list item */ | ||
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); | ||
templateNames.clear(); | ||
if (tmp != null) { | ||
List<String> tmpList = WPCConfiguration.convertPropertyToStringList(tmp); | ||
for (String tmpElement : tmpList) { | ||
templateNames.add(Page.normalizeTitle(tmpElement)); | ||
} | ||
} | ||
} | ||
|
||
/** Templates that shouldn't be used in list item */ | ||
private final List<String> templateNames = new ArrayList<>(); | ||
|
||
/** | ||
* Build the list of parameters for this algorithm. | ||
*/ | ||
@Override | ||
protected void addParameters() { | ||
super.addParameters(); | ||
addParameter(new AlgorithmParameter( | ||
PARAMETER_TEMPLATES, | ||
GT._T("Templates that shouldn't be used in list item"), | ||
new AlgorithmParameterElement[] { | ||
new AlgorithmParameterElement( | ||
"templates", | ||
GT._T("Template that shouldn't be used in list item")) | ||
}, | ||
true)); | ||
} | ||
} |
Oops, something went wrong.