3 * Undelete a page by fetching it from the archive table
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.
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.
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
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance
;
25 use MediaWiki\Title\Title
;
26 use MediaWiki\User\User
;
28 // @codeCoverageIgnoreStart
29 require_once __DIR__
. '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
32 class 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' );
41 public function execute() {
42 $username = $this->getOption( 'user', false );
43 $reason = $this->getOption( 'reason', '' );
44 $pageName = $this->getArg( 0 );
46 $title = Title
::newFromText( $pageName );
48 $this->fatalError( "Invalid title" );
50 if ( $username === false ) {
51 $user = User
::newSystemUser( 'Command line script', [ 'steal' => true ] );
53 $user = User
::newFromName( $username );
56 $this->fatalError( "Invalid username" );
59 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
60 $this->output( "Undeleting " . $title->getPrefixedDBkey() . "...\n" );
62 $this->beginTransactionRound( __METHOD__
);
63 $status = $this->getServiceContainer()->getUndeletePageFactory()
64 ->newUndeletePage( $page, $user )
65 ->undeleteUnsafe( $reason );
66 $this->commitTransactionRound( __METHOD__
);
68 if ( !$status->isGood() ) {
69 $this->fatalError( $status );
71 $this->output( "done\n" );
75 // @codeCoverageIgnoreStart
76 $maintClass = Undelete
::class;
77 require_once RUN_MAINTENANCE_IF_MAIN
;
78 // @codeCoverageIgnoreEnd