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 (see also copyright headers).

The below example program will print "Hello, World!":

$ php helloWorld.php Hello, World!

Boilerplate explained
We include. This defines  which provides the basis for all maintenance scripts, including facilities to parse command-line arguments, read console input, connect to a database, etc.

We declare our Maintenance subclass.

Tells 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  method is the entrypoint for maintenance scripts, and is where the main logic of your script will be. Avoid running any code from the constructor.

When our program is run from the command-line, the core maintenance framework will take care of initialising MediaWiki core and configuration etc, and then it will invoke this method.

Help command
One of the built-in features that all maintenance scripts enjoy is a  option. The above example boilerplate would produce the following help page:

$ php helloWorld.php --help

Usage: php helloWorld.php […]

Generic maintenance parameters: --help (-h): Display this help message --quiet (-q): Whether to suppress non-error output --conf: Location of LocalSettings.php, if not default --wiki: For specifying the wiki ID   --server: The protocol and server name to use in URL --profiler: Profiler output format (usually "text") …



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

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 [--help] …



ꯑꯄꯥꯝꯕ ꯈꯟꯕ ꯑꯃꯁꯨꯡ ꯃꯔꯩ ꯌꯦꯌꯅꯕ
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 […] … Script specific parameters: --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:

This provides a helfpul error message when the extension is not enabled. For example, during local development a particular extension might not yet be enabled in LocalSettings.php, or when operating a wiki farm an extension might be enabled on a subset of wikis.

Be aware that no code may be executed other than through the  method. Attempts to call MediaWiki core services, classes, or functions, or calling your own extension code prior to this, will cause errors or is unreliable and unsupported (e.g. ouside the class declaration, or in the constructor).

Writing tests
It's recommended to write tests for your maintenance scripts, like with any other class. See the Maintenance scripts guide for help and examples.