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  $services = MediaWikiServices::getInstance();
93  $lbFactory = $services->getDBLoadBalancerFactory();
94  $wikiPageFactory = $services->getWikiPageFactory();
95 
96  foreach ( $res as $row ) {
97  $lbFactory->waitForReplication();
98  $dbw->ping();
99  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
100  $page = $wikiPageFactory->newFromTitle( $title );
101  // FIXME: Deletion failures should be reported, not silently ignored.
102  $page->doDeleteArticleReal( 'No longer required', $user );
103  }
104 
105  $this->output( "done!\n", 'msg' );
106  }
107 }
108 
109 $maintClass = DeleteDefaultMessages::class;
110 require_once RUN_MAINTENANCE_IF_MAIN;
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:71
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:486
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
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')
Definition: User.php:558
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$res
$res
Definition: testCompression.php:57
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:156
$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:751
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$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:624
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:1393
DeleteDefaultMessages\execute
execute()
Do the actual work.
Definition: deleteDefaultMessages.php:43
$maintClass
$maintClass
Definition: deleteDefaultMessages.php:109
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:433
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
DeleteDefaultMessages\__construct
__construct()
Default constructor.
Definition: deleteDefaultMessages.php:36