Manual:Writing maintenance scripts/mni

This is a step-by-step tutorial on writing a maintenance script based on the  class (see ) which was introduced in MediaWiki 1.16 to make it easier to write command-line MediaWiki maintenance scripts.

ꯈꯨꯗꯝ ꯃꯌꯦꯛ
We'll walk through a  maintenance script that simply prints “Hello, World”. This program contains the minimum amount of code needed to run as well as the expected copyright header (for alternative headers, see copyright headers):

The program will just print out “Hello, World!” but already has a  (and other command line options). Sample output:

 $ php helloWorld.php Hello, World! $ php helloWorld.php --help

Usage: php helloWorld.php [--conf|--dbpass|--dbuser|--globals|--help|--quiet|--wiki] conf : Location of LocalSettings.php, if not default dbpass : The password to use for this script dbuser : The DB user to use for this script globals : Output globals at the end of processing for debugging help : Display this help message quiet : Whether to supress non-error output wiki : For specifying the wiki ID

ꯍꯦꯟꯅ ꯃꯊꯛꯇ ꯌꯦꯡꯁꯤꯟꯕ


 * We include .  This defines the   which has method to parse arguments, read the console, get the database, etc.  It is best to use the full path to.


 * We extend the Maintenance class and then, with


 * tell the Maintenance class to run the script using our  class, only if being executed from the command line.

Internally,  loads another file  which autoloads MediaWiki classes and configuration, and then


 * The  function that we've defined is executed, and our script does its work.

ꯀꯨꯞꯊꯕ ꯋꯥꯔꯣꯜ ꯍꯥꯞꯆꯤꯟꯕ
"ꯑꯗꯨꯕꯨ ꯃꯌꯦꯛ ꯌꯦꯡꯁꯤꯟꯕꯁꯤ ꯀꯤꯔꯤꯒꯤ ꯑꯣꯏꯗꯣꯏꯅꯣ ? " ꯅꯪꯅ ꯍꯡꯂꯛꯄꯗꯨ ꯑꯩ ꯇꯥꯔꯦ

We can put a description at the top of the " " output by using the  method in our constructor:

The output now gives us the description:

$ php helloWorld.php --help

Say hello.

Usage: php helloWorld.php [--conf|--dbpass|--dbuser|--globals|--help|--quiet|--wiki] …

ꯑꯄꯥꯝꯕ ꯈꯟꯕ ꯑꯃꯁꯨꯡ ꯃꯔꯩ ꯌꯦꯌꯅꯕ
Greeting the world is all well and good, but we want to be able to greet individuals, too.

To add a command-line option, add a constructor to  that calls  's   and update the   method to use the new option. 's parameters are, so:

This time, when executed, the output of the  script changes depending on the argument provided:

 $ php helloWorld.php Hello, World! $ php helloWorld.php --name=Mark Hello, Mark! $ php helloWorld.php --help

Say hello.

Usage: php helloWorld.php [--conf|--dbpass|--dbuser|--globals|--help|--name|--quiet|--wiki] …   name : Who to say Hello to

ꯁꯥꯡꯗꯣꯛꯄꯁꯤꯡ
If your maintenance script is for an extension, then you should add a requirement that the extension is installed:

Mostly this provides a nice error message when the extension is not enabled on that wiki (likely on wiki farms). ꯃꯁꯤꯅ ꯈꯛꯇꯅ ꯃꯥꯏ ꯄꯥꯛꯂꯅꯤ  ꯁꯤꯖꯤꯟꯅꯗꯨꯅ ꯁꯥꯡꯗꯣꯛꯄꯗ ꯫