MediaWiki  master
FileOp.php
Go to the documentation of this file.
1 <?php
23 use Psr\Log\LoggerInterface;
24 use Wikimedia\RequestTimeout\TimeoutException;
25 
37 abstract class FileOp {
39  protected $backend;
41  protected $logger;
42 
44  protected $params = [];
45 
47  protected $state = self::STATE_NEW;
49  protected $failed = false;
51  protected $async = false;
53  protected $cancelled = false;
54 
56  protected $sourceSize;
58  protected $sourceSha1;
59 
61  protected $overwriteSameCase;
62 
64  protected $destExists;
65 
66  /* Object life-cycle */
67  private const STATE_NEW = 1;
68  private const STATE_CHECKED = 2;
69  private const STATE_ATTEMPTED = 3;
70 
71  protected const ASSUMED_SHA1 = 'sha1';
72  protected const ASSUMED_EXISTS = 'exists';
73  protected const ASSUMED_SIZE = 'size';
74 
83  final public function __construct(
84  FileBackendStore $backend, array $params, LoggerInterface $logger
85  ) {
86  $this->backend = $backend;
87  $this->logger = $logger;
88  [ $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 
115  protected static function normalizeIfValidStoragePath( $path ) {
118 
119  return $res ?? $path;
120  }
121 
122  return $path;
123  }
124 
131  final public function getParam( $name ) {
132  return $this->params[$name] ?? null;
133  }
134 
140  final public function failed() {
141  return $this->failed;
142  }
143 
149  final public static function newPredicates() {
150  return [ self::ASSUMED_EXISTS => [], self::ASSUMED_SHA1 => [], self::ASSUMED_SIZE => [] ];
151  }
152 
158  final public static function newDependencies() {
159  return [ 'read' => [], 'write' => [] ];
160  }
161 
168  final public function applyDependencies( array $deps ) {
169  $deps['read'] += array_fill_keys( $this->storagePathsRead(), 1 );
170  $deps['write'] += array_fill_keys( $this->storagePathsChanged(), 1 );
171 
172  return $deps;
173  }
174 
181  final public function dependsOn( array $deps ) {
182  foreach ( $this->storagePathsChanged() as $path ) {
183  if ( isset( $deps['read'][$path] ) || isset( $deps['write'][$path] ) ) {
184  return true; // "output" or "anti" dependency
185  }
186  }
187  foreach ( $this->storagePathsRead() as $path ) {
188  if ( isset( $deps['write'][$path] ) ) {
189  return true; // "flow" dependency
190  }
191  }
192 
193  return false;
194  }
195 
204  final public function precheck( array &$predicates ) {
205  if ( $this->state !== self::STATE_NEW ) {
206  return StatusValue::newFatal( 'fileop-fail-state', self::STATE_NEW, $this->state );
207  }
208  $this->state = self::STATE_CHECKED;
209 
210  $status = StatusValue::newGood();
211  foreach ( $this->storagePathsReadOrChanged() as $path ) {
212  if ( !$this->backend->isPathUsableInternal( $path ) ) {
213  $status->fatal( 'backend-fail-usable', $path );
214  }
215  }
216  if ( !$status->isOK() ) {
217  return $status;
218  }
219 
220  $status = $this->doPrecheck( $predicates );
221  if ( !$status->isOK() ) {
222  $this->failed = true;
223  }
224 
225  return $status;
226  }
227 
232  protected function doPrecheck( array &$predicates ) {
233  return StatusValue::newGood();
234  }
235 
241  final public function attempt() {
242  if ( $this->state !== self::STATE_CHECKED ) {
243  return StatusValue::newFatal( 'fileop-fail-state', self::STATE_CHECKED, $this->state );
244  } elseif ( $this->failed ) { // failed precheck
245  return StatusValue::newFatal( 'fileop-fail-attempt-precheck' );
246  }
247  $this->state = self::STATE_ATTEMPTED;
248  if ( $this->cancelled ) {
249  $status = StatusValue::newGood(); // no-op
250  } else {
251  $status = $this->doAttempt();
252  if ( !$status->isOK() ) {
253  $this->failed = true;
254  $this->logFailure( 'attempt' );
255  }
256  }
257 
258  return $status;
259  }
260 
264  protected function doAttempt() {
265  return StatusValue::newGood();
266  }
267 
273  final public function attemptAsync() {
274  $this->async = true;
275  $result = $this->attempt();
276  $this->async = false;
277 
278  return $result;
279  }
280 
286  final public function attemptQuick() {
287  $this->state = self::STATE_CHECKED; // bypassed
288 
289  return $this->attempt();
290  }
291 
297  final public function attemptAsyncQuick() {
298  $this->state = self::STATE_CHECKED; // bypassed
299 
300  return $this->attemptAsync();
301  }
302 
308  protected function allowedParams() {
309  return [ [], [], [] ];
310  }
311 
318  protected function setFlags( array $params ) {
319  return [ 'async' => $this->async ] + $params;
320  }
321 
327  public function storagePathsRead() {
328  return [];
329  }
330 
336  public function storagePathsChanged() {
337  return [];
338  }
339 
345  final public function storagePathsReadOrChanged() {
346  return array_values( array_unique(
347  array_merge( $this->storagePathsRead(), $this->storagePathsChanged() )
348  ) );
349  }
350 
359  protected function precheckDestExistence( array $predicates ) {
360  $status = StatusValue::newGood();
361  // Record the size of source file/string
362  $this->sourceSize = $this->getSourceSize(); // FS file or data string
363  if ( $this->sourceSize === null ) { // file in storage?
364  $this->sourceSize = $this->fileSize( $this->params['src'], $predicates );
365  }
366  // Record the hash of source file/string
367  $this->sourceSha1 = $this->getSourceSha1Base36(); // FS file or data string
368  if ( $this->sourceSha1 === null ) { // file in storage?
369  $this->sourceSha1 = $this->fileSha1( $this->params['src'], $predicates );
370  }
371  // Record the existence of destination file
372  $this->destExists = $this->fileExists( $this->params['dst'], $predicates );
373  // Check if an incompatible file exists at the destination
374  $this->overwriteSameCase = false;
375  if ( $this->destExists ) {
376  if ( $this->getParam( 'overwrite' ) ) {
377  return $status; // OK, no conflict
378  } elseif ( $this->getParam( 'overwriteSame' ) ) {
379  // Operation does nothing other than return an OK or bad status
380  $dhash = $this->fileSha1( $this->params['dst'], $predicates );
381  $dsize = $this->fileSize( $this->params['dst'], $predicates );
382  // Check if hashes are valid and match each other...
383  if ( !strlen( $this->sourceSha1 ) || !strlen( $dhash ) ) {
384  $status->fatal( 'backend-fail-hashes' );
385  } elseif ( !is_int( $this->sourceSize ) || !is_int( $dsize ) ) {
386  $status->fatal( 'backend-fail-sizes' );
387  } elseif ( $this->sourceSha1 !== $dhash || $this->sourceSize !== $dsize ) {
388  // Give an error if the files are not identical
389  $status->fatal( 'backend-fail-notsame', $this->params['dst'] );
390  } else {
391  $this->overwriteSameCase = true; // OK
392  }
393  } else {
394  $status->fatal( 'backend-fail-alreadyexists', $this->params['dst'] );
395  }
396  } elseif ( $this->destExists === FileBackend::EXISTENCE_ERROR ) {
397  $status->fatal( 'backend-fail-stat', $this->params['dst'] );
398  }
399 
400  return $status;
401  }
402 
409  protected function getSourceSize() {
410  return null; // N/A
411  }
412 
419  protected function getSourceSha1Base36() {
420  return null; // N/A
421  }
422 
433  final protected function fileExists( $source, array $predicates ) {
434  if ( isset( $predicates[self::ASSUMED_EXISTS][$source] ) ) {
435  return $predicates[self::ASSUMED_EXISTS][$source]; // previous op assures this
436  } else {
437  $params = [ 'src' => $source, 'latest' => true ];
438 
439  return $this->backend->fileExists( $params );
440  }
441  }
442 
454  final protected function fileSize( $source, array $predicates ) {
455  if ( isset( $predicates[self::ASSUMED_SIZE][$source] ) ) {
456  return $predicates[self::ASSUMED_SIZE][$source]; // previous op assures this
457  } elseif (
458  isset( $predicates[self::ASSUMED_EXISTS][$source] ) &&
459  !$predicates[self::ASSUMED_EXISTS][$source]
460  ) {
461  return false; // previous op assures this
462  } else {
463  $params = [ 'src' => $source, 'latest' => true ];
464 
465  return $this->backend->getFileSize( $params );
466  }
467  }
468 
476  final protected function fileSha1( $source, array $predicates ) {
477  if ( isset( $predicates[self::ASSUMED_SHA1][$source] ) ) {
478  return $predicates[self::ASSUMED_SHA1][$source]; // previous op assures this
479  } elseif (
480  isset( $predicates[self::ASSUMED_EXISTS][$source] ) &&
481  !$predicates[self::ASSUMED_EXISTS][$source]
482  ) {
483  return false; // previous op assures this
484  } else {
485  $params = [ 'src' => $source, 'latest' => true ];
486 
487  return $this->backend->getFileSha1Base36( $params );
488  }
489  }
490 
496  public function getBackend() {
497  return $this->backend;
498  }
499 
505  final public function logFailure( $action ) {
507  $params['failedAction'] = $action;
508  try {
509  $this->logger->error( static::class .
510  " failed: " . FormatJson::encode( $params ) );
511  } catch ( TimeoutException $e ) {
512  throw $e;
513  } catch ( Exception $e ) {
514  // bad config? debug log error?
515  }
516  }
517 }
Base class for all backends using particular storage medium.
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
static normalizeStoragePath( $storagePath)
Normalize a storage path by cleaning up directory separators.
FileBackend helper class for representing operations.
Definition: FileOp.php:37
array $params
Definition: FileOp.php:44
static normalizeIfValidStoragePath( $path)
Normalize a string if it is a valid storage path.
Definition: FileOp.php:115
static newDependencies()
Get a new empty dependency tracking array for paths read/written to.
Definition: FileOp.php:158
fileSha1( $source, array $predicates)
Get the SHA-1 of a file in storage when this operation is attempted.
Definition: FileOp.php:476
static newPredicates()
Get a new empty predicates array for precheck()
Definition: FileOp.php:149
fileExists( $source, array $predicates)
Check if a file will exist in storage when this operation is attempted.
Definition: FileOp.php:433
allowedParams()
Get the file operation parameters.
Definition: FileOp.php:308
const ASSUMED_EXISTS
Definition: FileOp.php:72
attemptAsync()
Attempt the operation in the background.
Definition: FileOp.php:273
doPrecheck(array &$predicates)
Definition: FileOp.php:232
string bool $sourceSha1
Definition: FileOp.php:58
getParam( $name)
Get the value of the parameter with the given name.
Definition: FileOp.php:131
bool $overwriteSameCase
Definition: FileOp.php:61
storagePathsReadOrChanged()
Get a list of storage paths read from or written to for this operation.
Definition: FileOp.php:345
getSourceSize()
precheckDestExistence() helper function to get the source file size.
Definition: FileOp.php:409
const ASSUMED_SIZE
Definition: FileOp.php:73
const ASSUMED_SHA1
Definition: FileOp.php:71
storagePathsChanged()
Get a list of storage paths written to for this operation.
Definition: FileOp.php:336
int $state
Definition: FileOp.php:47
applyDependencies(array $deps)
Update a dependency tracking array to account for this operation.
Definition: FileOp.php:168
attempt()
Attempt the operation.
Definition: FileOp.php:241
precheck(array &$predicates)
Check preconditions of the operation without writing anything.
Definition: FileOp.php:204
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition: FileOp.php:318
bool $async
Definition: FileOp.php:51
bool $destExists
Definition: FileOp.php:64
fileSize( $source, array $predicates)
Get the size a file in storage will have when this operation is attempted.
Definition: FileOp.php:454
int bool $sourceSize
Definition: FileOp.php:56
logFailure( $action)
Log a file operation failure and preserve any temp files.
Definition: FileOp.php:505
storagePathsRead()
Get a list of storage paths read from for this operation.
Definition: FileOp.php:327
getSourceSha1Base36()
precheckDestExistence() helper function to get the source file SHA-1.
Definition: FileOp.php:419
attemptQuick()
Attempt the operation without regards to prechecks.
Definition: FileOp.php:286
LoggerInterface $logger
Definition: FileOp.php:41
bool $cancelled
Definition: FileOp.php:53
FileBackendStore $backend
Definition: FileOp.php:39
dependsOn(array $deps)
Check if this operation changes files listed in $paths.
Definition: FileOp.php:181
getBackend()
Get the backend this operation is for.
Definition: FileOp.php:496
attemptAsyncQuick()
Attempt the operation in the background without regards to prechecks.
Definition: FileOp.php:297
failed()
Check if this operation failed precheck() or attempt()
Definition: FileOp.php:140
__construct(FileBackendStore $backend, array $params, LoggerInterface $logger)
Build a new batch file operation transaction.
Definition: FileOp.php:83
doAttempt()
Definition: FileOp.php:264
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition: FileOp.php:359
bool $failed
Definition: FileOp.php:49
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:96
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85
$source
return true
Definition: router.php:90