Search in sources :

Example 1 with EnumQueryResult

use of org.wikipediacleaner.api.constants.EnumQueryResult 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 EnumQueryResult

use of org.wikipediacleaner.api.constants.EnumQueryResult in project wpcleaner by WPCleaner.

the class APIException method waitForRetry.

/**
 * Wait for retry.
 */
public void waitForRetry() {
    EnumQueryResult result = getQueryResult();
    if (result != null) {
        final Logger log = LoggerFactory.getLogger(MediaWikiAPI.class);
        if (log != null) {
            log.warn("Waiting after error '" + code + "'");
        }
        result.waitForRetry();
    }
}
Also used : EnumQueryResult(org.wikipediacleaner.api.constants.EnumQueryResult) Logger(org.slf4j.Logger)

Example 3 with EnumQueryResult

use of org.wikipediacleaner.api.constants.EnumQueryResult in project wpcleaner by WPCleaner.

the class AutomaticFixWorker method handleFound.

/**
 * Handle when errors are found in a page.
 *
 * @param algorithms Algorithms that were supposed to be found.
 * @param page Page.
 * @param analysis Page analysis.
 * @param prefix Optional prefix for information.
 * @param markFixed True to mark articles as fixed in Check Wiki.
 * @throws APIException Problem with API.
 */
private void handleFound(List<CheckErrorAlgorithm> algorithms, Page page, PageAnalysis analysis, String prefix) throws APIException {
    // Handle when no modifications are found
    if (!saveModifications) {
        return;
    }
    // Fix all errors that can be fixed
    String newContents = page.getContents();
    List<AlgorithmError.Progress> errorsFixed = new ArrayList<>();
    if (!preventBot(analysis)) {
        newContents = AutomaticFormatter.tidyArticle(page, newContents, allAlgorithms, true, errorsFixed);
    }
    // Check if error has been fixed
    boolean isFixed = false;
    if (!newContents.equals(page.getContents())) {
        for (AlgorithmError.Progress errorFixed : errorsFixed) {
            if (algorithms.contains(errorFixed.algorithm)) {
                isFixed = true;
            }
        }
    }
    // Save page if errors have been fixed
    if (isFixed) {
        try {
            StringBuilder comment = new StringBuilder();
            if ((extraComment != null) && (extraComment.trim().length() > 0)) {
                comment.append(extraComment.trim());
                comment.append(" - ");
            }
            comment.append(getWikipedia().getCWConfiguration().getComment(errorsFixed));
            setText(((prefix != null) ? (prefix + " - ") : "") + GT._T("Fixing page {0}", page.getTitle()));
            API api = APIFactory.getAPI();
            api.updatePage(getWikipedia(), page, newContents, comment.toString(), true, true, true, false);
            incrementModified();
            for (AlgorithmError.Progress errorFixed : errorsFixed) {
                CheckErrorAlgorithm usedAlgorithm = errorFixed.algorithm;
                CheckErrorPage errorPage = AlgorithmError.analyzeError(usedAlgorithm, page.getAnalysis(newContents, true));
                if ((errorPage != null) && (!errorPage.getErrorFound())) {
                    CheckWiki checkWiki = APIFactory.getCheckWiki();
                    checkWiki.markAsFixed(page, usedAlgorithm.getErrorNumberString());
                    if (selectedAlgorithms.contains(usedAlgorithm)) {
                        incrementMarked();
                    } else {
                        incrementMarkedOther();
                    }
                }
            }
        } catch (APIException e) {
            EnumQueryResult result = e.getQueryResult();
            LOGGER.error("Error updating page {}: ", page.getTitle(), result.getCode());
            if (result == EnumQueryResult.SPAM_BLACKLIST) {
                LOGGER.error("Error ignored, continuing work");
            } else {
                throw e;
            }
        }
    } else if (analyzeNonFixed) {
        Controller.runFullAnalysis(page.getTitle(), null, getWikipedia());
    }
}
Also used : ArrayList(java.util.ArrayList) EnumQueryResult(org.wikipediacleaner.api.constants.EnumQueryResult) AlgorithmError(org.wikipediacleaner.api.algorithm.AlgorithmError) CheckWiki(org.wikipediacleaner.api.check.CheckWiki) APIException(org.wikipediacleaner.api.APIException) CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) API(org.wikipediacleaner.api.API) CheckErrorPage(org.wikipediacleaner.api.check.CheckErrorPage)

Example 4 with EnumQueryResult

use of org.wikipediacleaner.api.constants.EnumQueryResult in project wpcleaner by WPCleaner.

the class MediaWikiAPI method updatePage.

/**
 * Update a page on Wikipedia.
 *
 * @param wikipedia Wikipedia.
 * @param page Page.
 * @param newContents New contents to use.
 * @param comment Comment.
 * @param bot True if the edit should be flagged as bot.
 * @param minor True if the modification should be tagged as minor.
 * @param automatic True if the modification is automatic.
 * @param forceWatch Force watching the page.
 * @return Result of the command.
 * @throws APIException Exception thrown by the API.
 */
@Override
public QueryResult updatePage(EnumWikipedia wikipedia, Page page, String newContents, String comment, boolean bot, boolean minor, boolean automatic, boolean forceWatch) throws APIException {
    if (page == null) {
        throw new APIException("Page is null");
    }
    if (newContents == null) {
        throw new APIException("Contents is null");
    }
    if (comment == null) {
        throw new APIException("Comment is null");
    }
    ConnectionInformation connection = wikipedia.getConnection();
    if ((connection.getLgToken() == null) && (connection.getLgUserId() == null) && (connection.getLgUserName() == null)) {
        throw new APIException("You must be logged in to update pages");
    }
    int attemptNumber = 0;
    QueryResult result = null;
    do {
        attemptNumber++;
        Map<String, String> properties = getProperties(ApiRequest.ACTION_EDIT, true);
        properties.put("assert", "user");
        if (page.getContentsTimestamp() != null) {
            properties.put("basetimestamp", page.getContentsTimestamp());
        }
        if (bot) {
            properties.put("bot", "");
        }
        if (minor) {
            properties.put("minor", "");
        }
        if (page.getStartTimestamp() != null) {
            properties.put("starttimestamp", page.getStartTimestamp());
        }
        properties.put("summary", comment);
        properties.put("text", newContents);
        properties.put("title", page.getTitle());
        if (wikipedia.getConnection().getEditToken() != null) {
            properties.put("token", wikipedia.getConnection().getEditToken());
        }
        properties.put("watchlist", forceWatch ? "watch" : "nochange");
        CommentManager.manageComment(wikipedia.getConfiguration(), properties, "summary", "tags", automatic);
        checkTimeForEdit(wikipedia.getConnection().getUser(), page.getNamespace());
        try {
            boolean hasCaptcha = false;
            do {
                hasCaptcha = false;
                try {
                    result = constructEdit(getRoot(wikipedia, properties, 3), "/api/edit");
                } catch (CaptchaException e) {
                    String captchaAnswer = getCaptchaAnswer(wikipedia, e);
                    if (captchaAnswer != null) {
                        properties.put("captchaid", e.getId());
                        properties.put("captchaword", captchaAnswer);
                        hasCaptcha = true;
                    } else {
                        throw new APIException("CAPTCHA", e);
                    }
                }
            } while (hasCaptcha);
        } catch (APIException e) {
            if (e.getHttpStatus() == HttpStatus.SC_GATEWAY_TIMEOUT) {
                log.warn("Gateway timeout, waiting to see if modification has been taken into account");
                waitBeforeRetrying();
                Page tmpPage = page.replicatePage();
                retrieveContents(wikipedia, Collections.singletonList(tmpPage), false, false);
                String tmpContents = tmpPage.getContents();
                if ((tmpContents != null) && (tmpContents.equals(newContents))) {
                    return QueryResult.createCorrectQuery(tmpPage.getPageId(), tmpPage.getTitle(), page.getPageId(), tmpPage.getPageId());
                }
            }
            if (attemptNumber > 1) {
                log.warn("Error updating page {}", page.getTitle());
                throw e;
            }
            EnumQueryResult queryResult = e.getQueryResult();
            if (queryResult == EnumQueryResult.BAD_TOKEN) {
                waitBeforeRetrying();
                log.warn("Retrieving tokens after a BAD_TOKEN answer");
                retrieveTokens(wikipedia);
            } else if ((queryResult != null) && (!queryResult.shouldRetry())) {
                log.warn("Error updating page {}", page.getTitle());
                throw e;
            }
        } catch (JDOMParseException e) {
            log.error("Error updating page: {}", e.getMessage());
            throw new APIException("Error parsing XML", e);
        }
    } while (result == null);
    return result;
}
Also used : JDOMParseException(org.jdom2.input.JDOMParseException) ConnectionInformation(org.wikipediacleaner.api.constants.ConnectionInformation) EnumQueryResult(org.wikipediacleaner.api.constants.EnumQueryResult) QueryResult(org.wikipediacleaner.api.data.QueryResult) APIException(org.wikipediacleaner.api.APIException) EnumQueryResult(org.wikipediacleaner.api.constants.EnumQueryResult) Page(org.wikipediacleaner.api.data.Page) EnumQueryPage(org.wikipediacleaner.api.constants.EnumQueryPage) CaptchaException(org.wikipediacleaner.api.CaptchaException)

Aggregations

EnumQueryResult (org.wikipediacleaner.api.constants.EnumQueryResult)4 ArrayList (java.util.ArrayList)2 APIException (org.wikipediacleaner.api.APIException)2 AlgorithmError (org.wikipediacleaner.api.algorithm.AlgorithmError)2 CheckErrorAlgorithm (org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm)2 Page (org.wikipediacleaner.api.data.Page)2 List (java.util.List)1 JDOMParseException (org.jdom2.input.JDOMParseException)1 Logger (org.slf4j.Logger)1 API (org.wikipediacleaner.api.API)1 CaptchaException (org.wikipediacleaner.api.CaptchaException)1 CheckErrorPage (org.wikipediacleaner.api.check.CheckErrorPage)1 CheckWiki (org.wikipediacleaner.api.check.CheckWiki)1 ConnectionInformation (org.wikipediacleaner.api.constants.ConnectionInformation)1 EnumQueryPage (org.wikipediacleaner.api.constants.EnumQueryPage)1 QueryResult (org.wikipediacleaner.api.data.QueryResult)1 UpdateDabWarningTools (org.wikipediacleaner.gui.swing.worker.UpdateDabWarningTools)1 Configuration (org.wikipediacleaner.utils.Configuration)1