Added --postime option syncFileBackend.
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 3 Mar 2013 12:45:15 +0000 (04:45 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Sun, 3 Mar 2013 12:45:15 +0000 (04:45 -0800)
* Useful when starting positions are made after copying started.

Change-Id: I34d996594753f7bc4449dac96a4822242c1897ee

includes/filebackend/filejournal/DBFileJournal.php
includes/filebackend/filejournal/FileJournal.php
maintenance/syncFileBackend.php

index 44c6567..73f29a9 100644 (file)
@@ -99,6 +99,22 @@ class DBFileJournal extends FileJournal {
                );
        }
 
+       /**
+        * @see FileJournal::doGetPositionAtTime()
+        * @param $time integer|string timestamp
+        * @return integer|false
+        */
+       protected function doGetPositionAtTime( $time ) {
+               $dbw = $this->getMasterDB();
+
+               $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
+               return $dbw->selectField( 'filejournal', 'fj_id',
+                       array( 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ),
+                       __METHOD__,
+                       array( 'ORDER BY' => 'fj_timestamp DESC' )
+               );
+       }
+
        /**
         * @see FileJournal::doGetChangeEntries()
         * @return Array
index d99384d..b0f39c3 100644 (file)
@@ -125,6 +125,23 @@ abstract class FileJournal {
         */
        abstract protected function doGetCurrentPosition();
 
+       /**
+        * Get the position ID of the latest journal entry at some point in time
+        *
+        * @param $time integer|string timestamp
+        * @return integer|false
+        */
+       final public function getPositionAtTime( $time ) {
+               return $this->doGetPositionAtTime( $time );
+       }
+
+       /**
+        * @see FileJournal::getPositionAtTime()
+        * @param $time integer|string timestamp
+        * @return integer|false
+        */
+       abstract protected function doGetPositionAtTime( $time );
+
        /**
         * Get an array of file change log entries.
         * A starting change ID and/or limit can be specified.
@@ -201,6 +218,15 @@ class NullFileJournal extends FileJournal {
                return false;
        }
 
+       /**
+        * @see FileJournal::doGetPositionAtTime()
+        * @param $time integer|string timestamp
+        * @return integer|false
+        */
+       protected function doGetPositionAtTime( $time ) {
+               return false;
+       }
+
        /**
         * @see FileJournal::doGetChangeEntries()
         * @return Array
index e279ccb..a93ad79 100644 (file)
@@ -39,6 +39,7 @@ class SyncFileBackend extends Maintenance {
                $this->addOption( 'end', 'Ending journal ID', false, true );
                $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( 'verbose', 'Verbose mode', false, false, 'v' );
                $this->setBatchSize( 50 );
        }
@@ -54,7 +55,11 @@ class SyncFileBackend extends Maintenance {
                        if ( !$this->hasOption( 'posdir' ) ) {
                                $this->error( "Param posdir required!", 1 );
                        }
-                       $id = (int)$src->getJournal()->getCurrentPosition(); // default to 0
+                       if ( $this->hasOption( 'postime' ) ) {
+                               $id = (int)$src->getJournal()->getPositionAtTime( $this->getOption( 'postime' ) );
+                       } else {
+                               $id = (int)$src->getJournal()->getCurrentPosition();
+                       }
                        $this->output( "Current journal position is $id.\n" );
                        if ( file_put_contents( $posFile, $id, LOCK_EX ) !== false ) {
                                $this->output( "Saved journal position file.\n" );