MediaWiki  master
deleteDefaultMessages.php
Go to the documentation of this file.
1 <?php
25 require_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;
108 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:144
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:472
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:315
DeleteDefaultMessages
Maintenance script that deletes all pages in the MediaWiki namespace which were last edited by "Media...
Definition: deleteDefaultMessages.php:35
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:533
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:51
$res
$res
Definition: testCompression.php:57
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:139
$dbr
$dbr
Definition: testCompression.php:54
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:755
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:143
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:233
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:595
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1342
DeleteDefaultMessages\execute
execute()
Do the actual work.
Definition: deleteDefaultMessages.php:43
$maintClass
$maintClass
Definition: deleteDefaultMessages.php:107
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:421
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:77
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:254
DeleteDefaultMessages\__construct
__construct()
Default constructor.
Definition: deleteDefaultMessages.php:36