MediaWiki REL1_35
deleteDefaultMessages.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26
28
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
39 ' which were last edited by "MediaWiki default"' );
40 $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' );
41 }
42
43 public function execute() {
44 global $wgUser;
45
46 $this->output( "Checking existence of old default messages..." );
47 $dbr = $this->getDB( DB_REPLICA );
48
49 $actorQuery = ActorMigration::newMigration()
50 ->getWhere( $dbr, 'rev_user', User::newFromName( 'MediaWiki default' ) );
51 $res = $dbr->select(
52 [ 'page', 'revision' ] + $actorQuery['tables'],
53 [ 'page_namespace', 'page_title' ],
54 [
55 'page_namespace' => NS_MEDIAWIKI,
56 $actorQuery['conds'],
57 ],
58 __METHOD__,
59 [],
60 [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins']
61 );
62
63 if ( $dbr->numRows( $res ) == 0 ) {
64 // No more messages left
65 $this->output( "done.\n" );
66 return;
67 }
68
69 $dryrun = $this->hasOption( 'dry-run' );
70 if ( $dryrun ) {
71 foreach ( $res as $row ) {
72 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
73 $this->output( "\n* [[$title]]" );
74 }
75 $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
76 return;
77 }
78
79 // Deletions will be made by $user temporarly added to the bot group
80 // in order to hide it in RecentChanges.
81 $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
82 if ( !$user ) {
83 $this->fatalError( "Invalid username" );
84 }
85 $user->addGroup( 'bot' );
86 $wgUser = $user;
87
88 // Handle deletion
89 $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
90 $dbw = $this->getDB( DB_MASTER );
91
92 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
93
94 foreach ( $res as $row ) {
95 $lbFactory->waitForReplication();
96 $dbw->ping();
97 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
98 $page = WikiPage::factory( $title );
99 // FIXME: Deletion failures should be reported, not silently ignored.
100 $page->doDeleteArticleReal( 'No longer required', $user );
101 }
102
103 $this->output( "done!\n", 'msg' );
104 }
105}
106
107$maintClass = DeleteDefaultMessages::class;
108require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
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.
hasOption( $name)
Checks to see if a particular option was set.
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:758
const NS_MEDIAWIKI
Definition Defines.php:78
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29