Merge "Upgrade wikimedia/remex-html to 2.0.1" into REL1_31
[lhc/web/wiklou.git] / maintenance / deleteDefaultMessages.php
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 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script that deletes all pages in the MediaWiki namespace
29 * which were last edited by "MediaWiki default".
30 *
31 * @ingroup Maintenance
32 */
33 class DeleteDefaultMessages extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
37 ' which were last edited by "MediaWiki default"' );
38 $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' );
39 }
40
41 public function execute() {
42 global $wgUser;
43
44 $this->output( "Checking existence of old default messages..." );
45 $dbr = $this->getDB( DB_REPLICA );
46
47 $actorQuery = ActorMigration::newMigration()
48 ->getWhere( $dbr, 'rev_user', User::newFromName( 'MediaWiki default' ) );
49 $res = $dbr->select(
50 [ 'page', 'revision' ] + $actorQuery['tables'],
51 [ 'page_namespace', 'page_title' ],
52 [
53 'page_namespace' => NS_MEDIAWIKI,
54 $actorQuery['conds'],
55 ],
56 __METHOD__,
57 [],
58 [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins']
59 );
60
61 if ( $dbr->numRows( $res ) == 0 ) {
62 // No more messages left
63 $this->output( "done.\n" );
64 return;
65 }
66
67 $dryrun = $this->hasOption( 'dry-run' );
68 if ( $dryrun ) {
69 foreach ( $res as $row ) {
70 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
71 $this->output( "\n* [[$title]]" );
72 }
73 $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
74 return;
75 }
76
77 // Deletions will be made by $user temporarly added to the bot group
78 // in order to hide it in RecentChanges.
79 $user = User::newFromName( 'MediaWiki default' );
80 if ( !$user ) {
81 $this->fatalError( "Invalid username" );
82 }
83 $user->addGroup( 'bot' );
84 $wgUser = $user;
85
86 // Handle deletion
87 $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
88 $dbw = $this->getDB( DB_MASTER );
89
90 foreach ( $res as $row ) {
91 wfWaitForSlaves();
92 $dbw->ping();
93 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
94 $page = WikiPage::factory( $title );
95 $error = ''; // Passed by ref
96 // FIXME: Deletion failures should be reported, not silently ignored.
97 $page->doDeleteArticle( 'No longer required', false, 0, true, $error, $user );
98 }
99
100 $this->output( "done!\n", 'msg' );
101 }
102 }
103
104 $maintClass = DeleteDefaultMessages::class;
105 require_once RUN_MAINTENANCE_IF_MAIN;