Search in sources :

Example 1 with CheckErrorAlgorithm

use of org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm in project wpcleaner by WPCleaner.

the class MediaWiki method replaceText.

/**
 * Replace text in a list of pages.
 *
 * @param pages List of pages.
 * @param replacements List of text replacements
 *        Key: Additional comments used for the modification.
 *        Value: Text replacements.
 * @param wiki Wiki.
 * @param comment Comment used for the modification.
 * @param report (Out) Report of changes made.
 * @param automaticCW Lit of CW fixing that should be done.
 * @param forceCW List of CW fixing that should be done even if no automatic replacement was done.
 * @param save True if modification should be saved.
 * @param updateDabWarning True to update disambiguation warning.
 * @param minor True if the modification should be tagged as minor.
 * @param pauseAfterEachEdit True to pause after each edit.
 * @param botFix True to apply bot fixes.
 * @param parent Parent window.
 * @return Count of modified pages.
 * @throws APIException Exception thrown by the API.
 */
public int replaceText(Page[] pages, Map<String, List<AutomaticFixing>> replacements, EnumWikipedia wiki, String comment, ModificationReport report, Collection<CheckErrorAlgorithm> automaticCW, Collection<CheckErrorAlgorithm> forceCW, boolean save, boolean updateDabWarning, boolean minor, boolean pauseAfterEachEdit, boolean botFix, Component parent) throws APIException {
    if ((pages == null) || (replacements == null) || (replacements.size() == 0)) {
        return 0;
    }
    // Initialize page loading
    Configuration config = Configuration.getConfiguration();
    int nThreads = Math.max(config.getInt(null, ConfigurationValueInteger.INTERROG_THREAD), 1);
    int currentPage = 0;
    while ((currentPage < nThreads) && (currentPage < pages.length)) {
        // TODO: withRedirects=false ?
        retrieveContents(wiki, pages[currentPage], false, true, false, true, false);
        // To release memory
        pages[currentPage] = null;
        currentPage++;
    }
    // Analyze pages
    UpdateDabWarningTools dabWarnings = new UpdateDabWarningTools(wiki, null, false, false);
    int count = 0;
    final API api = APIFactory.getAPI();
    StringBuilder details = new StringBuilder();
    StringBuilder fullComment = new StringBuilder();
    ModificationReport.Modification modification = null;
    boolean stopRequested = false;
    while (hasRemainingTask() && !shouldStop() && !stopRequested) {
        Object result = getNextResult();
        if (currentPage < pages.length) {
            // TODO: withRedirects=false ?
            retrieveContents(wiki, pages[currentPage], false, true, false, true, false);
            // To release memory
            pages[currentPage] = null;
            currentPage++;
        }
        if ((result != null) && (result instanceof Page)) {
            List<String> replacementsDone = new ArrayList<>();
            Page page = (Page) result;
            String oldContents = page.getContents();
            if (oldContents != null) {
                String newContents = oldContents;
                details.setLength(0);
                fullComment.setLength(0);
                if (report != null) {
                    modification = new ModificationReport.Modification(page.getTitle());
                }
                // Apply automatic fixing
                for (Entry<String, List<AutomaticFixing>> replacement : replacements.entrySet()) {
                    replacementsDone.clear();
                    String tmpContents = AutomaticFixing.apply(replacement.getValue(), newContents, replacementsDone);
                    if (!newContents.equals(tmpContents)) {
                        newContents = tmpContents;
                        // Update description
                        if (modification != null) {
                            for (String replacementDone : replacementsDone) {
                                modification.addModification(replacementDone);
                            }
                        }
                        // Memorize replacement
                        if ((replacement.getKey() != null) && (replacement.getKey().length() > 0)) {
                            if (details.length() > 0) {
                                details.append(", ");
                            }
                            details.append(replacement.getKey());
                        }
                    }
                }
                fullComment.append(wiki.createUpdatePageComment(comment, details.toString()));
                // Apply automatic CW fixing if needed
                if (automaticCW != null) {
                    // Apply fixing
                    List<AlgorithmError.Progress> usedAlgorithms = new ArrayList<>();
                    String tmpContents = AutomaticFormatter.tidyArticle(page, newContents, automaticCW, botFix, usedAlgorithms);
                    // Decide if modifications should be kept
                    boolean shouldKeep = (!oldContents.equals(newContents));
                    if (forceCW != null) {
                        for (AlgorithmError.Progress progress : usedAlgorithms) {
                            if (forceCW.contains(progress.algorithm)) {
                                shouldKeep = true;
                            }
                        }
                    }
                    // Keep modifications
                    if (shouldKeep) {
                        newContents = tmpContents;
                        if (!usedAlgorithms.isEmpty()) {
                            fullComment.append(" / ");
                            fullComment.append(wiki.getCWConfiguration().getComment(usedAlgorithms));
                            if (modification != null) {
                                for (AlgorithmError.Progress progress : usedAlgorithms) {
                                    CheckErrorAlgorithm algorithm = progress.algorithm;
                                    modification.addModification(algorithm.getShortDescriptionReplaced());
                                }
                            }
                        }
                    }
                }
                // Page contents has been modified
                if (!oldContents.equals(newContents)) {
                    if (report != null) {
                        report.addModification(modification);
                    }
                    // Save page
                    setText(GT._T("Updating page {0}", page.getTitle()));
                    count++;
                    if (save && !stopRequested) {
                        try {
                            api.updatePage(wiki, page, newContents, fullComment.toString(), true, minor, false, false);
                            if (updateDabWarning) {
                                List<Page> tmpList = new ArrayList<>(1);
                                tmpList.add(page);
                                dabWarnings.updateWarning(tmpList, null, null, null);
                            }
                            if (pauseAfterEachEdit) {
                                int answer = Utilities.displayYesNoAllWarning(parent, GT._T("The page {0} has been modified.", page.getTitle()) + "\n" + GT._T("Do you want to continue?"));
                                switch(answer) {
                                    case JOptionPane.YES_OPTION:
                                        break;
                                    case Utilities.YES_ALL_OPTION:
                                        pauseAfterEachEdit = false;
                                        break;
                                    default:
                                        stopRequested = true;
                                }
                            }
                        } catch (APIException e) {
                            EnumQueryResult error = e.getQueryResult();
                            if (report != null) {
                                report.addError(new ModificationReport.Error(page.getTitle(), error));
                            }
                            if (EnumQueryResult.PROTECTED_PAGE.equals(error)) {
                                System.err.println("Page " + page.getTitle() + " is protected.");
                            } else {
                                throw e;
                            }
                        }
                    }
                }
            }
        }
    }
    block(true);
    return count;
}
Also used : Configuration(org.wikipediacleaner.utils.Configuration) ArrayList(java.util.ArrayList) EnumQueryResult(org.wikipediacleaner.api.constants.EnumQueryResult) AlgorithmError(org.wikipediacleaner.api.algorithm.AlgorithmError) Page(org.wikipediacleaner.api.data.Page) AlgorithmError(org.wikipediacleaner.api.algorithm.AlgorithmError) UpdateDabWarningTools(org.wikipediacleaner.gui.swing.worker.UpdateDabWarningTools) CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with CheckErrorAlgorithm

use of org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm in project wpcleaner by WPCleaner.

the class Bot method executeSet.

/**
 * Execute an action of type Set.
 *
 * @param actionConfig Parameters of the action.
 * @return True if the action was executed.
 */
private boolean executeSet(Action actionConfig) {
    String[] actionArgs = actionConfig.actionArgs;
    if (actionArgs.length < 1) {
        return false;
    }
    String parameter = actionArgs[0];
    // Set AdditionalAlgorithms
    if ("AdditionalAlgorithms".equalsIgnoreCase(parameter)) {
        additionalAlgorithms.clear();
        if (actionArgs.length > 1) {
            extractAlgorithms(additionalAlgorithms, null, actionArgs, 1);
        }
        return true;
    }
    // Set Configuration
    if ("Configuration".equalsIgnoreCase(parameter)) {
        if (actionArgs.length > 2) {
            Configuration config = Configuration.getConfiguration();
            config.forceValue(actionArgs[1], actionArgs[2]);
            for (CheckErrorAlgorithm algorithm : CheckErrorAlgorithms.getAlgorithms(wiki)) {
                algorithm.setConfiguration(wiki.getWikiConfiguration(), wiki.getCWConfiguration(), wiki.getConfiguration());
            }
        }
        return true;
    }
    // Set Namespaces
    if ("Namespaces".equalsIgnoreCase(parameter)) {
        namespaces = new HashSet<>();
        int currentArg = 1;
        while (currentArg < actionArgs.length) {
            try {
                namespaces.add(Integer.valueOf(actionArgs[currentArg]));
            } catch (NumberFormatException e) {
                log.warn("Incorrect namespace {}", actionArgs[currentArg]);
            }
            currentArg++;
        }
        return true;
    }
    // Set Prefix
    if ("Prefix".equalsIgnoreCase(parameter)) {
        if (actionArgs.length > 1) {
            CommentManager.addExtraText(actionArgs[1].replaceAll("_", " "));
        }
        return true;
    }
    // Set Range
    if ("RangeBegin".equalsIgnoreCase(parameter)) {
        rangeBegin = null;
        if (actionArgs.length > 1) {
            rangeBegin = actionArgs[1];
        }
        return true;
    }
    if ("RangeEnd".equalsIgnoreCase(parameter)) {
        rangeEnd = null;
        if (actionArgs.length > 1) {
            rangeEnd = actionArgs[1];
        }
        return true;
    }
    // Set Time limit
    if ("TimeLimit".equalsIgnoreCase(parameter) && (actionArgs.length > 1)) {
        try {
            timeLimit = System.currentTimeMillis() + 1000 * Long.parseLong(actionArgs[1]);
        } catch (NumberFormatException e) {
            log.warn("Incorrect time limit {}", actionArgs[1]);
        }
        return true;
    }
    // Set Typo groups
    if ("TypoGroups".equalsIgnoreCase(parameter) && (actionArgs.length > 1)) {
        for (int numArg = 1; numArg < actionArgs.length; numArg++) {
            typoGroups.add(actionArgs[numArg]);
        }
        return true;
    }
    return false;
}
Also used : Configuration(org.wikipediacleaner.utils.Configuration) CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString)

Example 3 with CheckErrorAlgorithm

use of org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm in project wpcleaner by WPCleaner.

the class Bot method executeFixCheckWiki.

/**
 * Execute an action of type FixCheckWiki.
 *
 * @param actionConfig Parameters of the action.
 * @return True if the action was executed.
 */
private BasicWorker executeFixCheckWiki(Action actionConfig) {
    List<CheckErrorAlgorithm> algorithms = new ArrayList<>();
    List<CheckErrorAlgorithm> allAlgorithms = new ArrayList<>();
    if (actionConfig.actionArgs.length > 0) {
        extractAlgorithms(algorithms, allAlgorithms, actionConfig.actionArgs, 0);
    }
    AutomaticCWWorker worker = new AutomaticCWWorker(wiki, null, algorithms, 10000, true, allAlgorithms, null, true, false);
    worker.setRange(rangeBegin, rangeEnd);
    return worker;
}
Also used : CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) ArrayList(java.util.ArrayList) AutomaticCWWorker(org.wikipediacleaner.gui.swing.bot.AutomaticCWWorker)

Example 4 with CheckErrorAlgorithm

use of org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm in project wpcleaner by WPCleaner.

the class Bot method executeFixDump.

/**
 * Execute an action of type FixDump.
 *
 * @param actionConfig Parameters of the action.
 * @return True if the action was executed.
 */
private BasicWorker executeFixDump(Action actionConfig) {
    // Check for global parameters
    String[] actionArgs = actionConfig.actionArgs;
    int currentArg = 0;
    // Check for parameters
    if (actionArgs.length > currentArg + 1) {
        File dumpFile = getDumpFile(actionArgs[currentArg]);
        List<CheckErrorAlgorithm> algorithms = new ArrayList<>();
        List<CheckErrorAlgorithm> allAlgorithms = new ArrayList<>();
        extractAlgorithms(algorithms, allAlgorithms, actionArgs, currentArg + 1);
        return new FixDumpWorker(wiki, null, dumpFile, algorithms, allAlgorithms, namespaces);
    }
    return null;
}
Also used : CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) ArrayList(java.util.ArrayList) FixDumpWorker(org.wikipediacleaner.gui.swing.bot.FixDumpWorker) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) File(java.io.File)

Example 5 with CheckErrorAlgorithm

use of org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm in project wpcleaner by WPCleaner.

the class Bot method executeListCheckWiki.

/**
 * Execute an action of type ListCheckWiki.
 *
 * @param actionConfig Parameters of the action.
 * @return True if the action was executed.
 */
private BasicWorker executeListCheckWiki(Action actionConfig) {
    // Check for global parameters
    String[] actionArgs = actionConfig.actionArgs;
    int currentArg = 0;
    boolean check = true;
    boolean onlyRecheck = false;
    boolean optionsFinished = false;
    while (!optionsFinished && (actionArgs.length > currentArg)) {
        if ("-nocheck".equalsIgnoreCase(actionArgs[currentArg])) {
            check = false;
            currentArg++;
        } else if ("-onlyRecheck".equalsIgnoreCase(actionArgs[currentArg])) {
            onlyRecheck = true;
            currentArg++;
        } else {
            optionsFinished = true;
        }
    }
    // Check for parameters
    if (actionArgs.length > currentArg + 2) {
        File dumpFile = getDumpFile(actionArgs[currentArg]);
        List<CheckErrorAlgorithm> algorithms = new ArrayList<>();
        extractAlgorithms(algorithms, null, actionArgs, currentArg + 2);
        if (actionArgs[currentArg + 1].startsWith("wiki:")) {
            String pageName = actionArgs[currentArg + 1].substring(5);
            return new ListCWWorker(wiki, null, dumpFile, pageName, algorithms, namespaces, check, onlyRecheck);
        }
        File output = new File(actionArgs[currentArg + 1]);
        return new ListCWWorker(wiki, null, dumpFile, output, algorithms, namespaces, check);
    }
    return null;
}
Also used : CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) ArrayList(java.util.ArrayList) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) File(java.io.File) ListCWWorker(org.wikipediacleaner.gui.swing.bot.ListCWWorker) AutomaticListCWWorker(org.wikipediacleaner.gui.swing.bot.AutomaticListCWWorker)

Aggregations

CheckErrorAlgorithm (org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm)50 ArrayList (java.util.ArrayList)25 AlgorithmError (org.wikipediacleaner.api.algorithm.AlgorithmError)15 Configuration (org.wikipediacleaner.utils.Configuration)14 CheckErrorPage (org.wikipediacleaner.api.check.CheckErrorPage)13 ConfigurationValueString (org.wikipediacleaner.utils.ConfigurationValueString)11 WPCConfigurationString (org.wikipediacleaner.api.configuration.WPCConfigurationString)9 APIException (org.wikipediacleaner.api.APIException)6 CheckWiki (org.wikipediacleaner.api.check.CheckWiki)6 EnumWikipedia (org.wikipediacleaner.api.constants.EnumWikipedia)6 Page (org.wikipediacleaner.api.data.Page)6 PageAnalysis (org.wikipediacleaner.api.data.analysis.PageAnalysis)6 CheckErrorResult (org.wikipediacleaner.api.check.CheckErrorResult)5 API (org.wikipediacleaner.api.API)4 ActionListener (java.awt.event.ActionListener)3 File (java.io.File)3 JMenuItem (javax.swing.JMenuItem)3 JPopupMenu (javax.swing.JPopupMenu)3 WPCConfiguration (org.wikipediacleaner.api.configuration.WPCConfiguration)3 PageElementTemplate (org.wikipediacleaner.api.data.PageElementTemplate)3