Manual:Writing maintenance scripts

Version 1.16 of MediaWiki introduced the maintenance class (Maintenance.php) to make it easier to write command-line MediaWiki maintenance scripts.

This is written as a step-by-step tutorial.

Example Script
To describe writing these maintenance scripts, we'll walk through an hello-world.php, a script that simply prints “Hello, World”:

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

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

Usage: php hello-world.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

The above program contains the minimum amount of code as well as the expected copyright header. An overview:


 * Of course, if we're going to write a maintenance script, we have to include Maintenance.php. This takes care of setting up Autoloading and the like.
 * Of course, if we're going to write a maintenance script, we have to include Maintenance.php. This takes care of setting up Autoloading and the like.



To add an option, add a constructor with a call addOption and update the execute method to use the new option:

This time, when executed, the output of the hello-world.php script changes depending on the argument provided:

$ php helloWorld.php Hello, World! $ php helloWorld.php --name=Mark Hello, Mark! $ php helloWorld.php --help Usage: php hello-world.php [--conf|--dbpass|--dbuser|--globals|--help|--name|--quiet|--wiki] …   name : Who to say Hello to


 * extend Maintenance class
 * provide execute method
 * include maintenance/Maintance.php
 * define $maintClass at global
 * require_once( DO_MAINTENANCE );
 * “outputChanneled”???
 * overriding getDbType???
 * default params
 * MW_NO_SETUP
 * define( 'MW_CONFIG_CALLBACK', 'Installer::overrideConfig' );
 * Skeleton

Works if you set it after the constructor, suggest in the execute call.
 * i18n

// Load the installer's i18n file global $wgExtensionMessagesFiles; $wgExtensionMessagesFiles['MediawikiInstaller'] = './includes/installer/Installer.i18n.php';