MediaWiki REL1_37
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 $services = MediaWikiServices::getInstance();
45
46 $this->output( "Checking existence of old default messages..." );
47 $dbr = $this->getDB( DB_REPLICA );
48
49 $userFactory = $services->getUserFactory();
50 $actorQuery = ActorMigration::newMigration()
51 ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) );
52 $res = $dbr->select(
53 [ 'page', 'revision' ] + $actorQuery['tables'],
54 [ 'page_namespace', 'page_title' ],
55 [
56 'page_namespace' => NS_MEDIAWIKI,
57 $actorQuery['conds'],
58 ],
59 __METHOD__,
60 [],
61 [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins']
62 );
63
64 if ( $dbr->numRows( $res ) == 0 ) {
65 // No more messages left
66 $this->output( "done.\n" );
67 return;
68 }
69
70 $dryrun = $this->hasOption( 'dry-run' );
71 if ( $dryrun ) {
72 foreach ( $res as $row ) {
73 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
74 $this->output( "\n* [[$title]]" );
75 }
76 $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
77 return;
78 }
79
80 // Deletions will be made by $user temporarly added to the bot group
81 // in order to hide it in RecentChanges.
82 $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
83 if ( !$user ) {
84 $this->fatalError( "Invalid username" );
85 }
86 $userGroupManager = $services->getUserGroupManager();
87 $userGroupManager->addUserToGroup( $user, 'bot' );
89
90 // Handle deletion
91 $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
92 $dbw = $this->getDB( DB_PRIMARY );
93
94 $lbFactory = $services->getDBLoadBalancerFactory();
95 $wikiPageFactory = $services->getWikiPageFactory();
96
97 foreach ( $res as $row ) {
98 $lbFactory->waitForReplication();
99 $dbw->ping();
100 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
101 $page = $wikiPageFactory->newFromTitle( $title );
102 // FIXME: Deletion failures should be reported, not silently ignored.
103 $page->doDeleteArticleReal( 'No longer required', $user );
104 }
105
106 $this->output( "done!\n", 'msg' );
107 }
108}
109
110$maintClass = DeleteDefaultMessages::class;
111require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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.
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 setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:810
const DB_REPLICA
Definition defines.php:25
const DB_PRIMARY
Definition defines.php:27