MediaWiki REL1_28
LockManager.php
Go to the documentation of this file.
1<?php
6use Psr\Log\LoggerInterface;
7use Wikimedia\WaitConditionLoop;
8
47abstract 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 ) {
218 $status = StatusValue::newGood();
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 ) {
252 $status = StatusValue::newGood();
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Class for handling resource locking.
doUnlockByType(array $pathsByType)
array $lockTypeMap
Mapping of lock types to the type actually used.
lock(array $paths, $type=self::LOCK_EX, $timeout=0)
Lock the resources at the given abstract paths.
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
string $session
Random 32-char hex number.
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
const LOCK_SH
Lock types; stronger locks have higher values.
doLockByType(array $pathsByType)
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
LoggerInterface $logger
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
unlock(array $paths, $type=self::LOCK_EX)
Unlock the resources at the given abstract paths.
array $locksHeld
Map of (resource path => lock type => count)
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.
__construct(array $config)
Construct a new instance from configuration.
$res
Definition database.txt:21
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
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:1049
the array() calling protocol came about after MediaWiki 1.4rc1.
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' 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:2568
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:37