MediaWiki master
deleteBatch.php
Go to the documentation of this file.
1<?php
34
35require_once __DIR__ . '/Maintenance.php';
36
42class DeleteBatch extends Maintenance {
43
44 public function __construct() {
45 parent::__construct();
46 $this->addDescription( 'Deletes a batch of pages' );
47 $this->addOption( 'u', 'User to perform deletion', false, true );
48 $this->addOption( 'r', 'Reason to delete page', false, true );
49 $this->addOption( 'i', 'Interval to sleep (in seconds) between deletions' );
50 $this->addOption( 'by-id', 'Delete by page ID instead of by page name', false, false );
51 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
52 'If not given, stdin will be used.', false );
53 }
54
55 public function execute() {
56 # Change to current working directory
57 $oldCwd = getcwd();
58 chdir( $oldCwd );
59
60 # Options processing
61 $username = $this->getOption( 'u', false );
62 $reason = $this->getOption( 'r', '' );
63 $interval = $this->getOption( 'i', 0 );
64 $byId = $this->hasOption( 'by-id' );
65
66 if ( $username === false ) {
67 $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] );
68 } else {
69 $user = User::newFromName( $username );
70 }
71 if ( !$user ) {
72 $this->fatalError( "Invalid username" );
73 }
74 StubGlobalUser::setUser( $user );
75
76 if ( $this->hasArg( 0 ) ) {
77 $file = fopen( $this->getArg( 0 ), 'r' );
78 } else {
79 $file = $this->getStdin();
80 }
81
82 # Setup
83 if ( !$file ) {
84 $this->fatalError( "Unable to read file, exiting" );
85 }
86
87 $services = $this->getServiceContainer();
88 $wikiPageFactory = $services->getWikiPageFactory();
89 $repoGroup = $services->getRepoGroup();
90 $delPageFactory = $services->getDeletePageFactory();
91
92 # Handle each entry
93 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
94 $line = trim( fgets( $file ) );
95 if ( $line == '' ) {
96 continue;
97 }
98 if ( $byId === false ) {
99 $target = Title::newFromText( $line );
100 if ( $target === null ) {
101 $this->output( "Invalid title '$line' on line $linenum\n" );
102 continue;
103 }
104 if ( !$target->exists() ) {
105 $this->output( "Skipping nonexistent page '$line'\n" );
106 continue;
107 }
108 } else {
109 $target = Title::newFromID( (int)$line );
110 if ( $target === null ) {
111 $this->output( "Invalid page ID '$line' on line $linenum\n" );
112 continue;
113 }
114 if ( !$target->exists() ) {
115 $this->output( "Skipping nonexistent page ID '$line'\n" );
116 continue;
117 }
118 }
119 if ( $target->getNamespace() === NS_FILE ) {
120 $img = $repoGroup->findFile(
121 $target, [ 'ignoreRedirect' => true ]
122 );
123 if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) {
124 $this->output( " FAILED to delete associated file..." );
125 }
126 }
127 $page = $wikiPageFactory->newFromTitle( $target );
128 $delPage = $delPageFactory->newDeletePage( $page, $user );
129 $status = $delPage
130 ->forceImmediate( true )
131 ->deleteUnsafe( $reason );
132
133 if ( $status->isOK() ) {
134 $this->output( " Deleted!\n" );
135 } else {
136 $this->output( " FAILED to delete article\n" );
137 }
138
139 if ( $interval ) {
140 sleep( $interval );
141 }
142 $this->waitForReplication();
143 }
144 }
145}
146
147$maintClass = DeleteBatch::class;
148require_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, $multi=false)
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?
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
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.
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:78
internal since 1.36
Definition User.php:93
$maintClass