6e44d63f47f6c7d6d4423d147541392df2b89ef0
[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 * @file
13 * @ingroup Maintenance
14 */
15
16 require_once( "Maintenance.php" );
17
18 class DeleteBatch extends Maintenance {
19
20 public function __construct() {
21 parent::__construct();
22 $this->mDescription = "Deletes a batch of pages";
23 $this->addParam( 'u', "User to perform deletion", false, true );
24 $this->addParam( 'r', "Reason to delete page", false, true );
25 $this->addParam( 'i', "Interval to sleep between deletions" );
26 $this->addArgs( array( 'listfile' ) );
27 }
28
29 public function execute() {
30 global $wgUser;
31
32 # Change to current working directory
33 $oldCwd = getcwd();
34 chdir( $oldCwd );
35
36 # Options processing
37 $user = $this->getOption( 'u', 'Delete page script' );
38 $reason = $this->getOption( 'r', '' );
39 $interval = $this->getOption( 'i', 0 );
40 if( $this->hasArg() ) {
41 $file = fopen( $this->getArg(), 'r' );
42 } else {
43 $file = $this->getStdin();
44 }
45
46 # Setup
47 if( !$file ) {
48 $this->error( "Unable to read file, exiting\n", true );
49 }
50 $wgUser = User::newFromName( $user );
51 $dbw = wfGetDB( DB_MASTER );
52
53 # Handle each entry
54 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
55 $line = trim( fgets( $file ) );
56 if ( $line == '' ) {
57 continue;
58 }
59 $page = Title::newFromText( $line );
60 if ( is_null( $page ) ) {
61 $this->output( "Invalid title '$line' on line $linenum\n" );
62 continue;
63 }
64 if( !$page->exists() ) {
65 $this->output( "Skipping nonexistent page '$line'\n" );
66 continue;
67 }
68
69
70 $this->output( $page->getPrefixedText() );
71 $dbw->begin();
72 if( $page->getNamespace() == NS_FILE ) {
73 $art = new ImagePage( $page );
74 $img = wfFindFile( $art->mTitle );
75 if( !$img || !$img->delete( $reason ) ) {
76 $this->output( "FAILED to delete image file... " );
77 }
78 } else {
79 $art = new Article( $page );
80 }
81 $success = $art->doDeleteArticle( $reason );
82 $dbw->immediateCommit();
83 if ( $success ) {
84 $this->output( "\n" );
85 } else {
86 $this->output( " FAILED to delete article\n" );
87 }
88
89 if ( $interval ) {
90 sleep( $interval );
91 }
92 wfWaitForSlaves( 5 );
93 }
94 }
95 }
96
97 $maintClass = "DeleteBatch";
98 require_once( DO_MAINTENANCE );