Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / lockmanager / ScopedLock.php
blob679d4debb442a4d85573600d88b31cfa3b8ed723
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 /**
22 * Self-releasing locks.
24 * Helper for consumers of LockManager, to create locks that automatically
25 * release when an object is destroyed or goes out of scope.
27 * @ingroup LockManager
28 * @since 1.19
30 class ScopedLock {
31 /** @var LockManager */
32 protected $manager;
33 /** @var StatusValue */
34 protected $status;
35 /** @var array Map of lock types to resource paths */
36 protected $pathsByType;
38 /**
39 * @param LockManager $manager
40 * @param array $pathsByType Map of lock types to path lists
41 * @param StatusValue $status
43 protected function __construct(
44 LockManager $manager, array $pathsByType, StatusValue $status
45 ) {
46 $this->manager = $manager;
47 $this->pathsByType = $pathsByType;
48 $this->status = $status;
51 /**
52 * Get a ScopedLock object representing a lock on resource paths.
53 * Any locks are released once this object goes out of scope.
54 * The StatusValue object is updated with any errors or warnings.
56 * @param LockManager $manager
57 * @param array $paths List of storage paths or map of lock types to path lists
58 * @param int|string $type LockManager::LOCK_* constant or "mixed" and $paths
59 * can be a map of types to paths (since 1.22). Otherwise $type should be an
60 * integer and $paths should be a list of paths.
61 * @param StatusValue $status
62 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.22)
63 * @return ScopedLock|null Returns null on failure
65 public static function factory(
66 LockManager $manager, array $paths, $type, StatusValue $status, $timeout = 0
67 ) {
68 $pathsByType = is_int( $type ) ? [ $type => $paths ] : $paths;
69 $lockStatus = $manager->lockByType( $pathsByType, $timeout );
70 $status->merge( $lockStatus );
71 if ( $lockStatus->isOK() ) {
72 return new self( $manager, $pathsByType, $status );
75 return null;
78 /**
79 * Release a scoped lock and set any errors in the attached StatusValue object.
80 * This is useful for early release of locks before function scope is destroyed.
81 * This is the same as setting the lock object to null.
83 * @param ScopedLock|null &$lock
84 * @since 1.21
86 public static function release( ?ScopedLock &$lock = null ) {
87 $lock = null;
90 /**
91 * Release the locks when this goes out of scope
93 public function __destruct() {
94 $wasOk = $this->status->isOK();
95 $this->status->merge( $this->manager->unlockByType( $this->pathsByType ) );
96 if ( $wasOk ) {
97 // Make sure StatusValue is OK, despite any unlockFiles() fatals
98 $this->status->setResult( true, $this->status->value );