]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/cleanupImages.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / maintenance / cleanupImages.php
index 1c0edeb57a28daaf08bd3e0b2b303cedbdc3ca3e..e0da027f7618e3dbe9da8be0b316c291cdc8dbea 100644 (file)
@@ -1,13 +1,9 @@
 <?php
-/*
- * Script to clean up broken, unparseable upload filenames.
+/**
+ * Clean up broken, unparseable upload filenames.
  *
- * Usage: php cleanupImages.php [--fix]
- * Options:
- *   --fix  Actually clean up titles; otherwise just checks for them
- *
- * Copyright (C) 2005-2006 Brion Vibber <brion@pobox.com>
- * http://www.mediawiki.org/
+ * Copyright © 2005-2006 Brion Vibber <brion@pobox.com>
+ * https://www.mediawiki.org/
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
+ * @file
  * @author Brion Vibber <brion at pobox.com>
- * @addtogroup maintenance
+ * @ingroup Maintenance
  */
 
-require_once( 'commandLine.inc' );
-require_once( 'cleanupTable.inc' );
+require_once __DIR__ . '/cleanupTable.inc';
 
+/**
+ * Maintenance script to clean up broken, unparseable upload filenames.
+ *
+ * @ingroup Maintenance
+ */
 class ImageCleanup extends TableCleanup {
-       function __construct( $dryrun = false ) {
-               parent::__construct( 'image', $dryrun );
+       protected $defaultParams = [
+               'table' => 'image',
+               'conds' => [],
+               'index' => 'img_name',
+               'callback' => 'processRow',
+       ];
+
+       public function __construct() {
+               parent::__construct();
+               $this->addDescription( 'Script to clean up broken, unparseable upload filenames' );
        }
 
-       function processPage( $row ) {
+       protected function processRow( $row ) {
                global $wgContLang;
 
                $source = $row->img_name;
-               if( $source == '' ) {
+               if ( $source == '' ) {
                        // Ye olde empty rows. Just kill them.
                        $this->killRow( $source );
+
                        return $this->progress( 1 );
                }
-               
+
                $cleaned = $source;
-               
+
                // About half of old bad image names have percent-codes
                $cleaned = rawurldecode( $cleaned );
-               
+
+               // We also have some HTML entities there
+               $cleaned = Sanitizer::decodeCharReferences( $cleaned );
+
                // Some are old latin-1
                $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
-               
+
                // Many of remainder look like non-normalized unicode
-               $cleaned = UtfNormal::cleanUp( $cleaned );
-               
-               $title = Title::makeTitleSafe( NS_IMAGE, $cleaned );
-               
-               if( is_null( $title ) ) {
-                       $this->log( "page $source ($cleaned) is illegal." );
+               $cleaned = $wgContLang->normalize( $cleaned );
+
+               $title = Title::makeTitleSafe( NS_FILE, $cleaned );
+
+               if ( is_null( $title ) ) {
+                       $this->output( "page $source ($cleaned) is illegal.\n" );
                        $safe = $this->buildSafeTitle( $cleaned );
+                       if ( $safe === false ) {
+                               return $this->progress( 0 );
+                       }
                        $this->pokeFile( $source, $safe );
+
                        return $this->progress( 1 );
                }
 
-               if( $title->getDbKey() !== $source ) {
-                       $munged = $title->getDbKey();
-                       $this->log( "page $source ($munged) doesn't match self." );
+               if ( $title->getDBkey() !== $source ) {
+                       $munged = $title->getDBkey();
+                       $this->output( "page $source ($munged) doesn't match self.\n" );
                        $this->pokeFile( $source, $munged );
+
                        return $this->progress( 1 );
                }
 
-               $this->progress( 0 );
+               return $this->progress( 0 );
        }
-       
-       function killRow( $name ) {
-               if( $this->dryrun ) {
-                       $this->log( "DRY RUN: would delete bogus row '$name'" );
+
+       /**
+        * @param string $name
+        */
+       private function killRow( $name ) {
+               if ( $this->dryrun ) {
+                       $this->output( "DRY RUN: would delete bogus row '$name'\n" );
                } else {
-                       $this->log( "deleting bogus row '$name'" );
-                       $db = wfGetDB( DB_MASTER );
+                       $this->output( "deleting bogus row '$name'\n" );
+                       $db = $this->getDB( DB_MASTER );
                        $db->delete( 'image',
-                               array( 'img_name' => $name ),
+                               [ 'img_name' => $name ],
                                __METHOD__ );
                }
        }
-       
-       function filePath( $name ) {
+
+       private function filePath( $name ) {
                if ( !isset( $this->repo ) ) {
                        $this->repo = RepoGroup::singleton()->getLocalRepo();
                }
+
                return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
        }
-       
-       function pokeFile( $orig, $new ) {
+
+       private function imageExists( $name, $db ) {
+               return $db->selectField( 'image', '1', [ 'img_name' => $name ], __METHOD__ );
+       }
+
+       private function pageExists( $name, $db ) {
+               return $db->selectField(
+                       'page',
+                       '1',
+                       [ 'page_namespace' => NS_FILE, 'page_title' => $name ],
+                       __METHOD__
+               );
+       }
+
+       private function pokeFile( $orig, $new ) {
                $path = $this->filePath( $orig );
-               if( !file_exists( $path ) ) {
-                       $this->log( "missing file: $path" );
-                       return $this->killRow( $orig );
+               if ( !file_exists( $path ) ) {
+                       $this->output( "missing file: $path\n" );
+                       $this->killRow( $orig );
+
+                       return;
                }
-               
-               $db = wfGetDB( DB_MASTER );
+
+               $db = $this->getDB( DB_MASTER );
+
+               /*
+                * To prevent key collisions in the update() statements below,
+                * if the target title exists in the image table, or if both the
+                * original and target titles exist in the page table, append
+                * increasing version numbers until the target title exists in
+                * neither.  (See also T18916.)
+                */
                $version = 0;
                $final = $new;
-               
-               while( $db->selectField( 'image', 'img_name',
-                       array( 'img_name' => $final ), __METHOD__ ) ) {
-                       $this->log( "Rename conflicts with '$final'..." );
+               $conflict = ( $this->imageExists( $final, $db ) ||
+                       ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
+
+               while ( $conflict ) {
+                       $this->output( "Rename conflicts with '$final'...\n" );
                        $version++;
                        $final = $this->appendTitle( $new, "_$version" );
+                       $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
                }
-               
+
                $finalPath = $this->filePath( $final );
-               
-               if( $this->dryrun ) {
-                       $this->log( "DRY RUN: would rename $path to $finalPath" );
+
+               if ( $this->dryrun ) {
+                       $this->output( "DRY RUN: would rename $path to $finalPath\n" );
                } else {
-                       $this->log( "renaming $path to $finalPath" );
-                       $db->begin();
+                       $this->output( "renaming $path to $finalPath\n" );
+                       // @todo FIXME: Should this use File::move()?
+                       $this->beginTransaction( $db, __METHOD__ );
                        $db->update( 'image',
-                               array( 'img_name' => $final ),
-                               array( 'img_name' => $orig ),
+                               [ 'img_name' => $final ],
+                               [ 'img_name' => $orig ],
+                               __METHOD__ );
+                       $db->update( 'oldimage',
+                               [ 'oi_name' => $final ],
+                               [ 'oi_name' => $orig ],
+                               __METHOD__ );
+                       $db->update( 'page',
+                               [ 'page_title' => $final ],
+                               [ 'page_title' => $orig, 'page_namespace' => NS_FILE ],
                                __METHOD__ );
                        $dir = dirname( $finalPath );
-                       if( !file_exists( $dir ) ) {
-                               if( !mkdir( $dir, 0777, true ) ) {
-                                       $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
-                                       $db->rollback();
+                       if ( !file_exists( $dir ) ) {
+                               if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
+                                       $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
+                                       $this->rollbackTransaction( $db, __METHOD__ );
+
                                        return;
                                }
                        }
-                       if( rename( $path, $finalPath ) ) {
-                               $db->commit();
+                       if ( rename( $path, $finalPath ) ) {
+                               $this->commitTransaction( $db, __METHOD__ );
                        } else {
-                               $this->log( "RENAME FAILED" );
-                               $db->rollback();
+                               $this->error( "RENAME FAILED" );
+                               $this->rollbackTransaction( $db, __METHOD__ );
                        }
                }
        }
-       
-       function appendTitle( $name, $suffix ) {
+
+       private function appendTitle( $name, $suffix ) {
                return preg_replace( '/^(.*)(\..*?)$/',
                        "\\1$suffix\\2", $name );
        }
-       
-       function buildSafeTitle( $name ) {
-               global $wgLegalTitleChars;
+
+       private function buildSafeTitle( $name ) {
                $x = preg_replace_callback(
-                       "/([^$wgLegalTitleChars])/",
-                       array( $this, 'hexChar' ),
+                       '/([^' . Title::legalChars() . ']|~)/',
+                       [ $this, 'hexChar' ],
                        $name );
-               
-               $test = Title::makeTitleSafe( NS_IMAGE, $x );
-               if( is_null( $test ) || $test->getDbKey() !== $x ) {
-                       $this->log( "Unable to generate safe title from '$name', got '$x'" );
+
+               $test = Title::makeTitleSafe( NS_FILE, $x );
+               if ( is_null( $test ) || $test->getDBkey() !== $x ) {
+                       $this->error( "Unable to generate safe title from '$name', got '$x'" );
+
                        return false;
                }
-               
+
                return $x;
        }
 }
 
-$wgUser->setName( 'Conversion script' );
-$caps = new ImageCleanup( !isset( $options['fix'] ) );
-$caps->cleanup();
-
-
+$maintClass = "ImageCleanup";
+require_once RUN_MAINTENANCE_IF_MAIN;