|
|
|
Wikipedia service
This extension provides an easy way to retrieve a Wikipedia entry from Wikipedia. It uses the mediawiki Special:Export service to export an article, stores it on disk (cache), and produces a usable HTML block from the entry.
Adding zero.services.wikipedia to your project
To use the Wikipedia extension, add it to your project. You can do this with the Project Zero tooling by editing your config/ivy.xml file. Add the zero.services.wikipedia library to your dependencies. If you do not have the library installed, it is downloaded automatically when you save the ivy.xml file or the next time your resolve your project.
Requesting a Wikipedia entry
To perform a request, use the following REST API:
http://localhost:8080/resources/wikipedia/{wikiword}
In this example, {wikiword} is substituted with the name of the Wikipedia entry you want to retrieve.
The Wikipedia API
The Wikipedia service provides a Wikipedia class that can be called directly from Groovy, PHP, or Java code. The following example retrieves the Wikipedia entry in HTML:
getWikiHtml(String wikiWord, boolean truncate)
getWikiHtml(String wikiWord, String language, boolean truncate)
In this example, if truncate is true then the entry is truncated. If language is specified it should be a language code, for example en for English.
The following example retrieves the Wikipedia entry in HTML, as in the previous example, but this API should be used since the entry is retrieved from a local cache, if possible.
getWikiHtmlCache(String wikiWord, boolean truncate)
getWikiHtmlCache(String wikiWord, String language, boolean truncate)
In this example, if truncate is true, then the entry is truncated. If language is specified it should be a language code, for example en for English.
The following example retrieves the Wikipedia entry in Mediawiki markup:
getWikiMarkup(String wikiWord)
getWikiMarkup(String wikiWord, String language)
The following example truncates a given HTML block and places the truncatedMessage message at the end of the block.
truncateHtml(String html, String truncatedMessage)
Tweaking the Wikipedia API
There are several constants defined in the Wikipedia class that can be modified to tweak the behavior of the API, as shown in the following example.
public static final String DEFAULT_WIKIPEDIA_LANGUAGE = "en";
public static final String NOT_FOUND_MESSAGE = ": was not found.";
public static final String AMBIGUOS_MESSAGE = "This disambiguation page lists articles associated with the same title.";
//These settings are applied when requesting truncated text
public static final int TRUNCATE_MIN_CHARS = 512;
public static final int TRUNCATE_MAX_PARAS = 4;
public static final String TRUNCATED_LINK_TEXT = "More...";
//These settings are not related to truncation
public static final boolean REMOVE_IMAGES = false;
public static final boolean REMOVE_TABLES = false;
public static final boolean USE_IMAGE_THUMBNAILS = true;
//These directories will contain the data cached from Wikipedia
public static final String WIKIPEDIA_DIR = "wikipedia";
public static final String WIKIIMAGES_DIR = "wikiImages";
//This setting is used by the getWikiHtmlCache
public static long CACHE_MAX_AGE = 1 * 60 * 60 * 1000; // = 1 hour
Additional resources
|