Merge "Hide the caption of frameless or inline images in Parsoid HTML"
[lhc/web/wiklou.git] / maintenance / syncFileBackend.php
index a93ad79..b9493cd 100644 (file)
@@ -21,7 +21,7 @@
  * @ingroup Maintenance
  */
 
-require_once( __DIR__ . '/Maintenance.php' );
+require_once __DIR__ . '/Maintenance.php';
 
 /**
  * Maintenance script that syncs one file backend to another based on
@@ -40,6 +40,7 @@ class SyncFileBackend extends Maintenance {
                $this->addOption( 'posdir', 'Directory to read/record journal positions', false, true );
                $this->addOption( 'posdump', 'Just dump current journal position into the position dir.' );
                $this->addOption( 'postime', 'For position dumps, get the ID at this time', false, true );
+               $this->addOption( 'backoff', 'Stop at entries younger than this age (sec).', false, true );
                $this->addOption( 'verbose', 'Verbose mode', false, false, 'v' );
                $this->setBatchSize( 50 );
        }
@@ -57,10 +58,11 @@ class SyncFileBackend extends Maintenance {
                        }
                        if ( $this->hasOption( 'postime' ) ) {
                                $id = (int)$src->getJournal()->getPositionAtTime( $this->getOption( 'postime' ) );
+                               $this->output( "Requested journal position is $id.\n" );
                        } else {
                                $id = (int)$src->getJournal()->getCurrentPosition();
+                               $this->output( "Current journal position is $id.\n" );
                        }
-                       $this->output( "Current journal position is $id.\n" );
                        if ( file_put_contents( $posFile, $id, LOCK_EX ) !== false ) {
                                $this->output( "Saved journal position file.\n" );
                        } else {
@@ -69,6 +71,7 @@ class SyncFileBackend extends Maintenance {
                        if ( $this->isQuiet() ) {
                                print $id; // give a single machine-readable number
                        }
+
                        return;
                }
 
@@ -87,7 +90,13 @@ class SyncFileBackend extends Maintenance {
                } else {
                        $startFromPosFile = false;
                }
-               $end = $this->getOption( 'end', INF );
+
+               if ( $this->hasOption( 'backoff' ) ) {
+                       $time = time() - $this->getOption( 'backoff', 0 );
+                       $end = (int)$src->getJournal()->getPositionAtTime( $time );
+               } else {
+                       $end = $this->getOption( 'end', INF );
+               }
 
                $this->output( "Synchronizing backend '{$dst->getName()}' to '{$src->getName()}'...\n" );
                $this->output( "Starting journal position is $start.\n" );
@@ -95,8 +104,15 @@ class SyncFileBackend extends Maintenance {
                        $this->output( "Ending journal position is $end.\n" );
                }
 
+               // Periodically update the position file
+               $callback = function ( $pos ) use ( $startFromPosFile, $posFile, $start ) {
+                       if ( $startFromPosFile && $pos >= $start ) { // successfully advanced
+                               file_put_contents( $posFile, $pos, LOCK_EX );
+                       }
+               };
+
                // Actually sync the dest backend with the reference backend
-               $lastOKPos = $this->syncBackends( $src, $dst, $start, $end );
+               $lastOKPos = $this->syncBackends( $src, $dst, $start, $end, $callback );
 
                // Update the sync position file
                if ( $startFromPosFile && $lastOKPos >= $start ) { // successfully advanced
@@ -126,13 +142,16 @@ class SyncFileBackend extends Maintenance {
         * Sync $dst backend to $src backend based on the $src logs given after $start.
         * Returns the journal entry ID this advanced to and handled (inclusive).
         *
-        * @param $src FileBackend
-        * @param $dst FileBackend
-        * @param $start integer Starting journal position
-        * @param $end integer Starting journal position
-        * @return integer|false Journal entry ID or false if there are none
+        * @param FileBackend $src
+        * @param FileBackend $dst
+        * @param int $start Starting journal position
+        * @param int $end Starting journal position
+        * @param Closure $callback Callback to update any position file
+        * @return int|bool Journal entry ID or false if there are none
         */
-       protected function syncBackends( FileBackend $src, FileBackend $dst, $start, $end ) {
+       protected function syncBackends(
+               FileBackend $src, FileBackend $dst, $start, $end, Closure $callback
+       ) {
                $lastOKPos = 0; // failed
                $first = true; // first batch
 
@@ -163,6 +182,7 @@ class SyncFileBackend extends Maintenance {
                        $status = $this->syncFileBatch( array_keys( $pathsInBatch ), $src, $dst );
                        if ( $status->isOK() ) {
                                $lastOKPos = max( $lastOKPos, $lastPosInBatch );
+                               $callback( $lastOKPos ); // update position file
                        } else {
                                $this->error( print_r( $status->getErrorsArray(), true ) );
                                break; // no gaps; everything up to $lastPos must be OK
@@ -179,9 +199,9 @@ class SyncFileBackend extends Maintenance {
        /**
         * Sync particular files of backend $src to the corresponding $dst backend files
         *
-        * @param $paths Array
-        * @param $src FileBackend
-        * @param $dst FileBackend
+        * @param array $paths
+        * @param FileBackend $src
+        * @param FileBackend $dst
         * @return Status
         */
        protected function syncFileBatch( array $paths, FileBackend $src, FileBackend $dst ) {
@@ -202,6 +222,9 @@ class SyncFileBackend extends Maintenance {
                        return $status;
                }
 
+               $src->preloadFileStat( array( 'srcs' => $sPaths, 'latest' => 1 ) );
+               $dst->preloadFileStat( array( 'srcs' => $dPaths, 'latest' => 1 ) );
+
                $ops = array();
                $fsFiles = array();
                foreach ( $sPaths as $i => $sPath ) {
@@ -216,6 +239,7 @@ class SyncFileBackend extends Maintenance {
                                if ( !$fsFile ) {
                                        $this->error( "Unable to sync '$dPath': could not get local copy." );
                                        $status->fatal( 'backend-fail-internal', $src->getName() );
+
                                        return $status;
                                }
                                $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
@@ -232,6 +256,7 @@ class SyncFileBackend extends Maintenance {
                        } else { // error
                                $this->error( "Unable to sync '$dPath': could not stat file." );
                                $status->fatal( 'backend-fail-internal', $src->getName() );
+
                                return $status;
                        }
                }
@@ -254,8 +279,8 @@ class SyncFileBackend extends Maintenance {
        /**
         * Substitute the backend name of storage paths with that of a given one
         *
-        * @param $paths Array|string List of paths or single string path
-        * @return Array|string
+        * @param array|string $paths List of paths or single string path
+        * @return array|string
         */
        protected function replaceNamePaths( $paths, FileBackend $backend ) {
                return preg_replace(
@@ -277,4 +302,4 @@ class SyncFileBackend extends Maintenance {
 }
 
 $maintClass = "SyncFileBackend";
-require_once( RUN_MAINTENANCE_IF_MAIN );
+require_once RUN_MAINTENANCE_IF_MAIN;