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