Whitelist the <wbr> element.
[mediawiki.git] / includes / filebackend / lockmanager / LockManager.php
blobf0e54ec22f886e9fe2e6fafd83893e5ff51bb4af
1 <?php
2 /**
3 * @defgroup LockManager Lock management
4 * @ingroup FileBackend
5 */
7 /**
8 * Resource locking handling.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
26 * @ingroup LockManager
27 * @author Aaron Schulz
30 /**
31 * @brief Class for handling resource locking.
33 * Locks on resource keys can either be shared or exclusive.
35 * Implementations must keep track of what is locked by this proccess
36 * in-memory and support nested locking calls (using reference counting).
37 * At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op.
38 * Locks should either be non-blocking or have low wait timeouts.
40 * Subclasses should avoid throwing exceptions at all costs.
42 * @ingroup LockManager
43 * @since 1.19
45 abstract class LockManager {
46 /** @var Array Mapping of lock types to the type actually used */
47 protected $lockTypeMap = array(
48 self::LOCK_SH => self::LOCK_SH,
49 self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH
50 self::LOCK_EX => self::LOCK_EX
53 /** @var Array Map of (resource path => lock type => count) */
54 protected $locksHeld = array();
56 protected $domain; // string; domain (usually wiki ID)
57 protected $lockTTL; // integer; maximum time locks can be held
59 /** Lock types; stronger locks have higher values */
60 const LOCK_SH = 1; // shared lock (for reads)
61 const LOCK_UW = 2; // shared lock (for reads used to write elsewhere)
62 const LOCK_EX = 3; // exclusive lock (for writes)
64 /**
65 * Construct a new instance from configuration
67 * $config paramaters include:
68 * - domain : Domain (usually wiki ID) that all resources are relative to [optional]
69 * - lockTTL : Age (in seconds) at which resource locks should expire.
70 * This only applies if locks are not tied to a connection/process.
72 * @param $config Array
74 public function __construct( array $config ) {
75 $this->domain = isset( $config['domain'] ) ? $config['domain'] : wfWikiID();
76 if ( isset( $config['lockTTL'] ) ) {
77 $this->lockTTL = max( 1, $config['lockTTL'] );
78 } elseif ( PHP_SAPI === 'cli' ) {
79 $this->lockTTL = 2 * 3600;
80 } else {
81 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
82 $this->lockTTL = max( 5 * 60, 2 * (int)$met );
86 /**
87 * Lock the resources at the given abstract paths
89 * @param array $paths List of resource names
90 * @param $type integer LockManager::LOCK_* constant
91 * @param integer $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
92 * @return Status
94 final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
95 return $this->lockByType( array( $type => $paths ), $timeout );
98 /**
99 * Lock the resources at the given abstract paths
101 * @param array $paths Map of LockManager::LOCK_* constants to lists of storage paths
102 * @param integer $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
103 * @return Status
104 * @since 1.22
106 final public function lockByType( array $pathsByType, $timeout = 0 ) {
107 wfProfileIn( __METHOD__ );
108 $status = Status::newGood();
109 $pathsByType = $this->normalizePathsByType( $pathsByType );
110 $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times
111 $start = microtime( true );
112 do {
113 $status = $this->doLockByType( $pathsByType );
114 $elapsed = microtime( true ) - $start;
115 if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) {
116 break; // success, timeout, or clock set back
118 usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times
119 $elapsed = microtime( true ) - $start;
120 } while ( $elapsed < $timeout && $elapsed >= 0 );
121 wfProfileOut( __METHOD__ );
122 return $status;
126 * Unlock the resources at the given abstract paths
128 * @param array $paths List of storage paths
129 * @param $type integer LockManager::LOCK_* constant
130 * @return Status
132 final public function unlock( array $paths, $type = self::LOCK_EX ) {
133 return $this->unlockByType( array( $type => $paths ) );
137 * Unlock the resources at the given abstract paths
139 * @param array $paths Map of LockManager::LOCK_* constants to lists of storage paths
140 * @return Status
141 * @since 1.22
143 final public function unlockByType( array $pathsByType ) {
144 wfProfileIn( __METHOD__ );
145 $pathsByType = $this->normalizePathsByType( $pathsByType );
146 $status = $this->doUnlockByType( $pathsByType );
147 wfProfileOut( __METHOD__ );
148 return $status;
152 * Get the base 36 SHA-1 of a string, padded to 31 digits.
153 * Before hashing, the path will be prefixed with the domain ID.
154 * This should be used interally for lock key or file names.
156 * @param $path string
157 * @return string
159 final protected function sha1Base36Absolute( $path ) {
160 return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
164 * Get the base 16 SHA-1 of a string, padded to 31 digits.
165 * Before hashing, the path will be prefixed with the domain ID.
166 * This should be used interally for lock key or file names.
168 * @param $path string
169 * @return string
171 final protected function sha1Base16Absolute( $path ) {
172 return sha1( "{$this->domain}:{$path}" );
176 * Normalize the $paths array by converting LOCK_UW locks into the
177 * appropriate type and removing any duplicated paths for each lock type.
179 * @param array $paths Map of LockManager::LOCK_* constants to lists of storage paths
180 * @return Array
181 * @since 1.22
183 final protected function normalizePathsByType( array $pathsByType ) {
184 $res = array();
185 foreach ( $pathsByType as $type => $paths ) {
186 $res[$this->lockTypeMap[$type]] = array_unique( $paths );
188 return $res;
192 * @see LockManager::lockByType()
193 * @param array $paths Map of LockManager::LOCK_* constants to lists of storage paths
194 * @return Status
195 * @since 1.22
197 protected function doLockByType( array $pathsByType ) {
198 $status = Status::newGood();
199 $lockedByType = array(); // map of (type => paths)
200 foreach ( $pathsByType as $type => $paths ) {
201 $status->merge( $this->doLock( $paths, $type ) );
202 if ( $status->isOK() ) {
203 $lockedByType[$type] = $paths;
204 } else {
205 // Release the subset of locks that were acquired
206 foreach ( $lockedByType as $type => $paths ) {
207 $status->merge( $this->doUnlock( $paths, $type ) );
209 break;
212 return $status;
216 * Lock resources with the given keys and lock type
218 * @param array $paths List of storage paths
219 * @param $type integer LockManager::LOCK_* constant
220 * @return Status
222 abstract protected function doLock( array $paths, $type );
225 * @see LockManager::unlockByType()
226 * @param array $paths Map of LockManager::LOCK_* constants to lists of storage paths
227 * @return Status
228 * @since 1.22
230 protected function doUnlockByType( array $pathsByType ) {
231 $status = Status::newGood();
232 foreach ( $pathsByType as $type => $paths ) {
233 $status->merge( $this->doUnlock( $paths, $type ) );
235 return $status;
239 * Unlock resources with the given keys and lock type
241 * @param array $paths List of storage paths
242 * @param $type integer LockManager::LOCK_* constant
243 * @return Status
245 abstract protected function doUnlock( array $paths, $type );
249 * Simple version of LockManager that does nothing
250 * @since 1.19
252 class NullLockManager extends LockManager {
254 * @see LockManager::doLock()
255 * @param $paths array
256 * @param $type int
257 * @return Status
259 protected function doLock( array $paths, $type ) {
260 return Status::newGood();
264 * @see LockManager::doUnlock()
265 * @param $paths array
266 * @param $type int
267 * @return Status
269 protected function doUnlock( array $paths, $type ) {
270 return Status::newGood();