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