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