Mwn is a modern and comprehensive MediaWiki bot framework for Node.js, originally adapted from mwbot.
In addition to the MediaWiki Action API, the library also provides methods to talk to the Wikimedia EventStreams API, the ORES API, Pageviews API and WikiWho API.
This library uses mocha for tests and has extensive test coverage covering all commonly used code paths. Testing is automated using a CI workflow on Github Actions.
To install, run
npm install mwn.
Up-to-date documentation is hosted on Toolforge.
Handling multiple users and wikis: Mwn can seamlessly work with multiple bot users signed into the same wiki, and multiple wikis at the same time. You just have to create multiple bot instances – each one representing a wiki + user. Each bot instance uses an isolated cookie jar; all settings are also isolated.
Token handling: Tokens are automatically fetched as part of
bot.getTokensAndSiteInfo(). Once retrieved, they are stored in the bot state and can be reused any number of times. If any API request fails due to an expired or missing token, the request is automatically retried after fetching a new token.
bot.getTokens()can be used to refresh the token cache, though mwn manages this, so you'd never need to explicitly use that.
Maxlag: The default maxlag parameter used by mwn is 5 seconds. Requests failing due to maxlag will be automatically retried after pausing for a duration specified by
maxlagPause(default 5 seconds). A maximum of
maxRetrieswill take place (default 3).
Retries: Mwn automatically retries failing requests
bot.options.maxRetriestimes (default: 3). This is useful in case of connectivity resets and the like. As for errors raised by the API itself, note that MediaWiki generally handles these at the response level rather than the protocol level (they still emit a 200 OK response). Mwn will attempt retries for these errors based on the error code. For instance, if the error is
maxlag, retry is done after a delay. If it's
assertbotfailed(indicates a session loss), mwn will try to log in again and then retry. If it's
badtoken, retry is done after fetching a fresh edit token.
Parsing wikitext: Mwn provides methods for common wikitext parsing needs (templates, links, and simple tables).
Batch operations: Perform a large number of tasks (like page edits) with control over the concurrency (default 5). Failing actions can be set to automatically retry.
Patches are very much welcome. See https://mwn.toolforge.org/docs/developing for instructions.
Mwn is released under GNU Lesser General Public License (LGPL) v3.0, since it borrows quite a bit of code from MediaWiki core (GPL v2). LGPL is a more permissive variant of GNU GPL. Unlike GPL, it enables this library to be used in software not released under GPL-compatible licenses, and even in proprietary software. However, any derivatives of this library should be released under an GPL-compatible license (like LGPL).