MediaWiki  1.27.1
FSLockManager.php
Go to the documentation of this file.
1 <?php
36 class FSLockManager extends LockManager {
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 
44  protected $lockDir; // global dir for all servers
45 
47  protected $handles = [];
48 
55  function __construct( array $config ) {
56  parent::__construct( $config );
57 
58  $this->lockDir = $config['lockDirectory'];
59  }
60 
67  protected function doLock( array $paths, $type ) {
69 
70  $lockedPaths = []; // files locked in this attempt
71  foreach ( $paths as $path ) {
72  $status->merge( $this->doSingleLock( $path, $type ) );
73  if ( $status->isOK() ) {
74  $lockedPaths[] = $path;
75  } else {
76  // Abort and unlock everything
77  $status->merge( $this->doUnlock( $lockedPaths, $type ) );
78 
79  return $status;
80  }
81  }
82 
83  return $status;
84  }
85 
92  protected function doUnlock( array $paths, $type ) {
94 
95  foreach ( $paths as $path ) {
96  $status->merge( $this->doSingleUnlock( $path, $type ) );
97  }
98 
99  return $status;
100  }
101 
109  protected function doSingleLock( $path, $type ) {
111 
112  if ( isset( $this->locksHeld[$path][$type] ) ) {
113  ++$this->locksHeld[$path][$type];
114  } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
115  $this->locksHeld[$path][$type] = 1;
116  } else {
117  if ( isset( $this->handles[$path] ) ) {
118  $handle = $this->handles[$path];
119  } else {
120  MediaWiki\suppressWarnings();
121  $handle = fopen( $this->getLockPath( $path ), 'a+' );
122  MediaWiki\restoreWarnings();
123  if ( !$handle ) { // lock dir missing?
124  wfMkdirParents( $this->lockDir );
125  $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
126  }
127  }
128  if ( $handle ) {
129  // Either a shared or exclusive lock
130  $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
131  if ( flock( $handle, $lock | LOCK_NB ) ) {
132  // Record this lock as active
133  $this->locksHeld[$path][$type] = 1;
134  $this->handles[$path] = $handle;
135  } else {
136  fclose( $handle );
137  $status->fatal( 'lockmanager-fail-acquirelock', $path );
138  }
139  } else {
140  $status->fatal( 'lockmanager-fail-openlock', $path );
141  }
142  }
143 
144  return $status;
145  }
146 
154  protected function doSingleUnlock( $path, $type ) {
156 
157  if ( !isset( $this->locksHeld[$path] ) ) {
158  $status->warning( 'lockmanager-notlocked', $path );
159  } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
160  $status->warning( 'lockmanager-notlocked', $path );
161  } else {
162  $handlesToClose = [];
163  --$this->locksHeld[$path][$type];
164  if ( $this->locksHeld[$path][$type] <= 0 ) {
165  unset( $this->locksHeld[$path][$type] );
166  }
167  if ( !count( $this->locksHeld[$path] ) ) {
168  unset( $this->locksHeld[$path] ); // no locks on this path
169  if ( isset( $this->handles[$path] ) ) {
170  $handlesToClose[] = $this->handles[$path];
171  unset( $this->handles[$path] );
172  }
173  }
174  // Unlock handles to release locks and delete
175  // any lock files that end up with no locks on them...
176  if ( wfIsWindows() ) {
177  // Windows: for any process, including this one,
178  // calling unlink() on a locked file will fail
179  $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
180  $status->merge( $this->pruneKeyLockFiles( $path ) );
181  } else {
182  // Unix: unlink() can be used on files currently open by this
183  // process and we must do so in order to avoid race conditions
184  $status->merge( $this->pruneKeyLockFiles( $path ) );
185  $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
186  }
187  }
188 
189  return $status;
190  }
191 
197  private function closeLockHandles( $path, array $handlesToClose ) {
199  foreach ( $handlesToClose as $handle ) {
200  if ( !flock( $handle, LOCK_UN ) ) {
201  $status->fatal( 'lockmanager-fail-releaselock', $path );
202  }
203  if ( !fclose( $handle ) ) {
204  $status->warning( 'lockmanager-fail-closelock', $path );
205  }
206  }
207 
208  return $status;
209  }
210 
215  private function pruneKeyLockFiles( $path ) {
217  if ( !isset( $this->locksHeld[$path] ) ) {
218  # No locks are held for the lock file anymore
219  if ( !unlink( $this->getLockPath( $path ) ) ) {
220  $status->warning( 'lockmanager-fail-deletelock', $path );
221  }
222  unset( $this->handles[$path] );
223  }
224 
225  return $status;
226  }
227 
233  protected function getLockPath( $path ) {
234  return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
235  }
236 
240  function __destruct() {
241  while ( count( $this->locksHeld ) ) {
242  foreach ( $this->locksHeld as $path => $locks ) {
243  $this->doSingleUnlock( $path, self::LOCK_EX );
244  $this->doSingleUnlock( $path, self::LOCK_SH );
245  }
246  }
247  }
248 }
the array() calling protocol came about after MediaWiki 1.4rc1.
pruneKeyLockFiles($path)
closeLockHandles($path, array $handlesToClose)
wfMkdirParents($dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
__construct(array $config)
Construct a new instance from configuration.
wfIsWindows()
Check if the operating system is Windows.
getLockPath($path)
Get the path to the lock file for a key.
array $lockTypeMap
Mapping of lock types to the type actually used.
const LOCK_EX
Definition: LockManager.php:62
__destruct()
Make sure remaining locks get cleared for sanity.
const LOCK_SH
Lock types; stronger locks have higher values.
Definition: LockManager.php:60
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
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
doUnlock(array $paths, $type)
Class for handling resource locking.
Definition: LockManager.php:45
doSingleUnlock($path, $type)
Unlock a single resource key.
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:1004
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:2338
Simple version of LockManager based on using FS lock files.
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101
doLock(array $paths, $type)
doSingleLock($path, $type)
Lock a single resource key.
array $handles
Map of (locked key => lock file handle)