Add CollationFa
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / FSLockManager.php
index 9a6206f..2b660ec 100644 (file)
  * @since 1.19
  */
 class FSLockManager extends LockManager {
-       /** @var Array Mapping of lock types to the type actually used */
-       protected $lockTypeMap = array(
+       /** @var array Mapping of lock types to the type actually used */
+       protected $lockTypeMap = [
                self::LOCK_SH => self::LOCK_SH,
                self::LOCK_UW => self::LOCK_SH,
                self::LOCK_EX => self::LOCK_EX
-       );
+       ];
 
        protected $lockDir; // global dir for all servers
 
-       /** @var Array Map of (locked key => lock type => lock file handle) */
-       protected $handles = array();
+       /** @var array Map of (locked key => lock file handle) */
+       protected $handles = [];
 
        /**
         * Construct a new instance from configuration.
         *
-        * $config includes:
+        * @param array $config Includes:
         *   - lockDirectory : Directory containing the lock files
-        *
-        * @param array $config
         */
        function __construct( array $config ) {
                parent::__construct( $config );
@@ -62,14 +60,14 @@ class FSLockManager extends LockManager {
 
        /**
         * @see LockManager::doLock()
-        * @param $paths array
-        * @param $type int
+        * @param array $paths
+        * @param int $type
         * @return Status
         */
        protected function doLock( array $paths, $type ) {
                $status = Status::newGood();
 
-               $lockedPaths = array(); // files locked in this attempt
+               $lockedPaths = []; // files locked in this attempt
                foreach ( $paths as $path ) {
                        $status->merge( $this->doSingleLock( $path, $type ) );
                        if ( $status->isOK() ) {
@@ -77,6 +75,7 @@ class FSLockManager extends LockManager {
                        } else {
                                // Abort and unlock everything
                                $status->merge( $this->doUnlock( $lockedPaths, $type ) );
+
                                return $status;
                        }
                }
@@ -86,8 +85,8 @@ class FSLockManager extends LockManager {
 
        /**
         * @see LockManager::doUnlock()
-        * @param $paths array
-        * @param $type int
+        * @param array $paths
+        * @param int $type
         * @return Status
         */
        protected function doUnlock( array $paths, $type ) {
@@ -103,8 +102,8 @@ class FSLockManager extends LockManager {
        /**
         * Lock a single resource key
         *
-        * @param $path string
-        * @param $type integer
+        * @param string $path
+        * @param int $type
         * @return Status
         */
        protected function doSingleLock( $path, $type ) {
@@ -115,12 +114,16 @@ class FSLockManager extends LockManager {
                } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
                        $this->locksHeld[$path][$type] = 1;
                } else {
-                       wfSuppressWarnings();
-                       $handle = fopen( $this->getLockPath( $path ), 'a+' );
-                       wfRestoreWarnings();
-                       if ( !$handle ) { // lock dir missing?
-                               wfMkdirParents( $this->lockDir );
-                               $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
+                       if ( isset( $this->handles[$path] ) ) {
+                               $handle = $this->handles[$path];
+                       } else {
+                               MediaWiki\suppressWarnings();
+                               $handle = fopen( $this->getLockPath( $path ), 'a+' );
+                               MediaWiki\restoreWarnings();
+                               if ( !$handle ) { // lock dir missing?
+                                       wfMkdirParents( $this->lockDir );
+                                       $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
+                               }
                        }
                        if ( $handle ) {
                                // Either a shared or exclusive lock
@@ -128,7 +131,7 @@ class FSLockManager extends LockManager {
                                if ( flock( $handle, $lock | LOCK_NB ) ) {
                                        // Record this lock as active
                                        $this->locksHeld[$path][$type] = 1;
-                                       $this->handles[$path][$type] = $handle;
+                                       $this->handles[$path] = $handle;
                                } else {
                                        fclose( $handle );
                                        $status->fatal( 'lockmanager-fail-acquirelock', $path );
@@ -144,8 +147,8 @@ class FSLockManager extends LockManager {
        /**
         * Unlock a single resource key
         *
-        * @param $path string
-        * @param $type integer
+        * @param string $path
+        * @param int $type
         * @return Status
         */
        protected function doSingleUnlock( $path, $type ) {
@@ -156,28 +159,17 @@ class FSLockManager extends LockManager {
                } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
                        $status->warning( 'lockmanager-notlocked', $path );
                } else {
-                       $handlesToClose = array();
+                       $handlesToClose = [];
                        --$this->locksHeld[$path][$type];
                        if ( $this->locksHeld[$path][$type] <= 0 ) {
                                unset( $this->locksHeld[$path][$type] );
-                               // If a LOCK_SH comes in while we have a LOCK_EX, we don't
-                               // actually add a handler, so check for handler existence.
-                               if ( isset( $this->handles[$path][$type] ) ) {
-                                       if ( $type === self::LOCK_EX
-                                               && isset( $this->locksHeld[$path][self::LOCK_SH] )
-                                               && !isset( $this->handles[$path][self::LOCK_SH] ) )
-                                       {
-                                               // EX lock came first: move this handle to the SH one
-                                               $this->handles[$path][self::LOCK_SH] = $this->handles[$path][$type];
-                                       } else {
-                                               // Mark this handle to be unlocked and closed
-                                               $handlesToClose[] = $this->handles[$path][$type];
-                                       }
-                                       unset( $this->handles[$path][$type] );
-                               }
                        }
                        if ( !count( $this->locksHeld[$path] ) ) {
                                unset( $this->locksHeld[$path] ); // no locks on this path
+                               if ( isset( $this->handles[$path] ) ) {
+                                       $handlesToClose[] = $this->handles[$path];
+                                       unset( $this->handles[$path] );
+                               }
                        }
                        // Unlock handles to release locks and delete
                        // any lock files that end up with no locks on them...
@@ -198,8 +190,8 @@ class FSLockManager extends LockManager {
        }
 
        /**
-        * @param $path string
-        * @param $handlesToClose array
+        * @param string $path
+        * @param array $handlesToClose
         * @return Status
         */
        private function closeLockHandles( $path, array $handlesToClose ) {
@@ -212,11 +204,12 @@ class FSLockManager extends LockManager {
                                $status->warning( 'lockmanager-fail-closelock', $path );
                        }
                }
+
                return $status;
        }
 
        /**
-        * @param $path string
+        * @param string $path
         * @return Status
         */
        private function pruneKeyLockFiles( $path ) {
@@ -228,17 +221,17 @@ class FSLockManager extends LockManager {
                        }
                        unset( $this->handles[$path] );
                }
+
                return $status;
        }
 
        /**
         * Get the path to the lock file for a key
-        * @param $path string
+        * @param string $path
         * @return string
         */
        protected function getLockPath( $path ) {
-               $hash = self::sha1Base36( $path );
-               return "{$this->lockDir}/{$hash}.lock";
+               return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
        }
 
        /**