3 * Deletes all pages in the MediaWiki namespace which were last edited by
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
22 * @ingroup Maintenance
25 // @codeCoverageIgnoreStart
26 require_once __DIR__
. '/Maintenance.php';
27 // @codeCoverageIgnoreEnd
29 use MediaWiki\Maintenance\Maintenance
;
30 use MediaWiki\StubObject\StubGlobalUser
;
31 use MediaWiki\Title\Title
;
32 use MediaWiki\User\ActorMigration
;
33 use MediaWiki\User\User
;
36 * Maintenance script that deletes all pages in the MediaWiki namespace
37 * which were last edited by "MediaWiki default".
39 * @ingroup Maintenance
41 class 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' );
49 public function execute() {
50 $services = $this->getServiceContainer();
52 $this->output( "Checking existence of old default messages..." );
53 $dbr = $this->getReplicaDB();
55 $userFactory = $services->getUserFactory();
56 $actorQuery = ActorMigration
::newMigration()
57 ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) );
59 $res = $dbr->newSelectQueryBuilder()
60 ->select( [ 'page_namespace', 'page_title' ] )
62 ->join( 'revision', null, 'page_latest=rev_id' )
63 ->tables( $actorQuery['tables'] )
65 'page_namespace' => NS_MEDIAWIKI
,
68 ->joinConds( $actorQuery['joins'] )
69 ->caller( __METHOD__
)
72 if ( $res->numRows() == 0 ) {
73 // No more messages left
74 $this->output( "done.\n" );
78 $dryrun = $this->hasOption( 'dry-run' );
80 foreach ( $res as $row ) {
81 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
82 $this->output( "\n* [[$title]]" );
84 $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
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 ] );
92 $this->fatalError( "Invalid username" );
94 $userGroupManager = $services->getUserGroupManager();
95 $userGroupManager->addUserToGroup( $user, 'bot' );
96 StubGlobalUser
::setUser( $user );
99 $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
100 $dbw = $this->getPrimaryDB();
102 $wikiPageFactory = $services->getWikiPageFactory();
103 $delPageFactory = $services->getDeletePageFactory();
105 foreach ( $res as $row ) {
106 $this->waitForReplication();
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' );
114 $this->output( "done!\n", 'msg' );
118 // @codeCoverageIgnoreStart
119 $maintClass = DeleteDefaultMessages
::class;
120 require_once RUN_MAINTENANCE_IF_MAIN
;
121 // @codeCoverageIgnoreEnd