Merge "Don't fallback from uk to ru"
[lhc/web/wiklou.git] / includes / libs / filebackend / fileop / FileOp.php
1 <?php
2 /**
3 * Helper class for representing operations with transaction support.
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 FileBackend
22 * @author Aaron Schulz
23 */
24 use Psr\Log\LoggerInterface;
25
26 /**
27 * FileBackend helper class for representing operations.
28 * Do not use this class from places outside FileBackend.
29 *
30 * Methods called from FileOpBatch::attempt() should avoid throwing
31 * exceptions at all costs. FileOp objects should be lightweight in order
32 * to support large arrays in memory and serialization.
33 *
34 * @ingroup FileBackend
35 * @since 1.19
36 */
37 abstract class FileOp {
38 /** @var array */
39 protected $params = [];
40
41 /** @var FileBackendStore */
42 protected $backend;
43 /** @var LoggerInterface */
44 protected $logger;
45
46 /** @var int */
47 protected $state = self::STATE_NEW;
48
49 /** @var bool */
50 protected $failed = false;
51
52 /** @var bool */
53 protected $async = false;
54
55 /** @var string */
56 protected $batchId;
57
58 /** @var bool Operation is not a no-op */
59 protected $doOperation = true;
60
61 /** @var string */
62 protected $sourceSha1;
63
64 /** @var bool */
65 protected $overwriteSameCase;
66
67 /** @var bool */
68 protected $destExists;
69
70 /* Object life-cycle */
71 const STATE_NEW = 1;
72 const STATE_CHECKED = 2;
73 const STATE_ATTEMPTED = 3;
74
75 /**
76 * Build a new batch file operation transaction
77 *
78 * @param FileBackendStore $backend
79 * @param array $params
80 * @param LoggerInterface $logger PSR logger instance
81 * @throws FileBackendError
82 */
83 final public function __construct(
84 FileBackendStore $backend, array $params, LoggerInterface $logger
85 ) {
86 $this->backend = $backend;
87 $this->logger = $logger;
88 list( $required, $optional, $paths ) = $this->allowedParams();
89 foreach ( $required as $name ) {
90 if ( isset( $params[$name] ) ) {
91 $this->params[$name] = $params[$name];
92 } else {
93 throw new InvalidArgumentException( "File operation missing parameter '$name'." );
94 }
95 }
96 foreach ( $optional as $name ) {
97 if ( isset( $params[$name] ) ) {
98 $this->params[$name] = $params[$name];
99 }
100 }
101 foreach ( $paths as $name ) {
102 if ( isset( $this->params[$name] ) ) {
103 // Normalize paths so the paths to the same file have the same string
104 $this->params[$name] = self::normalizeIfValidStoragePath( $this->params[$name] );
105 }
106 }
107 }
108
109 /**
110 * Normalize a string if it is a valid storage path
111 *
112 * @param string $path
113 * @return string
114 */
115 protected static function normalizeIfValidStoragePath( $path ) {
116 if ( FileBackend::isStoragePath( $path ) ) {
117 $res = FileBackend::normalizeStoragePath( $path );
118
119 return ( $res !== null ) ? $res : $path;
120 }
121
122 return $path;
123 }
124
125 /**
126 * Set the batch UUID this operation belongs to
127 *
128 * @param string $batchId
129 */
130 final public function setBatchId( $batchId ) {
131 $this->batchId = $batchId;
132 }
133
134 /**
135 * Get the value of the parameter with the given name
136 *
137 * @param string $name
138 * @return mixed Returns null if the parameter is not set
139 */
140 final public function getParam( $name ) {
141 return isset( $this->params[$name] ) ? $this->params[$name] : null;
142 }
143
144 /**
145 * Check if this operation failed precheck() or attempt()
146 *
147 * @return bool
148 */
149 final public function failed() {
150 return $this->failed;
151 }
152
153 /**
154 * Get a new empty predicates array for precheck()
155 *
156 * @return array
157 */
158 final public static function newPredicates() {
159 return [ 'exists' => [], 'sha1' => [] ];
160 }
161
162 /**
163 * Get a new empty dependency tracking array for paths read/written to
164 *
165 * @return array
166 */
167 final public static function newDependencies() {
168 return [ 'read' => [], 'write' => [] ];
169 }
170
171 /**
172 * Update a dependency tracking array to account for this operation
173 *
174 * @param array $deps Prior path reads/writes; format of FileOp::newPredicates()
175 * @return array
176 */
177 final public function applyDependencies( array $deps ) {
178 $deps['read'] += array_fill_keys( $this->storagePathsRead(), 1 );
179 $deps['write'] += array_fill_keys( $this->storagePathsChanged(), 1 );
180
181 return $deps;
182 }
183
184 /**
185 * Check if this operation changes files listed in $paths
186 *
187 * @param array $deps Prior path reads/writes; format of FileOp::newPredicates()
188 * @return bool
189 */
190 final public function dependsOn( array $deps ) {
191 foreach ( $this->storagePathsChanged() as $path ) {
192 if ( isset( $deps['read'][$path] ) || isset( $deps['write'][$path] ) ) {
193 return true; // "output" or "anti" dependency
194 }
195 }
196 foreach ( $this->storagePathsRead() as $path ) {
197 if ( isset( $deps['write'][$path] ) ) {
198 return true; // "flow" dependency
199 }
200 }
201
202 return false;
203 }
204
205 /**
206 * Get the file journal entries for this file operation
207 *
208 * @param array $oPredicates Pre-op info about files (format of FileOp::newPredicates)
209 * @param array $nPredicates Post-op info about files (format of FileOp::newPredicates)
210 * @return array
211 */
212 final public function getJournalEntries( array $oPredicates, array $nPredicates ) {
213 if ( !$this->doOperation ) {
214 return []; // this is a no-op
215 }
216 $nullEntries = [];
217 $updateEntries = [];
218 $deleteEntries = [];
219 $pathsUsed = array_merge( $this->storagePathsRead(), $this->storagePathsChanged() );
220 foreach ( array_unique( $pathsUsed ) as $path ) {
221 $nullEntries[] = [ // assertion for recovery
222 'op' => 'null',
223 'path' => $path,
224 'newSha1' => $this->fileSha1( $path, $oPredicates )
225 ];
226 }
227 foreach ( $this->storagePathsChanged() as $path ) {
228 if ( $nPredicates['sha1'][$path] === false ) { // deleted
229 $deleteEntries[] = [
230 'op' => 'delete',
231 'path' => $path,
232 'newSha1' => ''
233 ];
234 } else { // created/updated
235 $updateEntries[] = [
236 'op' => $this->fileExists( $path, $oPredicates ) ? 'update' : 'create',
237 'path' => $path,
238 'newSha1' => $nPredicates['sha1'][$path]
239 ];
240 }
241 }
242
243 return array_merge( $nullEntries, $updateEntries, $deleteEntries );
244 }
245
246 /**
247 * Check preconditions of the operation without writing anything.
248 * This must update $predicates for each path that the op can change
249 * except when a failing StatusValue object is returned.
250 *
251 * @param array $predicates
252 * @return StatusValue
253 */
254 final public function precheck( array &$predicates ) {
255 if ( $this->state !== self::STATE_NEW ) {
256 return StatusValue::newFatal( 'fileop-fail-state', self::STATE_NEW, $this->state );
257 }
258 $this->state = self::STATE_CHECKED;
259 $status = $this->doPrecheck( $predicates );
260 if ( !$status->isOK() ) {
261 $this->failed = true;
262 }
263
264 return $status;
265 }
266
267 /**
268 * @param array $predicates
269 * @return StatusValue
270 */
271 protected function doPrecheck( array &$predicates ) {
272 return StatusValue::newGood();
273 }
274
275 /**
276 * Attempt the operation
277 *
278 * @return StatusValue
279 */
280 final public function attempt() {
281 if ( $this->state !== self::STATE_CHECKED ) {
282 return StatusValue::newFatal( 'fileop-fail-state', self::STATE_CHECKED, $this->state );
283 } elseif ( $this->failed ) { // failed precheck
284 return StatusValue::newFatal( 'fileop-fail-attempt-precheck' );
285 }
286 $this->state = self::STATE_ATTEMPTED;
287 if ( $this->doOperation ) {
288 $status = $this->doAttempt();
289 if ( !$status->isOK() ) {
290 $this->failed = true;
291 $this->logFailure( 'attempt' );
292 }
293 } else { // no-op
294 $status = StatusValue::newGood();
295 }
296
297 return $status;
298 }
299
300 /**
301 * @return StatusValue
302 */
303 protected function doAttempt() {
304 return StatusValue::newGood();
305 }
306
307 /**
308 * Attempt the operation in the background
309 *
310 * @return StatusValue
311 */
312 final public function attemptAsync() {
313 $this->async = true;
314 $result = $this->attempt();
315 $this->async = false;
316
317 return $result;
318 }
319
320 /**
321 * Get the file operation parameters
322 *
323 * @return array (required params list, optional params list, list of params that are paths)
324 */
325 protected function allowedParams() {
326 return [ [], [], [] ];
327 }
328
329 /**
330 * Adjust params to FileBackendStore internal file calls
331 *
332 * @param array $params
333 * @return array (required params list, optional params list)
334 */
335 protected function setFlags( array $params ) {
336 return [ 'async' => $this->async ] + $params;
337 }
338
339 /**
340 * Get a list of storage paths read from for this operation
341 *
342 * @return array
343 */
344 public function storagePathsRead() {
345 return [];
346 }
347
348 /**
349 * Get a list of storage paths written to for this operation
350 *
351 * @return array
352 */
353 public function storagePathsChanged() {
354 return [];
355 }
356
357 /**
358 * Check for errors with regards to the destination file already existing.
359 * Also set the destExists, overwriteSameCase and sourceSha1 member variables.
360 * A bad StatusValue will be returned if there is no chance it can be overwritten.
361 *
362 * @param array $predicates
363 * @return StatusValue
364 */
365 protected function precheckDestExistence( array $predicates ) {
366 $status = StatusValue::newGood();
367 // Get hash of source file/string and the destination file
368 $this->sourceSha1 = $this->getSourceSha1Base36(); // FS file or data string
369 if ( $this->sourceSha1 === null ) { // file in storage?
370 $this->sourceSha1 = $this->fileSha1( $this->params['src'], $predicates );
371 }
372 $this->overwriteSameCase = false;
373 $this->destExists = $this->fileExists( $this->params['dst'], $predicates );
374 if ( $this->destExists ) {
375 if ( $this->getParam( 'overwrite' ) ) {
376 return $status; // OK
377 } elseif ( $this->getParam( 'overwriteSame' ) ) {
378 $dhash = $this->fileSha1( $this->params['dst'], $predicates );
379 // Check if hashes are valid and match each other...
380 if ( !strlen( $this->sourceSha1 ) || !strlen( $dhash ) ) {
381 $status->fatal( 'backend-fail-hashes' );
382 } elseif ( $this->sourceSha1 !== $dhash ) {
383 // Give an error if the files are not identical
384 $status->fatal( 'backend-fail-notsame', $this->params['dst'] );
385 } else {
386 $this->overwriteSameCase = true; // OK
387 }
388
389 return $status; // do nothing; either OK or bad status
390 } else {
391 $status->fatal( 'backend-fail-alreadyexists', $this->params['dst'] );
392
393 return $status;
394 }
395 }
396
397 return $status;
398 }
399
400 /**
401 * precheckDestExistence() helper function to get the source file SHA-1.
402 * Subclasses should overwride this if the source is not in storage.
403 *
404 * @return string|bool Returns false on failure
405 */
406 protected function getSourceSha1Base36() {
407 return null; // N/A
408 }
409
410 /**
411 * Check if a file will exist in storage when this operation is attempted
412 *
413 * @param string $source Storage path
414 * @param array $predicates
415 * @return bool
416 */
417 final protected function fileExists( $source, array $predicates ) {
418 if ( isset( $predicates['exists'][$source] ) ) {
419 return $predicates['exists'][$source]; // previous op assures this
420 } else {
421 $params = [ 'src' => $source, 'latest' => true ];
422
423 return $this->backend->fileExists( $params );
424 }
425 }
426
427 /**
428 * Get the SHA-1 of a file in storage when this operation is attempted
429 *
430 * @param string $source Storage path
431 * @param array $predicates
432 * @return string|bool False on failure
433 */
434 final protected function fileSha1( $source, array $predicates ) {
435 if ( isset( $predicates['sha1'][$source] ) ) {
436 return $predicates['sha1'][$source]; // previous op assures this
437 } elseif ( isset( $predicates['exists'][$source] ) && !$predicates['exists'][$source] ) {
438 return false; // previous op assures this
439 } else {
440 $params = [ 'src' => $source, 'latest' => true ];
441
442 return $this->backend->getFileSha1Base36( $params );
443 }
444 }
445
446 /**
447 * Get the backend this operation is for
448 *
449 * @return FileBackendStore
450 */
451 public function getBackend() {
452 return $this->backend;
453 }
454
455 /**
456 * Log a file operation failure and preserve any temp files
457 *
458 * @param string $action
459 */
460 final public function logFailure( $action ) {
461 $params = $this->params;
462 $params['failedAction'] = $action;
463 try {
464 $this->logger->error( get_class( $this ) .
465 " failed (batch #{$this->batchId}): " . FormatJson::encode( $params ) );
466 } catch ( Exception $e ) {
467 // bad config? debug log error?
468 }
469 }
470 }