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  $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' );
88  StubGlobalUser::setUser( $user );
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;
111 require_once RUN_MAINTENANCE_IF_MAIN;
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:72
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:191
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
DeleteDefaultMessages
Maintenance script that deletes all pages in the MediaWiki namespace which were last edited by "Media...
Definition: deleteDefaultMessages.php:35
StubGlobalUser\setUser
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
Definition: StubGlobalUser.php:79
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$res
$res
Definition: testCompression.php:57
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:76
$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:809
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$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:651
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
DeleteDefaultMessages\execute
execute()
Do the actual work.
Definition: deleteDefaultMessages.php:43
$maintClass
$maintClass
Definition: deleteDefaultMessages.php:110
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
DeleteDefaultMessages\__construct
__construct()
Default constructor.
Definition: deleteDefaultMessages.php:36