Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 68 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
DeleteBatch | |
0.00% |
0 / 65 |
|
0.00% |
0 / 2 |
380 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
342 |
1 | <?php |
2 | /** |
3 | * Deletes a batch of pages. |
4 | * Usage: php deleteBatch.php [-u <user>] [-r <reason>] [-i <interval>] [--by-id] [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\StubObject\StubGlobalUser; |
32 | use MediaWiki\Title\Title; |
33 | use MediaWiki\User\User; |
34 | |
35 | require_once __DIR__ . '/Maintenance.php'; |
36 | |
37 | /** |
38 | * Maintenance script to delete a batch of pages. |
39 | * |
40 | * @ingroup Maintenance |
41 | */ |
42 | class DeleteBatch extends Maintenance { |
43 | |
44 | public function __construct() { |
45 | parent::__construct(); |
46 | $this->addDescription( 'Deletes a batch of pages' ); |
47 | $this->addOption( 'u', 'User to perform deletion', false, true ); |
48 | $this->addOption( 'r', 'Reason to delete page', false, true ); |
49 | $this->addOption( 'i', 'Interval to sleep (in seconds) between deletions' ); |
50 | $this->addOption( 'by-id', 'Delete by page ID instead of by page name', false, false ); |
51 | $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' . |
52 | 'If not given, stdin will be used.', false ); |
53 | } |
54 | |
55 | public function execute() { |
56 | # Change to current working directory |
57 | $oldCwd = getcwd(); |
58 | chdir( $oldCwd ); |
59 | |
60 | # Options processing |
61 | $username = $this->getOption( 'u', false ); |
62 | $reason = $this->getOption( 'r', '' ); |
63 | $interval = $this->getOption( 'i', 0 ); |
64 | $byId = $this->hasOption( 'by-id' ); |
65 | |
66 | if ( $username === false ) { |
67 | $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] ); |
68 | } else { |
69 | $user = User::newFromName( $username ); |
70 | } |
71 | if ( !$user ) { |
72 | $this->fatalError( "Invalid username" ); |
73 | } |
74 | StubGlobalUser::setUser( $user ); |
75 | |
76 | if ( $this->hasArg( 0 ) ) { |
77 | $file = fopen( $this->getArg( 0 ), 'r' ); |
78 | } else { |
79 | $file = $this->getStdin(); |
80 | } |
81 | |
82 | # Setup |
83 | if ( !$file ) { |
84 | $this->fatalError( "Unable to read file, exiting" ); |
85 | } |
86 | |
87 | $services = $this->getServiceContainer(); |
88 | $wikiPageFactory = $services->getWikiPageFactory(); |
89 | $repoGroup = $services->getRepoGroup(); |
90 | $delPageFactory = $services->getDeletePageFactory(); |
91 | |
92 | # Handle each entry |
93 | for ( $linenum = 1; !feof( $file ); $linenum++ ) { |
94 | $line = trim( fgets( $file ) ); |
95 | if ( $line == '' ) { |
96 | continue; |
97 | } |
98 | if ( $byId === false ) { |
99 | $target = Title::newFromText( $line ); |
100 | if ( $target === null ) { |
101 | $this->output( "Invalid title '$line' on line $linenum\n" ); |
102 | continue; |
103 | } |
104 | if ( !$target->exists() ) { |
105 | $this->output( "Skipping nonexistent page '$line'\n" ); |
106 | continue; |
107 | } |
108 | } else { |
109 | $target = Title::newFromID( (int)$line ); |
110 | if ( $target === null ) { |
111 | $this->output( "Invalid page ID '$line' on line $linenum\n" ); |
112 | continue; |
113 | } |
114 | if ( !$target->exists() ) { |
115 | $this->output( "Skipping nonexistent page ID '$line'\n" ); |
116 | continue; |
117 | } |
118 | } |
119 | if ( $target->getNamespace() === NS_FILE ) { |
120 | $img = $repoGroup->findFile( |
121 | $target, [ 'ignoreRedirect' => true ] |
122 | ); |
123 | if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) { |
124 | $this->output( " FAILED to delete associated file..." ); |
125 | } |
126 | } |
127 | $page = $wikiPageFactory->newFromTitle( $target ); |
128 | $delPage = $delPageFactory->newDeletePage( $page, $user ); |
129 | $status = $delPage |
130 | ->forceImmediate( true ) |
131 | ->deleteUnsafe( $reason ); |
132 | |
133 | if ( $status->isOK() ) { |
134 | $this->output( " Deleted!\n" ); |
135 | } else { |
136 | $this->output( " FAILED to delete article\n" ); |
137 | } |
138 | |
139 | if ( $interval ) { |
140 | sleep( $interval ); |
141 | } |
142 | $this->waitForReplication(); |
143 | } |
144 | } |
145 | } |
146 | |
147 | $maintClass = DeleteBatch::class; |
148 | require_once RUN_MAINTENANCE_IF_MAIN; |