Skip to content

About

A cli for mediawiki

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

20 Commits

Folders and files

Repository files navigation

mediawiki-cli

A command-line tool for interacting with MediaWiki APIs.

Installation

cargo build --release
# binary at target/release/mediawiki-cli

Global Options

Option Default Description
--api-url https://prts.wiki/api.php MediaWiki API endpoint
--cookie — Custom Cookie header, overrides stored login cookie
--account auto Which stored login session to use: auto | user | bot (global)
--log-level warn Log level: error | warn | info | debug | trace
--json off Output command results as JSON (global, works on any subcommand)

JSON Output

Pass --json (positioned anywhere on the command line) to get structured JSON instead of the human-readable text output. Lists become JSON arrays, detail views become JSON objects:

mediawiki-cli --json page info "Main Page"
# { "title": ..., "pageid": ..., "url": ..., "categories": [...] }

mediawiki-cli page history "Main Page" --limit 5 --json
# [ { "revid": ..., "timestamp": ..., "user": ..., "size": ..., "comment": ... }, ... ]

mediawiki-cli page search "arknights" --json
# [ { "title": ..., "pageid": ..., "snippet": ... }, ... ]

mediawiki-cli cargo query --tables building_skill2 --fields name,room --json
# [ { "name": ..., "room": ... }, ... ]  (rows unwrapped from the API's "title" nesting)

page get --json returns { title, pageid, revid, content }; page diff --json returns { fromrevid, torevid, fromtitle, totitle, diff }; page edit --json returns the edit result object from the API; auth status --json returns { "user": <userinfo>|null, "bot": <userinfo>|null } (with --cookie, the raw userinfo object of that cookie instead). api prints the raw API response object (pretty-printed; identical with or without --json); API-level error responses are printed raw too — stdout gets the full error object, stderr gets the error code, and the exit status is nonzero. Log output (via --log-level) goes to stderr, so stdout stays valid JSON when piping to e.g. jq.

Commands

auth login <username>

Log in as a regular user and store the user session cookie. Prompts for password interactively. Supports 2FA — if the wiki requires a second factor, you'll be prompted for it automatically.

mediawiki-cli auth login MyUser

auth login-bot <username>

Log in a bot account (username from Special:BotPasswords, e.g. MyBot@cli) via action=login and store the bot session cookie. The password is read from MEDIAWIKI_BOT_PASSWORD if set, otherwise prompted interactively. Bot credentials are saved locally so an expired bot session can be re-established automatically.

mediawiki-cli auth login-bot MyBot@cli

auth status

Show the login status of both stored sessions (user and bot): username, user groups, rights, edit count, registration date, and email — or no stored session if that account never logged in. With --cookie, report the status of that cookie instead of the stored sessions.

mediawiki-cli auth status
# user: logged in as MyUser (id=123)
#   Groups: sysop, bureaucrat
# bot: logged in as MyBot@cli (id=456)

auth forget [user|bot]

Clear stored session cookies and saved bot credentials for this wiki. With no argument both accounts are forgotten; pass user or bot to clear only that one (bot also removes the saved bot credentials).

mediawiki-cli auth forget        # forget both
mediawiki-cli auth forget user   # forget only the user session

page get [<title>] [--revid <ID>]

Fetch raw wikitext for a page or a specific revision. You must specify either a title or --revid.

mediawiki-cli page get "Main Page"
mediawiki-cli page get --revid 12345

page edit <title> [options]

Edit a page. Content can be provided via stdin, a file, the --content flag, the --replace flag, or as a null edit.

Option Description
-s, --summary <text> Edit summary
--minor Mark as minor edit
--create-only Only create the page; fail if it already exists
-f, --file <path> Read content from a file
-c, --content <text> Provide content directly on the command line
--replace <OLD> <NEW> Replace a single occurrence of OLD with NEW in the page
--null-edit Submit a null edit (resubmit current content without changes)
# from stdin
echo "new content" | mediawiki-cli page edit "Sandbox" -s "update"

# from file
mediawiki-cli page edit "Sandbox" -f content.txt -s "update from file"

# inline content
mediawiki-cli page edit "Sandbox" --content "hello world"

# find-and-replace (exact one match required)
mediawiki-cli page edit "Sandbox" --replace "old text" "new text"

# null edit
mediawiki-cli page edit "Sandbox" --null-edit

page parse [options]

Render wikitext and print the resulting HTML without saving anything — useful for previewing template/parser-function output before editing a live page. Wikitext comes from stdin, -c, or -f (same as page edit).

Option Description
--title <title> Parse in the context of this page title (affects link resolution etc.)
-c, --content <text> Provide wikitext directly on the command line
-f, --file <path> Read wikitext from a file
-t, --text Print plain text extracted with html5ever instead of raw HTML: full entity decoding, <head>/<style>/<script> contents dropped, newlines between block elements. Good for grep and diffing, not layout-faithful
mediawiki-cli page parse -c '{{#expr:1+1}}'
echo 'x' | mediawiki-cli page parse --title "Sandbox" --text

page html <title> [--text]

Fetch the rendered HTML of a page as a browser sees it. Requests the page URL directly (index.php?title=... derived from --api-url) with the selected login session's cookie, bypassing anti-bot blocks on anonymous traffic. Add --text to extract plain text instead, using the same html5ever-based extractor as page parse --text.

mediawiki-cli page html "Main Page" | grep -c "some-string"
mediawiki-cli page html "Main Page" --text | head

page purge <title> [title...]

Purge the server-side cache of one or more pages (re-render without an edit). Batch-friendly replacement for a null edit when you just need to refresh a page.

mediawiki-cli page purge "Main Page" "Template:Foo"

page info [<title>] [--revid <ID>] [--templates]

Show page metadata (title, page ID, URL, categories). Use --templates to list all transcluded templates.

mediawiki-cli page info "Main Page"
mediawiki-cli page info "Main Page" --templates
mediawiki-cli page info --revid 12345

page history <title> [--limit <N>]

List page revision history. Outputs rev ID, timestamp, user, size, and comment in a tab-separated format.

mediawiki-cli page history "Main Page"
mediawiki-cli page history "Main Page" --limit 50

page category <title> [--limit <N>]

List all pages in a category. The category name can be provided with or without the "Category:" prefix. By default, all members are listed; use --limit to cap the number of results.

mediawiki-cli page category "Templates"
mediawiki-cli page category "Category:Templates" --limit 50

page search <query> [--limit <N>]

Search pages by keyword. Outputs title, page ID, and snippet.

mediawiki-cli page search "arknights"
mediawiki-cli page search "arknights" --limit 20

page embedded-in <title> [--limit <N>]

List pages that embed (transclude) the given page. Useful for finding where a template is used.

mediawiki-cli page embedded-in "Template:Infobox"
mediawiki-cli page embedded-in "Template:Infobox" --limit 50

cargo tables

List all Cargo database tables on the wiki. Outputs table names sorted alphabetically.

mediawiki-cli cargo tables

cargo fields <table>

Show all fields and their types for a given Cargo table. Outputs field name and type (e.g. String, Wikitext) in a tab-separated format.

mediawiki-cli cargo fields chara

cargo query --tables <tables> --fields <fields> [options]

Run a Cargo query against the wiki database. Results are output as tab-separated values with a header row.

Option Description
--tables <tables> Comma-separated table names to query
--fields <fields> Comma-separated field names to retrieve
--where <clause> SQL-style WHERE condition
--join-on <clause> SQL-style JOIN ON condition
--group-by <clause> SQL-style GROUP BY clause
--having <clause> SQL-style HAVING clause
--order-by <clause> SQL-style ORDER BY clause
--limit <N> Maximum number of results (default: 50)
--offset <N> Query offset
mediawiki-cli cargo query --tables chara --fields name,rarity --limit 5
mediawiki-cli cargo query --tables building_skill2 --fields name,room --where "room='控制中枢'" --limit 10

api <KEY=VALUE>... [--post] [--token]

Escape hatch: forward arbitrary parameters to the MediaWiki API as-is and print the raw JSON response. Useful when no dedicated subcommand exists for an action.

mediawiki-cli api action=query meta=siteinfo
mediawiki-cli api action=query titles="首页" prop=info
mediawiki-cli --json api action=query list=recentchanges rclimit=5
Option Description
--post Send the request as POST (wiki-modifying actions require POST; also avoids URL-length limits for large payloads). With --post an expired login session is a hard error instead of falling back to anonymous, like page edit
--token Fetch a CSRF token first and append it as the token parameter (for write actions)

Parameters are KEY=VALUE pairs split on the first = (values may contain =); repeated keys become repeated parameters (titles=A titles=B). An action= parameter is required. format and formatversion are always forced to json/2 — a user-supplied value is dropped with a warning — so responses use the same shape as every other command. Write actions need both flags together (--post --token); --token without --post warns, because MediaWiki rejects write requests that aren't POSTed. If the response contains an API error object it is printed to stdout like any other response, and the command exits nonzero with the error code on stderr.

Authentication

mediawiki-cli keeps two independent login sessions per wiki, stored in ~/.config/mediawiki-cli/cookies.json under separate slots (<api-url>#user and <api-url>#bot), so logging in as one never overwrites the other:

  • user session — auth login (clientlogin API, supports 2FA)
  • bot session — auth login-bot (action=login with a BotPasswords account); the bot password is saved to ~/.config/mediawiki-cli/credentials.json so an expired bot session is re-established automatically

Every command picks a session via --account:

  • --account user / --account bot — use exactly that session (no silent fallback; a missing session means anonymous requests, with a warning)
  • --account auto (the default) — user session first: use it if valid, otherwise fall back to the bot session (re-logging in the bot from saved credentials if needed)

If saved bot credentials exist but the automatic re-login fails (e.g. the bot password was revoked), the command exits with an error instead of silently continuing anonymously — re-run auth login-bot to fix it. A corrupt or unreadable credentials.json is treated the same way: commands warn and continue without auto re-login rather than failing. (Session resolution only happens for wiki-facing commands; auth subcommands always work directly on the stored state.)

page edit additionally refuses to run with an expired session: on wikis that allow anonymous editing, continuing would attribute the edit to your IP, so it fails with a hint to re-run auth login / auth login-bot instead.

--cookie still overrides both and skips session resolution entirely. Which session was chosen is logged at info level (--log-level info), and auth status shows the state of both sessions.

About

A cli for mediawiki

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages