Skip to content

Commit

Permalink
T257585: Restrict automatic bot fix for #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas authored and Nicolas committed Jul 10, 2020
1 parent 3f1ff8c commit 09e57bb
Showing 1 changed file with 21 additions and 4 deletions.
Expand Up @@ -11,6 +11,7 @@
import java.util.List;

import org.wikipediacleaner.api.check.CheckErrorResult;
import org.wikipediacleaner.api.data.Page;
import org.wikipediacleaner.api.data.PageElementTitle;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;

Expand Down Expand Up @@ -58,10 +59,15 @@ public boolean analyze(
CheckErrorResult errorResult = createCheckErrorResult(
analysis,
title.getBeginIndex(), title.getEndIndex());
if ((firstDetection) && (titleIndex == titles.size() - 1)) {
errorResult.addReplacement(
PageElementTitle.createTitle(2, title.getTitle(), title.getAfterTitle()),
true);
if (firstDetection) {
if (titleIndex == titles.size() - 1) {
errorResult.addReplacement(
PageElementTitle.createTitle(2, title.getTitle(), title.getAfterTitle()),
true);
} else if ((titleIndex == 0) &&
Page.areSameTitle(analysis.getPage().getTitle(), title.getTitle())) {
errorResult.addReplacement(title.getAfterTitle(), true);
}
}
errorResult.addEditTocAction(title);
errors.add(errorResult);
Expand All @@ -82,6 +88,10 @@ protected String internalBotFix(PageAnalysis analysis) {
if (!analysis.areTitlesReliable()) {
return contents;
}
if (!analysis.getPage().isInMainNamespace() ||
!analysis.getPage().isArticle()) {
return contents;
}

// Compute minimum title level
List<PageElementTitle> titles = analysis.getTitles();
Expand All @@ -93,14 +103,21 @@ protected String internalBotFix(PageAnalysis analysis) {
return contents;
}
int minTitle = Integer.MAX_VALUE;
int minTitleCount = 0;
for (PageElementTitle title : titles) {
if (title.getLevel() < minTitle) {
minTitle = title.getLevel();
minTitleCount = 1;
} else if (title.getLevel() == minTitle) {
minTitleCount++;
}
}
if (minTitle > 1) {
return contents;
}
if ((minTitleCount == 1) && (titles.size() > 1)) {
return fixUsingAutomaticReplacement(analysis);
}

// Replace titles
StringBuilder tmp = new StringBuilder();
Expand Down

0 comments on commit 09e57bb

Please sign in to comment.