MediaWiki
REL1_20
|
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 foreach ( $srcPathsRel as $srcPathRel ) { 00138 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel"; 00139 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel"; 00140 if ( $this->hasOption( 'utf8only' ) && !mb_check_encoding( $srcPath, 'UTF-8' ) ) { 00141 $this->error( "Detected illegal (non-UTF8) path for $srcPath." ); 00142 continue; 00143 } elseif ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) { 00144 $this->output( "Already have $srcPathRel.\n" ); 00145 continue; // assume already copied... 00146 } 00147 // Note: getLocalReference() is fast for FS backends 00148 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) ); 00149 if ( !$fsFile ) { 00150 $this->error( "Could not get local copy of $srcPath.", 1 ); // die 00151 } elseif ( !$fsFile->exists() ) { 00152 // FSFileBackends just return the path for getLocalReference() and paths with 00153 // illegal slashes may get normalized to a different path. This can cause the 00154 // local reference to not exist...skip these broken files. 00155 $this->error( "Detected possible illegal path for $srcPath." ); 00156 continue; 00157 } 00158 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed 00159 // Note: prepare() is usually fast for key/value backends 00160 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ), 'bypassReadOnly' => 1 ) ); 00161 if ( !$status->isOK() ) { 00162 $this->error( print_r( $status->getErrorsArray(), true ) ); 00163 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die 00164 } 00165 $ops[] = array( 'op' => 'store', 00166 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 ); 00167 $copiedRel[] = $srcPathRel; 00168 } 00169 00170 $t_start = microtime( true ); 00171 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) ); 00172 if ( !$status->isOK() ) { 00173 sleep( 10 ); // wait and retry copy again 00174 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) ); 00175 } 00176 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 ); 00177 if ( !$status->isOK() ) { 00178 $this->error( print_r( $status->getErrorsArray(), true ) ); 00179 $this->error( "Could not copy file batch.", 1 ); // die 00180 } elseif ( count( $copiedRel ) ) { 00181 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" . 00182 implode( "\n", $copiedRel ) . "\n\n" ); 00183 } 00184 } 00185 00186 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) { 00187 $skipHash = $this->hasOption( 'skiphash' ); 00188 return ( 00189 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) ) 00190 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit 00191 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) ) 00192 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit 00193 ) && ( $skipHash || ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) ) 00194 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) ) 00195 ) ) 00196 ); 00197 } 00198 } 00199 00200 $maintClass = 'CopyFileBackend'; 00201 require_once( RUN_MAINTENANCE_IF_MAIN );