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 ->from( 'page' )
59 ->join( 'revision', null, 'page_latest=rev_id' )
60 ->tables( $actorQuery['tables'] )
61 ->where( [
62 'page_namespace' => NS_MEDIAWIKI,
63 $actorQuery['conds'],
64 ] )
65 ->joinConds( $actorQuery['joins'] )
66 ->caller( __METHOD__ )
67 ->fetchResultSet();
68
69 if ( $res->numRows() == 0 ) {
70 // No more messages left
71 $this->output( "done.\n" );
72 return;
73 }
74
75 $dryrun = $this->hasOption( 'dry-run' );
76 if ( $dryrun ) {
77 foreach ( $res as $row ) {
78 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
79 $this->output( "\n* [[$title]]" );
80 }
81 $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
82 return;
83 }
84
85 // Deletions will be made by $user temporarily added to the bot group
86 // in order to hide it in RecentChanges.
87 $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
88 if ( !$user ) {
89 $this->fatalError( "Invalid username" );
90 }
91 $userGroupManager = $services->getUserGroupManager();
92 $userGroupManager->addUserToGroup( $user, 'bot' );
93 StubGlobalUser::setUser( $user );
94
95 // Handle deletion
96 $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
97 $dbw = $this->getPrimaryDB();
98
99 $wikiPageFactory = $services->getWikiPageFactory();
100 $delPageFactory = $services->getDeletePageFactory();
101
102 foreach ( $res as $row ) {
103 $this->waitForReplication();
104 $dbw->ping();
105 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
106 $page = $wikiPageFactory->newFromTitle( $title );
107 // FIXME: Deletion failures should be reported, not silently ignored.
108 $delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' );
109 }
110
111 $this->output( "done!\n", 'msg' );
112 }
113}
114
115$maintClass = DeleteDefaultMessages::class;
116require_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