MediaWiki  1.27.3
LockManager.php
Go to the documentation of this file.
1 <?php
45 abstract class LockManager {
47  protected $lockTypeMap = [
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 = [];
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( 5, $config['lockTTL'] );
76  } elseif ( PHP_SAPI === 'cli' ) {
77  $this->lockTTL = 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( [ $type => $paths ], $timeout );
94  }
95 
104  final public function lockByType( array $pathsByType, $timeout = 0 ) {
105  $pathsByType = $this->normalizePathsByType( $pathsByType );
106  $msleep = [ 0, 50, 100, 300, 500 ]; // retry backoff times
107  $start = microtime( true );
108  do {
109  $status = $this->doLockByType( $pathsByType );
110  $elapsed = microtime( true ) - $start;
111  if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) {
112  break; // success, timeout, or clock set back
113  }
114  usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times
115  $elapsed = microtime( true ) - $start;
116  } while ( $elapsed < $timeout && $elapsed >= 0 );
117 
118  return $status;
119  }
120 
128  final public function unlock( array $paths, $type = self::LOCK_EX ) {
129  return $this->unlockByType( [ $type => $paths ] );
130  }
131 
139  final public function unlockByType( array $pathsByType ) {
140  $pathsByType = $this->normalizePathsByType( $pathsByType );
141  $status = $this->doUnlockByType( $pathsByType );
142 
143  return $status;
144  }
145 
154  final protected function sha1Base36Absolute( $path ) {
155  return Wikimedia\base_convert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
156  }
157 
166  final protected function sha1Base16Absolute( $path ) {
167  return sha1( "{$this->domain}:{$path}" );
168  }
169 
178  final protected function normalizePathsByType( array $pathsByType ) {
179  $res = [];
180  foreach ( $pathsByType as $type => $paths ) {
181  $res[$this->lockTypeMap[$type]] = array_unique( $paths );
182  }
183 
184  return $res;
185  }
186 
193  protected function doLockByType( array $pathsByType ) {
195  $lockedByType = []; // map of (type => paths)
196  foreach ( $pathsByType as $type => $paths ) {
197  $status->merge( $this->doLock( $paths, $type ) );
198  if ( $status->isOK() ) {
199  $lockedByType[$type] = $paths;
200  } else {
201  // Release the subset of locks that were acquired
202  foreach ( $lockedByType as $lType => $lPaths ) {
203  $status->merge( $this->doUnlock( $lPaths, $lType ) );
204  }
205  break;
206  }
207  }
208 
209  return $status;
210  }
211 
219  abstract protected function doLock( array $paths, $type );
220 
227  protected function doUnlockByType( array $pathsByType ) {
229  foreach ( $pathsByType as $type => $paths ) {
230  $status->merge( $this->doUnlock( $paths, $type ) );
231  }
232 
233  return $status;
234  }
235 
243  abstract protected function doUnlock( array $paths, $type );
244 }
245 
251  protected function doLock( array $paths, $type ) {
252  return Status::newGood();
253  }
254 
255  protected function doUnlock( array $paths, $type ) {
256  return Status::newGood();
257  }
258 }
doUnlockByType(array $pathsByType)
sha1Base16Absolute($path)
Get the base 16 SHA-1 of a string, padded to 31 digits.
unlockByType(array $pathsByType)
Unlock the resources at the given abstract paths.
sha1Base36Absolute($path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
the array() calling protocol came about after MediaWiki 1.4rc1.
Simple version of LockManager that does nothing.
__construct(array $config)
Construct a new instance from configuration.
Definition: LockManager.php:72
lock(array $paths, $type=self::LOCK_EX, $timeout=0)
Lock the resources at the given abstract paths.
Definition: LockManager.php:92
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
doLock(array $paths, $type)
const LOCK_UW
Definition: LockManager.php:61
const LOCK_EX
Definition: LockManager.php:62
$res
Definition: database.txt:21
array $locksHeld
Map of (resource path => lock type => count)
Definition: LockManager.php:54
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
const LOCK_SH
Lock types; stronger locks have higher values.
Definition: LockManager.php:60
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
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
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
doLockByType(array $pathsByType)
Class for handling resource locking.
Definition: LockManager.php:45
unlock(array $paths, $type=self::LOCK_EX)
Unlock the resources at the given abstract paths.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1008
array $lockTypeMap
Mapping of lock types to the type actually used.
Definition: LockManager.php:47
doUnlock(array $paths, $type)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2342
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101