Search in sources :

Example 1 with RecentChange

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

the class MonitorRCWindow method extractRecentChanges.

/**
 * Extract a list of recent changes for the same page.
 *
 * @param allRC List of recent changes.
 * @return List of recent changes for one page.
 */
private List<RecentChange> extractRecentChanges(List<RecentChange> allRC) {
    if ((allRC == null) || (allRC.isEmpty())) {
        return null;
    }
    List<RecentChange> result = new ArrayList<>();
    String title = allRC.get(0).getTitle();
    Iterator<RecentChange> itRC = allRC.iterator();
    while (itRC.hasNext()) {
        RecentChange rc = itRC.next();
        if (Page.areSameTitle(title, rc.getTitle())) {
            result.add(rc);
            itRC.remove();
        }
    }
    return result;
}
Also used : RecentChange(org.wikipediacleaner.api.data.RecentChange) ArrayList(java.util.ArrayList)

Example 2 with RecentChange

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

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

the class RecentChangesTableModel method removeRecentChanges.

/**
 * Remove all recent changes for a given title.
 *
 * @param title Title.
 */
public void removeRecentChanges(String title) {
    Iterator<RecentChange> itRC = recentChanges.iterator();
    while (itRC.hasNext()) {
        RecentChange rc = itRC.next();
        if (Page.areSameTitle(title, rc.getTitle())) {
            itRC.remove();
        }
    }
    cleanUpList();
}
Also used : RecentChange(org.wikipediacleaner.api.data.RecentChange)

Example 4 with RecentChange

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

the class RecentChangesRunnable method run.

/**
 * Regularly query the API for recent changes.
 *
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    while (!shouldStop) {
        try {
            List<RecentChange> recentChanges = new ArrayList<>();
            start = api.getRecentChanges(wiki, start, recentChanges);
            if (!recentChanges.isEmpty()) {
                Date currentTime = DataManager.convertIso8601DateTime(start);
                manager.fireRecentChanges(recentChanges, currentTime);
            }
        } catch (APIException e) {
        // Nothing to do.
        } catch (ParseException e) {
        // Nothing to do.
        }
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
        // Nothing to do.
        }
    }
}
Also used : RecentChange(org.wikipediacleaner.api.data.RecentChange) APIException(org.wikipediacleaner.api.APIException) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) Date(java.util.Date)

Example 5 with RecentChange

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

the class ApiXmlRecentChangesResult method executeRecentChanges.

/**
 * Execute recent changes request.
 *
 * @param properties Properties defining request.
 * @param recentChanges The list of recent changes to be filled.
 * @return The timestamp to use as a starting point for the next call.
 * @throws APIException Exception thrown by the API.
 */
@Override
public String executeRecentChanges(Map<String, String> properties, List<RecentChange> recentChanges) throws APIException {
    String nextStart = null;
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Get recent changes list
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/recentchanges/rc", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            boolean isAnonymous = currentNode.getAttribute("anon") != null;
            boolean isBot = currentNode.getAttribute("bot") != null;
            boolean isMinor = currentNode.getAttribute("minor") != null;
            boolean isNew = currentNode.getAttribute("new") != null;
            boolean isRedirect = currentNode.getAttribute("redirect") != null;
            String comment = currentNode.getAttributeValue("comment");
            String ns = currentNode.getAttributeValue("ns");
            String pageId = currentNode.getAttributeValue("pageid");
            String rcId = currentNode.getAttributeValue("rcid");
            String revId = currentNode.getAttributeValue("revid");
            String timestamp = currentNode.getAttributeValue("timestamp");
            if (nextStart == null) {
                nextStart = timestamp;
            }
            String title = currentNode.getAttributeValue("title");
            String type = currentNode.getAttributeValue("type");
            String user = currentNode.getAttributeValue("user");
            String logType = currentNode.getAttributeValue("logtype");
            String logAction = currentNode.getAttributeValue("logaction");
            try {
                RecentChange rc = new RecentChange(Integer.valueOf(rcId), Integer.valueOf(ns), title, Integer.valueOf(pageId), Integer.valueOf(revId));
                rc.setAnonymous(isAnonymous);
                rc.setBot(isBot);
                rc.setComment(comment);
                rc.setLogAction(logAction);
                rc.setLogType(logType);
                rc.setMinor(isMinor);
                rc.setNew(isNew);
                rc.setRedirect(isRedirect);
                rc.setTimestamp(timestamp);
                rc.setType(type);
                rc.setUser(user);
                recentChanges.add(0, rc);
            } catch (NumberFormatException e) {
                log.error("Error loading recent changes", e);
            }
        }
    } catch (JDOMException e) {
        log.error("Error loading recent changes", e);
        throw new APIException("Error parsing XML", e);
    }
    return nextStart;
}
Also used : RecentChange(org.wikipediacleaner.api.data.RecentChange) APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) JDOMException(org.jdom2.JDOMException)

Aggregations

RecentChange (org.wikipediacleaner.api.data.RecentChange)5 ArrayList (java.util.ArrayList)3 APIException (org.wikipediacleaner.api.APIException)3 ParseException (java.text.ParseException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Element (org.jdom2.Element)1 JDOMException (org.jdom2.JDOMException)1 WPCConfiguration (org.wikipediacleaner.api.configuration.WPCConfiguration)1 WPCConfigurationLong (org.wikipediacleaner.api.configuration.WPCConfigurationLong)1 Page (org.wikipediacleaner.api.data.Page)1 Stats (org.wikipediacleaner.gui.swing.worker.UpdateWarningTools.Stats)1