Search in sources :

Example 1 with PageElementTemplate

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);
    }
}
Also used : PageElementTemplate(org.wikipediacleaner.api.data.PageElementTemplate) PageElementLanguageLink(org.wikipediacleaner.api.data.PageElementLanguageLink) PageElementCategory(org.wikipediacleaner.api.data.PageElementCategory) PageAnalysis(org.wikipediacleaner.api.data.analysis.PageAnalysis) Page(org.wikipediacleaner.api.data.Page) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString)

Example 2 with PageElementTemplate

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;
}
Also used : PageElementTemplate(org.wikipediacleaner.api.data.PageElementTemplate) CheckErrorResult(org.wikipediacleaner.api.check.CheckErrorResult) PageElementCategory(org.wikipediacleaner.api.data.PageElementCategory) PageElementTitle(org.wikipediacleaner.api.data.PageElementTitle)

Example 3 with PageElementTemplate

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();
}
Also used : PageElementTemplate(org.wikipediacleaner.api.data.PageElementTemplate) Parameter(org.wikipediacleaner.api.data.PageElementTemplate.Parameter)

Example 4 with PageElementTemplate

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;
}
Also used : PageElementMagicWord(org.wikipediacleaner.api.data.PageElementMagicWord) PageElementTemplate(org.wikipediacleaner.api.data.PageElementTemplate) CheckErrorResult(org.wikipediacleaner.api.check.CheckErrorResult) PageElementMagicWord(org.wikipediacleaner.api.data.PageElementMagicWord) MagicWord(org.wikipediacleaner.api.data.contents.magicword.MagicWord) ErrorLevel(org.wikipediacleaner.api.check.CheckErrorResult.ErrorLevel) Page(org.wikipediacleaner.api.data.Page) PageElementParameter(org.wikipediacleaner.api.data.PageElementParameter) PageElementFunction(org.wikipediacleaner.api.data.PageElementFunction) FunctionMagicWordType(org.wikipediacleaner.api.data.contents.magicword.FunctionMagicWordType) MagicWordType(org.wikipediacleaner.api.data.contents.magicword.MagicWordType) SimpleMagicWordType(org.wikipediacleaner.api.data.contents.magicword.SimpleMagicWordType)

Example 5 with PageElementTemplate

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;
}
Also used : PageElementTemplate(org.wikipediacleaner.api.data.PageElementTemplate) PageElementTag(org.wikipediacleaner.api.data.PageElementTag) ArrayList(java.util.ArrayList)

Aggregations

PageElementTemplate (org.wikipediacleaner.api.data.PageElementTemplate)103 CheckErrorResult (org.wikipediacleaner.api.check.CheckErrorResult)51 PageElementTag (org.wikipediacleaner.api.data.PageElementTag)24 PageElementFunction (org.wikipediacleaner.api.data.PageElementFunction)23 ArrayList (java.util.ArrayList)20 Parameter (org.wikipediacleaner.api.data.PageElementTemplate.Parameter)20 PageElementExternalLink (org.wikipediacleaner.api.data.PageElementExternalLink)17 WPCConfigurationString (org.wikipediacleaner.api.configuration.WPCConfigurationString)16 PageAnalysis (org.wikipediacleaner.api.data.analysis.PageAnalysis)13 PageElementInternalLink (org.wikipediacleaner.api.data.PageElementInternalLink)12 PageElementTitle (org.wikipediacleaner.api.data.PageElementTitle)12 PageElement (org.wikipediacleaner.api.data.PageElement)11 AlgorithmParameter (org.wikipediacleaner.api.algorithm.AlgorithmParameter)10 Page (org.wikipediacleaner.api.data.Page)10 PageElementCategory (org.wikipediacleaner.api.data.PageElementCategory)10 PageElementImage (org.wikipediacleaner.api.data.PageElementImage)10 ConfigurationValueString (org.wikipediacleaner.utils.ConfigurationValueString)10 ActionExternalViewer (org.wikipediacleaner.gui.swing.action.ActionExternalViewer)9 ContentsComment (org.wikipediacleaner.api.data.contents.comment.ContentsComment)8 PageElementISBN (org.wikipediacleaner.api.data.PageElementISBN)7