Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 119 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
CleanupImages | |
0.00% |
0 / 116 |
|
0.00% |
0 / 9 |
702 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
processRow | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
30 | |||
killRow | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
filePath | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
imageExists | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
pageExists | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
pokeFile | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
110 | |||
appendTitle | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
buildSafeTitle | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * Clean up broken, unparseable upload filenames. |
4 | * |
5 | * Copyright © 2005-2006 Brooke Vibber <bvibber@wikimedia.org> |
6 | * https://www.mediawiki.org/ |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; if not, write to the Free Software Foundation, Inc., |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | * http://www.gnu.org/copyleft/gpl.html |
22 | * |
23 | * @file |
24 | * @author Brooke Vibber <bvibber@wikimedia.org> |
25 | * @ingroup Maintenance |
26 | */ |
27 | |
28 | use MediaWiki\Parser\Sanitizer; |
29 | use MediaWiki\Title\Title; |
30 | |
31 | require_once __DIR__ . '/TableCleanup.php'; |
32 | |
33 | /** |
34 | * Maintenance script to clean up broken, unparseable upload filenames. |
35 | * |
36 | * @ingroup Maintenance |
37 | */ |
38 | class CleanupImages extends TableCleanup { |
39 | protected $defaultParams = [ |
40 | 'table' => 'image', |
41 | 'conds' => [], |
42 | 'index' => 'img_name', |
43 | 'callback' => 'processRow', |
44 | ]; |
45 | |
46 | /** @var LocalRepo|null */ |
47 | private $repo; |
48 | |
49 | public function __construct() { |
50 | parent::__construct(); |
51 | $this->addDescription( 'Script to clean up broken, unparseable upload filenames' ); |
52 | } |
53 | |
54 | protected function processRow( $row ) { |
55 | $source = $row->img_name; |
56 | if ( $source == '' ) { |
57 | // Ye olde empty rows. Just kill them. |
58 | $this->killRow( $source ); |
59 | |
60 | $this->progress( 1 ); |
61 | return; |
62 | } |
63 | |
64 | $cleaned = $source; |
65 | |
66 | // About half of old bad image names have percent-codes |
67 | $cleaned = rawurldecode( $cleaned ); |
68 | |
69 | // We also have some HTML entities there |
70 | $cleaned = Sanitizer::decodeCharReferences( $cleaned ); |
71 | |
72 | $contLang = $this->getServiceContainer()->getContentLanguage(); |
73 | |
74 | // Some are old latin-1 |
75 | $cleaned = $contLang->checkTitleEncoding( $cleaned ); |
76 | |
77 | // Many of remainder look like non-normalized unicode |
78 | $cleaned = $contLang->normalize( $cleaned ); |
79 | |
80 | $title = Title::makeTitleSafe( NS_FILE, $cleaned ); |
81 | |
82 | if ( $title === null ) { |
83 | $this->output( "page $source ($cleaned) is illegal.\n" ); |
84 | $safe = $this->buildSafeTitle( $cleaned ); |
85 | if ( $safe === false ) { |
86 | $this->progress( 0 ); |
87 | return; |
88 | } |
89 | $this->pokeFile( $source, $safe ); |
90 | |
91 | $this->progress( 1 ); |
92 | return; |
93 | } |
94 | |
95 | if ( $title->getDBkey() !== $source ) { |
96 | $munged = $title->getDBkey(); |
97 | $this->output( "page $source ($munged) doesn't match self.\n" ); |
98 | $this->pokeFile( $source, $munged ); |
99 | |
100 | $this->progress( 1 ); |
101 | return; |
102 | } |
103 | |
104 | $this->progress( 0 ); |
105 | } |
106 | |
107 | /** |
108 | * @param string $name |
109 | */ |
110 | private function killRow( $name ) { |
111 | if ( $this->dryrun ) { |
112 | $this->output( "DRY RUN: would delete bogus row '$name'\n" ); |
113 | } else { |
114 | $this->output( "deleting bogus row '$name'\n" ); |
115 | $db = $this->getPrimaryDB(); |
116 | $db->newDeleteQueryBuilder() |
117 | ->deleteFrom( 'image' ) |
118 | ->where( [ 'img_name' => $name ] ) |
119 | ->caller( __METHOD__ ) |
120 | ->execute(); |
121 | } |
122 | } |
123 | |
124 | /** |
125 | * @param string $name |
126 | * @return string |
127 | */ |
128 | private function filePath( $name ) { |
129 | if ( $this->repo === null ) { |
130 | $this->repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
131 | } |
132 | |
133 | return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name; |
134 | } |
135 | |
136 | private function imageExists( $name, $db ) { |
137 | return (bool)$db->newSelectQueryBuilder() |
138 | ->select( '1' ) |
139 | ->from( 'image' ) |
140 | ->where( [ 'img_name' => $name ] ) |
141 | ->caller( __METHOD__ ) |
142 | ->fetchField(); |
143 | } |
144 | |
145 | private function pageExists( $name, $db ) { |
146 | return (bool)$db->newSelectQueryBuilder() |
147 | ->select( '1' ) |
148 | ->from( 'page' ) |
149 | ->where( [ |
150 | 'page_namespace' => NS_FILE, |
151 | 'page_title' => $name, |
152 | ] ) |
153 | ->caller( __METHOD__ ) |
154 | ->fetchField(); |
155 | } |
156 | |
157 | private function pokeFile( $orig, $new ) { |
158 | $path = $this->filePath( $orig ); |
159 | if ( !file_exists( $path ) ) { |
160 | $this->output( "missing file: $path\n" ); |
161 | $this->killRow( $orig ); |
162 | |
163 | return; |
164 | } |
165 | |
166 | $db = $this->getPrimaryDB(); |
167 | |
168 | /* |
169 | * To prevent key collisions in the update() statements below, |
170 | * if the target title exists in the image table, or if both the |
171 | * original and target titles exist in the page table, append |
172 | * increasing version numbers until the target title exists in |
173 | * neither. (See also T18916.) |
174 | */ |
175 | $version = 0; |
176 | $final = $new; |
177 | $conflict = ( $this->imageExists( $final, $db ) || |
178 | ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) ); |
179 | |
180 | while ( $conflict ) { |
181 | $this->output( "Rename conflicts with '$final'...\n" ); |
182 | $version++; |
183 | $final = $this->appendTitle( $new, "_$version" ); |
184 | $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) ); |
185 | } |
186 | |
187 | $finalPath = $this->filePath( $final ); |
188 | |
189 | if ( $this->dryrun ) { |
190 | $this->output( "DRY RUN: would rename $path to $finalPath\n" ); |
191 | } else { |
192 | $this->output( "renaming $path to $finalPath\n" ); |
193 | // @todo FIXME: Should this use File::move()? |
194 | $this->beginTransaction( $db, __METHOD__ ); |
195 | $db->newUpdateQueryBuilder() |
196 | ->update( 'image' ) |
197 | ->set( [ 'img_name' => $final ] ) |
198 | ->where( [ 'img_name' => $orig ] ) |
199 | ->caller( __METHOD__ ) |
200 | ->execute(); |
201 | $db->newUpdateQueryBuilder() |
202 | ->update( 'oldimage' ) |
203 | ->set( [ 'oi_name' => $final ] ) |
204 | ->where( [ 'oi_name' => $orig ] ) |
205 | ->caller( __METHOD__ ) |
206 | ->execute(); |
207 | $db->newUpdateQueryBuilder() |
208 | ->update( 'page' ) |
209 | ->set( [ 'page_title' => $final ] ) |
210 | ->where( [ 'page_title' => $orig, 'page_namespace' => NS_FILE ] ) |
211 | ->caller( __METHOD__ ) |
212 | ->execute(); |
213 | $dir = dirname( $finalPath ); |
214 | if ( !file_exists( $dir ) ) { |
215 | if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) { |
216 | $this->output( "RENAME FAILED, COULD NOT CREATE $dir" ); |
217 | $this->rollbackTransaction( $db, __METHOD__ ); |
218 | |
219 | return; |
220 | } |
221 | } |
222 | if ( rename( $path, $finalPath ) ) { |
223 | $this->commitTransaction( $db, __METHOD__ ); |
224 | } else { |
225 | $this->error( "RENAME FAILED" ); |
226 | $this->rollbackTransaction( $db, __METHOD__ ); |
227 | } |
228 | } |
229 | } |
230 | |
231 | private function appendTitle( $name, $suffix ) { |
232 | return preg_replace( '/^(.*)(\..*?)$/', |
233 | "\\1$suffix\\2", $name ); |
234 | } |
235 | |
236 | private function buildSafeTitle( $name ) { |
237 | $x = preg_replace_callback( |
238 | '/([^' . Title::legalChars() . ']|~)/', |
239 | [ $this, 'hexChar' ], |
240 | $name ); |
241 | |
242 | $test = Title::makeTitleSafe( NS_FILE, $x ); |
243 | if ( $test === null || $test->getDBkey() !== $x ) { |
244 | $this->error( "Unable to generate safe title from '$name', got '$x'" ); |
245 | |
246 | return false; |
247 | } |
248 | |
249 | return $x; |
250 | } |
251 | } |
252 | |
253 | $maintClass = CleanupImages::class; |
254 | require_once RUN_MAINTENANCE_IF_MAIN; |