Merge "Address gender support issues in page enotif subject and intro"
[lhc/web/wiklou.git] / maintenance / copyFileBackend.php
index 498bc6b..4e3c7fa 100644 (file)
@@ -21,7 +21,7 @@
  * @ingroup Maintenance
  */
 
-require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+require_once( __DIR__ . '/Maintenance.php' );
 
 /**
  * Copy all files in one container of one backend to another.
@@ -45,6 +45,7 @@ class CopyFileBackend extends Maintenance {
                $this->addOption( 'ratefile', 'File to check periodically for batch size', false, true );
                $this->addOption( 'skiphash', 'Skip SHA-1 sync checks for files' );
                $this->addOption( 'missingonly', 'Only copy files missing from destination listing' );
+               $this->addOption( 'utf8only', 'Skip source files that do not have valid UTF-8 names' );
                $this->setBatchSize( 50 );
        }
 
@@ -56,6 +57,10 @@ class CopyFileBackend extends Maintenance {
 
                $rateFile = $this->getOption( 'ratefile' );
 
+               if ( $this->hasOption( 'utf8only' ) && !extension_loaded( 'mbstring' ) ) {
+                       $this->error( "Cannot check for UTF-8, mbstring extension missing.", 1 ); // die
+               }
+
                $count = 0;
                foreach ( $containers as $container ) {
                        if ( $subDir != '' ) {
@@ -129,17 +134,38 @@ class CopyFileBackend extends Maintenance {
                $ops = array();
                $fsFiles = array();
                $copiedRel = array(); // for output message
+
+               // Download the batch of source files into backend cache...
+               if ( $this->hasOption( 'missingonly' ) ) {
+                       $srcPaths = array();
+                       foreach ( $srcPathsRel as $srcPathRel ) {
+                               $srcPaths[] = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
+                       }
+                       $fsFiles = $src->getLocalReferenceMulti( array( 'srcs' => $srcPaths, 'latest' => 1 ) );
+               }
+
+               // Determine what files need to be copied over...
                foreach ( $srcPathsRel as $srcPathRel ) {
                        $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
                        $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
-                       if ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
+                       if ( $this->hasOption( 'utf8only' ) && !mb_check_encoding( $srcPath, 'UTF-8' ) ) {
+                               $this->error( "Detected illegal (non-UTF8) path for $srcPath." );
+                               continue;
+                       } elseif ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
                                $this->output( "Already have $srcPathRel.\n" );
                                continue; // assume already copied...
                        }
-                       // Note: getLocalReference() is fast for FS backends
-                       $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
+                       $fsFile = array_key_exists( $srcPath, $fsFiles )
+                               ? $fsFiles[$srcPath]
+                               : $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
                        if ( !$fsFile ) {
                                $this->error( "Could not get local copy of $srcPath.", 1 ); // die
+                       } elseif ( !$fsFile->exists() ) {
+                               // FSFileBackends just return the path for getLocalReference() and paths with
+                               // illegal slashes may get normalized to a different path. This can cause the
+                               // local reference to not exist...skip these broken files.
+                               $this->error( "Detected possible illegal path for $srcPath." );
+                               continue;
                        }
                        $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
                        // Note: prepare() is usually fast for key/value backends
@@ -153,6 +179,7 @@ class CopyFileBackend extends Maintenance {
                        $copiedRel[] = $srcPathRel;
                }
 
+               // Copy in the batch of source files...
                $t_start = microtime( true );
                $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
                if ( !$status->isOK() ) {