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