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