MediaWiki master
FileStatePredicates.php
Go to the documentation of this file.
1<?php
23
24use Closure;
25
38 protected const EXISTS = 'exists';
39 protected const SIZE = 'size';
40 protected const SHA1 = 'sha1';
41
43 private $fileStateByPath;
44
45 public function __construct() {
46 $this->fileStateByPath = [];
47 }
48
56 public function assumeFileExists( string $path, $size, $sha1Base36 ) {
57 $this->fileStateByPath[$path] = [
58 self::EXISTS => true,
59 self::SIZE => $size,
60 self::SHA1 => $sha1Base36
61 ];
62 }
63
69 public function assumeFileDoesNotExist( string $path ) {
70 $this->fileStateByPath[$path] = [
71 self::EXISTS => false,
72 self::SIZE => false,
73 self::SHA1 => false
74 ];
75 }
76
84 public function resolveFileExistence( string $path, $curExistenceFunc ) {
85 return self::resolveFileProperty( $path, self::EXISTS, $curExistenceFunc );
86 }
87
95 public function resolveFileSize( string $path, $curSizeFunc ) {
96 return self::resolveFileProperty( $path, self::SIZE, $curSizeFunc );
97 }
98
106 public function resolveFileSha1Base36( string $path, $curSha1Func ) {
107 return self::resolveFileProperty( $path, self::SHA1, $curSha1Func );
108 }
109
116 private function resolveFileProperty( $path, $property, $curValueFunc ) {
117 if ( isset( $this->fileStateByPath[$path] ) ) {
118 // File is predicated to have a counterfactual state
119 $value = $this->fileStateByPath[$path][$property];
120 if ( $value instanceof Closure ) {
121 $value = $value();
122 $this->fileStateByPath[$path][$property] = $value;
123 }
124 } else {
125 // File is not predicated to have a counterfactual state; use the current state
126 $value = $curValueFunc( $path );
127 }
128
129 return $value;
130 }
131
136 public function snapshot( array $paths ) {
137 $snapshot = new self();
138 foreach ( $paths as $path ) {
139 if ( isset( $this->fileStateByPath[$path] ) ) {
140 $snapshot->fileStateByPath[$path] = $this->fileStateByPath[$path];
141 }
142 }
143
144 return $snapshot;
145 }
146}
147
149class_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.