Search in sources :

Example 16 with APIException

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

the class ApiXmlCategoriesResult method executeCategories.

/**
 * Execute categories request.
 *
 * @param properties Properties defining request.
 * @param page Page.
 * @param list List to be filled with categories.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeCategories(Map<String, String> properties, Page page, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve back links
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/pages/page/categories/cl", Filters.element());
        List<Element> listCategories = xpa.evaluate(root);
        Iterator<Element> itCategory = listCategories.iterator();
        while (itCategory.hasNext()) {
            Element currentCategory = itCategory.next();
            String pageId = currentCategory.getAttributeValue("pageid");
            String ns = currentCategory.getAttributeValue("ns");
            String title = currentCategory.getAttributeValue("title");
            Page category = DataManager.getPage(getWiki(), title, null, null, null);
            category.setNamespace(ns);
            category.setPageId(pageId);
            if (currentCategory.getAttribute("missing") != null) {
                category.setExisting(Boolean.FALSE);
            }
            if (!list.contains(category)) {
                list.add(category);
            }
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/categories", properties);
    } catch (JDOMException e) {
        log.error("Error loading templates", 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 17 with APIException

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

the class ApiXmlLinksResult method executeLinks.

/**
 * Execute links request.
 *
 * @param properties Properties defining request.
 * @param lists Map of lists to be filled with links.
 * @param normalization Map containing information about title normalization (key=From, value=To).
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeLinks(Map<String, String> properties, Map<String, List<Page>> lists, Map<String, String> normalization) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve normalization information
        retrieveNormalization(root, normalization);
        // Retrieve back links
        XPathExpression<Element> xpaPages = XPathFactory.instance().compile("/api/query/pages/page", Filters.element());
        List<Element> listPages = xpaPages.evaluate(root);
        Iterator<Element> itPage = listPages.iterator();
        XPathExpression<Element> xpaLinks = XPathFactory.instance().compile("links/pl", Filters.element());
        while (itPage.hasNext()) {
            Element pageNode = itPage.next();
            String pageTitle = pageNode.getAttributeValue("title");
            List<Page> links = lists.get(pageTitle);
            if (links == null) {
                links = new ArrayList<>();
                lists.put(pageTitle, links);
            }
            List<Element> listLinks = xpaLinks.evaluate(pageNode);
            Iterator<Element> itLinks = listLinks.iterator();
            while (itLinks.hasNext()) {
                Element linkNode = itLinks.next();
                Page link = DataManager.getPage(getWiki(), linkNode.getAttributeValue("title"), null, null, null);
                link.setNamespace(linkNode.getAttributeValue("ns"));
                links.add(link);
            }
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/links", properties);
    } catch (JDOMException e) {
        log.error("Error loading links", 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 18 with APIException

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

the class ApiXmlLinksResult method executeLinks.

/**
 * Execute links request.
 *
 * @param properties Properties defining request.
 * @param links List to be filled with links.
 * @param knownPages Already known pages.
 * @param normalization Map containing information about title normalization (key=From, value=To).
 * @param redirects List of redirects filled by the method.
 * @param useDisambig Flag indicating if disambiguation property should be used.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeLinks(Map<String, String> properties, List<Page> links, List<Page> knownPages, Map<String, String> normalization, List<Page> redirects, boolean useDisambig) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve normalization information
        retrieveNormalization(root, normalization);
        // Retrieve back links
        XPathExpression<Element> xpaPages = XPathFactory.instance().compile("/api/query/pages/page", Filters.element());
        List<Element> listLinks = xpaPages.evaluate(root);
        Iterator<Element> itLinks = listLinks.iterator();
        while (itLinks.hasNext()) {
            Element linkNode = itLinks.next();
            Page link = getPage(getWiki(), linkNode, knownPages, useDisambig);
            if ((redirects != null) && (link.getRedirects().isRedirect())) {
                redirects.add(link);
            }
            links.add(link);
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/links", properties);
    } catch (JDOMException e) {
        log.error("Error loading links", 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 19 with APIException

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

the class ApiXmlPagePropsResult method setDiambiguationStatus.

/**
 * Set disambiguation status of a list of pages.
 *
 * @param properties Properties defining request.
 * @param pages List of pages for which disambiguation status needs to be set.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean setDiambiguationStatus(Map<String, String> properties, Collection<Page> pages) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Manage redirects and missing pages
        updateRedirect(root, pages);
        // Set disambiguation status
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/pages/page", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        List<Page> tmpPages = new ArrayList<>();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            String title = currentNode.getAttributeValue("title");
            for (Page p : pages) {
                tmpPages.clear();
                Iterator<Page> it = p.getRedirects().getIteratorWithPage();
                while (it.hasNext()) {
                    Page p2 = it.next();
                    tmpPages.add(p2);
                    if ((p2.getTitle() != null) && (Page.areSameTitle(p2.getTitle(), title))) {
                        Boolean disambig = Boolean.FALSE;
                        Element pageProps = currentNode.getChild("pageprops");
                        if ((pageProps != null) && (pageProps.getAttribute("disambiguation") != null)) {
                            disambig = Boolean.TRUE;
                        }
                        for (Page p3 : tmpPages) {
                            p3.setDisambiguationPage(disambig);
                        }
                    }
                }
            }
        }
        return false;
    } catch (JDOMException e) {
        log.error("Error updating disambiguation status", e);
        throw new APIException("Error parsing XML", e);
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Page(org.wikipediacleaner.api.data.Page) JDOMException(org.jdom2.JDOMException)

Example 20 with APIException

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

the class ApiXmlRedirectsResult method executeRedirects.

/**
 * Execute redirects request.
 *
 * @param properties Properties defining request.
 * @param page Page.
 * @param list List to be filled with redirects to the page.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeRedirects(Map<String, String> properties, Page page, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve redirects
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/pages/page/redirects/rd", Filters.element());
        List<Element> listRedirects = xpa.evaluate(root);
        Iterator<Element> itRedirects = listRedirects.iterator();
        while (itRedirects.hasNext()) {
            Element currentRedirect = itRedirects.next();
            Page link = DataManager.getPage(getWiki(), currentRedirect.getAttributeValue("title"), null, null, null);
            link.setNamespace(currentRedirect.getAttributeValue("ns"));
            link.setPageId(currentRedirect.getAttributeValue("pageid"));
            // TODO: Check if fragment is available
            link.getRedirects().add(page, null);
            if (!list.contains(link)) {
                list.add(link);
            }
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/redirects", properties);
    } catch (JDOMException e) {
        log.error("Error loading redirects", 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)

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