MediaWiki master
deleteBatch.php
Go to the documentation of this file.
1<?php
21
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
31class DeleteBatch extends Maintenance {
32
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Deletes a batch of pages' );
36 $this->addOption( 'u', 'User to perform deletion', false, true );
37 $this->addOption( 'r', 'Reason to delete page', false, true );
38 $this->addOption( 'i', 'Interval to sleep (in seconds) between deletions' );
39 $this->addOption( 'by-id', 'Delete by page ID instead of by page name', false, false );
40 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
41 'If not given, stdin will be used.', false );
42 }
43
44 public function execute() {
45 # Change to current working directory
46 $oldCwd = getcwd();
47 chdir( $oldCwd );
48
49 # Options processing
50 $username = $this->getOption( 'u', false );
51 $reason = $this->getOption( 'r', '' );
52 $interval = $this->getOption( 'i', 0 );
53 $byId = $this->hasOption( 'by-id' );
54
55 if ( $username === false ) {
56 $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] );
57 } else {
58 $user = User::newFromName( $username );
59 }
60 if ( !$user ) {
61 $this->fatalError( "Invalid username" );
62 }
63 StubGlobalUser::setUser( $user );
64
65 if ( $this->hasArg( 0 ) ) {
66 $file = fopen( $this->getArg( 0 ), 'r' );
67 } else {
68 $file = $this->getStdin();
69 }
70
71 # Setup
72 if ( !$file ) {
73 $this->fatalError( "Unable to read file, exiting" );
74 }
75
76 $services = $this->getServiceContainer();
77 $wikiPageFactory = $services->getWikiPageFactory();
78 $repoGroup = $services->getRepoGroup();
79 $delPageFactory = $services->getDeletePageFactory();
80
81 # Handle each entry
82 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
83 $line = trim( fgets( $file ) );
84 if ( $line == '' ) {
85 continue;
86 }
87 if ( $byId === false ) {
88 $target = Title::newFromText( $line );
89 if ( $target === null ) {
90 $this->output( "Invalid title '$line' on line $linenum\n" );
91 continue;
92 }
93 if ( !$target->exists() ) {
94 $this->output( "Skipping nonexistent page '$line'\n" );
95 continue;
96 }
97 } else {
98 $target = Title::newFromID( (int)$line );
99 if ( $target === null ) {
100 $this->output( "Invalid page ID '$line' on line $linenum\n" );
101 continue;
102 }
103 if ( !$target->exists() ) {
104 $this->output( "Skipping nonexistent page ID '$line'\n" );
105 continue;
106 }
107 }
108 if ( $target->getNamespace() === NS_FILE ) {
109 $img = $repoGroup->findFile(
110 $target, [ 'ignoreRedirect' => true ]
111 );
112 if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) {
113 $this->output( " FAILED to delete associated file..." );
114 }
115 }
116 $page = $wikiPageFactory->newFromTitle( $target );
117 $delPage = $delPageFactory->newDeletePage( $page, $user );
118 $status = $delPage
119 ->forceImmediate( true )
120 ->deleteUnsafe( $reason );
121
122 if ( $status->isOK() ) {
123 $this->output( " Deleted $line!\n" );
124 } else {
125 $this->output( " FAILED to delete page $line\n" );
126 $this->error( $status );
127 }
128
129 if ( $interval ) {
130 sleep( $interval );
131 }
132 $this->waitForReplication();
133 }
134 }
135}
136
137// @codeCoverageIgnoreStart
138$maintClass = DeleteBatch::class;
139require_once RUN_MAINTENANCE_IF_MAIN;
140// @codeCoverageIgnoreEnd
const NS_FILE
Definition Defines.php:57
Maintenance script to delete a batch of pages.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
hasArg( $argId=0)
Does a given argument exist?
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
getStdin( $len=null)
Return input from stdin.
addDescription( $text)
Set the description text.
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Represents a title within MediaWiki.
Definition Title.php:69
User class for the MediaWiki software.
Definition User.php:130
$maintClass