My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
MediaWikiAPISupport  
Helper classes for the MediaWiki api.php.
Featured
Updated Sep 14, 2010 by axelclk@gmail.com

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 http://en.wikipedia.org/w/api.php lists all parameters for en.wikipedia.org.

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

	public static void testQueryCategories001() {
		String[] listOfTitleStrings = { "Main Page", "API" };
		User user = new User("", "", "http://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

(Needs revision 635) 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.

		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":

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("", "", "http://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();
	}
}

Example - Get all interlanguage links

Returns all interlanguage links from the given page.

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("", "", "http://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".

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("", "", "http://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

Note: This snippet runs in releases >= 3.0.16 (see unreleased)

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

	public static void testQueryCategoryMembersPages() {
		User user = new User("", "", "http://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();
		}
	}

Example - Get the raw wiki content for all pages contained in a search

Note: This snippet runs in releases >= 3.0.16 (see unreleased)

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

	public static void testQuerySearchResults() {
		User user = new User("", "", "http://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();
		}
	}
Powered by Google Project Hosting