From 22ac627fd3a02c2e2c4b53dfecfaf409d8324b10 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 4 Oct 2012 11:43:59 -0700 Subject: [PATCH] Added "posdump" option to syncFileBackend to dump the current position. Change-Id: Iabb027788796e98e06e3c6e97d14f74819cc6c95 --- .../filebackend/filejournal/DBFileJournal.php | 13 +++++++++ .../filebackend/filejournal/FileJournal.php | 27 +++++++++++++++++-- maintenance/syncFileBackend.php | 27 +++++++++++++++++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/includes/filebackend/filejournal/DBFileJournal.php b/includes/filebackend/filejournal/DBFileJournal.php index f6268c258f..34f3e53d50 100644 --- a/includes/filebackend/filejournal/DBFileJournal.php +++ b/includes/filebackend/filejournal/DBFileJournal.php @@ -83,6 +83,19 @@ class DBFileJournal extends FileJournal { return $status; } + /** + * @see FileJournal::doGetCurrentPosition() + * @return integer|false + */ + protected function doGetCurrentPosition() { + $dbw = $this->getMasterDB(); + + return $dbw->selectField( 'filejournal', 'MAX(fj_id)', + array( 'fj_backend' => $this->backend ), + __METHOD__ + ); + } + /** * @see FileJournal::doGetChangeEntries() * @return Array diff --git a/includes/filebackend/filejournal/FileJournal.php b/includes/filebackend/filejournal/FileJournal.php index ce029bbeaf..3bc0df74f5 100644 --- a/includes/filebackend/filejournal/FileJournal.php +++ b/includes/filebackend/filejournal/FileJournal.php @@ -110,6 +110,21 @@ abstract class FileJournal { */ abstract protected function doLogChangeBatch( array $entries, $batchId ); + /** + * Get the position ID of the latest journal entry + * + * @return integer|false + */ + final public function getCurrentPosition() { + return $this->doGetCurrentPosition(); + } + + /** + * @see FileJournal::getCurrentPosition() + * @return integer|false + */ + abstract protected function doGetCurrentPosition(); + /** * Get an array of file change log entries. * A starting change ID and/or limit can be specified. @@ -169,7 +184,7 @@ abstract class FileJournal { */ class NullFileJournal extends FileJournal { /** - * @see FileJournal::logChangeBatch() + * @see FileJournal::doLogChangeBatch() * @param $entries array * @param $batchId string * @return Status @@ -178,6 +193,14 @@ class NullFileJournal extends FileJournal { return Status::newGood(); } + /** + * @see FileJournal::doGetCurrentPosition() + * @return integer|false + */ + protected function doGetCurrentPosition() { + return false; + } + /** * @see FileJournal::doGetChangeEntries() * @return Array @@ -187,7 +210,7 @@ class NullFileJournal extends FileJournal { } /** - * @see FileJournal::purgeOldLogs() + * @see FileJournal::doPurgeOldLogs() * @return Status */ protected function doPurgeOldLogs() { diff --git a/maintenance/syncFileBackend.php b/maintenance/syncFileBackend.php index a29647b50a..e279ccb1f3 100644 --- a/maintenance/syncFileBackend.php +++ b/maintenance/syncFileBackend.php @@ -34,21 +34,44 @@ class SyncFileBackend extends Maintenance { parent::__construct(); $this->mDescription = "Sync one file backend with another using the journal"; $this->addOption( 'src', 'Name of backend to sync from', true, true ); - $this->addOption( 'dst', 'Name of destination backend to sync', true, true ); + $this->addOption( 'dst', 'Name of destination backend to sync', false, true ); $this->addOption( 'start', 'Starting journal ID', false, true ); $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( 'verbose', 'Verbose mode', false, false, 'v' ); $this->setBatchSize( 50 ); } public function execute() { $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) ); - $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) ); $posDir = $this->getOption( 'posdir' ); $posFile = $posDir ? $posDir . '/' . wfWikiID() : false; + if ( $this->hasOption( 'posdump' ) ) { + // Just dump the current position into the specified position dir + if ( !$this->hasOption( 'posdir' ) ) { + $this->error( "Param posdir required!", 1 ); + } + $id = (int)$src->getJournal()->getCurrentPosition(); // default to 0 + $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 { + $this->output( "Could not save journal position file.\n" ); + } + if ( $this->isQuiet() ) { + print $id; // give a single machine-readable number + } + return; + } + + if ( !$this->hasOption( 'dst' ) ) { + $this->error( "Param dst required!", 1 ); + } + $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) ); + $start = $this->getOption( 'start', 0 ); if ( !$start && $posFile && is_dir( $posDir ) ) { $start = is_file( $posFile ) -- 2.20.1