MediaWiki  1.28.0
cleanupImages.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/cleanupTable.inc';
29 
35 class ImageCleanup extends TableCleanup {
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 ) {
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 bug 16916.)
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";
224 require_once RUN_MAINTENANCE_IF_MAIN;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
if(count($args)==0) $dir
pageExists($name, $db)
wfMkdirParents($dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
rollbackTransaction(IDatabase $dbw, $fname)
Rollback the transcation on a DB handle.
$source
progress($updated)
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
imageExists($name, $db)
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
const DB_MASTER
Definition: defines.php:23
$maintClass
static decodeCharReferences($text)
Decode any character references, numeric or named entities, in the text and return a UTF-8 string...
Definition: Sanitizer.php:1500
buildSafeTitle($name)
Generic class to cleanup a database table.
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
pokeFile($orig, $new)
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
Maintenance script to clean up broken, unparseable upload filenames.
const NS_FILE
Definition: Defines.php:62
addDescription($text)
Set the description text.
output($out, $channel=null)
Throw some output to the user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error($err, $die=0)
Throw an error to the user.
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:56
static legalChars()
Get a regex character class describing the legal characters in a link.
Definition: Title.php:593
appendTitle($name, $suffix)
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300