Search in sources :

Example 26 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class ApiXmlProtectedTitlesResult method executeProtectedTitles.

/**
 * Execute protected titles request.
 *
 * @param properties Properties defining request.
 * @param list List to be filled with protected titles.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeProtectedTitles(Map<String, String> properties, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve embedding pages
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/protectedtitles/pt", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            if ("infinity".equals(currentNode.getAttributeValue("expiry"))) {
                Page page = DataManager.getPage(getWiki(), currentNode.getAttributeValue("title"), null, null, null);
                page.setNamespace(currentNode.getAttributeValue("ns"));
                list.add(page);
            }
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/protectedtitles", properties);
    } catch (JDOMException e) {
        log.error("Error loading protected titles list", e);
        throw new APIException("Error parsing XML", e);
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) Page(org.wikipediacleaner.api.data.Page) JDOMException(org.jdom2.JDOMException)

Example 27 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class ApiXmlQueryPageResult method executeQueryPage.

/**
 * Execute query page request.
 *
 * @param properties Properties defining request.
 * @param list List to be filled with query pages.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeQueryPage(Map<String, String> properties, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve query pages
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/querypage/results/page", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            Page page = DataManager.getPage(getWiki(), currentNode.getAttributeValue("title"), null, null, null);
            page.setNamespace(currentNode.getAttributeValue("ns"));
            list.add(page);
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/querypage", properties);
    } catch (JDOMException e) {
        log.error("Error loading query page list", e);
        throw new APIException("Error parsing XML", e);
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) Page(org.wikipediacleaner.api.data.Page) JDOMException(org.jdom2.JDOMException)

Example 28 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class ListCWWorker method outputResultToPage.

/**
 * Output result of the analysis to a page on the wiki.
 *
 * @param algorithm Algorithm.
 * @param pages List of detections to put in the result.
 * @param outputPage Page name.
 * @return True if the analysis was completely saved on the wiki.
 * @throws APIException Error with MediaWiki API.
 */
private boolean outputResultToPage(CheckErrorAlgorithm algorithm, List<Detection> pages, String outputPage) throws APIException {
    // Determine page to which the error should be written
    if (outputPage == null) {
        return true;
    }
    String truePageName = MessageFormat.format(pageName, algorithm.getErrorNumberString());
    logCW.info("Writing dump analysis results for error " + algorithm.getErrorNumberString() + " to page " + truePageName);
    // Retrieve page information
    Page page = DataManager.createSimplePage(getWikipedia(), truePageName, null, null, null);
    API api = APIFactory.getAPI();
    api.retrieveContents(getWikipedia(), Collections.singletonList(page), false, false);
    String initialContents = page.getContents();
    Integer initialRevisionId = page.getRevisionId();
    if ((initialContents == null) || (initialRevisionId == null)) {
        throw new APIException("Unable to read page " + truePageName);
    }
    // Generate result
    logCW.info("Preparing results of dump analysis for error " + algorithm.getErrorNumberString());
    int nbPages = pages.size();
    final Long maxSize = getWikipedia().getWikiConfiguration().getMaxArticleSize();
    final List<Detection> tmpPages = new ArrayList<>(pages);
    // Loop
    boolean fullySaved = true;
    int attemptCount = 0;
    long currentMaxSize = (maxSize != null) ? maxSize : Long.MAX_VALUE;
    while (attemptCount < 10) {
        attemptCount++;
        // Check that page hasn't been modified
        api.retrieveContents(getWikipedia(), Collections.singletonList(page), false, false);
        String contents = page.getContents();
        if (!initialRevisionId.equals(page.getRevisionId()) || !initialContents.equals(contents)) {
            logCW.info("Page " + truePageName + " has been modified");
            throw new APIException("Page " + truePageName + " has been modified");
        }
        // Find place holders in the page
        int begin = -1;
        int end = -1;
        for (ContentsComment comment : page.getAnalysis(contents, true).comments().getAll()) {
            String value = comment.getComment().trim();
            if ("BOT BEGIN".equals(value)) {
                if (begin < 0) {
                    begin = comment.getEndIndex();
                }
            } else if ("BOT END".equals(value)) {
                end = comment.getBeginIndex();
            }
        }
        if ((begin < 0) || (end < 0) || (end < begin)) {
            throw new APIException("Page " + truePageName + " doesn't have place holders for the result");
        }
        // Build new text for the page
        long internalMaxSize = currentMaxSize;
        final StringBuilder newText = new StringBuilder();
        newText.append(contents.substring(0, begin));
        newText.append("\n");
        internalMaxSize -= newText.toString().getBytes(StandardCharsets.UTF_8).length;
        String suffix = contents.substring(end);
        internalMaxSize -= suffix.getBytes(StandardCharsets.UTF_8).length;
        fullySaved &= appendResult(tmpPages, internalMaxSize, newText);
        newText.append(contents.substring(end));
        final String text = newText.toString();
        // Update page
        try {
            if (!text.equals(contents)) {
                int currentNbPages = tmpPages.size();
                String nbPagesToDisplay = (currentNbPages == nbPages) ? "" + nbPages : "" + currentNbPages + "/" + nbPages;
                api.updatePage(getWikipedia(), page, text, "Dump analysis for error n°" + algorithm.getErrorNumberString() + " (" + nbPagesToDisplay + " pages)", false, true, true, false);
            }
            return fullySaved;
        } catch (APIException e) {
            // Check if it can be due to a page too big
            boolean tooBig = false;
            if (EnumQueryResult.CONTENT_TOO_BIG.equals(e.getQueryResult())) {
                tooBig = true;
            }
            if ((e.getCause() != null) && (e.getCause() instanceof SocketTimeoutException)) {
                tooBig = true;
                try {
                    TimeUnit.MINUTES.sleep(15);
                } catch (InterruptedException ie) {
                // Nothing to do
                }
            }
            // Try reducing the result if it's too big
            if (!tooBig) {
                throw e;
            }
            currentMaxSize = Math.max(0, currentMaxSize - 100000);
            logCW.info("Trying with smaller list (" + currentMaxSize + " bytes)");
        }
    }
    return fullySaved;
}
Also used : ArrayList(java.util.ArrayList) Page(org.wikipediacleaner.api.data.Page) ContentsComment(org.wikipediacleaner.api.data.contents.comment.ContentsComment) APIException(org.wikipediacleaner.api.APIException) SocketTimeoutException(java.net.SocketTimeoutException) API(org.wikipediacleaner.api.API)

Example 29 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class ListCWWorker method construct.

/**
 * Compute the value to be returned by the <code>get</code> method.
 *
 * @return Object returned by the <code>get</code> method.
 * @see org.wikipediacleaner.gui.swing.basic.BasicWorker#construct()
 */
@Override
public Object construct() {
    if ((dumpFile == null) || !dumpFile.canRead() || !dumpFile.isFile()) {
        return null;
    }
    if ((output == null) && (pageName == null)) {
        return null;
    }
    if (output != null) {
        if (!output.canWrite()) {
            return null;
        }
        if (!output.getName().contains("{0}") && !output.isDirectory()) {
            return null;
        }
    }
    if ((selectedAlgorithms == null) || selectedAlgorithms.isEmpty()) {
        return null;
    }
    CWPageProcessor pageProcessor = new CWPageProcessor(getWikipedia(), this, selectedNamespaces);
    if (onlyRecheck) {
        try {
            List<Page> outputPages = new ArrayList<>();
            for (AlgorithmInformation algorithm : selectedAlgorithms) {
                String truePageName = MessageFormat.format(pageName, algorithm.algorithm.getErrorNumberString());
                Page page = DataManager.createSimplePage(getWikipedia(), truePageName, null, null, null);
                outputPages.add(page);
            }
            API api = APIFactory.getAPI();
            api.retrieveLinks(getWikipedia(), outputPages);
            for (Page page : outputPages) {
                List<Page> links = page.getLinks();
                if (links != null) {
                    for (Page link : links) {
                        pageProcessor.addPage(link);
                    }
                }
            }
            logCW.info("List of pages contains {} pages", pageProcessor.getPagesListSize());
        } catch (APIException e) {
        // Nothing to do
        }
    }
    DumpProcessor dumpProcessor = new DumpProcessor(pageProcessor);
    dumpProcessor.processDump(dumpFile);
    while (!pageProcessor.hasFinished()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        // Nothing to do
        }
    }
    logCW.info("Beginning of result output");
    for (AlgorithmInformation algorithm : selectedAlgorithms) {
        Map<String, Detection> pages = algorithm.getDetections();
        if (pages == null) {
            pages = new HashMap<>();
        }
        outputResult(algorithm.algorithm, pages.values());
    }
    logCW.info("End of result output");
    reportProgress();
    return null;
}
Also used : ArrayList(java.util.ArrayList) Page(org.wikipediacleaner.api.data.Page) DumpProcessor(org.wikipediacleaner.api.dump.DumpProcessor) APIException(org.wikipediacleaner.api.APIException) API(org.wikipediacleaner.api.API)

Example 30 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class CheckCategoryLinkAction method actionPerformed.

/* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
@Override
public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
    try {
        Namespace categoryNamespace = toWikipedia.getWikiConfiguration().getNamespace(Namespace.CATEGORY);
        String categoryName = PageElementCategory.DEFAULT_NAME;
        if (categoryNamespace != null) {
            if (!PageElementCategory.DEFAULT_NAME.equals(categoryNamespace.getCanonicalTitle())) {
                categoryName = categoryNamespace.getCanonicalTitle();
            } else {
                for (String alias : categoryNamespace.getAliases()) {
                    if (!PageElementCategory.DEFAULT_NAME.equals(alias)) {
                        categoryName = alias;
                        break;
                    }
                }
            }
        }
        API api = APIFactory.getAPI();
        Page category = DataManager.createSimplePage(toWikipedia, "Category:" + title, null, null, Namespace.CATEGORY);
        api.retrieveContents(toWikipedia, Collections.singletonList(category), false, false);
        if (category.isExisting() == null) {
            Utilities.displayWarning(textPane.getParent(), GT._T("Unable to find if category {0} exists in \"{1}\".", new Object[] { title, toWikipedia.toString() }));
            return;
        }
        if (Boolean.TRUE.equals(category.isExisting())) {
            String replace = categoryName + ":" + title + ((order != null) ? "|" + order : "");
            int answer = Utilities.displayYesNoWarning(textPane.getParent(), GT._T("The category {0} exists in \"{1}\".\n" + "Do you want to replace the category by [[{2}]] ?", new Object[] { title, toWikipedia.toString(), replace }));
            if (answer == JOptionPane.YES_OPTION) {
                int startOffset = element.getStartOffset();
                int endOffset = element.getEndOffset();
                try {
                    textPane.getDocument().remove(startOffset, endOffset - startOffset);
                    textPane.getDocument().insertString(startOffset, "[[" + replace + "]]", element.getAttributes());
                    textPane.setCaretPosition(startOffset);
                    textPane.moveCaretPosition(startOffset + replace.length());
                } catch (BadLocationException e1) {
                // Nothing to be done
                }
            }
            return;
        }
        String languageLink = api.getLanguageLink(fromWikipedia, toWikipedia, "Category:" + title);
        if (languageLink == null) {
            Utilities.displayInformationMessage(textPane.getParent(), GT._T("The category {0} in the {1} Wikipedia doesn''t have a language link to the {2} Wikipedia.\n" + "It doesn''t exist either in the {2} Wikipedia.", new Object[] { title, fromWikipedia.getSettings().getCode(), toWikipedia.getSettings().getCode() }));
            return;
        }
        String replace = languageLink + ((order != null) ? "|" + order : "");
        int answer = Utilities.displayYesNoWarning(textPane.getParent(), GT._T("The category {0} doesn''t exist in the {2} Wikipedia.\n" + "In the {1} Wikipedia, it has a language link to the {2} Wikipedia: {3}.\n" + "Do you want to replace the category by [[{3}]] ?", new Object[] { title, fromWikipedia.getSettings().getCode(), toWikipedia.getSettings().getCode(), replace }));
        if (answer == JOptionPane.YES_OPTION) {
            int startOffset = element.getStartOffset();
            int endOffset = element.getEndOffset();
            try {
                textPane.getDocument().remove(startOffset, endOffset - startOffset);
                textPane.getDocument().insertString(startOffset, "[[" + replace + "]]", element.getAttributes());
                textPane.setCaretPosition(startOffset);
                textPane.moveCaretPosition(startOffset + languageLink.length() + 4);
            } catch (BadLocationException e1) {
            // Nothing to be done
            }
        }
    } catch (APIException e1) {
    // 
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) API(org.wikipediacleaner.api.API) Page(org.wikipediacleaner.api.data.Page) Namespace(org.wikipediacleaner.api.data.Namespace) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

APIException (org.wikipediacleaner.api.APIException)128 Page (org.wikipediacleaner.api.data.Page)70 API (org.wikipediacleaner.api.API)42 Element (org.jdom2.Element)41 JDOMException (org.jdom2.JDOMException)37 ArrayList (java.util.ArrayList)36 EnumWikipedia (org.wikipediacleaner.api.constants.EnumWikipedia)15 IOException (java.io.IOException)12 WPCConfigurationString (org.wikipediacleaner.api.configuration.WPCConfigurationString)12 Configuration (org.wikipediacleaner.utils.Configuration)11 ConfigurationValueString (org.wikipediacleaner.utils.ConfigurationValueString)11 WPCConfiguration (org.wikipediacleaner.api.configuration.WPCConfiguration)10 MediaWiki (org.wikipediacleaner.api.MediaWiki)9 PageAnalysis (org.wikipediacleaner.api.data.analysis.PageAnalysis)9 HashMap (java.util.HashMap)8 EnumQueryPage (org.wikipediacleaner.api.constants.EnumQueryPage)7 BufferedInputStream (java.io.BufferedInputStream)6 InputStream (java.io.InputStream)6 GZIPInputStream (java.util.zip.GZIPInputStream)6 Header (org.apache.commons.httpclient.Header)6