use of org.wikipediacleaner.api.data.PageElementTemplate in project wpcleaner by WPCleaner.
the class ActionInsertPredefinedText method actionAddTemplate.
/**
* Action called when a template is selected to be added.
*
* @param templateName Template name.
*/
public void actionAddTemplate(String templateName) {
if ((templateName == null) || (pageProvider == null)) {
return;
}
Page page = pageProvider.getPage();
if (page == null) {
return;
}
String contents = pane.getText();
PageAnalysis analysis = page.getAnalysis(contents, false);
// Check that the template isn't already applied
if (analysis.hasTemplate(templateName) != null) {
return;
}
// Find where to add the template
int crBefore = 0;
int crAfter = 2;
int index = contents.length();
List<PageElementTemplate> templates = analysis.getTemplates();
if ((templates != null) && (!templates.isEmpty())) {
index = templates.get(0).getBeginIndex();
crAfter = 1;
int indexNewLine = contents.indexOf('\n');
if ((indexNewLine > 0) && (indexNewLine > index)) {
crBefore = 2;
}
} else {
List<PageElementCategory> categories = analysis.getCategories();
if ((categories != null) && (!categories.isEmpty())) {
index = categories.get(0).getBeginIndex();
} else {
List<PageElementLanguageLink> langLinks = analysis.getLanguageLinks();
if ((langLinks != null) && (!langLinks.isEmpty())) {
index = langLinks.get(0).getBeginIndex();
} else {
int indexNewLine = contents.indexOf('\n');
if (indexNewLine > 0) {
index = indexNewLine;
}
crBefore = 2;
crAfter = 0;
}
}
}
// Add the template
StringBuilder newContents = new StringBuilder();
if (index > 0) {
newContents.append(contents.substring(0, index));
}
for (int i = 0; i < crBefore; i++) {
newContents.append("\n");
}
newContents.append(TemplateBuilder.from(templateName).toString());
for (int i = 0; i < crAfter; i++) {
newContents.append("\n");
}
if (index < contents.length()) {
newContents.append(contents.substring(index));
}
pane.changeText(newContents.toString());
if (listener != null) {
listener.templateInserted(templateName);
}
}
use of org.wikipediacleaner.api.data.PageElementTemplate in project wpcleaner by WPCleaner.
the class CheckErrorAlgorithm052 method analyze.
/**
* 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;
}
// Searching for last headline
PageElementTitle title = getLastTitle(analysis);
if (title == null) {
return false;
}
// Checking every category
boolean result = false;
for (PageElementCategory category : analysis.getCategories()) {
// Decide if error should be reported
boolean shouldReport = category.getBeginIndex() < title.getBeginIndex();
if (shouldReport && !ignoreTemplates.isEmpty()) {
int beginIndex = category.getBeginIndex();
PageElementTemplate template = analysis.isInTemplate(beginIndex);
if (template != null) {
Set<String> parameters = ignoreTemplates.get(template.getTemplateName());
if (parameters != null) {
if (parameters.isEmpty()) {
shouldReport = false;
} else {
PageElementTemplate.Parameter param = template.getParameterAtIndex(beginIndex);
if ((param != null) && parameters.contains(param.getComputedName())) {
shouldReport = false;
}
}
}
}
}
// Report error
if (shouldReport) {
if (errors == null) {
return true;
}
result = true;
CheckErrorResult errorResult = createCheckErrorResult(analysis, category.getBeginIndex(), category.getEndIndex());
String categoryName = category.getName();
if ((categoryName == null) || ("".equals(categoryName))) {
errorResult.addReplacement("", true);
}
errors.add(errorResult);
}
}
return result;
}
use of org.wikipediacleaner.api.data.PageElementTemplate in project wpcleaner by WPCleaner.
the class CheckErrorAlgorithm055 method getPossibleEndInTemplate.
/**
* Find a possible end for a single small tag in a template.
*
* @param analysis Page analysis.
* @param tag Current small tag.
* @return Possible end if found, -1 otherwise.
*/
private int getPossibleEndInTemplate(PageAnalysis analysis, PageElementTag tag) {
// Check if in template
PageElementTemplate template = analysis.isInTemplate(tag.getBeginIndex());
if (template == null) {
return -1;
}
Parameter param = template.getParameterAtIndex(tag.getBeginIndex());
if (param == null) {
return -1;
}
return param.getEndIndex();
}
use of org.wikipediacleaner.api.data.PageElementTemplate in project wpcleaner by WPCleaner.
the class CheckErrorAlgorithm034 method analyze.
/**
* 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;
}
if (analysis.isInNamespace(Namespace.TEMPLATE)) {
return false;
}
// Check every position
Page page = analysis.getPage();
String contents = analysis.getContents();
int maxLen = contents.length();
boolean result = false;
int currentIndex = 0;
while (currentIndex < maxLen) {
int nextIndex = currentIndex;
if (contents.startsWith("__", currentIndex)) {
nextIndex = currentIndex + 2;
PageElementMagicWord magicWord = analysis.isInMagicWord(currentIndex);
if ((magicWord != null) && (magicWord.getBeginIndex() == currentIndex)) {
nextIndex = magicWord.getEndIndex();
MagicWordType magicWordType = magicWord.getMagicWord().getType();
if (SimpleMagicWordType.FORCE_TOC.equals(magicWordType) || SimpleMagicWordType.INDEX.equals(magicWordType) || SimpleMagicWordType.NO_INDEX.equals(magicWordType) || SimpleMagicWordType.NO_NEW_SECTION_LINK.equals(magicWordType)) {
result = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, magicWord.getBeginIndex(), magicWord.getEndIndex());
errorResult.addReplacement("");
errors.add(errorResult);
}
}
} else if (contents.startsWith("{{", currentIndex)) {
boolean done = false;
// Check for templates beginning with '{{{' instead of '{{'
if (!done && contents.startsWith("{{{", currentIndex)) {
PageElementTemplate currentTemplate = analysis.isInTemplate(currentIndex);
PageElementTemplate nextTemplate = analysis.isInTemplate(currentIndex + 1);
if ((nextTemplate != null) && (currentIndex + 1 == nextTemplate.getBeginIndex()) && ((currentTemplate == null) || (currentTemplate.getBeginIndex() < currentIndex - 1))) {
result = true;
done = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, currentIndex, currentIndex + 3);
errorResult.addReplacement("{{");
errors.add(errorResult);
nextIndex = currentIndex + 3;
}
}
// Check for parameters
if (!done) {
PageElementParameter parameter = analysis.isInParameter(currentIndex);
if ((parameter != null) && (parameter.getBeginIndex() == currentIndex)) {
result = true;
done = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, parameter.getBeginIndex(), parameter.getEndIndex());
if (parameter.getParameterCount() == 1) {
String value = parameter.getParameterValue(0);
if (value != null) {
errorResult.addReplacement(value);
}
}
errors.add(errorResult);
nextIndex = parameter.getEndIndex();
}
}
// Check for functions
if (!done) {
PageElementFunction function = analysis.isInFunction(currentIndex);
if ((function != null) && (function.getBeginIndex() == currentIndex)) {
MagicWord magicWord = function.getMagicWord();
MagicWordType magicWordType = magicWord.getType();
boolean isOk = false;
ErrorLevel errorLevel = ErrorLevel.ERROR;
if (FunctionMagicWordType.DEFAULT_SORT.equals(magicWordType) || FunctionMagicWordType.FORMAT_NUM.equals(magicWordType) || FunctionMagicWordType.DISPLAY_TITLE.equals(magicWordType)) {
isOk = true;
}
if (!isOk && FunctionMagicWordType.TAG.equals(magicWordType) && (function.getParameterCount() > 0) && (WikiTagType.REF.isPossibleName(function.getParameterValue(0)))) {
isOk = true;
}
if (!isOk) {
if (FunctionMagicWordType.INVOKE.equals(magicWordType) || FunctionMagicWordType.SAFE_SUBST.equals(magicWordType) || FunctionMagicWordType.SUBST.equals(magicWordType)) {
errorLevel = ErrorLevel.WARNING;
}
}
if (!isOk) {
result = true;
done = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, function.getBeginIndex(), function.getEndIndex(), errorLevel);
if (FunctionMagicWordType.PAGE_NAME.equals(magicWordType)) {
errorResult.addReplacement(page.getTitle());
}
if (FunctionMagicWordType.IF_EXPR.equals(magicWordType)) {
for (int param = 1; param < function.getParameterCount(); param++) {
errorResult.addReplacement(function.getParameterValue(param));
}
}
if ((analysis.isInTag(currentIndex, WikiTagType.GALLERY) == null) && (analysis.isInTag(currentIndex, WikiTagType.INCLUDEONLY) == null) && (analysis.isInTag(currentIndex, WikiTagType.REF) == null) && (analysis.isInTag(currentIndex, WikiTagType.TIMELINE) == null) && (!FunctionMagicWordType.INVOKE.equals(magicWordType)) && (!FunctionMagicWordType.SUBST.equals(magicWordType)) && (!FunctionMagicWordType.SAFE_SUBST.equals(magicWordType))) {
errorResult.addReplacement("{{subst:" + contents.substring(function.getBeginIndex() + 2, function.getEndIndex()));
}
errors.add(errorResult);
nextIndex = function.getEndIndex();
} else {
nextIndex = currentIndex + 2;
}
}
}
}
currentIndex = Math.max(nextIndex, currentIndex + 1);
}
return result;
}
use of org.wikipediacleaner.api.data.PageElementTemplate in project wpcleaner by WPCleaner.
the class CheckErrorAlgorithm028 method getTableEnds.
/**
* Construct list of table ends.
*
* @param analysis Page analysis.
* @return List of table ends.
*/
private List<TableElement> getTableEnds(PageAnalysis analysis) {
List<TableElement> list = new ArrayList<>();
// Find tables ending by |}
String contents = analysis.getContents();
int index = contents.indexOf("|}");
while (index >= 0) {
if (shouldCount(analysis, index)) {
PageElementTemplate template = analysis.isInTemplate(index);
if ((template == null) || (template.getEndIndex() != index + 3)) {
list.add(new TableElement(index, index + 2, false));
}
}
index = contents.indexOf("|}", index + 1);
}
// Find tables ending by </table>
List<PageElementTag> tags = analysis.getTags(HtmlTagType.TABLE);
for (PageElementTag tag : tags) {
if (!tag.isFullTag() && tag.isEndTag()) {
if (shouldCount(analysis, index)) {
list.add(new TableElement(tag.getBeginIndex(), tag.getEndIndex(), false));
}
}
}
// Find tables ending by a template
if (!templateNames.isEmpty()) {
List<PageElementTemplate> templates = analysis.getTemplates();
for (PageElementTemplate template : templates) {
if (templateNames.contains(template.getTemplateName())) {
list.add(new TableElement(template.getBeginIndex(), template.getEndIndex(), false));
}
}
}
Collections.sort(list);
return list;
}
Aggregations