* (bug 21503) There's now a "reason" field when creating account for other users
[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 * @ingroup Maintenance
28 */
29
30 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
31
32 class DeleteBatch extends Maintenance {
33
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Deletes a batch of pages";
37 $this->addOption( 'u', "User to perform deletion", false, true );
38 $this->addOption( 'r', "Reason to delete page", false, true );
39 $this->addOption( 'i', "Interval to sleep between deletions" );
40 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines', false );
41 }
42
43 public function execute() {
44 global $wgUser;
45
46 # Change to current working directory
47 $oldCwd = getcwd();
48 chdir( $oldCwd );
49
50 # Options processing
51 $user = $this->getOption( 'u', 'Delete page script' );
52 $reason = $this->getOption( 'r', '' );
53 $interval = $this->getOption( 'i', 0 );
54 if ( $this->hasArg() ) {
55 $file = fopen( $this->getArg(), 'r' );
56 } else {
57 $file = $this->getStdin();
58 }
59
60 # Setup
61 if ( !$file ) {
62 $this->error( "Unable to read file, exiting", true );
63 }
64 $wgUser = User::newFromName( $user );
65 $dbw = wfGetDB( DB_MASTER );
66
67 # Handle each entry
68 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
69 $line = trim( fgets( $file ) );
70 if ( $line == '' ) {
71 continue;
72 }
73 $page = Title::newFromText( $line );
74 if ( is_null( $page ) ) {
75 $this->output( "Invalid title '$line' on line $linenum\n" );
76 continue;
77 }
78 if ( !$page->exists() ) {
79 $this->output( "Skipping nonexistent page '$line'\n" );
80 continue;
81 }
82
83
84 $this->output( $page->getPrefixedText() );
85 $dbw->begin();
86 if ( $page->getNamespace() == NS_FILE ) {
87 $art = new ImagePage( $page );
88 $img = wfFindFile( $art->mTitle );
89 if ( !$img || !$img->delete( $reason ) ) {
90 $this->output( "FAILED to delete image file... " );
91 }
92 } else {
93 $art = new Article( $page );
94 }
95 $success = $art->doDeleteArticle( $reason );
96 $dbw->commit();
97 if ( $success ) {
98 $this->output( "\n" );
99 } else {
100 $this->output( " FAILED to delete article\n" );
101 }
102
103 if ( $interval ) {
104 sleep( $interval );
105 }
106 wfWaitForSlaves( 5 );
107 }
108 }
109 }
110
111 $maintClass = "DeleteBatch";
112 require_once( DO_MAINTENANCE );