MediaWiki REL1_28
FSLockManager.php
Go to the documentation of this file.
1<?php
38 protected $lockTypeMap = [
39 self::LOCK_SH => self::LOCK_SH,
40 self::LOCK_UW => self::LOCK_SH,
41 self::LOCK_EX => self::LOCK_EX
42 ];
43
45 protected $lockDir;
46
48 protected $handles = [];
49
51 protected $isWindows;
52
59 function __construct( array $config ) {
60 parent::__construct( $config );
61
62 $this->lockDir = $config['lockDirectory'];
63 $this->isWindows = ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' );
64 }
65
72 protected function doLock( array $paths, $type ) {
73 $status = StatusValue::newGood();
74
75 $lockedPaths = []; // files locked in this attempt
76 foreach ( $paths as $path ) {
77 $status->merge( $this->doSingleLock( $path, $type ) );
78 if ( $status->isOK() ) {
79 $lockedPaths[] = $path;
80 } else {
81 // Abort and unlock everything
82 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
83
84 return $status;
85 }
86 }
87
88 return $status;
89 }
90
97 protected function doUnlock( array $paths, $type ) {
98 $status = StatusValue::newGood();
99
100 foreach ( $paths as $path ) {
101 $status->merge( $this->doSingleUnlock( $path, $type ) );
102 }
103
104 return $status;
105 }
106
114 protected function doSingleLock( $path, $type ) {
115 $status = StatusValue::newGood();
116
117 if ( isset( $this->locksHeld[$path][$type] ) ) {
118 ++$this->locksHeld[$path][$type];
119 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
120 $this->locksHeld[$path][$type] = 1;
121 } else {
122 if ( isset( $this->handles[$path] ) ) {
123 $handle = $this->handles[$path];
124 } else {
125 MediaWiki\suppressWarnings();
126 $handle = fopen( $this->getLockPath( $path ), 'a+' );
127 if ( !$handle ) { // lock dir missing?
128 mkdir( $this->lockDir, 0777, true );
129 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
130 }
131 MediaWiki\restoreWarnings();
132 }
133 if ( $handle ) {
134 // Either a shared or exclusive lock
135 $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
136 if ( flock( $handle, $lock | LOCK_NB ) ) {
137 // Record this lock as active
138 $this->locksHeld[$path][$type] = 1;
139 $this->handles[$path] = $handle;
140 } else {
141 fclose( $handle );
142 $status->fatal( 'lockmanager-fail-acquirelock', $path );
143 }
144 } else {
145 $status->fatal( 'lockmanager-fail-openlock', $path );
146 }
147 }
148
149 return $status;
150 }
151
159 protected function doSingleUnlock( $path, $type ) {
160 $status = StatusValue::newGood();
161
162 if ( !isset( $this->locksHeld[$path] ) ) {
163 $status->warning( 'lockmanager-notlocked', $path );
164 } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
165 $status->warning( 'lockmanager-notlocked', $path );
166 } else {
167 $handlesToClose = [];
168 --$this->locksHeld[$path][$type];
169 if ( $this->locksHeld[$path][$type] <= 0 ) {
170 unset( $this->locksHeld[$path][$type] );
171 }
172 if ( !count( $this->locksHeld[$path] ) ) {
173 unset( $this->locksHeld[$path] ); // no locks on this path
174 if ( isset( $this->handles[$path] ) ) {
175 $handlesToClose[] = $this->handles[$path];
176 unset( $this->handles[$path] );
177 }
178 }
179 // Unlock handles to release locks and delete
180 // any lock files that end up with no locks on them...
181 if ( $this->isWindows ) {
182 // Windows: for any process, including this one,
183 // calling unlink() on a locked file will fail
184 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
185 $status->merge( $this->pruneKeyLockFiles( $path ) );
186 } else {
187 // Unix: unlink() can be used on files currently open by this
188 // process and we must do so in order to avoid race conditions
189 $status->merge( $this->pruneKeyLockFiles( $path ) );
190 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
191 }
192 }
193
194 return $status;
195 }
196
202 private function closeLockHandles( $path, array $handlesToClose ) {
203 $status = StatusValue::newGood();
204 foreach ( $handlesToClose as $handle ) {
205 if ( !flock( $handle, LOCK_UN ) ) {
206 $status->fatal( 'lockmanager-fail-releaselock', $path );
207 }
208 if ( !fclose( $handle ) ) {
209 $status->warning( 'lockmanager-fail-closelock', $path );
210 }
211 }
212
213 return $status;
214 }
215
220 private function pruneKeyLockFiles( $path ) {
221 $status = StatusValue::newGood();
222 if ( !isset( $this->locksHeld[$path] ) ) {
223 # No locks are held for the lock file anymore
224 if ( !unlink( $this->getLockPath( $path ) ) ) {
225 $status->warning( 'lockmanager-fail-deletelock', $path );
226 }
227 unset( $this->handles[$path] );
228 }
229
230 return $status;
231 }
232
238 protected function getLockPath( $path ) {
239 return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
240 }
241
245 function __destruct() {
246 while ( count( $this->locksHeld ) ) {
247 foreach ( $this->locksHeld as $path => $locks ) {
248 $this->doSingleUnlock( $path, self::LOCK_EX );
249 $this->doSingleUnlock( $path, self::LOCK_SH );
250 }
251 }
252 }
253}
Simple version of LockManager based on using FS lock files.
doSingleLock( $path, $type)
Lock a single resource key.
pruneKeyLockFiles( $path)
string $lockDir
Global dir for all servers.
__destruct()
Make sure remaining locks get cleared for sanity.
doLock(array $paths, $type)
array $handles
Map of (locked key => lock file handle)
doSingleUnlock( $path, $type)
Unlock a single resource key.
__construct(array $config)
Construct a new instance from configuration.
closeLockHandles( $path, array $handlesToClose)
getLockPath( $path)
Get the path to the lock file for a key.
array $lockTypeMap
Mapping of lock types to the type actually used.
doUnlock(array $paths, $type)
Class for handling resource locking.
const LOCK_SH
Lock types; stronger locks have higher values.
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