MediaWiki  1.23.1
LockManager.php
Go to the documentation of this file.
1 <?php
45 abstract class LockManager {
47  protected $lockTypeMap = array(
48  self::LOCK_SH => self::LOCK_SH,
49  self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH
50  self::LOCK_EX => self::LOCK_EX
51  );
52 
54  protected $locksHeld = array();
55 
56  protected $domain; // string; domain (usually wiki ID)
57  protected $lockTTL; // integer; maximum time locks can be held
58 
60  const LOCK_SH = 1; // shared lock (for reads)
61  const LOCK_UW = 2; // shared lock (for reads used to write elsewhere)
62  const LOCK_EX = 3; // exclusive lock (for writes)
63 
72  public function __construct( array $config ) {
73  $this->domain = isset( $config['domain'] ) ? $config['domain'] : wfWikiID();
74  if ( isset( $config['lockTTL'] ) ) {
75  $this->lockTTL = max( 1, $config['lockTTL'] );
76  } elseif ( PHP_SAPI === 'cli' ) {
77  $this->lockTTL = 2 * 3600;
78  } else {
79  $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
80  $this->lockTTL = max( 5 * 60, 2 * (int)$met );
81  }
82  }
83 
92  final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
93  return $this->lockByType( array( $type => $paths ), $timeout );
94  }
95 
104  final public function lockByType( array $pathsByType, $timeout = 0 ) {
105  wfProfileIn( __METHOD__ );
106  $status = Status::newGood();
107  $pathsByType = $this->normalizePathsByType( $pathsByType );
108  $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times
109  $start = microtime( true );
110  do {
111  $status = $this->doLockByType( $pathsByType );
112  $elapsed = microtime( true ) - $start;
113  if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) {
114  break; // success, timeout, or clock set back
115  }
116  usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times
117  $elapsed = microtime( true ) - $start;
118  } while ( $elapsed < $timeout && $elapsed >= 0 );
119  wfProfileOut( __METHOD__ );
120 
121  return $status;
122  }
123 
131  final public function unlock( array $paths, $type = self::LOCK_EX ) {
132  return $this->unlockByType( array( $type => $paths ) );
133  }
134 
142  final public function unlockByType( array $pathsByType ) {
143  wfProfileIn( __METHOD__ );
144  $pathsByType = $this->normalizePathsByType( $pathsByType );
145  $status = $this->doUnlockByType( $pathsByType );
146  wfProfileOut( __METHOD__ );
147 
148  return $status;
149  }
150 
159  final protected function sha1Base36Absolute( $path ) {
160  return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
161  }
162 
171  final protected function sha1Base16Absolute( $path ) {
172  return sha1( "{$this->domain}:{$path}" );
173  }
174 
183  final protected function normalizePathsByType( array $pathsByType ) {
184  $res = array();
185  foreach ( $pathsByType as $type => $paths ) {
186  $res[$this->lockTypeMap[$type]] = array_unique( $paths );
187  }
188 
189  return $res;
190  }
191 
198  protected function doLockByType( array $pathsByType ) {
199  $status = Status::newGood();
200  $lockedByType = array(); // map of (type => paths)
201  foreach ( $pathsByType as $type => $paths ) {
202  $status->merge( $this->doLock( $paths, $type ) );
203  if ( $status->isOK() ) {
204  $lockedByType[$type] = $paths;
205  } else {
206  // Release the subset of locks that were acquired
207  foreach ( $lockedByType as $lType => $lPaths ) {
208  $status->merge( $this->doUnlock( $lPaths, $lType ) );
209  }
210  break;
211  }
212  }
213 
214  return $status;
215  }
216 
224  abstract protected function doLock( array $paths, $type );
225 
232  protected function doUnlockByType( array $pathsByType ) {
233  $status = Status::newGood();
234  foreach ( $pathsByType as $type => $paths ) {
235  $status->merge( $this->doUnlock( $paths, $type ) );
236  }
237 
238  return $status;
239  }
240 
248  abstract protected function doUnlock( array $paths, $type );
249 }
250 
255 class NullLockManager extends LockManager {
256  protected function doLock( array $paths, $type ) {
257  return Status::newGood();
258  }
259 
260  protected function doUnlock( array $paths, $type ) {
261  return Status::newGood();
262  }
263 }
LockManager
Class for handling resource locking.
Definition: LockManager.php:45
NullLockManager\doLock
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
Definition: LockManager.php:254
LockManager\$locksHeld
array $locksHeld
Map of (resource path => lock type => count) *.
Definition: LockManager.php:52
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LockManager\LOCK_SH
const LOCK_SH
Lock types; stronger locks have higher values.
Definition: LockManager.php:58
LockManager\sha1Base16Absolute
sha1Base16Absolute( $path)
Get the base 16 SHA-1 of a string, padded to 31 digits.
Definition: LockManager.php:169
LockManager\LOCK_UW
const LOCK_UW
Definition: LockManager.php:59
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
Status\newGood
static newGood( $value=null)
Factory function for good results.
Definition: Status.php:77
NullLockManager\doUnlock
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
Definition: LockManager.php:258
LockManager\$lockTypeMap
array $lockTypeMap
Mapping of lock types to the type actually used *.
Definition: LockManager.php:46
LockManager\normalizePathsByType
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
Definition: LockManager.php:181
LockManager\$lockTTL
$lockTTL
Definition: LockManager.php:55
LockManager\sha1Base36Absolute
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
Definition: LockManager.php:157
NullLockManager
Simple version of LockManager that does nothing.
Definition: LockManager.php:253
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
LockManager\doUnlockByType
doUnlockByType(array $pathsByType)
Definition: LockManager.php:230
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
LockManager\unlock
unlock(array $paths, $type=self::LOCK_EX)
Unlock the resources at the given abstract paths.
Definition: LockManager.php:129
LockManager\lock
lock(array $paths, $type=self::LOCK_EX, $timeout=0)
Lock the resources at the given abstract paths.
Definition: LockManager.php:90
LockManager\unlockByType
unlockByType(array $pathsByType)
Unlock the resources at the given abstract paths.
Definition: LockManager.php:140
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3604
LockManager\doUnlock
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
wfBaseConvert
wfBaseConvert( $input, $sourceBase, $destBase, $pad=1, $lowercase=true, $engine='auto')
Convert an arbitrarily-long digit string from one numeric base to another, optionally zero-padding to...
Definition: GlobalFunctions.php:3368
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
LockManager\__construct
__construct(array $config)
Construct a new instance from configuration.
Definition: LockManager.php:70
LockManager\LOCK_EX
const LOCK_EX
Definition: LockManager.php:60
LockManager\doLock
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
$res
$res
Definition: database.txt:21
LockManager\doLockByType
doLockByType(array $pathsByType)
Definition: LockManager.php:196
LockManager\lockByType
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
Definition: LockManager.php:102
LockManager\$domain
$domain
Definition: LockManager.php:54
$type
$type
Definition: testCompression.php:46