]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/deleteBatch.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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. ' .
41                         'If not given, stdin will be used.', false );
42         }
43
44         public function execute() {
45                 global $wgUser;
46
47                 # Change to current working directory
48                 $oldCwd = getcwd();
49                 chdir( $oldCwd );
50
51                 # Options processing
52                 $user = $this->getOption( 'u', 'Delete page script' );
53                 $reason = $this->getOption( 'r', '' );
54                 $interval = $this->getOption( 'i', 0 );
55                 if ( $this->hasArg() ) {
56                         $file = fopen( $this->getArg(), 'r' );
57                 } else {
58                         $file = $this->getStdin();
59                 }
60
61                 # Setup
62                 if ( !$file ) {
63                         $this->error( "Unable to read file, exiting", true );
64                 }
65                 $wgUser = User::newFromName( $user );
66                 $dbw = wfGetDB( DB_MASTER );
67
68                 # Handle each entry
69                 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
70                         $line = trim( fgets( $file ) );
71                         if ( $line == '' ) {
72                                 continue;
73                         }
74                         $page = Title::newFromText( $line );
75                         if ( is_null( $page ) ) {
76                                 $this->output( "Invalid title '$line' on line $linenum\n" );
77                                 continue;
78                         }
79                         if ( !$page->exists() ) {
80                                 $this->output( "Skipping nonexistent page '$line'\n" );
81                                 continue;
82                         }
83
84
85                         $this->output( $page->getPrefixedText() );
86                         $dbw->begin();
87                         if ( $page->getNamespace() == NS_FILE ) {
88                                 $art = new ImagePage( $page );
89                                 $img = wfFindFile( $art->mTitle );
90                                 if ( !$img || !$img->delete( $reason ) ) {
91                                         $this->output( "FAILED to delete image file... " );
92                                 }
93                         } else {
94                                 $art = new Article( $page );
95                         }
96                         $success = $art->doDeleteArticle( $reason );
97                         $dbw->commit();
98                         if ( $success ) {
99                                 $this->output( "\n" );
100                         } else {
101                                 $this->output( " FAILED to delete article\n" );
102                         }
103
104                         if ( $interval ) {
105                                 sleep( $interval );
106                         }
107                         wfWaitForSlaves( 5 );
108 }
109         }
110 }
111
112 $maintClass = "DeleteBatch";
113 require_once( RUN_MAINTENANCE_IF_MAIN );