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