Add partial support for running Parsoid selser tests
[mediawiki.git] / maintenance / deleteBatch.php
blobacdb83f7cd6f44c14227840af96a3b0aee48fc33
1 <?php
2 /**
3 * Deletes a batch of pages.
4 * Usage: php deleteBatch.php [-u <user>] [-r <reason>] [-i <interval>] [listfile]
5 * where
6 * [listfile] is a file where each line contains the title of a page to be
7 * deleted, standard input is used if listfile is not given.
8 * <user> is the username
9 * <reason> is the delete reason
10 * <interval> is the number of seconds to sleep for after each delete
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
27 * @file
28 * @ingroup Maintenance
31 use MediaWiki\MediaWikiServices;
33 require_once __DIR__ . '/Maintenance.php';
35 /**
36 * Maintenance script to delete a batch of pages.
38 * @ingroup Maintenance
40 class DeleteBatch extends Maintenance {
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 between deletions" );
48 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
49 'If not given, stdin will be used.', false );
52 public function execute() {
53 # Change to current working directory
54 $oldCwd = getcwd();
55 chdir( $oldCwd );
57 # Options processing
58 $username = $this->getOption( 'u', false );
59 $reason = $this->getOption( 'r', '' );
60 $interval = $this->getOption( 'i', 0 );
62 if ( $username === false ) {
63 $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] );
64 } else {
65 $user = User::newFromName( $username );
67 if ( !$user ) {
68 $this->fatalError( "Invalid username" );
70 StubGlobalUser::setUser( $user );
72 if ( $this->hasArg( 0 ) ) {
73 $file = fopen( $this->getArg( 0 ), 'r' );
74 } else {
75 $file = $this->getStdin();
78 # Setup
79 if ( !$file ) {
80 $this->fatalError( "Unable to read file, exiting" );
83 $services = MediaWikiServices::getInstance();
84 $lbFactory = $services->getDBLoadBalancerFactory();
85 $wikiPageFactory = $services->getWikiPageFactory();
86 $repoGroup = $services->getRepoGroup();
88 # Handle each entry
89 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
90 $line = trim( fgets( $file ) );
91 if ( $line == '' ) {
92 continue;
94 $title = Title::newFromText( $line );
95 if ( $title === null ) {
96 $this->output( "Invalid title '$line' on line $linenum\n" );
97 continue;
99 if ( !$title->exists() ) {
100 $this->output( "Skipping nonexistent page '$line'\n" );
101 continue;
104 $this->output( $title->getPrefixedText() );
105 if ( $title->getNamespace() === NS_FILE ) {
106 $img = $repoGroup->findFile(
107 $title, [ 'ignoreRedirect' => true ]
109 if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) {
110 $this->output( " FAILED to delete associated file..." );
113 $page = $wikiPageFactory->newFromTitle( $title );
114 $error = '';
115 $status = $page->doDeleteArticleReal(
116 $reason,
117 $user,
118 false,
119 null,
120 $error,
121 null,
123 'delete',
124 true
126 if ( $status->isOK() ) {
127 $this->output( " Deleted!\n" );
128 } else {
129 $this->output( " FAILED to delete article\n" );
132 if ( $interval ) {
133 sleep( $interval );
135 $lbFactory->waitForReplication();
140 $maintClass = DeleteBatch::class;
141 require_once RUN_MAINTENANCE_IF_MAIN;