MediaWiki  1.29.2
LockManager.php
Go to the documentation of this file.
1 <?php
6 use Psr\Log\LoggerInterface;
7 use Wikimedia\WaitConditionLoop;
8 
47 abstract class LockManager {
49  protected $logger;
50 
52  protected $lockTypeMap = [
53  self::LOCK_SH => self::LOCK_SH,
54  self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH
55  self::LOCK_EX => self::LOCK_EX
56  ];
57 
59  protected $locksHeld = [];
60 
61  protected $domain; // string; domain (usually wiki ID)
62  protected $lockTTL; // integer; maximum time locks can be held
63 
65  protected $session;
66 
68  const LOCK_SH = 1; // shared lock (for reads)
69  const LOCK_UW = 2; // shared lock (for reads used to write elsewhere)
70  const LOCK_EX = 3; // exclusive lock (for writes)
71 
73  const MAX_LOCK_TTL = 7200; // 2 hours
74 
83  public function __construct( array $config ) {
84  $this->domain = isset( $config['domain'] ) ? $config['domain'] : 'global';
85  if ( isset( $config['lockTTL'] ) ) {
86  $this->lockTTL = max( 5, $config['lockTTL'] );
87  } elseif ( PHP_SAPI === 'cli' ) {
88  $this->lockTTL = 3600;
89  } else {
90  $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
91  $this->lockTTL = max( 5 * 60, 2 * (int)$met );
92  }
93 
94  // Upper bound on how long to keep lock structures around. This is useful when setting
95  // TTLs, as the "lockTTL" value may vary based on CLI mode and app server group. This is
96  // a "safe" value that can be used to avoid clobbering other locks that use high TTLs.
97  $this->lockTTL = min( $this->lockTTL, self::MAX_LOCK_TTL );
98 
99  $random = [];
100  for ( $i = 1; $i <= 5; ++$i ) {
101  $random[] = mt_rand( 0, 0xFFFFFFF );
102  }
103  $this->session = md5( implode( '-', $random ) );
104 
105  $this->logger = isset( $config['logger'] ) ? $config['logger'] : new \Psr\Log\NullLogger();
106  }
107 
116  final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
117  return $this->lockByType( [ $type => $paths ], $timeout );
118  }
119 
128  final public function lockByType( array $pathsByType, $timeout = 0 ) {
129  $pathsByType = $this->normalizePathsByType( $pathsByType );
130 
131  $status = null;
132  $loop = new WaitConditionLoop(
133  function () use ( &$status, $pathsByType ) {
134  $status = $this->doLockByType( $pathsByType );
135 
136  return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE;
137  },
138  $timeout
139  );
140  $loop->invoke();
141 
142  return $status;
143  }
144 
152  final public function unlock( array $paths, $type = self::LOCK_EX ) {
153  return $this->unlockByType( [ $type => $paths ] );
154  }
155 
163  final public function unlockByType( array $pathsByType ) {
164  $pathsByType = $this->normalizePathsByType( $pathsByType );
165  $status = $this->doUnlockByType( $pathsByType );
166 
167  return $status;
168  }
169 
178  final protected function sha1Base36Absolute( $path ) {
179  return Wikimedia\base_convert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
180  }
181 
190  final protected function sha1Base16Absolute( $path ) {
191  return sha1( "{$this->domain}:{$path}" );
192  }
193 
202  final protected function normalizePathsByType( array $pathsByType ) {
203  $res = [];
204  foreach ( $pathsByType as $type => $paths ) {
205  $res[$this->lockTypeMap[$type]] = array_unique( $paths );
206  }
207 
208  return $res;
209  }
210 
217  protected function doLockByType( array $pathsByType ) {
219  $lockedByType = []; // map of (type => paths)
220  foreach ( $pathsByType as $type => $paths ) {
221  $status->merge( $this->doLock( $paths, $type ) );
222  if ( $status->isOK() ) {
223  $lockedByType[$type] = $paths;
224  } else {
225  // Release the subset of locks that were acquired
226  foreach ( $lockedByType as $lType => $lPaths ) {
227  $status->merge( $this->doUnlock( $lPaths, $lType ) );
228  }
229  break;
230  }
231  }
232 
233  return $status;
234  }
235 
243  abstract protected function doLock( array $paths, $type );
244 
251  protected function doUnlockByType( array $pathsByType ) {
253  foreach ( $pathsByType as $type => $paths ) {
254  $status->merge( $this->doUnlock( $paths, $type ) );
255  }
256 
257  return $status;
258  }
259 
267  abstract protected function doUnlock( array $paths, $type );
268 }
LockManager
Class for handling resource locking.
Definition: LockManager.php:47
LockManager\$locksHeld
array $locksHeld
Map of (resource path => lock type => count)
Definition: LockManager.php:59
LockManager\LOCK_SH
const LOCK_SH
Lock types; stronger locks have higher values.
Definition: LockManager.php:68
LockManager\sha1Base16Absolute
sha1Base16Absolute( $path)
Get the base 16 SHA-1 of a string, padded to 31 digits.
Definition: LockManager.php:190
LockManager\LOCK_UW
const LOCK_UW
Definition: LockManager.php:69
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$res
$res
Definition: database.txt:21
$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 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:2536
LockManager\$lockTypeMap
array $lockTypeMap
Mapping of lock types to the type actually used.
Definition: LockManager.php:52
php
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
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:202
LockManager\$lockTTL
$lockTTL
Definition: LockManager.php:62
LockManager\sha1Base36Absolute
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
Definition: LockManager.php:178
LockManager\doUnlockByType
doUnlockByType(array $pathsByType)
Definition: LockManager.php:251
LockManager\unlock
unlock(array $paths, $type=self::LOCK_EX)
Unlock the resources at the given abstract paths.
Definition: LockManager.php:152
LockManager\lock
lock(array $paths, $type=self::LOCK_EX, $timeout=0)
Lock the resources at the given abstract paths.
Definition: LockManager.php:116
LockManager\unlockByType
unlockByType(array $pathsByType)
Unlock the resources at the given abstract paths.
Definition: LockManager.php:163
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
LockManager\$logger
LoggerInterface $logger
Definition: LockManager.php:49
LockManager\$session
string $session
Random 32-char hex number.
Definition: LockManager.php:65
LockManager\doUnlock
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
$path
$path
Definition: NoLocalSettings.php:26
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:83
LockManager\LOCK_EX
const LOCK_EX
Definition: LockManager.php:70
LockManager\doLock
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
LockManager\doLockByType
doLockByType(array $pathsByType)
Definition: LockManager.php:217
LockManager\lockByType
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
Definition: LockManager.php:128
LockManager\$domain
$domain
Definition: LockManager.php:61
array
the array() calling protocol came about after MediaWiki 1.4rc1.