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->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
51 'If not given, stdin will be used.', false );
52 }
53
54 public function execute() {
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 StubGlobalUser::setUser( $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 = $this->getServiceContainer();
86 $wikiPageFactory = $services->getWikiPageFactory();
87 $repoGroup = $services->getRepoGroup();
88 $delPageFactory = $services->getDeletePageFactory();
89
90 # Handle each entry
91 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
92 $line = trim( fgets( $file ) );
93 if ( $line == '' ) {
94 continue;
95 }
96 $title = Title::newFromText( $line );
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 $delPage = $delPageFactory->newDeletePage( $page, $user );
117 $status = $delPage
118 ->forceImmediate( true )
119 ->deleteUnsafe( $reason );
120
121 if ( $status->isOK() ) {
122 $this->output( " Deleted!\n" );
123 } else {
124 $this->output( " FAILED to delete article\n" );
125 }
126
127 if ( $interval ) {
128 sleep( $interval );
129 }
130 $this->waitForReplication();
131 }
132 }
133}
134
135$maintClass = DeleteBatch::class;
136require_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.
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:79
internal since 1.36
Definition User.php:96
$maintClass
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42