Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Undelete
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Undelete a page by fetching it from the archive table
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Maintenance\Maintenance;
25use MediaWiki\Title\Title;
26use MediaWiki\User\User;
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