Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.96% covered (success)
97.96%
48 / 49
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteDefaultMessages
97.96% covered (success)
97.96%
48 / 49
50.00% covered (danger)
50.00%
1 / 2
7
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
97.78% covered (success)
97.78%
44 / 45
0.00% covered (danger)
0.00%
0 / 1
6
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
25// @codeCoverageIgnoreStart
26require_once __DIR__ . '/Maintenance.php';
27// @codeCoverageIgnoreEnd
28
29use MediaWiki\Maintenance\Maintenance;
30use MediaWiki\StubObject\StubGlobalUser;
31use MediaWiki\Title\Title;
32use MediaWiki\User\ActorMigration;
33use MediaWiki\User\User;
34
35/**
36 * Maintenance script that deletes all pages in the MediaWiki namespace
37 * which were last edited by "MediaWiki default".
38 *
39 * @ingroup Maintenance
40 */
41class DeleteDefaultMessages extends Maintenance {
42    public function __construct() {
43        parent::__construct();
44        $this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
45            ' which were last edited by "MediaWiki default"' );
46        $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' );
47    }
48
49    public function execute() {
50        $services = $this->getServiceContainer();
51
52        $this->output( "Checking existence of old default messages..." );
53        $dbr = $this->getReplicaDB();
54
55        $userFactory = $services->getUserFactory();
56        $actorQuery = ActorMigration::newMigration()
57            ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) );
58
59        $res = $dbr->newSelectQueryBuilder()
60            ->select( [ 'page_namespace', 'page_title' ] )
61            ->from( 'page' )
62            ->join( 'revision', null, 'page_latest=rev_id' )
63            ->tables( $actorQuery['tables'] )
64            ->where( [
65                'page_namespace' => NS_MEDIAWIKI,
66                $actorQuery['conds'],
67            ] )
68            ->joinConds( $actorQuery['joins'] )
69            ->caller( __METHOD__ )
70            ->fetchResultSet();
71
72        if ( $res->numRows() == 0 ) {
73            // No more messages left
74            $this->output( "done.\n" );
75            return;
76        }
77
78        $dryrun = $this->hasOption( 'dry-run' );
79        if ( $dryrun ) {
80            foreach ( $res as $row ) {
81                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
82                $this->output( "\n* [[$title]]" );
83            }
84            $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
85            return;
86        }
87
88        // Deletions will be made by $user temporarily added to the bot group
89        // in order to hide it in RecentChanges.
90        $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
91        if ( !$user ) {
92            $this->fatalError( "Invalid username" );
93        }
94        $userGroupManager = $services->getUserGroupManager();
95        $userGroupManager->addUserToGroup( $user, 'bot' );
96        StubGlobalUser::setUser( $user );
97
98        // Handle deletion
99        $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
100        $dbw = $this->getPrimaryDB();
101
102        $wikiPageFactory = $services->getWikiPageFactory();
103        $delPageFactory = $services->getDeletePageFactory();
104
105        foreach ( $res as $row ) {
106            $this->waitForReplication();
107            $dbw->ping();
108            $title = Title::makeTitle( $row->page_namespace, $row->page_title );
109            $page = $wikiPageFactory->newFromTitle( $title );
110            // FIXME: Deletion failures should be reported, not silently ignored.
111            $delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' );
112        }
113
114        $this->output( "done!\n", 'msg' );
115    }
116}
117
118// @codeCoverageIgnoreStart
119$maintClass = DeleteDefaultMessages::class;
120require_once RUN_MAINTENANCE_IF_MAIN;
121// @codeCoverageIgnoreEnd