MediaWiki REL1_30
cleanupImages.php
Go to the documentation of this file.
1<?php
28require_once __DIR__ . '/cleanupTable.inc';
29
36 protected $defaultParams = [
37 'table' => 'image',
38 'conds' => [],
39 'index' => 'img_name',
40 'callback' => 'processRow',
41 ];
42
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( 'Script to clean up broken, unparseable upload filenames' );
46 }
47
48 protected function processRow( $row ) {
49 global $wgContLang;
50
51 $source = $row->img_name;
52 if ( $source == '' ) {
53 // Ye olde empty rows. Just kill them.
54 $this->killRow( $source );
55
56 return $this->progress( 1 );
57 }
58
59 $cleaned = $source;
60
61 // About half of old bad image names have percent-codes
62 $cleaned = rawurldecode( $cleaned );
63
64 // We also have some HTML entities there
65 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
66
67 // Some are old latin-1
68 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
69
70 // Many of remainder look like non-normalized unicode
71 $cleaned = $wgContLang->normalize( $cleaned );
72
73 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
74
75 if ( is_null( $title ) ) {
76 $this->output( "page $source ($cleaned) is illegal.\n" );
77 $safe = $this->buildSafeTitle( $cleaned );
78 if ( $safe === false ) {
79 return $this->progress( 0 );
80 }
81 $this->pokeFile( $source, $safe );
82
83 return $this->progress( 1 );
84 }
85
86 if ( $title->getDBkey() !== $source ) {
87 $munged = $title->getDBkey();
88 $this->output( "page $source ($munged) doesn't match self.\n" );
89 $this->pokeFile( $source, $munged );
90
91 return $this->progress( 1 );
92 }
93
94 return $this->progress( 0 );
95 }
96
100 private function killRow( $name ) {
101 if ( $this->dryrun ) {
102 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
103 } else {
104 $this->output( "deleting bogus row '$name'\n" );
105 $db = $this->getDB( DB_MASTER );
106 $db->delete( 'image',
107 [ 'img_name' => $name ],
108 __METHOD__ );
109 }
110 }
111
112 private function filePath( $name ) {
113 if ( !isset( $this->repo ) ) {
114 $this->repo = RepoGroup::singleton()->getLocalRepo();
115 }
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', [ 'img_name' => $name ], __METHOD__ );
122 }
123
124 private function pageExists( $name, $db ) {
125 return $db->selectField(
126 'page',
127 '1',
128 [ '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
139 return;
140 }
141
142 $db = $this->getDB( DB_MASTER );
143
144 /*
145 * To prevent key collisions in the update() statements below,
146 * if the target title exists in the image table, or if both the
147 * original and target titles exist in the page table, append
148 * increasing version numbers until the target title exists in
149 * neither. (See also T18916.)
150 */
151 $version = 0;
152 $final = $new;
153 $conflict = ( $this->imageExists( $final, $db ) ||
154 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
155
156 while ( $conflict ) {
157 $this->output( "Rename conflicts with '$final'...\n" );
158 $version++;
159 $final = $this->appendTitle( $new, "_$version" );
160 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
161 }
162
163 $finalPath = $this->filePath( $final );
164
165 if ( $this->dryrun ) {
166 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
167 } else {
168 $this->output( "renaming $path to $finalPath\n" );
169 // @todo FIXME: Should this use File::move()?
170 $this->beginTransaction( $db, __METHOD__ );
171 $db->update( 'image',
172 [ 'img_name' => $final ],
173 [ 'img_name' => $orig ],
174 __METHOD__ );
175 $db->update( 'oldimage',
176 [ 'oi_name' => $final ],
177 [ 'oi_name' => $orig ],
178 __METHOD__ );
179 $db->update( 'page',
180 [ 'page_title' => $final ],
181 [ 'page_title' => $orig, 'page_namespace' => NS_FILE ],
182 __METHOD__ );
183 $dir = dirname( $finalPath );
184 if ( !file_exists( $dir ) ) {
185 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
186 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
187 $this->rollbackTransaction( $db, __METHOD__ );
188
189 return;
190 }
191 }
192 if ( rename( $path, $finalPath ) ) {
193 $this->commitTransaction( $db, __METHOD__ );
194 } else {
195 $this->error( "RENAME FAILED" );
196 $this->rollbackTransaction( $db, __METHOD__ );
197 }
198 }
199 }
200
201 private function appendTitle( $name, $suffix ) {
202 return preg_replace( '/^(.*)(\..*?)$/',
203 "\\1$suffix\\2", $name );
204 }
205
206 private function buildSafeTitle( $name ) {
207 $x = preg_replace_callback(
208 '/([^' . Title::legalChars() . ']|~)/',
209 [ $this, 'hexChar' ],
210 $name );
211
212 $test = Title::makeTitleSafe( NS_FILE, $x );
213 if ( is_null( $test ) || $test->getDBkey() !== $x ) {
214 $this->error( "Unable to generate safe title from '$name', got '$x'" );
215
216 return false;
217 }
218
219 return $x;
220 }
221}
222
223$maintClass = "ImageCleanup";
224require_once RUN_MAINTENANCE_IF_MAIN;
$dir
Definition Autoload.php:8
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Maintenance script to clean up broken, unparseable upload filenames.
pokeFile( $orig, $new)
__construct()
Default constructor.
buildSafeTitle( $name)
appendTitle( $name, $suffix)
imageExists( $name, $db)
pageExists( $name, $db)
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
addDescription( $text)
Set the description text.
rollbackTransaction(IDatabase $dbw, $fname)
Rollback the transcation on a DB handle.
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
Generic class to cleanup a database table.
progress( $updated)
$maintClass
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2581
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
const NS_FILE
Definition Defines.php:71
require_once RUN_MAINTENANCE_IF_MAIN
$source
const DB_MASTER
Definition defines.php:26