Merge "Purge Squid variant pages based on page language (not $wgContLang)"
[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. ' .
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 $username = $this->getOption( 'u', 'Delete page script' );
53 $reason = $this->getOption( 'r', '' );
54 $interval = $this->getOption( 'i', 0 );
55
56 $user = User::newFromName( $username );
57 if ( !$user ) {
58 $this->error( "Invalid username", true );
59 }
60 $wgUser = $user;
61
62 if ( $this->hasArg() ) {
63 $file = fopen( $this->getArg(), 'r' );
64 } else {
65 $file = $this->getStdin();
66 }
67
68 # Setup
69 if ( !$file ) {
70 $this->error( "Unable to read file, exiting", true );
71 }
72
73 $dbw = wfGetDB( DB_MASTER );
74
75 # Handle each entry
76 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
77 $line = trim( fgets( $file ) );
78 if ( $line == '' ) {
79 continue;
80 }
81 $title = Title::newFromText( $line );
82 if ( is_null( $title ) ) {
83 $this->output( "Invalid title '$line' on line $linenum\n" );
84 continue;
85 }
86 if ( !$title->exists() ) {
87 $this->output( "Skipping nonexistent page '$line'\n" );
88 continue;
89 }
90
91 $this->output( $title->getPrefixedText() );
92 $dbw->begin( __METHOD__ );
93 if ( $title->getNamespace() == NS_FILE ) {
94 $img = wfFindFile( $title );
95 if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
96 $this->output( " FAILED to delete associated file... " );
97 }
98 }
99 $page = WikiPage::factory( $title );
100 $error = '';
101 $success = $page->doDeleteArticle( $reason, false, 0, false, $error, $user );
102 $dbw->commit( __METHOD__ );
103 if ( $success ) {
104 $this->output( " Deleted!\n" );
105 } else {
106 $this->output( " FAILED to delete article\n" );
107 }
108
109 if ( $interval ) {
110 sleep( $interval );
111 }
112 wfWaitForSlaves();
113 }
114 }
115 }
116
117 $maintClass = "DeleteBatch";
118 require_once( RUN_MAINTENANCE_IF_MAIN );