MediaWiki
REL1_21
|
00001 <?php 00024 require_once( __DIR__ . '/Maintenance.php' ); 00025 00037 class CopyFileBackend extends Maintenance { 00038 public function __construct() { 00039 parent::__construct(); 00040 $this->mDescription = "Copy files in one backend to another."; 00041 $this->addOption( 'src', 'Backend containing the source files', true, true ); 00042 $this->addOption( 'dst', 'Backend where files should be copied to', true, true ); 00043 $this->addOption( 'containers', 'Pipe separated list of containers', true, true ); 00044 $this->addOption( 'subdir', 'Only do items in this child directory', false, true ); 00045 $this->addOption( 'ratefile', 'File to check periodically for batch size', false, true ); 00046 $this->addOption( 'skiphash', 'Skip SHA-1 sync checks for files' ); 00047 $this->addOption( 'missingonly', 'Only copy files missing from destination listing' ); 00048 $this->addOption( 'utf8only', 'Skip source files that do not have valid UTF-8 names' ); 00049 $this->setBatchSize( 50 ); 00050 } 00051 00052 public function execute() { 00053 $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) ); 00054 $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) ); 00055 $containers = explode( '|', $this->getOption( 'containers' ) ); 00056 $subDir = $this->getOption( rtrim( 'subdir', '/' ), '' ); 00057 00058 $rateFile = $this->getOption( 'ratefile' ); 00059 00060 if ( $this->hasOption( 'utf8only' ) && !extension_loaded( 'mbstring' ) ) { 00061 $this->error( "Cannot check for UTF-8, mbstring extension missing.", 1 ); // die 00062 } 00063 00064 $count = 0; 00065 foreach ( $containers as $container ) { 00066 if ( $subDir != '' ) { 00067 $backendRel = "$container/$subDir"; 00068 $this->output( "Doing container '$container', directory '$subDir'...\n" ); 00069 } else { 00070 $backendRel = $container; 00071 $this->output( "Doing container '$container'...\n" ); 00072 } 00073 00074 $srcPathsRel = $src->getFileList( array( 00075 'dir' => $src->getRootStoragePath() . "/$backendRel" ) ); 00076 if ( $srcPathsRel === null ) { 00077 $this->error( "Could not list files in $container.", 1 ); // die 00078 } 00079 00080 // Do a listing comparison if specified 00081 if ( $this->hasOption( 'missingonly' ) ) { 00082 $relFilesSrc = array(); 00083 $relFilesDst = array(); 00084 foreach ( $srcPathsRel as $srcPathRel ) { 00085 $relFilesSrc[] = $srcPathRel; 00086 } 00087 $dstPathsRel = $dst->getFileList( array( 00088 'dir' => $dst->getRootStoragePath() . "/$backendRel" ) ); 00089 if ( $dstPathsRel === null ) { 00090 $this->error( "Could not list files in $container.", 1 ); // die 00091 } 00092 foreach ( $dstPathsRel as $dstPathRel ) { 00093 $relFilesDst[] = $dstPathRel; 00094 } 00095 // Only copy the missing files over in the next loop 00096 $srcPathsRel = array_diff( $relFilesSrc, $relFilesDst ); 00097 $this->output( count( $srcPathsRel ) . " file(s) need to be copied.\n" ); 00098 unset( $relFilesSrc ); 00099 unset( $relFilesDst ); 00100 } 00101 00102 $batchPaths = array(); 00103 foreach ( $srcPathsRel as $srcPathRel ) { 00104 // Check up on the rate file periodically to adjust the concurrency 00105 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) { 00106 $this->mBatchSize = max( 1, (int)file_get_contents( $rateFile ) ); 00107 $this->output( "Batch size is now {$this->mBatchSize}.\n" ); 00108 } 00109 $batchPaths[$srcPathRel] = 1; // remove duplicates 00110 if ( count( $batchPaths ) >= $this->mBatchSize ) { 00111 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst ); 00112 $batchPaths = array(); // done 00113 } 00114 ++$count; 00115 } 00116 if ( count( $batchPaths ) ) { // left-overs 00117 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst ); 00118 $batchPaths = array(); // done 00119 } 00120 00121 if ( $subDir != '' ) { 00122 $this->output( "Finished container '$container', directory '$subDir'.\n" ); 00123 } else { 00124 $this->output( "Finished container '$container'.\n" ); 00125 } 00126 } 00127 00128 $this->output( "Done [$count file(s)].\n" ); 00129 } 00130 00131 protected function copyFileBatch( 00132 array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst 00133 ) { 00134 $ops = array(); 00135 $fsFiles = array(); 00136 $copiedRel = array(); // for output message 00137 00138 // Download the batch of source files into backend cache... 00139 if ( $this->hasOption( 'missingonly' ) ) { 00140 $srcPaths = array(); 00141 foreach ( $srcPathsRel as $srcPathRel ) { 00142 $srcPaths[] = $src->getRootStoragePath() . "/$backendRel/$srcPathRel"; 00143 } 00144 $t_start = microtime( true ); 00145 $fsFiles = $src->getLocalReferenceMulti( array( 'srcs' => $srcPaths, 'latest' => 1 ) ); 00146 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 ); 00147 $this->output( "\nDownloaded these file(s) [{$ellapsed_ms}ms]:\n" . 00148 implode( "\n", $srcPaths ) . "\n\n" ); 00149 } 00150 00151 // Determine what files need to be copied over... 00152 foreach ( $srcPathsRel as $srcPathRel ) { 00153 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel"; 00154 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel"; 00155 if ( $this->hasOption( 'utf8only' ) && !mb_check_encoding( $srcPath, 'UTF-8' ) ) { 00156 $this->error( "Detected illegal (non-UTF8) path for $srcPath." ); 00157 continue; 00158 } elseif ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) { 00159 $this->output( "Already have $srcPathRel.\n" ); 00160 continue; // assume already copied... 00161 } 00162 $fsFile = array_key_exists( $srcPath, $fsFiles ) 00163 ? $fsFiles[$srcPath] 00164 : $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) ); 00165 if ( !$fsFile ) { 00166 $this->error( "Could not get local copy of $srcPath.", 1 ); // die 00167 } elseif ( !$fsFile->exists() ) { 00168 // FSFileBackends just return the path for getLocalReference() and paths with 00169 // illegal slashes may get normalized to a different path. This can cause the 00170 // local reference to not exist...skip these broken files. 00171 $this->error( "Detected possible illegal path for $srcPath." ); 00172 continue; 00173 } 00174 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed 00175 // Note: prepare() is usually fast for key/value backends 00176 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ), 'bypassReadOnly' => 1 ) ); 00177 if ( !$status->isOK() ) { 00178 $this->error( print_r( $status->getErrorsArray(), true ) ); 00179 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die 00180 } 00181 $ops[] = array( 'op' => 'store', 00182 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 ); 00183 $copiedRel[] = $srcPathRel; 00184 } 00185 00186 // Copy in the batch of source files... 00187 $t_start = microtime( true ); 00188 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) ); 00189 if ( !$status->isOK() ) { 00190 sleep( 10 ); // wait and retry copy again 00191 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) ); 00192 } 00193 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 ); 00194 if ( !$status->isOK() ) { 00195 $this->error( print_r( $status->getErrorsArray(), true ) ); 00196 $this->error( "Could not copy file batch.", 1 ); // die 00197 } elseif ( count( $copiedRel ) ) { 00198 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" . 00199 implode( "\n", $copiedRel ) . "\n\n" ); 00200 } 00201 } 00202 00203 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) { 00204 $skipHash = $this->hasOption( 'skiphash' ); 00205 return ( 00206 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) ) 00207 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit 00208 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) ) 00209 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit 00210 ) && ( $skipHash || ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) ) 00211 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) ) 00212 ) ) 00213 ); 00214 } 00215 } 00216 00217 $maintClass = 'CopyFileBackend'; 00218 require_once( RUN_MAINTENANCE_IF_MAIN );