MediaWiki master
FileStatePredicates.php
Go to the documentation of this file.
1<?php
9
10use Closure;
11
24 protected const EXISTS = 'exists';
25 protected const SIZE = 'size';
26 protected const SHA1 = 'sha1';
27
29 private $fileStateByPath;
30
31 public function __construct() {
32 $this->fileStateByPath = [];
33 }
34
42 public function assumeFileExists( string $path, $size, $sha1Base36 ) {
43 $this->fileStateByPath[$path] = [
44 self::EXISTS => true,
45 self::SIZE => $size,
46 self::SHA1 => $sha1Base36
47 ];
48 }
49
55 public function assumeFileDoesNotExist( string $path ) {
56 $this->fileStateByPath[$path] = [
57 self::EXISTS => false,
58 self::SIZE => false,
59 self::SHA1 => false
60 ];
61 }
62
70 public function resolveFileExistence( string $path, $curExistenceFunc ) {
71 return self::resolveFileProperty( $path, self::EXISTS, $curExistenceFunc );
72 }
73
81 public function resolveFileSize( string $path, $curSizeFunc ) {
82 return self::resolveFileProperty( $path, self::SIZE, $curSizeFunc );
83 }
84
92 public function resolveFileSha1Base36( string $path, $curSha1Func ) {
93 return self::resolveFileProperty( $path, self::SHA1, $curSha1Func );
94 }
95
102 private function resolveFileProperty( $path, $property, $curValueFunc ) {
103 if ( isset( $this->fileStateByPath[$path] ) ) {
104 // File is predicated to have a counterfactual state
105 $value = $this->fileStateByPath[$path][$property];
106 if ( $value instanceof Closure ) {
107 $value = $value();
108 $this->fileStateByPath[$path][$property] = $value;
109 }
110 } else {
111 // File is not predicated to have a counterfactual state; use the current state
112 $value = $curValueFunc( $path );
113 }
114
115 return $value;
116 }
117
122 public function snapshot( array $paths ) {
123 $snapshot = new self();
124 foreach ( $paths as $path ) {
125 if ( isset( $this->fileStateByPath[$path] ) ) {
126 $snapshot->fileStateByPath[$path] = $this->fileStateByPath[$path];
127 }
128 }
129
130 return $snapshot;
131 }
132}
133
135class_alias( FileStatePredicates::class, 'FileStatePredicates' );
Helper class for tracking counterfactual file states when pre-checking file operation batches.
resolveFileSha1Base36(string $path, $curSha1Func)
Get the hypothetical SHA-1 hash of a file given predicated and current state of files.
assumeFileDoesNotExist(string $path)
Predicate that no file exists at the path.
resolveFileSize(string $path, $curSizeFunc)
Get the hypothetical size of a file given predicated and current state of files.
assumeFileExists(string $path, $size, $sha1Base36)
Predicate that a file exists at the path.
resolveFileExistence(string $path, $curExistenceFunc)
Get the hypothetical existance a file given predicated and current state of files.