MediaWiki master
deleteDefaultMessages.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26
31
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
42 ' which were last edited by "MediaWiki default"' );
43 $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' );
44 }
45
46 public function execute() {
47 $services = $this->getServiceContainer();
48
49 $this->output( "Checking existence of old default messages..." );
50 $dbr = $this->getReplicaDB();
51
52 $userFactory = $services->getUserFactory();
53 $actorQuery = ActorMigration::newMigration()
54 ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) );
55
56 $res = $dbr->newSelectQueryBuilder()
57 ->select( [ 'page_namespace', 'page_title' ] )
58 ->tables( [ 'page', 'revision' ] + $actorQuery['tables'] )
59 ->where( [
60 'page_namespace' => NS_MEDIAWIKI,
61 $actorQuery['conds'],
62 ] )
63 ->joinConds( [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins'] )
64 ->caller( __METHOD__ )
65 ->fetchResultSet();
66
67 if ( $res->numRows() == 0 ) {
68 // No more messages left
69 $this->output( "done.\n" );
70 return;
71 }
72
73 $dryrun = $this->hasOption( 'dry-run' );
74 if ( $dryrun ) {
75 foreach ( $res as $row ) {
76 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
77 $this->output( "\n* [[$title]]" );
78 }
79 $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
80 return;
81 }
82
83 // Deletions will be made by $user temporarily added to the bot group
84 // in order to hide it in RecentChanges.
85 $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
86 if ( !$user ) {
87 $this->fatalError( "Invalid username" );
88 }
89 $userGroupManager = $services->getUserGroupManager();
90 $userGroupManager->addUserToGroup( $user, 'bot' );
91 StubGlobalUser::setUser( $user );
92
93 // Handle deletion
94 $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
95 $dbw = $this->getPrimaryDB();
96
97 $wikiPageFactory = $services->getWikiPageFactory();
98 $delPageFactory = $services->getDeletePageFactory();
99
100 foreach ( $res as $row ) {
101 $this->waitForReplication();
102 $dbw->ping();
103 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
104 $page = $wikiPageFactory->newFromTitle( $title );
105 // FIXME: Deletion failures should be reported, not silently ignored.
106 $delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' );
107 }
108
109 $this->output( "done!\n", 'msg' );
110 }
111}
112
113$maintClass = DeleteDefaultMessages::class;
114require_once RUN_MAINTENANCE_IF_MAIN;
const NS_MEDIAWIKI
Definition Defines.php:72
Maintenance script that deletes all pages in the MediaWiki namespace which were last edited by "Media...
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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:93