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