MediaWiki REL1_39
deleteBatch.php
Go to the documentation of this file.
1<?php
32
33require_once __DIR__ . '/Maintenance.php';
34
40class 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 (in seconds) between deletions' );
48 $this->addOption( 'by-id', 'Delete by page ID instead of by page name', false, false );
49 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
50 'If not given, stdin will be used.', false );
51 }
52
53 public function execute() {
54 # Change to current working directory
55 $oldCwd = getcwd();
56 chdir( $oldCwd );
57
58 # Options processing
59 $username = $this->getOption( 'u', false );
60 $reason = $this->getOption( 'r', '' );
61 $interval = $this->getOption( 'i', 0 );
62 $byId = $this->hasOption( 'by-id' );
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 }
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 }
96 if ( $byId === false ) {
97 $target = Title::newFromText( $line );
98 if ( $target === null ) {
99 $this->output( "Invalid title '$line' on line $linenum\n" );
100 continue;
101 }
102 if ( !$target->exists() ) {
103 $this->output( "Skipping nonexistent page '$line'\n" );
104 continue;
105 }
106 } else {
107 $target = Title::newFromID( (int)$line );
108 if ( $target === null ) {
109 $this->output( "Invalid page ID '$line' on line $linenum\n" );
110 continue;
111 }
112 if ( !$target->exists() ) {
113 $this->output( "Skipping nonexistent page ID '$line'\n" );
114 continue;
115 }
116 }
117 if ( $target->getNamespace() === NS_FILE ) {
118 $img = $repoGroup->findFile(
119 $target, [ 'ignoreRedirect' => true ]
120 );
121 if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) {
122 $this->output( " FAILED to delete associated file..." );
123 }
124 }
125 $page = $wikiPageFactory->newFromTitle( $target );
126 $error = '';
127 $status = $page->doDeleteArticleReal(
128 $reason,
129 $user,
130 false,
131 null,
132 $error,
133 null,
134 [],
135 'delete',
136 true
137 );
138 if ( $status->isOK() ) {
139 $this->output( " Deleted!\n" );
140 } else {
141 $this->output( " FAILED to delete article\n" );
142 }
143
144 if ( $interval ) {
145 sleep( $interval );
146 }
147 $lbFactory->waitForReplication();
148 }
149 }
150}
151
152$maintClass = DeleteBatch::class;
153require_once RUN_MAINTENANCE_IF_MAIN;
const NS_FILE
Definition Defines.php:70
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)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newFromName( $name, $validate='valid')
Definition User.php:598
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:806
$maintClass
$line
Definition mcc.php:119
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42