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