Merge "Pass phpcs-strict on maintenance/ (8/8)"
[lhc/web/wiklou.git] / maintenance / cleanupImages.php
1 <?php
2 /**
3 * Clean up broken, unparseable upload filenames.
4 *
5 * Usage: php cleanupImages.php [--fix]
6 * Options:
7 * --fix Actually clean up titles; otherwise just checks for them
8 *
9 * Copyright © 2005-2006 Brion Vibber <brion@pobox.com>
10 * https://www.mediawiki.org/
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 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
30 */
31
32 require_once __DIR__ . '/cleanupTable.inc';
33
34 /**
35 * Maintenance script to clean up broken, unparseable upload filenames.
36 *
37 * @ingroup Maintenance
38 */
39 class ImageCleanup extends TableCleanup {
40 protected $defaultParams = array(
41 'table' => 'image',
42 'conds' => array(),
43 'index' => 'img_name',
44 'callback' => 'processRow',
45 );
46
47 public function __construct() {
48 parent::__construct();
49 $this->mDescription = "Script to clean up broken, unparseable upload filenames";
50 }
51
52 protected function processRow( $row ) {
53 global $wgContLang;
54
55 $source = $row->img_name;
56 if ( $source == '' ) {
57 // Ye olde empty rows. Just kill them.
58 $this->killRow( $source );
59 return $this->progress( 1 );
60 }
61
62 $cleaned = $source;
63
64 // About half of old bad image names have percent-codes
65 $cleaned = rawurldecode( $cleaned );
66
67 // We also have some HTML entities there
68 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
69
70 // Some are old latin-1
71 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
72
73 // Many of remainder look like non-normalized unicode
74 $cleaned = $wgContLang->normalize( $cleaned );
75
76 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
77
78 if ( is_null( $title ) ) {
79 $this->output( "page $source ($cleaned) is illegal.\n" );
80 $safe = $this->buildSafeTitle( $cleaned );
81 if ( $safe === false ) {
82 return $this->progress( 0 );
83 }
84 $this->pokeFile( $source, $safe );
85 return $this->progress( 1 );
86 }
87
88 if ( $title->getDBkey() !== $source ) {
89 $munged = $title->getDBkey();
90 $this->output( "page $source ($munged) doesn't match self.\n" );
91 $this->pokeFile( $source, $munged );
92 return $this->progress( 1 );
93 }
94
95 return $this->progress( 0 );
96 }
97
98 /**
99 * @param string $name
100 */
101 private function killRow( $name ) {
102 if ( $this->dryrun ) {
103 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
104 } else {
105 $this->output( "deleting bogus row '$name'\n" );
106 $db = wfGetDB( DB_MASTER );
107 $db->delete( 'image',
108 array( 'img_name' => $name ),
109 __METHOD__ );
110 }
111 }
112
113 private function filePath( $name ) {
114 if ( !isset( $this->repo ) ) {
115 $this->repo = RepoGroup::singleton()->getLocalRepo();
116 }
117 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
118 }
119
120 private function imageExists( $name, $db ) {
121 return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__ );
122 }
123
124 private function pageExists( $name, $db ) {
125 return $db->selectField(
126 'page',
127 '1',
128 array( 'page_namespace' => NS_FILE, 'page_title' => $name ),
129 __METHOD__
130 );
131 }
132
133 private function pokeFile( $orig, $new ) {
134 $path = $this->filePath( $orig );
135 if ( !file_exists( $path ) ) {
136 $this->output( "missing file: $path\n" );
137 $this->killRow( $orig );
138 return;
139 }
140
141 $db = wfGetDB( DB_MASTER );
142
143 /*
144 * To prevent key collisions in the update() statements below,
145 * if the target title exists in the image table, or if both the
146 * original and target titles exist in the page table, append
147 * increasing version numbers until the target title exists in
148 * neither. (See also bug 16916.)
149 */
150 $version = 0;
151 $final = $new;
152 $conflict = ( $this->imageExists( $final, $db ) ||
153 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
154
155 while ( $conflict ) {
156 $this->output( "Rename conflicts with '$final'...\n" );
157 $version++;
158 $final = $this->appendTitle( $new, "_$version" );
159 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
160 }
161
162 $finalPath = $this->filePath( $final );
163
164 if ( $this->dryrun ) {
165 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
166 } else {
167 $this->output( "renaming $path to $finalPath\n" );
168 // @todo FIXME: Should this use File::move()?
169 $db->begin( __METHOD__ );
170 $db->update( 'image',
171 array( 'img_name' => $final ),
172 array( 'img_name' => $orig ),
173 __METHOD__ );
174 $db->update( 'oldimage',
175 array( 'oi_name' => $final ),
176 array( 'oi_name' => $orig ),
177 __METHOD__ );
178 $db->update( 'page',
179 array( 'page_title' => $final ),
180 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
181 __METHOD__ );
182 $dir = dirname( $finalPath );
183 if ( !file_exists( $dir ) ) {
184 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
185 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
186 $db->rollback( __METHOD__ );
187 return;
188 }
189 }
190 if ( rename( $path, $finalPath ) ) {
191 $db->commit( __METHOD__ );
192 } else {
193 $this->error( "RENAME FAILED" );
194 $db->rollback( __METHOD__ );
195 }
196 }
197 }
198
199 private function appendTitle( $name, $suffix ) {
200 return preg_replace( '/^(.*)(\..*?)$/',
201 "\\1$suffix\\2", $name );
202 }
203
204 private function buildSafeTitle( $name ) {
205 $x = preg_replace_callback(
206 '/([^' . Title::legalChars() . ']|~)/',
207 array( $this, 'hexChar' ),
208 $name );
209
210 $test = Title::makeTitleSafe( NS_FILE, $x );
211 if ( is_null( $test ) || $test->getDBkey() !== $x ) {
212 $this->error( "Unable to generate safe title from '$name', got '$x'" );
213 return false;
214 }
215
216 return $x;
217 }
218 }
219
220 $maintClass = "ImageCleanup";
221 require_once RUN_MAINTENANCE_IF_MAIN;