MediaWiki
master
|
00001 <?php 00024 require_once( __DIR__ . '/Maintenance.php' ); 00025 00032 class SyncFileBackend extends Maintenance { 00033 public function __construct() { 00034 parent::__construct(); 00035 $this->mDescription = "Sync one file backend with another using the journal"; 00036 $this->addOption( 'src', 'Name of backend to sync from', true, true ); 00037 $this->addOption( 'dst', 'Name of destination backend to sync', false, true ); 00038 $this->addOption( 'start', 'Starting journal ID', false, true ); 00039 $this->addOption( 'end', 'Ending journal ID', false, true ); 00040 $this->addOption( 'posdir', 'Directory to read/record journal positions', false, true ); 00041 $this->addOption( 'posdump', 'Just dump current journal position into the position dir.' ); 00042 $this->addOption( 'verbose', 'Verbose mode', false, false, 'v' ); 00043 $this->setBatchSize( 50 ); 00044 } 00045 00046 public function execute() { 00047 $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) ); 00048 00049 $posDir = $this->getOption( 'posdir' ); 00050 $posFile = $posDir ? $posDir . '/' . wfWikiID() : false; 00051 00052 if ( $this->hasOption( 'posdump' ) ) { 00053 // Just dump the current position into the specified position dir 00054 if ( !$this->hasOption( 'posdir' ) ) { 00055 $this->error( "Param posdir required!", 1 ); 00056 } 00057 $id = (int)$src->getJournal()->getCurrentPosition(); // default to 0 00058 $this->output( "Current journal position is $id.\n" ); 00059 if ( file_put_contents( $posFile, $id, LOCK_EX ) !== false ) { 00060 $this->output( "Saved journal position file.\n" ); 00061 } else { 00062 $this->output( "Could not save journal position file.\n" ); 00063 } 00064 if ( $this->isQuiet() ) { 00065 print $id; // give a single machine-readable number 00066 } 00067 return; 00068 } 00069 00070 if ( !$this->hasOption( 'dst' ) ) { 00071 $this->error( "Param dst required!", 1 ); 00072 } 00073 $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) ); 00074 00075 $start = $this->getOption( 'start', 0 ); 00076 if ( !$start && $posFile && is_dir( $posDir ) ) { 00077 $start = is_file( $posFile ) 00078 ? (int)trim( file_get_contents( $posFile ) ) 00079 : 0; 00080 ++$start; // we already did this ID, start with the next one 00081 $startFromPosFile = true; 00082 } else { 00083 $startFromPosFile = false; 00084 } 00085 $end = $this->getOption( 'end', INF ); 00086 00087 $this->output( "Synchronizing backend '{$dst->getName()}' to '{$src->getName()}'...\n" ); 00088 $this->output( "Starting journal position is $start.\n" ); 00089 if ( is_finite( $end ) ) { 00090 $this->output( "Ending journal position is $end.\n" ); 00091 } 00092 00093 // Actually sync the dest backend with the reference backend 00094 $lastOKPos = $this->syncBackends( $src, $dst, $start, $end ); 00095 00096 // Update the sync position file 00097 if ( $startFromPosFile && $lastOKPos >= $start ) { // successfully advanced 00098 if ( file_put_contents( $posFile, $lastOKPos, LOCK_EX ) !== false ) { 00099 $this->output( "Updated journal position file.\n" ); 00100 } else { 00101 $this->output( "Could not update journal position file.\n" ); 00102 } 00103 } 00104 00105 if ( $lastOKPos === false ) { 00106 if ( !$start ) { 00107 $this->output( "No journal entries found.\n" ); 00108 } else { 00109 $this->output( "No new journal entries found.\n" ); 00110 } 00111 } else { 00112 $this->output( "Stopped synchronization at journal position $lastOKPos.\n" ); 00113 } 00114 00115 if ( $this->isQuiet() ) { 00116 print $lastOKPos; // give a single machine-readable number 00117 } 00118 } 00119 00130 protected function syncBackends( FileBackend $src, FileBackend $dst, $start, $end ) { 00131 $lastOKPos = 0; // failed 00132 $first = true; // first batch 00133 00134 if ( $start > $end ) { // sanity 00135 $this->error( "Error: given starting ID greater than ending ID.", 1 ); 00136 } 00137 00138 do { 00139 $limit = min( $this->mBatchSize, $end - $start + 1 ); // don't go pass ending ID 00140 $this->output( "Doing id $start to " . ( $start + $limit - 1 ) . "...\n" ); 00141 00142 $entries = $src->getJournal()->getChangeEntries( $start, $limit, $next ); 00143 $start = $next; // start where we left off next time 00144 if ( $first && !count( $entries ) ) { 00145 return false; // nothing to do 00146 } 00147 $first = false; 00148 00149 $lastPosInBatch = 0; 00150 $pathsInBatch = array(); // changed paths 00151 foreach ( $entries as $entry ) { 00152 if ( $entry['op'] !== 'null' ) { // null ops are just for reference 00153 $pathsInBatch[$entry['path']] = 1; // remove duplicates 00154 } 00155 $lastPosInBatch = $entry['id']; 00156 } 00157 00158 $status = $this->syncFileBatch( array_keys( $pathsInBatch ), $src, $dst ); 00159 if ( $status->isOK() ) { 00160 $lastOKPos = max( $lastOKPos, $lastPosInBatch ); 00161 } else { 00162 $this->error( print_r( $status->getErrorsArray(), true ) ); 00163 break; // no gaps; everything up to $lastPos must be OK 00164 } 00165 00166 if ( !$start ) { 00167 $this->output( "End of journal entries.\n" ); 00168 } 00169 } while ( $start && $start <= $end ); 00170 00171 return $lastOKPos; 00172 } 00173 00182 protected function syncFileBatch( array $paths, FileBackend $src, FileBackend $dst ) { 00183 $status = Status::newGood(); 00184 if ( !count( $paths ) ) { 00185 return $status; // nothing to do 00186 } 00187 00188 // Source: convert internal backend names (FileBackendMultiWrite) to the public one 00189 $sPaths = $this->replaceNamePaths( $paths, $src ); 00190 // Destination: get corresponding path name 00191 $dPaths = $this->replaceNamePaths( $paths, $dst ); 00192 00193 // Lock the live backend paths from modification 00194 $sLock = $src->getScopedFileLocks( $sPaths, LockManager::LOCK_UW, $status ); 00195 $eLock = $dst->getScopedFileLocks( $dPaths, LockManager::LOCK_EX, $status ); 00196 if ( !$status->isOK() ) { 00197 return $status; 00198 } 00199 00200 $ops = array(); 00201 $fsFiles = array(); 00202 foreach ( $sPaths as $i => $sPath ) { 00203 $dPath = $dPaths[$i]; // destination 00204 $sExists = $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) ); 00205 if ( $sExists === true ) { // exists in source 00206 if ( $this->filesAreSame( $src, $dst, $sPath, $dPath ) ) { 00207 continue; // avoid local copies for non-FS backends 00208 } 00209 // Note: getLocalReference() is fast for FS backends 00210 $fsFile = $src->getLocalReference( array( 'src' => $sPath, 'latest' => 1 ) ); 00211 if ( !$fsFile ) { 00212 $this->error( "Unable to sync '$dPath': could not get local copy." ); 00213 $status->fatal( 'backend-fail-internal', $src->getName() ); 00214 return $status; 00215 } 00216 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed 00217 // Note: prepare() is usually fast for key/value backends 00218 $status->merge( $dst->prepare( array( 00219 'dir' => dirname( $dPath ), 'bypassReadOnly' => 1 ) ) ); 00220 if ( !$status->isOK() ) { 00221 return $status; 00222 } 00223 $ops[] = array( 'op' => 'store', 00224 'src' => $fsFile->getPath(), 'dst' => $dPath, 'overwrite' => 1 ); 00225 } elseif ( $sExists === false ) { // does not exist in source 00226 $ops[] = array( 'op' => 'delete', 'src' => $dPath, 'ignoreMissingSource' => 1 ); 00227 } else { // error 00228 $this->error( "Unable to sync '$dPath': could not stat file." ); 00229 $status->fatal( 'backend-fail-internal', $src->getName() ); 00230 return $status; 00231 } 00232 } 00233 00234 $t_start = microtime( true ); 00235 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) ); 00236 if ( !$status->isOK() ) { 00237 sleep( 10 ); // wait and retry copy again 00238 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) ); 00239 } 00240 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 ); 00241 if ( $status->isOK() && $this->getOption( 'verbose' ) ) { 00242 $this->output( "Synchronized these file(s) [{$ellapsed_ms}ms]:\n" . 00243 implode( "\n", $dPaths ) . "\n" ); 00244 } 00245 00246 return $status; 00247 } 00248 00255 protected function replaceNamePaths( $paths, FileBackend $backend ) { 00256 return preg_replace( 00257 '!^mwstore://([^/]+)!', 00258 StringUtils::escapeRegexReplacement( "mwstore://" . $backend->getName() ), 00259 $paths // string or array 00260 ); 00261 } 00262 00263 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) { 00264 return ( 00265 ( $src->getFileSize( array( 'src' => $sPath ) ) 00266 === $dst->getFileSize( array( 'src' => $dPath ) ) // short-circuit 00267 ) && ( $src->getFileSha1Base36( array( 'src' => $sPath ) ) 00268 === $dst->getFileSha1Base36( array( 'src' => $dPath ) ) 00269 ) 00270 ); 00271 } 00272 } 00273 00274 $maintClass = "SyncFileBackend"; 00275 require_once( RUN_MAINTENANCE_IF_MAIN );