Search in sources :

Example 1 with UpdateDabWarningTools

use of org.wikipediacleaner.gui.swing.worker.UpdateDabWarningTools 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 UpdateDabWarningTools

use of org.wikipediacleaner.gui.swing.worker.UpdateDabWarningTools in project wpcleaner by WPCleaner.

the class MonitorRCWindow method createComponents.

/**
 * @return Window components.
 */
@Override
protected Component createComponents() {
    JPanel panel = new JPanel(new GridBagLayout());
    // Initialize constraints
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.gridheight = 1;
    constraints.gridwidth = 1;
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.insets = new Insets(0, 0, 0, 0);
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.weightx = 0;
    constraints.weighty = 0;
    // Recent changes table
    constraints.fill = GridBagConstraints.BOTH;
    constraints.weightx = 1;
    constraints.weighty = 1;
    modelRC = new RecentChangesTableModel(null);
    tableRC = new JTable(modelRC);
    modelRC.configureColumnModel(tableRC.getColumnModel());
    JScrollPane scrollRC = new JScrollPane(tableRC);
    scrollRC.setMinimumSize(new Dimension(300, 200));
    scrollRC.setPreferredSize(new Dimension(800, 300));
    scrollRC.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    panel.add(scrollRC, constraints);
    constraints.gridy++;
    // Interesting recent changes table
    constraints.fill = GridBagConstraints.BOTH;
    constraints.weightx = 1;
    constraints.weighty = 1;
    modelRCInteresting = new RecentChangesTableModel(null);
    tableRCInteresting = new JTable(modelRCInteresting);
    modelRCInteresting.configureColumnModel(tableRCInteresting.getColumnModel());
    JScrollPane scrollRCInteresting = new JScrollPane(tableRCInteresting);
    scrollRCInteresting.setMinimumSize(new Dimension(300, 200));
    scrollRCInteresting.setPreferredSize(new Dimension(800, 300));
    scrollRCInteresting.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    panel.add(scrollRCInteresting, constraints);
    constraints.gridy++;
    // Buttons
    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton buttonClose = ActionDispose.createButton(getParentComponent(), true, false);
    buttonPanel.add(buttonClose);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.gridx = 0;
    constraints.weightx = 1;
    constraints.weighty = 0;
    panel.add(buttonPanel, constraints);
    constraints.gridy++;
    updateComponentState();
    monitoredPages = new HashMap<>();
    createDabWarning = new UpdateDabWarningTools(getWikipedia(), this, true);
    updateDabWarning = new UpdateDabWarningTools(getWikipedia(), this, false);
    API api = APIFactory.getAPI();
    api.addRecentChangesListener(getWikipedia(), this);
    return panel;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) FlowLayout(java.awt.FlowLayout) GridBagLayout(java.awt.GridBagLayout) JButton(javax.swing.JButton) Dimension(java.awt.Dimension) UpdateDabWarningTools(org.wikipediacleaner.gui.swing.worker.UpdateDabWarningTools) JTable(javax.swing.JTable) API(org.wikipediacleaner.api.API)

Aggregations

UpdateDabWarningTools (org.wikipediacleaner.gui.swing.worker.UpdateDabWarningTools)2 Dimension (java.awt.Dimension)1 FlowLayout (java.awt.FlowLayout)1 GridBagConstraints (java.awt.GridBagConstraints)1 GridBagLayout (java.awt.GridBagLayout)1 Insets (java.awt.Insets)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JButton (javax.swing.JButton)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1 JTable (javax.swing.JTable)1 API (org.wikipediacleaner.api.API)1 AlgorithmError (org.wikipediacleaner.api.algorithm.AlgorithmError)1 CheckErrorAlgorithm (org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm)1 EnumQueryResult (org.wikipediacleaner.api.constants.EnumQueryResult)1 Page (org.wikipediacleaner.api.data.Page)1 Configuration (org.wikipediacleaner.utils.Configuration)1