Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteDefaultMessages
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Deletes all pages in the MediaWiki namespace which were last edited by
4 * "MediaWiki default".
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25require_once __DIR__ . '/Maintenance.php';
26
27use MediaWiki\StubObject\StubGlobalUser;
28use MediaWiki\Title\Title;
29use MediaWiki\User\ActorMigration;
30use MediaWiki\User\User;
31
32/**
33 * Maintenance script that deletes all pages in the MediaWiki namespace
34 * which were last edited by "MediaWiki default".
35 *
36 * @ingroup Maintenance
37 */
38class DeleteDefaultMessages extends Maintenance {
39    public function __construct() {
40        parent::__construct();
41        $this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
42            ' which were last edited by "MediaWiki default"' );
43        $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' );
44    }
45
46    public function execute() {
47        $services = $this->getServiceContainer();
48
49        $this->output( "Checking existence of old default messages..." );
50        $dbr = $this->getReplicaDB();
51
52        $userFactory = $services->getUserFactory();
53        $actorQuery = ActorMigration::newMigration()
54            ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) );
55
56        $res = $dbr->newSelectQueryBuilder()
57            ->select( [ 'page_namespace', 'page_title' ] )
58            ->tables( [ 'page', 'revision' ] + $actorQuery['tables'] )
59            ->where( [
60                'page_namespace' => NS_MEDIAWIKI,
61                $actorQuery['conds'],
62            ] )
63            ->joinConds( [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins'] )
64            ->caller( __METHOD__ )
65            ->fetchResultSet();
66
67        if ( $res->numRows() == 0 ) {
68            // No more messages left
69            $this->output( "done.\n" );
70            return;
71        }
72
73        $dryrun = $this->hasOption( 'dry-run' );
74        if ( $dryrun ) {
75            foreach ( $res as $row ) {
76                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
77                $this->output( "\n* [[$title]]" );
78            }
79            $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
80            return;
81        }
82
83        // Deletions will be made by $user temporarily added to the bot group
84        // in order to hide it in RecentChanges.
85        $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
86        if ( !$user ) {
87            $this->fatalError( "Invalid username" );
88        }
89        $userGroupManager = $services->getUserGroupManager();
90        $userGroupManager->addUserToGroup( $user, 'bot' );
91        StubGlobalUser::setUser( $user );
92
93        // Handle deletion
94        $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
95        $dbw = $this->getPrimaryDB();
96
97        $wikiPageFactory = $services->getWikiPageFactory();
98        $delPageFactory = $services->getDeletePageFactory();
99
100        foreach ( $res as $row ) {
101            $this->waitForReplication();
102            $dbw->ping();
103            $title = Title::makeTitle( $row->page_namespace, $row->page_title );
104            $page = $wikiPageFactory->newFromTitle( $title );
105            // FIXME: Deletion failures should be reported, not silently ignored.
106            $delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' );
107        }
108
109        $this->output( "done!\n", 'msg' );
110    }
111}
112
113$maintClass = DeleteDefaultMessages::class;
114require_once RUN_MAINTENANCE_IF_MAIN;