MediaWiki 1.43.1
deleteDefaultMessages.php
Go to the documentation of this file.
1<?php
25// @codeCoverageIgnoreStart
26require_once __DIR__ . '/Maintenance.php';
27// @codeCoverageIgnoreEnd
28
33
40class 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' );
46 }
47
48 public function execute() {
49 $services = $this->getServiceContainer();
50
51 $this->output( "Checking existence of old default messages..." );
52 $dbr = $this->getReplicaDB();
53
54 $userFactory = $services->getUserFactory();
55 $actorQuery = ActorMigration::newMigration()
56 ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) );
57
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();
70
71 if ( $res->numRows() == 0 ) {
72 // No more messages left
73 $this->output( "done.\n" );
74 return;
75 }
76
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]]" );
82 }
83 $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
84 return;
85 }
86
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" );
92 }
93 $userGroupManager = $services->getUserGroupManager();
94 $userGroupManager->addUserToGroup( $user, 'bot' );
95 StubGlobalUser::setUser( $user );
96
97 // Handle deletion
98 $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
99 $dbw = $this->getPrimaryDB();
100
101 $wikiPageFactory = $services->getWikiPageFactory();
102 $delPageFactory = $services->getDeletePageFactory();
103
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' );
111 }
112
113 $this->output( "done!\n", 'msg' );
114 }
115}
116
117// @codeCoverageIgnoreStart
118$maintClass = DeleteDefaultMessages::class;
119require_once RUN_MAINTENANCE_IF_MAIN;
120// @codeCoverageIgnoreEnd
const NS_MEDIAWIKI
Definition Defines.php:73
Maintenance script that deletes all pages in the MediaWiki namespace which were last edited by "Media...
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Represents a title within MediaWiki.
Definition Title.php:78
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
internal since 1.36
Definition User.php:94