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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations