Merge "Set $wgNoFollowLinks to false iff "Authorized editors only" selected"
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / LSLockManager.php
1 <?php
2 /**
3 * Version of LockManager based on using lock daemon servers.
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 */
23
24 /**
25 * Manage locks using a lock daemon server.
26 *
27 * Version of LockManager based on using lock daemon servers.
28 * This is meant for multi-wiki systems that may share files.
29 * All locks are non-blocking, which avoids deadlocks.
30 *
31 * All lock requests for a resource, identified by a hash string, will map
32 * to one bucket. Each bucket maps to one or several peer servers, each
33 * running LockServerDaemon.php, listening on a designated TCP port.
34 * A majority of peers must agree for a lock to be acquired.
35 *
36 * @ingroup LockManager
37 * @since 1.19
38 */
39 class LSLockManager extends QuorumLockManager {
40 /** @var array Mapping of lock types to the type actually used */
41 protected $lockTypeMap = array(
42 self::LOCK_SH => self::LOCK_SH,
43 self::LOCK_UW => self::LOCK_SH,
44 self::LOCK_EX => self::LOCK_EX
45 );
46
47 /** @var array Map of server names to server config */
48 protected $lockServers; // (server name => server config array)
49
50 /** @var array Map Server connections (server name => resource) */
51 protected $conns = array();
52
53 protected $connTimeout; // float number of seconds
54 protected $session = ''; // random SHA-1 string
55
56 /**
57 * Construct a new instance from configuration.
58 *
59 * @param array $config Paramaters include:
60 * - lockServers : Associative array of server names to configuration.
61 * Configuration is an associative array that includes:
62 * - host : IP address/hostname
63 * - port : TCP port
64 * - authKey : Secret string the lock server uses
65 * - srvsByBucket : Array of 1-16 consecutive integer keys, starting from 0,
66 * each having an odd-numbered list of server names (peers) as values.
67 * - connTimeout : Lock server connection attempt timeout. [optional]
68 */
69 public function __construct( array $config ) {
70 parent::__construct( $config );
71
72 $this->lockServers = $config['lockServers'];
73 // Sanitize srvsByBucket config to prevent PHP errors
74 $this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' );
75 $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
76
77 if ( isset( $config['connTimeout'] ) ) {
78 $this->connTimeout = $config['connTimeout'];
79 } else {
80 $this->connTimeout = 3; // use some sane amount
81 }
82
83 $this->session = wfRandomString( 32 ); // 128 bits
84 }
85
86 /**
87 * @see QuorumLockManager::getLocksOnServer()
88 * @param string $lockSrv
89 * @param array $paths
90 * @param int $type
91 * @return Status
92 */
93 protected function getLocksOnServer( $lockSrv, array $paths, $type ) {
94 $status = Status::newGood();
95
96 // Send out the command and get the response...
97 $type = ( $type == self::LOCK_SH ) ? 'SH' : 'EX';
98 $keys = array_unique( array_map( array( $this, 'sha1Base36Absolute' ), $paths ) );
99 $response = $this->sendCommand( $lockSrv, 'ACQUIRE', $type, $keys );
100
101 if ( $response !== 'ACQUIRED' ) {
102 foreach ( $paths as $path ) {
103 $status->fatal( 'lockmanager-fail-acquirelock', $path );
104 }
105 }
106
107 return $status;
108 }
109
110 /**
111 * @see QuorumLockManager::freeLocksOnServer()
112 * @param string $lockSrv
113 * @param array $paths
114 * @param int $type
115 * @return Status
116 */
117 protected function freeLocksOnServer( $lockSrv, array $paths, $type ) {
118 $status = Status::newGood();
119
120 // Send out the command and get the response...
121 $type = ( $type == self::LOCK_SH ) ? 'SH' : 'EX';
122 $keys = array_unique( array_map( array( $this, 'sha1Base36Absolute' ), $paths ) );
123 $response = $this->sendCommand( $lockSrv, 'RELEASE', $type, $keys );
124
125 if ( $response !== 'RELEASED' ) {
126 foreach ( $paths as $path ) {
127 $status->fatal( 'lockmanager-fail-releaselock', $path );
128 }
129 }
130
131 return $status;
132 }
133
134 /**
135 * @see QuorumLockManager::releaseAllLocks()
136 * @return Status
137 */
138 protected function releaseAllLocks() {
139 $status = Status::newGood();
140
141 foreach ( $this->conns as $lockSrv => $conn ) {
142 $response = $this->sendCommand( $lockSrv, 'RELEASE_ALL', '', array() );
143 if ( $response !== 'RELEASED_ALL' ) {
144 $status->fatal( 'lockmanager-fail-svr-release', $lockSrv );
145 }
146 }
147
148 return $status;
149 }
150
151 /**
152 * @see QuorumLockManager::isServerUp()
153 * @param string $lockSrv
154 * @return bool
155 */
156 protected function isServerUp( $lockSrv ) {
157 return (bool)$this->getConnection( $lockSrv );
158 }
159
160 /**
161 * Send a command and get back the response
162 *
163 * @param string $lockSrv
164 * @param string $action
165 * @param string $type
166 * @param array $values
167 * @return string|bool
168 */
169 protected function sendCommand( $lockSrv, $action, $type, $values ) {
170 $conn = $this->getConnection( $lockSrv );
171 if ( !$conn ) {
172 return false; // no connection
173 }
174 $authKey = $this->lockServers[$lockSrv]['authKey'];
175 // Build of the command as a flat string...
176 $values = implode( '|', $values );
177 $key = hash_hmac( 'sha1', "{$this->session}\n{$action}\n{$type}\n{$values}", $authKey );
178 // Send out the command...
179 if ( fwrite( $conn, "{$this->session}:$key:$action:$type:$values\n" ) === false ) {
180 return false;
181 }
182 // Get the response...
183 $response = fgets( $conn );
184 if ( $response === false ) {
185 return false;
186 }
187
188 return trim( $response );
189 }
190
191 /**
192 * Get (or reuse) a connection to a lock server
193 *
194 * @param string $lockSrv
195 * @return resource
196 */
197 protected function getConnection( $lockSrv ) {
198 if ( !isset( $this->conns[$lockSrv] ) ) {
199 $cfg = $this->lockServers[$lockSrv];
200 wfSuppressWarnings();
201 $errno = $errstr = '';
202 $conn = fsockopen( $cfg['host'], $cfg['port'], $errno, $errstr, $this->connTimeout );
203 wfRestoreWarnings();
204 if ( $conn === false ) {
205 return null;
206 }
207 $sec = floor( $this->connTimeout );
208 $usec = floor( ( $this->connTimeout - floor( $this->connTimeout ) ) * 1e6 );
209 stream_set_timeout( $conn, $sec, $usec );
210 $this->conns[$lockSrv] = $conn;
211 }
212
213 return $this->conns[$lockSrv];
214 }
215
216 /**
217 * Make sure remaining locks get cleared for sanity
218 */
219 function __destruct() {
220 $this->releaseAllLocks();
221 foreach ( $this->conns as $conn ) {
222 fclose( $conn );
223 }
224 }
225 }