phpcs: More require/include is not a function
[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 * http://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 $name string
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( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ );
126 }
127
128 private function pokeFile( $orig, $new ) {
129 $path = $this->filePath( $orig );
130 if ( !file_exists( $path ) ) {
131 $this->output( "missing file: $path\n" );
132 $this->killRow( $orig );
133 return;
134 }
135
136 $db = wfGetDB( DB_MASTER );
137
138 /*
139 * To prevent key collisions in the update() statements below,
140 * if the target title exists in the image table, or if both the
141 * original and target titles exist in the page table, append
142 * increasing version numbers until the target title exists in
143 * neither. (See also bug 16916.)
144 */
145 $version = 0;
146 $final = $new;
147 $conflict = ( $this->imageExists( $final, $db ) ||
148 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
149
150 while ( $conflict ) {
151 $this->output( "Rename conflicts with '$final'...\n" );
152 $version++;
153 $final = $this->appendTitle( $new, "_$version" );
154 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
155 }
156
157 $finalPath = $this->filePath( $final );
158
159 if ( $this->dryrun ) {
160 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
161 } else {
162 $this->output( "renaming $path to $finalPath\n" );
163 // @todo FIXME: Should this use File::move()?
164 $db->begin( __METHOD__ );
165 $db->update( 'image',
166 array( 'img_name' => $final ),
167 array( 'img_name' => $orig ),
168 __METHOD__ );
169 $db->update( 'oldimage',
170 array( 'oi_name' => $final ),
171 array( 'oi_name' => $orig ),
172 __METHOD__ );
173 $db->update( 'page',
174 array( 'page_title' => $final ),
175 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
176 __METHOD__ );
177 $dir = dirname( $finalPath );
178 if ( !file_exists( $dir ) ) {
179 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
180 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
181 $db->rollback( __METHOD__ );
182 return;
183 }
184 }
185 if ( rename( $path, $finalPath ) ) {
186 $db->commit( __METHOD__ );
187 } else {
188 $this->error( "RENAME FAILED" );
189 $db->rollback( __METHOD__ );
190 }
191 }
192 }
193
194 private function appendTitle( $name, $suffix ) {
195 return preg_replace( '/^(.*)(\..*?)$/',
196 "\\1$suffix\\2", $name );
197 }
198
199 private function buildSafeTitle( $name ) {
200 $x = preg_replace_callback(
201 '/([^' . Title::legalChars() . ']|~)/',
202 array( $this, 'hexChar' ),
203 $name );
204
205 $test = Title::makeTitleSafe( NS_FILE, $x );
206 if ( is_null( $test ) || $test->getDBkey() !== $x ) {
207 $this->error( "Unable to generate safe title from '$name', got '$x'" );
208 return false;
209 }
210
211 return $x;
212 }
213 }
214
215 $maintClass = "ImageCleanup";
216 require_once RUN_MAINTENANCE_IF_MAIN;