MediaWiki REL1_39
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
53 $res = $dbr->newSelectQueryBuilder()
54 ->select( [ 'page_namespace', 'page_title' ] )
55 ->tables( [ 'page', 'revision' ] + $actorQuery['tables'] )
56 ->where( [
57 'page_namespace' => NS_MEDIAWIKI,
58 $actorQuery['conds'],
59 ] )
60 ->joinConds( [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins'] )
61 ->caller( __METHOD__ )
62 ->fetchResultSet();
63
64 if ( $res->numRows() == 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 temporarily 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.
Service locator for MediaWiki core services.
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:806
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28