Manual:Writing maintenance scripts/pt-br

A versão 1.16 do MediaWiki introduziu a classe de manutenção (Maintenance.php) para tornar mais fácil a escrita de scripts de linha de comando de manutenção do software.

Esta página foi escrita como um tutorial passo-a-passo.

Exemplo de script
Para descrever a criação de scripts de manutenção, vamos escrever um arquivo helloWorld.php, um script que simplesmente imprime “Hello, World”. Este programa contém a quantidade mínima de código necessário para ser executado, bem como o cabeçalho de direitos autorais esperado (para cabeçalhos alternativos, consulte cabeçalhos de direitos autorais):

O programa simplesmente imprimirá “Hello, World!” porém já possui um parâmetro --help (e outras opções de linha de comando). Saída de exemplo:

$ 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

An overview:


 * Line 12
 * 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.  It is best to use the full path to Maintenance.php.


 * Line 14
 * We override Maintenance class and then, in


 * Line 20 & 21
 * tell the Maintenance class to run the script using the HelloWorld class, only if being executed from the command line.


 * Line 15
 * The execute function that we've provided is executed.

Option and Argument Parsing
Greeting the world is all well and good, but we want to be able to greet individuals, too.

To add an option, add a constructor with a call addOption and update the execute method to use the new option. addOption's parameters are $name, $description, $required = false, $withArg = false, $shortName = false:

This time, when executed, the output of the helloWorld.php</tt> 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 helloWorld.php [--conf|--dbpass|--dbuser|--globals|--help|--name|--quiet|--wiki] …   name : Who to say Hello to

I18N
In order for the maintenance script to use the i18n file, it needs to be set in the appropriate place. Generally, this should be after the constructor. It is probably best to put it in the execute</tt> function.