MediaWiki  master
deleteBatch.php
Go to the documentation of this file.
1 <?php
32 
33 require_once __DIR__ . '/Maintenance.php';
34 
40 class DeleteBatch extends Maintenance {
41 
42  public function __construct() {
43  parent::__construct();
44  $this->addDescription( 'Deletes a batch of pages' );
45  $this->addOption( 'u', "User to perform deletion", false, true );
46  $this->addOption( 'r', "Reason to delete page", false, true );
47  $this->addOption( 'i', "Interval to sleep between deletions" );
48  $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
49  'If not given, stdin will be used.', false );
50  }
51 
52  public function execute() {
53  global $wgUser;
54 
55  # Change to current working directory
56  $oldCwd = getcwd();
57  chdir( $oldCwd );
58 
59  # Options processing
60  $username = $this->getOption( 'u', false );
61  $reason = $this->getOption( 'r', '' );
62  $interval = $this->getOption( 'i', 0 );
63 
64  if ( $username === false ) {
65  $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] );
66  } else {
67  $user = User::newFromName( $username );
68  }
69  if ( !$user ) {
70  $this->fatalError( "Invalid username" );
71  }
72  $wgUser = $user;
73 
74  if ( $this->hasArg( 0 ) ) {
75  $file = fopen( $this->getArg( 0 ), 'r' );
76  } else {
77  $file = $this->getStdin();
78  }
79 
80  # Setup
81  if ( !$file ) {
82  $this->fatalError( "Unable to read file, exiting" );
83  }
84 
85  $services = MediaWikiServices::getInstance();
86  $lbFactory = $services->getDBLoadBalancerFactory();
87  $wikiPageFactory = $services->getWikiPageFactory();
88  $repoGroup = $services->getRepoGroup();
89 
90  # Handle each entry
91  for ( $linenum = 1; !feof( $file ); $linenum++ ) {
92  $line = trim( fgets( $file ) );
93  if ( $line == '' ) {
94  continue;
95  }
97  if ( $title === null ) {
98  $this->output( "Invalid title '$line' on line $linenum\n" );
99  continue;
100  }
101  if ( !$title->exists() ) {
102  $this->output( "Skipping nonexistent page '$line'\n" );
103  continue;
104  }
105 
106  $this->output( $title->getPrefixedText() );
107  if ( $title->getNamespace() === NS_FILE ) {
108  $img = $repoGroup->findFile(
109  $title, [ 'ignoreRedirect' => true ]
110  );
111  if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) {
112  $this->output( " FAILED to delete associated file... " );
113  }
114  }
115  $page = $wikiPageFactory->newFromTitle( $title );
116  $error = '';
117  $status = $page->doDeleteArticleReal(
118  $reason,
119  $user,
120  false,
121  null,
122  $error,
123  null,
124  [],
125  'delete',
126  true
127  );
128  if ( $status->isOK() ) {
129  $this->output( " Deleted!\n" );
130  } else {
131  $this->output( " FAILED to delete article\n" );
132  }
133 
134  if ( $interval ) {
135  sleep( $interval );
136  }
137  $lbFactory->waitForReplication();
138  }
139  }
140 }
141 
142 $maintClass = DeleteBatch::class;
143 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:328
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:405
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:165
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
NS_FILE
const NS_FILE
Definition: Defines.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:545
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:336
DeleteBatch\execute
execute()
Do the actual work.
Definition: deleteBatch.php:52
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:738
DeleteBatch\__construct
__construct()
Default constructor.
Definition: deleteBatch.php:42
DeleteBatch
Maintenance script to delete a batch of pages.
Definition: deleteBatch.php:40
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
$maintClass
$maintClass
Definition: deleteBatch.php:142
$line
$line
Definition: mcc.php:119
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:298
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:433
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:351