Search in sources :

Example 1 with Stats

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

the class MonitorRCWindow method recentChanges.

/**
 * Callback to be notified about recent changes.
 *
 * @param newRC List of recent changes.
 * @param currentTime Current time.
 * @see org.wikipediacleaner.api.RecentChangesListener#recentChanges(java.util.List, java.util.Date)
 */
@Override
public void recentChanges(List<RecentChange> newRC, Date currentTime) {
    // Retrieve configuration
    WPCConfiguration config = getWikipedia().getConfiguration();
    long delayForNew = config.getLong(WPCConfigurationLong.RC_NEW_ARTICLE_WITH_DAB_DELAY) * 60 * 1000;
    long delayMonitoring = config.getLong(WPCConfigurationLong.RC_KEEP_MONITORING_DELAY) * 60 * 1000;
    // Add new recent changes to the list
    modelRC.addRecentChanges(newRC);
    // Remove old changes
    List<RecentChange> filteredNewRC = new ArrayList<>();
    for (RecentChange rc : newRC) {
        if (currentTime.getTime() < rc.getTimestamp().getTime() + delayForNew) {
            filteredNewRC.add(rc);
        }
    }
    // Check if an update has been made on a monitored page
    for (RecentChange rc : filteredNewRC) {
        if (monitoredPages.containsKey(rc.getTitle())) {
            Page page = DataManager.createSimplePage(getWikipedia(), rc.getTitle(), null, null, null);
            try {
                updateDabWarning.updateWarning(Collections.singletonList(page), null, null, null);
            } catch (APIException e) {
            // Nothing to do
            }
            monitoredPages.put(rc.getTitle(), Long.valueOf(currentTime.getTime()));
        }
    }
    // Check monitored pages for expired delay
    Iterator<Entry<String, Long>> itPages = monitoredPages.entrySet().iterator();
    while (itPages.hasNext()) {
        Entry<String, Long> entry = itPages.next();
        if (currentTime.getTime() > entry.getValue().longValue() + delayMonitoring) {
            itPages.remove();
        }
    }
    // Update list of interesting recent changes
    for (RecentChange rc : filteredNewRC) {
        if (isInterestingNamespace(rc)) {
            if (RecentChange.TYPE_NEW.equals(rc.getType())) {
                if (rc.isNew()) {
                    modelRCInteresting.addRecentChange(rc);
                }
            } else if (RecentChange.TYPE_EDIT.equals(rc.getType())) {
                if (modelRCInteresting.containsRecentChange(rc.getTitle())) {
                    modelRCInteresting.addRecentChange(rc);
                }
            } else if (RecentChange.TYPE_LOG.equals(rc.getType())) {
                if (RecentChange.LOG_TYPE_DELETE.equals(rc.getLogType()) && RecentChange.LOG_ACTION_DELETE_DELETE.equals(rc.getLogAction())) {
                    modelRCInteresting.removeRecentChanges(rc.getTitle());
                }
            }
        }
    }
    // Check if interesting recent changes are old enough
    List<RecentChange> interestingRC = modelRCInteresting.getRecentChanges();
    List<Page> pages = new ArrayList<>();
    Map<String, String> creators = new HashMap<>();
    Map<String, List<String>> modifiers = new HashMap<>();
    while (!interestingRC.isEmpty()) {
        // Retrieve synthetic information about recent changes for one title
        List<RecentChange> listRC = extractRecentChanges(interestingRC);
        String title = listRC.get(0).getTitle();
        String creator = null;
        List<String> pageModifiers = new ArrayList<>();
        boolean oldEnough = true;
        boolean redirect = false;
        for (int rcNum = listRC.size(); rcNum > 0; rcNum--) {
            RecentChange rc = listRC.get(rcNum - 1);
            if (currentTime.getTime() <= rc.getTimestamp().getTime() + delayForNew) {
                oldEnough = false;
            }
            String user = rc.getUser();
            redirect = rc.isRedirect();
            if (rc.isNew()) {
                creator = user;
            } else {
                if (!rc.isBot()) {
                    if ((creator == null) || (!creator.equals(user))) {
                        if (!pageModifiers.contains(user)) {
                            pageModifiers.add(user);
                        }
                    }
                }
            }
        }
        if (oldEnough) {
            modelRCInteresting.removeRecentChanges(title);
            if (!redirect) {
                Page page = DataManager.createSimplePage(getWikipedia(), title, null, null, null);
                pages.add(page);
                creators.put(title, creator);
                modifiers.put(title, pageModifiers);
            }
        }
    }
    // Update disambiguation warnings
    if (!pages.isEmpty()) {
        try {
            Stats stats = new Stats();
            createDabWarning.updateWarning(pages, creators, modifiers, stats);
            List<Page> updatedPages = stats.getUpdatedPages();
            if (updatedPages != null) {
                for (Page page : updatedPages) {
                    monitoredPages.put(page.getTitle(), Long.valueOf(currentTime.getTime()));
                }
            }
        } catch (APIException e) {
        // Nothing to do
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Page(org.wikipediacleaner.api.data.Page) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) Entry(java.util.Map.Entry) RecentChange(org.wikipediacleaner.api.data.RecentChange) APIException(org.wikipediacleaner.api.APIException) WPCConfigurationLong(org.wikipediacleaner.api.configuration.WPCConfigurationLong) Stats(org.wikipediacleaner.gui.swing.worker.UpdateWarningTools.Stats) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Stats

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

the class UpdateUnknownParameterWarningWorker method construct.

/* (non-Javadoc)
   * @see org.wikipediacleaner.gui.swing.basic.BasicWorker#construct()
   */
@Override
public Object construct() {
    long startTime = System.currentTimeMillis();
    EnumWikipedia wiki = getWikipedia();
    int lastCount = 0;
    Stats stats = new Stats();
    UpdateUnknownParameterWarningTools tools = new UpdateUnknownParameterWarningTools(wiki, this, true, automaticEdit);
    tools.setUsePurge(false);
    try {
        if (!useList) {
            listWarningPages(tools);
            // Ask for confirmation
            if (getWindow() != null) {
                int answer = getWindow().displayYesNoWarning(GT._T("Analysis found {0} articles to check for unknown parameter errors.\n" + "Do you want to update the warnings ?", Integer.valueOf(warningPages.size()).toString()));
                if (answer != JOptionPane.YES_OPTION) {
                    return Integer.valueOf(0);
                }
            }
            // Sort the list of articles
            Collections.sort(warningPages, PageComparator.getTitleFirstComparator());
            if (warningPages.isEmpty()) {
                return Integer.valueOf(0);
            }
        }
        // Working with sublists
        tools.setContentsAvailable(contentsAvailable);
        tools.prepareErrorsMap();
        if (simulation) {
            tools.setSimulation(true);
        }
        String lastTitle = null;
        while (!warningPages.isEmpty()) {
            // Creating sublist
            List<Page> sublist = tools.extractSublist(warningPages, 10, false);
            if (sublist.isEmpty()) {
                displayStats(stats, startTime);
                return Integer.valueOf(stats.getUpdatedPagesCount());
            }
            // Update warning
            boolean finish = false;
            while (!finish) {
                finish = true;
                try {
                    lastTitle = sublist.get(sublist.size() - 1).getTitle();
                    tools.updateWarning(sublist, null, null, stats);
                } catch (APIException e) {
                    if (getWindow() != null) {
                        int answer = getWindow().displayYesNoWarning(GT._T("An error occurred when updating unknown parameter warnings. Do you want to continue ?\n\n" + "Error: {0}", e.getMessage()));
                        if (answer != JOptionPane.YES_OPTION) {
                            return e;
                        }
                        finish = false;
                    }
                }
                if (shouldStop()) {
                    Configuration config = Configuration.getConfiguration();
                    config.setString(null, ConfigurationValueString.LAST_UNKNOWN_PARAMETER_WARNING, lastTitle);
                    displayStats(stats, startTime);
                    return Integer.valueOf(stats.getUpdatedPagesCount());
                }
            }
            if (stats.getUpdatedPagesCount() > lastCount) {
                lastCount = stats.getUpdatedPagesCount();
            /*if (getWindow() != null) {
            int answer = getWindow().displayYesNoWarning(
                "This feature is currently under development, please check the modification.\n" +
                "Do you want to continue ?");
            if (answer != JOptionPane.YES_OPTION) {
              return Integer.valueOf(lastCount);
            }
          } else {
            return Integer.valueOf(lastCount);
          }*/
            }
        }
        if (warningPages.isEmpty()) {
            Configuration config = Configuration.getConfiguration();
            config.setString(null, ConfigurationValueString.LAST_UNKNOWN_PARAMETER_WARNING, (String) null);
        }
    } catch (APIException e) {
        return e;
    }
    displayStats(stats, startTime);
    return Integer.valueOf(stats.getUpdatedPagesCount());
}
Also used : APIException(org.wikipediacleaner.api.APIException) Configuration(org.wikipediacleaner.utils.Configuration) Stats(org.wikipediacleaner.gui.swing.worker.UpdateWarningTools.Stats) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString)

Example 3 with Stats

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

the class UpdateISBNWarningWorker method construct.

/* (non-Javadoc)
   * @see org.wikipediacleaner.gui.swing.basic.BasicWorker#construct()
   */
@Override
public Object construct() {
    long startTime = System.currentTimeMillis();
    EnumWikipedia wiki = getWikipedia();
    int lastCount = 0;
    Stats stats = new Stats();
    Map<String, List<String>> errors = null;
    UpdateISBNWarningTools tools = new UpdateISBNWarningTools(wiki, this, true, automaticEdit);
    try {
        if (!useList) {
            listWarningPages(tools);
            // Ask for confirmation
            if (getWindow() != null) {
                int answer = getWindow().displayYesNoWarning(GT._T("Analysis found {0} articles to check for {1} errors.\n" + "Do you want to update the warnings ?", new Object[] { Integer.valueOf(warningPages.size()).toString(), "ISBN" }));
                if (answer != JOptionPane.YES_OPTION) {
                    return Integer.valueOf(0);
                }
            }
            // Sort the list of articles
            Collections.sort(warningPages, PageComparator.getTitleFirstComparator());
            if (warningPages.isEmpty()) {
                return Integer.valueOf(0);
            }
        }
        // Working with sublists
        tools.setContentsAvailable(contentsAvailable);
        tools.prepareErrorsMap();
        if (simulation) {
            tools.setSimulation(true);
        }
        String lastTitle = null;
        while (!warningPages.isEmpty()) {
            // Creating sublist
            List<Page> sublist = tools.extractSublist(warningPages, 10, false);
            if (sublist.isEmpty()) {
                errors = tools.getErrorsMap();
                displayResult(stats, startTime, errors);
                return Integer.valueOf(stats.getUpdatedPagesCount());
            }
            // Update warning
            boolean finish = false;
            while (!finish) {
                finish = true;
                try {
                    lastTitle = sublist.get(sublist.size() - 1).getTitle();
                    tools.updateWarning(sublist, null, null, stats);
                } catch (APIException e) {
                    if (getWindow() != null) {
                        int answer = getWindow().displayYesNoWarning(GT._T("An error occurred when updating {1} warnings. Do you want to continue ?\n\n" + "Error: {0}", new Object[] { e.getMessage(), "ISBN" }));
                        if (answer != JOptionPane.YES_OPTION) {
                            return e;
                        }
                        finish = false;
                    }
                }
                if (shouldStop()) {
                    Configuration config = Configuration.getConfiguration();
                    config.setString(null, ConfigurationValueString.LAST_ISBN_WARNING, lastTitle);
                    displayResult(stats, startTime, null);
                    return Integer.valueOf(stats.getUpdatedPagesCount());
                }
            }
            if (stats.getUpdatedPagesCount() > lastCount) {
                lastCount = stats.getUpdatedPagesCount();
            /*if (getWindow() != null) {
            int answer = getWindow().displayYesNoWarning(
                "This feature is currently under development, please check the modification.\n" +
                "Do you want to continue ?");
            if (answer != JOptionPane.YES_OPTION) {
              return Integer.valueOf(lastCount);
            }
          } else {
            return Integer.valueOf(lastCount);
          }*/
            }
        }
        errors = tools.getErrorsMap();
        if (warningPages.isEmpty()) {
            Configuration config = Configuration.getConfiguration();
            config.setString(null, ConfigurationValueString.LAST_ISBN_WARNING, (String) null);
        }
    } catch (APIException e) {
        return e;
    }
    displayResult(stats, startTime, errors);
    return Integer.valueOf(stats.getUpdatedPagesCount());
}
Also used : WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) Configuration(org.wikipediacleaner.utils.Configuration) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) APIException(org.wikipediacleaner.api.APIException) Stats(org.wikipediacleaner.gui.swing.worker.UpdateWarningTools.Stats) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) ArrayList(java.util.ArrayList) List(java.util.List) WPCConfigurationStringList(org.wikipediacleaner.api.configuration.WPCConfigurationStringList)

Example 4 with Stats

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

the class UpdateISSNWarningWorker method construct.

/* (non-Javadoc)
   * @see org.wikipediacleaner.gui.swing.basic.BasicWorker#construct()
   */
@Override
public Object construct() {
    long startTime = System.currentTimeMillis();
    EnumWikipedia wiki = getWikipedia();
    int lastCount = 0;
    Stats stats = new Stats();
    Map<String, List<String>> errors = null;
    UpdateISSNWarningTools tools = new UpdateISSNWarningTools(wiki, this, true, automaticEdit);
    try {
        if (!useList) {
            listWarningPages(tools);
            // Ask for confirmation
            if (getWindow() != null) {
                int answer = getWindow().displayYesNoWarning(GT._T("Analysis found {0} articles to check for {1} errors.\n" + "Do you want to update the warnings ?", new Object[] { Integer.valueOf(warningPages.size()).toString(), "ISSN" }));
                if (answer != JOptionPane.YES_OPTION) {
                    return Integer.valueOf(0);
                }
            }
            // Sort the list of articles
            Collections.sort(warningPages, PageComparator.getTitleFirstComparator());
            if (warningPages.isEmpty()) {
                return Integer.valueOf(0);
            }
        }
        // Working with sublists
        tools.setContentsAvailable(contentsAvailable);
        tools.prepareErrorsMap();
        if (simulation) {
            tools.setSimulation(true);
        }
        String lastTitle = null;
        while (!warningPages.isEmpty()) {
            // Creating sublist
            List<Page> sublist = tools.extractSublist(warningPages, 10, false);
            if (sublist.isEmpty()) {
                errors = tools.getErrorsMap();
                displayResult(stats, startTime, errors);
                return Integer.valueOf(stats.getUpdatedPagesCount());
            }
            // Update warning
            boolean finish = false;
            while (!finish) {
                finish = true;
                try {
                    lastTitle = sublist.get(sublist.size() - 1).getTitle();
                    tools.updateWarning(sublist, null, null, stats);
                } catch (APIException e) {
                    if (getWindow() != null) {
                        int answer = getWindow().displayYesNoWarning(GT._T("An error occurred when updating {1} warnings. Do you want to continue ?\n\n" + "Error: {0}", new Object[] { e.getMessage(), "ISSN" }));
                        if (answer != JOptionPane.YES_OPTION) {
                            return e;
                        }
                        finish = false;
                    }
                }
                if (shouldStop()) {
                    Configuration config = Configuration.getConfiguration();
                    config.setString(null, ConfigurationValueString.LAST_ISSN_WARNING, lastTitle);
                    displayResult(stats, startTime, null);
                    return Integer.valueOf(stats.getUpdatedPagesCount());
                }
            }
            if (stats.getUpdatedPagesCount() > lastCount) {
                lastCount = stats.getUpdatedPagesCount();
            /*if (getWindow() != null) {
            int answer = getWindow().displayYesNoWarning(
                "This feature is currently under development, please check the modification.\n" +
                "Do you want to continue ?");
            if (answer != JOptionPane.YES_OPTION) {
              return Integer.valueOf(lastCount);
            }
          } else {
            return Integer.valueOf(lastCount);
          }*/
            }
        }
        errors = tools.getErrorsMap();
        if (warningPages.isEmpty()) {
            Configuration config = Configuration.getConfiguration();
            config.setString(null, ConfigurationValueString.LAST_ISSN_WARNING, (String) null);
        }
    } catch (APIException e) {
        return e;
    }
    displayResult(stats, startTime, errors);
    return Integer.valueOf(stats.getUpdatedPagesCount());
}
Also used : WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) Configuration(org.wikipediacleaner.utils.Configuration) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) APIException(org.wikipediacleaner.api.APIException) Stats(org.wikipediacleaner.gui.swing.worker.UpdateWarningTools.Stats) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) ArrayList(java.util.ArrayList) List(java.util.List) WPCConfigurationStringList(org.wikipediacleaner.api.configuration.WPCConfigurationStringList)

Example 5 with Stats

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

the class UpdateDabWarningWorker method construct.

/* (non-Javadoc)
   * @see org.wikipediacleaner.gui.swing.basic.BasicWorker#construct()
   */
@Override
public Object construct() {
    long startTime = System.currentTimeMillis();
    EnumWikipedia wikipedia = getWikipedia();
    WPCConfiguration configuration = wikipedia.getConfiguration();
    setText(GT._T("Retrieving MediaWiki API"));
    int lastCount = 0;
    Stats stats = new Stats();
    UpdateDabWarningTools tools = new UpdateDabWarningTools(wikipedia, this, true, automaticEdit);
    try {
        if (!useList) {
            listWarningPages(tools);
            // Ask for confirmation
            if (getWindow() != null) {
                int answer = getWindow().displayYesNoWarning(GT._T("Analysis found {0} articles with disambiguation warning {1}.\n" + "Do you want to update the disambiguation warnings ?", new Object[] { Integer.valueOf(warningPages.size()), TemplateBuilder.from(configuration.getString(WPCConfigurationString.DAB_WARNING_TEMPLATE)).toString() }));
                if (answer != JOptionPane.YES_OPTION) {
                    return Integer.valueOf(0);
                }
            }
            // Sort the list of articles (trying a temporary ArrayList for performance)
            if (warningPages.isEmpty()) {
                return Integer.valueOf(0);
            }
            List<Page> tmpWarningPages = new ArrayList<>(warningPages);
            Collections.sort(tmpWarningPages, PageComparator.getTitleFirstComparator());
            warningPages.clear();
            warningPages.addAll(tmpWarningPages);
        }
        // Working with sublists
        tools.setContentsAvailable(contentsAvailable);
        tools.setLinksAvailable(linksAvailable);
        tools.setDabInformationAvailable(dabInformationAvailable);
        if (!useList) {
            setText(GT._T("Retrieving disambiguation pages"));
            tools.preloadDabPages();
        }
        String lastTitle = null;
        int countUnsaved = 0;
        while (!warningPages.isEmpty()) {
            // Creating sublist
            List<Page> sublist = tools.extractSublist(warningPages, 10, false);
            if (sublist.isEmpty()) {
                displayStats(stats, startTime);
                return Integer.valueOf(stats.getUpdatedPagesCount());
            }
            countUnsaved += sublist.size();
            // Update warning
            boolean finish = false;
            while (!finish) {
                finish = true;
                try {
                    lastTitle = sublist.get(sublist.size() - 1).getTitle();
                    tools.updateWarning(sublist, null, null, stats);
                } catch (APIException e) {
                    if (getWindow() != null) {
                        int answer = getWindow().displayYesNoWarning(GT._T("An error occurred when updating disambiguation warnings. Do you want to continue ?\n\n" + "Error: {0}", e.getMessage()));
                        if (answer != JOptionPane.YES_OPTION) {
                            return e;
                        }
                        finish = false;
                    }
                }
                if (shouldStop() || (countUnsaved > 1000)) {
                    Configuration config = Configuration.getConfiguration();
                    config.setString(null, ConfigurationValueString.LAST_DAB_WARNING, lastTitle);
                    countUnsaved = 0;
                }
                if (shouldStop()) {
                    displayStats(stats, startTime);
                    return Integer.valueOf(stats.getUpdatedPagesCount());
                }
            }
            if (stats.getUpdatedPagesCount() > lastCount) {
                lastCount = stats.getUpdatedPagesCount();
            /*if (getWindow() != null) {
            int answer = getWindow().displayYesNoWarning(
                "This feature is currently under development, please check the modification.\n" +
                "Do you want to continue ?");
            if (answer != JOptionPane.YES_OPTION) {
              return Integer.valueOf(lastCount);
            }
          } else {
            return Integer.valueOf(lastCount);
          }*/
            }
        }
        if (warningPages.isEmpty()) {
            Configuration config = Configuration.getConfiguration();
            config.setString(null, ConfigurationValueString.LAST_DAB_WARNING, (String) null);
        }
    } catch (APIException e) {
        return e;
    }
    displayStats(stats, startTime);
    return Integer.valueOf(stats.getUpdatedPagesCount());
}
Also used : WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) Configuration(org.wikipediacleaner.utils.Configuration) ArrayList(java.util.ArrayList) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) APIException(org.wikipediacleaner.api.APIException) Stats(org.wikipediacleaner.gui.swing.worker.UpdateWarningTools.Stats) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia)

Aggregations

APIException (org.wikipediacleaner.api.APIException)6 Page (org.wikipediacleaner.api.data.Page)6 Stats (org.wikipediacleaner.gui.swing.worker.UpdateWarningTools.Stats)6 WPCConfigurationString (org.wikipediacleaner.api.configuration.WPCConfigurationString)5 EnumWikipedia (org.wikipediacleaner.api.constants.EnumWikipedia)5 Configuration (org.wikipediacleaner.utils.Configuration)5 ConfigurationValueString (org.wikipediacleaner.utils.ConfigurationValueString)5 ArrayList (java.util.ArrayList)4 WPCConfiguration (org.wikipediacleaner.api.configuration.WPCConfiguration)4 List (java.util.List)3 WPCConfigurationStringList (org.wikipediacleaner.api.configuration.WPCConfigurationStringList)2 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 WPCConfigurationLong (org.wikipediacleaner.api.configuration.WPCConfigurationLong)1 RecentChange (org.wikipediacleaner.api.data.RecentChange)1