EarwigBot’s answer to the Pywikipedia framework is the Wiki Toolset (earwigbot.wiki), which you will mainly access through bot.wiki.
bot.wiki provides three methods for the management of Sites - get_site(), add_site(), and remove_site(). Sites are objects that simply represent a MediaWiki site. A single instance of EarwigBot (i.e. a single working directory) is expected to relate to a single site or group of sites using the same login info (like all WMF wikis with CentralAuth).
Load your default site (the one that you picked during setup) with site = bot.wiki.get_site().
Skip this section if you’re only working with one site.
If a site is already known to the bot (meaning that it is stored in the sites.db file, which includes just your default wiki at first), you can load a site with site = bot.wiki.get_site(name), where name might be "enwiki" or "frwiktionary" (you can also do site = bot.wiki.get_site(project="wikipedia", lang="en")). Recall that not giving any arguments to get_site() will return the default site.
add_site() is used to add new sites to the sites database. It may be called with similar arguments as get_site(), but the difference is important. get_site() only needs enough information to identify the site in its database, which is usually just its name; the database stores all other necessary connection info. With add_site(), you need to provide enough connection info so the toolset can successfully access the site’s API/SQL databases and store that information for later. That might not be much; for WMF wikis, you can usually use code like this:
project, lang = "wikipedia", "es"
try:
site = bot.wiki.get_site(project=project, lang=lang)
except earwigbot.SiteNotFoundError:
# Load site info from http://es.wikipedia.org/w/api.php:
site = bot.wiki.add_site(project=project, lang=lang)
This works because EarwigBot assumes that the URL for the site is "//{lang}.{project}.org", the API is at /w/api.php, and the SQL connection info (if any) is stored as config.wiki["sql"]. This might change if you’re dealing with non-WMF wikis, where the code might look something more like:
project, lang = "mywiki", "it"
try:
site = bot.wiki.get_site(project=project, lang=lang)
except earwigbot.SiteNotFoundError:
# Load site info from http://mysite.net/mywiki/it/s/api.php:
base_url = "http://mysite.net/" + project + "/" + lang
db_name = lang + project + "_p"
sql = {host: "sql.mysite.net", db: db_name}
site = bot.wiki.add_site(base_url=base_url, script_path="/s", sql=sql)
remove_site() does the opposite of add_site(): give it a site’s name or a project/lang pair like get_site() takes, and it’ll remove that site from the sites database.
earwigbot.wiki.Site objects provide the following attributes:
and the following methods:
Create earwigbot.wiki.Page objects with site.get_page(title), page.toggle_talk(), user.get_userpage(), or user.get_talkpage(). They provide the following attributes:
and the following methods:
Additionally, Category objects (created with site.get_category(name) or site.get_page(title) where title is in the Category: namespace) provide the following additional attributes:
And the following additional method:
Create earwigbot.wiki.User objects with site.get_user(name) or page.get_creator(). They provide the following attributes:
and the following methods:
Not all aspects of the toolset are covered here. Explore its code and docstrings to learn how to use it in a more hands-on fashion. For reference, bot.wiki is an instance of earwigbot.wiki.SitesDB tied to the sites.db file in the bot’s working directory.