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