Search in sources :

Example 16 with Page

use of org.wikipediacleaner.api.data.Page 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 17 with Page

use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.

the class ActionUpdateWarning method actionDisambiguationWarning.

/**
 * Update disambiguation warnings on talk page.
 */
public void actionDisambiguationWarning() {
    // Check selection
    if (wikiProvider == null) {
        return;
    }
    EnumWikipedia wiki = wikiProvider.getWiki();
    if (wiki == null) {
        return;
    }
    List<Page> pages = getPages();
    if ((pages == null) || (pages.isEmpty())) {
        return;
    }
    // Check configuration
    WPCConfiguration wpcConfig = wiki.getConfiguration();
    String template = wpcConfig.getString(WPCConfigurationString.DAB_WARNING_TEMPLATE);
    if ((template == null) || (template.trim().length() == 0)) {
        Utilities.displayMessageForMissingConfiguration(parent, WPCConfigurationString.DAB_WARNING_TEMPLATE.getAttributeName());
        return;
    }
    // Ask for confirmation
    int answer = Utilities.displayYesNoWarning(parent, GT._T("Do you want to update the disambiguation warning on talk page?"));
    if (answer != JOptionPane.YES_OPTION) {
        return;
    }
    // Update warning
    UpdateDabWarningWorker worker = new UpdateDabWarningWorker(wiki, window, pages, false, false, false, false);
    worker.start();
}
Also used : UpdateDabWarningWorker(org.wikipediacleaner.gui.swing.worker.UpdateDabWarningWorker) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration)

Example 18 with Page

use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.

the class ActionUpdateWarning method actionDuplicateArgsWarning.

/**
 * Update duplicate arguments warnings on talk page.
 */
public void actionDuplicateArgsWarning() {
    // Check selection
    if (wikiProvider == null) {
        return;
    }
    EnumWikipedia wiki = wikiProvider.getWiki();
    if (wiki == null) {
        return;
    }
    List<Page> pages = getPages();
    if ((pages == null) || (pages.isEmpty())) {
        return;
    }
    // Check configuration
    WPCConfiguration wpcConfig = wiki.getConfiguration();
    String template = wpcConfig.getString(WPCConfigurationString.DUPLICATE_ARGS_WARNING_TEMPLATE);
    if ((template == null) || (template.trim().length() == 0)) {
        Utilities.displayMessageForMissingConfiguration(parent, WPCConfigurationString.DUPLICATE_ARGS_WARNING_TEMPLATE.getAttributeName());
        return;
    }
    // Ask for confirmation
    int answer = Utilities.displayYesNoWarning(parent, GT._T("Do you want to update the duplicate arguments warning on talk page?"));
    if (answer != JOptionPane.YES_OPTION) {
        return;
    }
    // Update warning
    UpdateDuplicateArgsWarningWorker worker = new UpdateDuplicateArgsWarningWorker(wiki, window, pages, false, false);
    worker.start();
}
Also used : UpdateDuplicateArgsWarningWorker(org.wikipediacleaner.gui.swing.worker.UpdateDuplicateArgsWarningWorker) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration)

Example 19 with Page

use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.

the class ActionUpdateWarning method actionISBNWarning.

/**
 * Update ISBN warnings on talk page.
 */
public void actionISBNWarning() {
    // Check selection
    if (wikiProvider == null) {
        return;
    }
    EnumWikipedia wiki = wikiProvider.getWiki();
    if (wiki == null) {
        return;
    }
    List<Page> pages = getPages();
    if ((pages == null) || (pages.isEmpty())) {
        return;
    }
    // Check configuration
    WPCConfiguration wpcConfig = wiki.getConfiguration();
    String template = wpcConfig.getString(WPCConfigurationString.ISBN_WARNING_TEMPLATE);
    if ((template == null) || (template.trim().length() == 0)) {
        Utilities.displayMessageForMissingConfiguration(parent, WPCConfigurationString.ISBN_WARNING_TEMPLATE.getAttributeName());
        return;
    }
    // Ask for confirmation
    int answer = Utilities.displayYesNoWarning(parent, GT._T("Do you want to update the ISBN warning on talk page?"));
    if (answer != JOptionPane.YES_OPTION) {
        return;
    }
    // Update warning
    UpdateISBNWarningWorker worker = new UpdateISBNWarningWorker(wiki, window, pages, false, false);
    worker.start();
}
Also used : EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) UpdateISBNWarningWorker(org.wikipediacleaner.gui.swing.worker.UpdateISBNWarningWorker) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration)

Example 20 with Page

use of org.wikipediacleaner.api.data.Page 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)

Aggregations

Page (org.wikipediacleaner.api.data.Page)205 APIException (org.wikipediacleaner.api.APIException)71 ArrayList (java.util.ArrayList)66 API (org.wikipediacleaner.api.API)47 WPCConfigurationString (org.wikipediacleaner.api.configuration.WPCConfigurationString)39 ConfigurationValueString (org.wikipediacleaner.utils.ConfigurationValueString)32 WPCConfiguration (org.wikipediacleaner.api.configuration.WPCConfiguration)30 EnumQueryPage (org.wikipediacleaner.api.constants.EnumQueryPage)29 EnumWikipedia (org.wikipediacleaner.api.constants.EnumWikipedia)29 Configuration (org.wikipediacleaner.utils.Configuration)26 Element (org.jdom2.Element)25 JDOMException (org.jdom2.JDOMException)21 HashMap (java.util.HashMap)15 CheckErrorPage (org.wikipediacleaner.api.check.CheckErrorPage)15 PageAnalysis (org.wikipediacleaner.api.data.analysis.PageAnalysis)13 List (java.util.List)12 MediaWiki (org.wikipediacleaner.api.MediaWiki)12 JMenu (javax.swing.JMenu)10 PageElementTemplate (org.wikipediacleaner.api.data.PageElementTemplate)10 ActionWatchPage (org.wikipediacleaner.gui.swing.action.ActionWatchPage)10