From 786db87907beb24da18586d1ae61f1ab6151657b Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 31 Jan 2014 15:32:52 -0800 Subject: [PATCH] Added some lock()/unlock() support for SQLite using lock file emulation Change-Id: Ifd70d06055c74715c52758615c3081dde409c9a6 --- includes/db/DatabaseSqlite.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index fa827d1097..2e23d73dc2 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -41,8 +41,11 @@ class DatabaseSqlite extends DatabaseBase { /** @var PDO */ protected $mConn; + /** @var FSLockManager (hopefully on the same server as the DB) */ + protected $lockMgr; + function __construct( $p = null ) { - global $wgSharedDB; + global $wgSharedDB, $wgSQLiteDataDir; if ( !is_array( $p ) ) { // legacy calling pattern wfDeprecated( __METHOD__ . " method called without parameter array.", "1.22" ); @@ -67,6 +70,8 @@ class DatabaseSqlite extends DatabaseBase { } } } + + $this->lockMgr = new FSLockManager( array( 'lockDirectory' => "$wgSQLiteDataDir/locks" ) ); } /** @@ -866,6 +871,22 @@ class DatabaseSqlite extends DatabaseBase { return $s; } + public function lock( $lockName, $method, $timeout = 5 ) { + global $wgSQLiteDataDir; + + if ( !is_dir( "$wgSQLiteDataDir/locks" ) ) { // create dir as needed + if ( !is_writable( $wgSQLiteDataDir ) || !mkdir( "$wgSQLiteDataDir/locks" ) ) { + throw new DBError( "Cannot create directory \"$wgSQLiteDataDir/locks\"." ); + } + } + + return $this->lockMgr->lock( array( $lockName ), LockManager::LOCK_EX, $timeout )->isOK(); + } + + public function unlock( $lockName, $method ) { + return $this->lockMgr->unlock( array( $lockName ), LockManager::LOCK_EX )->isOK(); + } + /** * Build a concatenation list to feed into a SQL query * -- 2.20.1