MediaWiki  master
deleteDefaultMessages.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
30 
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
41  ' which were last edited by "MediaWiki default"' );
42  $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' );
43  }
44 
45  public function execute() {
46  $services = MediaWikiServices::getInstance();
47 
48  $this->output( "Checking existence of old default messages..." );
49  $dbr = $this->getDB( DB_REPLICA );
50 
51  $userFactory = $services->getUserFactory();
52  $actorQuery = ActorMigration::newMigration()
53  ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) );
54 
55  $res = $dbr->newSelectQueryBuilder()
56  ->select( [ 'page_namespace', 'page_title' ] )
57  ->tables( [ 'page', 'revision' ] + $actorQuery['tables'] )
58  ->where( [
59  'page_namespace' => NS_MEDIAWIKI,
60  $actorQuery['conds'],
61  ] )
62  ->joinConds( [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins'] )
63  ->caller( __METHOD__ )
64  ->fetchResultSet();
65 
66  if ( $res->numRows() == 0 ) {
67  // No more messages left
68  $this->output( "done.\n" );
69  return;
70  }
71 
72  $dryrun = $this->hasOption( 'dry-run' );
73  if ( $dryrun ) {
74  foreach ( $res as $row ) {
75  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
76  $this->output( "\n* [[$title]]" );
77  }
78  $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
79  return;
80  }
81 
82  // Deletions will be made by $user temporarily added to the bot group
83  // in order to hide it in RecentChanges.
84  $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
85  if ( !$user ) {
86  $this->fatalError( "Invalid username" );
87  }
88  $userGroupManager = $services->getUserGroupManager();
89  $userGroupManager->addUserToGroup( $user, 'bot' );
90  StubGlobalUser::setUser( $user );
91 
92  // Handle deletion
93  $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
94  $dbw = $this->getDB( DB_PRIMARY );
95 
96  $wikiPageFactory = $services->getWikiPageFactory();
97 
98  foreach ( $res as $row ) {
99  $this->waitForReplication();
100  $dbw->ping();
101  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
102  $page = $wikiPageFactory->newFromTitle( $title );
103  // FIXME: Deletion failures should be reported, not silently ignored.
104  $page->doDeleteArticleReal( 'No longer required', $user );
105  }
106 
107  $this->output( "done!\n", 'msg' );
108  }
109 }
110 
111 $maintClass = DeleteDefaultMessages::class;
112 require_once RUN_MAINTENANCE_IF_MAIN;
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...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
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.
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:641
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:799
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28