API: Document a level of 'all' in action=protect
[mediawiki.git] / maintenance / deleteBatch.php
blob0020446b41653d5d3555273928961e945a938c77
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 require_once __DIR__ . '/Maintenance.php';
33 /**
34 * Maintenance script to delete a batch of pages.
36 * @ingroup Maintenance
38 class DeleteBatch extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Deletes a batch of pages' );
43 $this->addOption( 'u', "User to perform deletion", false, true );
44 $this->addOption( 'r', "Reason to delete page", false, true );
45 $this->addOption( 'i', "Interval to sleep between deletions" );
46 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
47 'If not given, stdin will be used.', false );
50 public function execute() {
51 global $wgUser;
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->error( "Invalid username", true );
70 $wgUser = $user;
72 if ( $this->hasArg() ) {
73 $file = fopen( $this->getArg(), 'r' );
74 } else {
75 $file = $this->getStdin();
78 # Setup
79 if ( !$file ) {
80 $this->error( "Unable to read file, exiting", true );
83 $dbw = $this->getDB( DB_MASTER );
85 # Handle each entry
86 // @codingStandardsIgnoreStart Ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
87 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
88 // @codingStandardsIgnoreEnd
89 $line = trim( fgets( $file ) );
90 if ( $line == '' ) {
91 continue;
93 $title = Title::newFromText( $line );
94 if ( is_null( $title ) ) {
95 $this->output( "Invalid title '$line' on line $linenum\n" );
96 continue;
98 if ( !$title->exists() ) {
99 $this->output( "Skipping nonexistent page '$line'\n" );
100 continue;
103 $this->output( $title->getPrefixedText() );
104 if ( $title->getNamespace() == NS_FILE ) {
105 $img = wfFindFile( $title, [ 'ignoreRedirect' => true ] );
106 if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
107 $this->output( " FAILED to delete associated file... " );
110 $page = WikiPage::factory( $title );
111 $error = '';
112 $success = $page->doDeleteArticle( $reason, false, 0, true, $error, $user );
113 if ( $success ) {
114 $this->output( " Deleted!\n" );
115 } else {
116 $this->output( " FAILED to delete article\n" );
119 if ( $interval ) {
120 sleep( $interval );
122 wfWaitForSlaves();
127 $maintClass = "DeleteBatch";
128 require_once RUN_MAINTENANCE_IF_MAIN;