Getting Started

System requirements

  • at minimum JRE 1.5

Post Download

After you have downloaded the last JWBF package, extract it and you’ll find the JWBF related jars (possibly seperated in bin and lib folder). After you have added them to your build path, you can create a simple class to test the framework with MediaWiki:

import net.sourceforge.jwbf.core.contentRep.Article;
import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;

public class Basic {
   public static void main(String[] args) throws Exception {
      MediaWikiBot b = new MediaWikiBot("http://en.wikipedia.org/w/");
      b.login("UserName", "***");
      Article article = b.getArticle("Main Page");
      System.out.println(article.getText());
   }
}

Extending JWBF MediaWiki

In the example above we used two basic action of the interface WikiBot (login and readContent). In the following section, we give an introduction to create own actions for MediaWiki (MW) over a MediaWikiBot (REST). At the beginning, some basics definitions:

  • A bot is the central action handler which forwards our set of requests to the MediaWiki installation.
  • An action is a set of requests and the handler of its responses; it implements the interface ContentProcessable

Example

For example our action should find the absolute URL of an image in MediaWiki context. So before we can create our action, we had to determine the request URL(s) to get the response. We take a look at the API documentation and find there an URL like : api.php?action=query&titles= Image:Test.gif &prop=imageinfo&iiprop=url&format=xml.

In the next step we start our action class:

...
@SupportedBy({ MW1_11, MW1_12, MW1_13 })
public class ImageInfo extends MWAction {

...

        /**
         *
         * Get an absolute url to an image.
         * @param name of, like "Test.gif"
         * @param bot a
         * @throws VersionException if not supported
         */

        public ImageInfo(MediaWikiBot bot, String name) throws VersionException {
                super(bot.getVersion());
                this.bot = bot;

                msg = new Get("/api.php?action=query&titles=Image:"
                                        + MediaWiki.encode(name) + "&prop=imageinfo"
                                        + "&iiprop=url&format=xml");

        }

How you can see in the src, we annotate since MediaWiki API supports our action. In the given example the support beginns, and is tested, at version 1.11 and the action is also tested with 1.12 and 1.13. Newer version will work properbly, but it's not sure. The superclass MWAction provides some helper methods to check this annotation and it also implements the interface ContentProcessable.

A few lines later, we add the requestURL to our action.

Is then a method called like getUrlAsString(), the bot have to forward the action to the MW installation and then the answer receives in our action in the following method.

@Override
public String processAllReturningText(String s) throws ProcessException {
   findUrlOfImage(s);
   return "";
}

Here we have to find the requested URL of the image in the returning text and store it in an object variable to return it with getUrlAsString(). Done (For complete src of this action see ImageInfo).

See also

If it is not required to receive a String but rather a complex type or a list; it makes it easier to implement the related interfaces to an action. In the following src snippet the action AllPageTitles, which returns all articlenames of a wiki (in this case of the main namespace), implements the interfaces Iterable<String>, Iterator<String> so it is very easy to itterate over its contents.

MediaWikiBot bot = new MediaWikiBot(...);
AllPageTitles apt = new AllPageTitles(bot, MediaWiki.NS_MAIN);
for (String articleName : apt) {
   System.out.println(articleName);
}