WikiBot was originally a PHP-based framework for bots I run on Wikipedia, however when it broke due to changes in mediawiki code, I decided to rewrite it in Ruby, using the MediaWiki API instead of screen-scraping. This is the result.
By default, the bot uses the English Wikipedia. A different instance of MediaWiki can be used by specifying the :api
key when instantiating the class.
The bot is not yet fully-featured, in that it doesn’t abstract every possible MediaWiki API feature. However, unsupported queries can be constructed and will return a Hash based on the result XML (see Examples).
WikiBot is now a gem on rubygems, so installation is as simple as
gem install wikibot
If you want to install manually, the following gem dependencies are required:
- curb >= 0.5.4.0 (the taf2-curb gem can be used as well)
- xml-simple >= 1.0.12
- deep_merge >= 0.1.0
- andand >= 1.3.1
wb = WikiBot::Bot.new( "username", "password", { options } )
When initializing a new WikiBot, the following options are available:
autologin
(default: false) – specifies if the bot should log into the mediawiki automatically.WikiBot::Bot#login
can be used to login otherwise.api
(default:http://en.wikipedia.org/w/api.php
) – the URL of the API to usereadonly
(default: false) – if true, the bot will not perform any write operationdebug
(default: false) – outputs Curb debug messages
Logging in is not required for queries that use GET
, but is for queries that use POST
.
wb = WikiBot::Bot.new("username", "password")
page = wb.page("Main Page")
text = page.text
wb = WikiBot::Bot.new("username", "password", :autologin => true, :api => "http://fr.wikipedia.org/w/api.php")
WikiBot::Bot#query_api
can be used to send an arbitrary query to the API. For this example, we’ll get image info from the Main Page.
wb = WikiBot::Bot.new("username", "password", :autologin => true)
query_data = {
:action => :query,
:prop => :images,
:titles => "Main Page"
}
xml_hash = wb.query_api(:get, query_data)