Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / maintenance / deleteBatch.php
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
11 *
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.
16 *
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.
21 *
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
26 *
27 * @file
28 * @ingroup Maintenance
29 */
30
31 use MediaWiki\MediaWikiServices;
32
33 require_once __DIR__ . '/Maintenance.php';
34
35 /**
36 * Maintenance script to delete a batch of pages.
37 *
38 * @ingroup Maintenance
39 */
40 class DeleteBatch extends Maintenance {
41
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 );
50 }
51
52 public function execute() {
53 global $wgUser;
54
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 $wgUser = $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 # Handle each entry
86 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
87 $line = trim( fgets( $file ) );
88 if ( $line == '' ) {
89 continue;
90 }
91 $title = Title::newFromText( $line );
92 if ( is_null( $title ) ) {
93 $this->output( "Invalid title '$line' on line $linenum\n" );
94 continue;
95 }
96 if ( !$title->exists() ) {
97 $this->output( "Skipping nonexistent page '$line'\n" );
98 continue;
99 }
100
101 $this->output( $title->getPrefixedText() );
102 if ( $title->getNamespace() == NS_FILE ) {
103 $img = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
104 $title, [ 'ignoreRedirect' => true ]
105 );
106 if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
107 $this->output( " FAILED to delete associated file... " );
108 }
109 }
110 $page = WikiPage::factory( $title );
111 $error = '';
112 $success = $page->doDeleteArticle( $reason, false, null, null, $error, $user, true );
113 if ( $success ) {
114 $this->output( " Deleted!\n" );
115 } else {
116 $this->output( " FAILED to delete article\n" );
117 }
118
119 if ( $interval ) {
120 sleep( $interval );
121 }
122 wfWaitForSlaves();
123 }
124 }
125 }
126
127 $maintClass = DeleteBatch::class;
128 require_once RUN_MAINTENANCE_IF_MAIN;