ꯈꯨꯠꯄꯥꯏ:ꯃꯌꯦꯛ ꯏꯕ ꯌꯦꯡꯁꯤꯟꯅꯕ

This page is a translated version of the page Manual:Writing maintenance scripts and the translation is 30% complete.

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

ꯈꯨꯗꯝ ꯃꯌꯦꯛ

We'll walk through a helloWorld.php 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):

<?php

/**
 * To the extent possible under law,  I, Mark Hershberger, have waived all copyright and
 * related or neighboring rights to Hello World. This work is published from the
 * United States.
 *
 * @copyright CC0 http://creativecommons.org/publicdomain/zero/1.0/
 * @author Mark A. Hershberger <mah@everybody.org>
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

class HelloWorld extends Maintenance {
	public function execute() {
		$this->output( "Hello, World!" );
	}
}

$maintClass = HelloWorld::class;

require_once RUN_MAINTENANCE_IF_MAIN;

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 helloWorld.php [--conf|--dbgroupdefault|--dbpass|--dbuser|--globals|--help|--memory-limit|--mwdebug|--profiler|--quiet|--server|--wiki]

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
    --globals: Output globals at the end of processing for debugging
    --memory-limit: Set a specific memory limit for the script, "max"
        for no limit or "default" to avoid changing it
    --server: The protocol and server name to use in URLs, e.g.
        https://en.wikipedia.org. This is sometimes necessary because server name
        detection may fail in command line scripts.
    --profiler: Profiler output format (usually "text")
    --mwdebug: Enable built-in MediaWiki development settings

Script dependant parameters:
    --dbuser: The DB user to use for this script
    --dbpass: The password to use for this script
    --dbgroupdefault: The default DB group to use.

$

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

require_once __DIR__ . "/Maintenance.php";
We include Maintenance.php. This defines the abstract class Maintenance which has method to parse arguments, read the console, get the database, etc. It is best to use the full path to Maintenance.php.
class HelloWorld extends Maintenance {
We extend the Maintenance class and then, with
$maintClass = HelloWorld::class;

require_once RUN_MAINTENANCE_IF_MAIN;
tell the Maintenance class to run the script using our HelloWorld class, only if being executed from the command line.

Internally, RUN_MAINTENANCE_IF_MAIN loads another file doMaintenance.php which autoloads MediaWiki classes and configuration, and then

	public function execute() {
The execute() function that we've defined is executed, and our script does its work.

ꯀꯨꯞꯊꯕ ꯋꯥꯔꯣꯜ ꯍꯥꯞꯆꯤꯟꯕ

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

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

	public function __construct() {
		parent::__construct();

		$this->addDescription( 'Say hello.' );
	}

The output now gives us the description:

$ php helloWorld.php --help

Say hello.

Usage: php helloWorld.php [--conf|--dbgroupdefault|--dbpass|--dbuser|--globals|--help|--memory-limit|--mwdebug|--profiler|--quiet|--server|--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 class HelloWorld that calls Maintenance's addOption() and update the execute() method to use the new option. addOption()'s parameters are $name, $description, $required = false, $withArg = false, $shortName = false, so:

	public function __construct() {
		parent::__construct();

		$this->addDescription( 'Say hello.' );
		$this->addOption( 'name', 'Who to say Hello to', false, true );
	}

	public function execute() {
		$name = $this->getOption( 'name', 'World' );
		$this->output( "Hello, $name!" );
	}

This time, when executed, the output of the helloWorld.php 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|--dbgroupdefault|--dbpass|--dbuser|--globals|--help|--memory-limit|--mwdebug|--name|--profiler|--quiet|--server|--wiki]
…
Script specific parameters:
    --name: Who to say Hello to

ꯁꯥꯡꯗꯣꯛꯄꯁꯤꯡ

MediaWiki version:
1.28
Gerrit change 301709

If your maintenance script is for an extension, then you should add a requirement that the extension is installed:

	public function __construct() {
		parent::__construct();
		$this->requireExtension( 'FooBar' );
		$this->addOption( 'name', 'Who to say Hello to', false, true );
	}

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

Be aware that classes defined by your extension will not be available until hitting the execute-function. Attempts to create instances prior to this, e.g. in the constructor, will cause class not found exceptions.

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.