MediaWiki master
undelete.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
32class Undelete extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Undelete a page' );
36 $this->addOption( 'user', 'The user to perform the undeletion', false, true, 'u' );
37 $this->addOption( 'reason', 'The reason to undelete', false, true, 'r' );
38 $this->addArg( 'pagename', 'Page to undelete' );
39 }
40
41 public function execute() {
42 $username = $this->getOption( 'user', false );
43 $reason = $this->getOption( 'reason', '' );
44 $pageName = $this->getArg( 0 );
45
46 $title = Title::newFromText( $pageName );
47 if ( !$title ) {
48 $this->fatalError( "Invalid title" );
49 }
50 if ( $username === false ) {
51 $user = User::newSystemUser( 'Command line script', [ 'steal' => true ] );
52 } else {
53 $user = User::newFromName( $username );
54 }
55 if ( !$user ) {
56 $this->fatalError( "Invalid username" );
57 }
58
59 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
60 $this->output( "Undeleting " . $title->getPrefixedDBkey() . "...\n" );
61
62 $this->beginTransactionRound( __METHOD__ );
63 $status = $this->getServiceContainer()->getUndeletePageFactory()
64 ->newUndeletePage( $page, $user )
65 ->undeleteUnsafe( $reason );
66 $this->commitTransactionRound( __METHOD__ );
67
68 if ( !$status->isGood() ) {
69 $this->fatalError( $status );
70 }
71 $this->output( "done\n" );
72 }
73}
74
75// @codeCoverageIgnoreStart
76$maintClass = Undelete::class;
77require_once RUN_MAINTENANCE_IF_MAIN;
78// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:78
User class for the MediaWiki software.
Definition User.php:120
__construct()
Default constructor.
Definition undelete.php:33
execute()
Do the actual work.
Definition undelete.php:41
$maintClass
Definition undelete.php:76