Wiki

Clone wiki

info.bliki.wiki / MediaWikiAPISupport

Helper classes for the MediaWiki api.php.

Helper Classes for the MediaWiki API page can be found in the package: info.bliki.api. All query parameters are described in the api.php page of the requested wiki. For example Wikipedia API lists all parameters for en.wikipedia.org.

For example this snippet determines the categories used in the Wikimedia Main Page and Wikimedia API page:

#!java
    public static void testQueryCategories001() {
        String[] listOfTitleStrings = { "Main Page", "API" };
        User user = new User("", "", "https://meta.wikimedia.org/w/api.php");
        user.login();
        List<Page> listOfPages = user.queryCategories(listOfTitleStrings);
        for (Page page : listOfPages) {
            // print page information
            System.out.println(page.toString());
            for (int j = 0; j < page.sizeOfCategoryList(); j++) {
                Category cat = page.getCategory(j);
                // print every category in this page
                System.out.println(cat.toString());
            }
        }
    }

Note: from releases >= 3.0.16 on the Category class is replaced by the PageInfo class in the last example.

Connecting through a HTTP proxy

In case you are using a proxy you can set the HostConfiguration for the connector. The HostConfiguration holds all of the variables needed to describe an HTTP connection to a host. This includes remote host, port and protocol, proxy host and port, local address, and virtual host.

#!java
        Connector conn = user.getConnector();
        HttpClient client = conn.getClient();
        HostConfiguration hostConfiguration = client.getHostConfiguration();
        hostConfiguration.setProxy("<proxyHost>", 80);
        client.setHostConfiguration(hostConfiguration);

Troubleshooting

See: Trouble Shooting HttpClient

Example - Get all members of a category

Get all members of the category "Category:Physics":

#!java
package info.bliki.api;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.xml.sax.SAXException;

public class APICategoryMembers {
    public APITest() {
        super();
    }

    public static void testQueryCategoryMembers() {
        User user = new User("", "", "https://en.wikipedia.org/w/api.php");
        user.login();
        String[] valuePairs = { "list", "categorymembers", "cmtitle", "Category:Physics" };
        Connector connector = new Connector();
        String rawXmlResponse = connector.queryXML(user, valuePairs);
        if (rawXmlResponse == null) {
            System.out.println("Got no XML result for the query");
        }
        System.out.println(rawXmlResponse);

        // When more results are available, use "cmcontinue" from last query result
        // to continue
        String[] valuePairs2 = { "list", "categorymembers", "cmtitle", "Category:Physics", "cmcontinue", "Awards|" };
        rawXmlResponse = connector.queryXML(user, valuePairs2);
        if (rawXmlResponse == null) {
            System.out.println("Got no XML result for the query");
        }
        System.out.println(rawXmlResponse);

    }


    public static void main(String[] args) {
        testQueryCategoryMembers();
    }
}

Returns all interlanguage links from the given page.

#!java
package info.bliki.api;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.xml.sax.SAXException;

public class LangLinksTest {
    public LangLinksTest() {
        super();
    }

    public static void testQueryLangLinks() {
        User user = new User("", "", "https://en.wikipedia.org/w/api.php");
        user.login();
        String[] valuePairs = { "prop", "langlinks", "titles", "England" };
        Connector connector = new Connector();
        String rawXmlResponse = connector.queryXML(user, valuePairs);
        if (rawXmlResponse == null) {
            System.out.println("Got no XML result for the query");
        }
        System.out.println(rawXmlResponse);

        // When more results are available, use "llcontinue" from last query result
        // to continue
        String[] valuePairs2 = { "prop", "langlinks", "titles", "England", "llcontinue", "9316|bar" };

        rawXmlResponse = connector.queryXML(user, valuePairs2);
        if (rawXmlResponse == null) {
            System.out.println("Got no XML result for the query");
        }
        System.out.println(rawXmlResponse);

    }

    public static void main(String[] args) {
        testQueryLangLinks();
    }
}

Example - Get the image URL

Get the images url from "upload.wikimedia.org" for "File:Mona Lisa.jpg".

#!java
package info.bliki.api;

import java.util.List;

/**
 * Example for querying image info data
 */
public class QueryImageInfoExample {
    public static void main(String[] args) {
        String pageName = "File:Mona Lisa.jpg";
        User user = new User("", "", "https://en.wikipedia.org/w/api.php");
        Connector connector = new Connector();
        user = connector.login(user);

        System.out.println("PAGE-NAME: " + pageName);
        // set image width thumb size to 200px
        List<Page> pages = user.queryImageinfo(new String[] { pageName }, 200);

        System.out.println("PAGES: " + pages.get(0).getTitle());

        if (pages != null) {
            System.out.println("PAGES: " + pages.size());

        } else {
            System.out.println("PAGES: NULL!");
        }

        for (Page page : pages) {
            System.out.println("IMG-THUMB-URL: " + page.getImageThumbUrl());
            System.out.println("IMG-URL: " + page.getImageUrl());
        }
    }

}

Example - Get the raw wiki content for all category members

Get the raw wiki content for all members of the category Category:Physics

#!java
    public static void testQueryCategoryMembersPages() {
        User user = new User("", "", "https://en.wikipedia.org/w/api.php");
        user.login();
        String[] valuePairs = { "list", "categorymembers", "cmtitle", "Category:Physics" };
        String[] valuePairsContinue = new String[6];
        String cmContinue = "||";
        for (int i = 0; i < valuePairs.length; i++) {
            valuePairsContinue[i] = valuePairs[i];
        }
        valuePairsContinue[4] = "cmcontinue";
        valuePairsContinue[5] = "";
        Connector connector = new Connector();
        List<PageInfo> resultCategoryMembers = new ArrayList<PageInfo>(1024);
        XMLCategoryMembersParser parser;
        try {
            // get all categorymembers
            String responseBody = connector.queryXML(user, valuePairs);
            while (responseBody != null) {
                parser = new XMLCategoryMembersParser(responseBody);
                parser.parse();
                cmContinue = parser.getCmContinue();
                System.out.println(">>>>> " + cmContinue);
                List<PageInfo> listOfPages = parser.getPagesList();
                resultCategoryMembers.addAll(listOfPages);
                for (PageInfo categoryMember : listOfPages) {
                    // print page information
                    System.out.println(categoryMember.toString());
                }
                if (cmContinue.length() > 0) {
                    // use the cmcontinue from the last query to get the next block of
                    // category members
                    valuePairsContinue[5] = cmContinue;
                    responseBody = connector.queryXML(user, valuePairsContinue);
                } else {
                    break;
                }
            }
            // get the content of the category members with namespace==0
            int count = 0;
            List<String> strList = new ArrayList<String>();
            for (PageInfo categoryMember : resultCategoryMembers) {
                if (categoryMember.getNs().equals("0")) {
                    // namespace "0" - all titles without a namespace prefix
                    strList.add(categoryMember.getTitle());
                    if (++count == 10) {
                        List<Page> listOfPages = user.queryContent(strList);
                        for (Page page : listOfPages) {
                            // System.out.println(page.getTitle());
                            // print the raw content of the wiki page:
                            System.out.println(page.getCurrentContent());
                        }
                        count = 0;
                        strList = new ArrayList<String>();
                    }
                }
            }
            if (count != 0) {
                List<Page> listOfPages = user.queryContent(strList);
                for (Page page : listOfPages) {
                    // System.out.println(page.getTitle());
                    // print the raw content of the wiki page:
                    System.out.println(page.getCurrentContent());
                }
            }
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Get the raw wiki content for all search result pages for the words forrest gump

#!java
    public static void testQuerySearchResults() {
        User user = new User("", "", "https://en.wikipedia.org/w/api.php");
        user.login();
        // search for all pages which contain "forrest gump"
        String[] valuePairs = { "list", "search", "srsearch", "forrest gump" };
        String[] valuePairsContinue = new String[6];
        String srOffset = "0";
        for (int i = 0; i < valuePairs.length; i++) {
            valuePairsContinue[i] = valuePairs[i];
        }
        valuePairsContinue[4] = "sroffset";
        valuePairsContinue[5] = "";
        Connector connector = new Connector();
        List<SearchResult> resultSearchResults = new ArrayList<SearchResult>(1024);
        XMLSearchParser parser;
        try {
            // get all search results
            String responseBody = connector.queryXML(user, valuePairs);
            while (responseBody != null) {
                parser = new XMLSearchParser(responseBody);
                parser.parse();
                srOffset = parser.getSrOffset();
                System.out.println(">>>>> " + srOffset);
                List<SearchResult> listOfSearchResults = parser.getSearchResultList();
                resultSearchResults.addAll(listOfSearchResults);
                for (SearchResult searchResult : listOfSearchResults) {
                    // print search result information
                    System.out.println(searchResult.toString());
                }
                if (srOffset.length() > 0) {
                    // use the sroffset from the last query to get the next block of
                    // search results
                    valuePairsContinue[5] = srOffset;
                    responseBody = connector.queryXML(user, valuePairsContinue);
                } else {
                    break;
                }
            }
            // get the content of the category members with namespace==0
            int count = 0;
            List<String> strList = new ArrayList<String>();
            for (SearchResult searchResult : resultSearchResults) {
                if (searchResult.getNs().equals("0")) {
                    // namespace "0" - all titles without a namespace prefix
                    strList.add(searchResult.getTitle());
                    if (++count == 10) {
                        List<Page> listOfPages = user.queryContent(strList);
                        for (Page page : listOfPages) {
                            System.out.println(page.getTitle());
                            // print the raw content of the wiki page:
                            // System.out.println(page.getCurrentContent());
                        }
                        count = 0;
                        strList = new ArrayList<String>();
                    }
                }
            }
            if (count != 0) {
                List<Page> listOfPages = user.queryContent(strList);
                for (Page page : listOfPages) {
                    System.out.println(page.getTitle());
                    // print the raw content of the wiki page:
                    // System.out.println(page.getCurrentContent());
                }
            }
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Updated