MediaWiki master
FileStatePredicates.php
Go to the documentation of this file.
1<?php
34 protected const EXISTS = 'exists';
35 protected const SIZE = 'size';
36 protected const SHA1 = 'sha1';
37
39 private $fileStateByPath;
40
41 public function __construct() {
42 $this->fileStateByPath = [];
43 }
44
52 public function assumeFileExists( string $path, $size, $sha1Base36 ) {
53 $this->fileStateByPath[$path] = [
54 self::EXISTS => true,
55 self::SIZE => $size,
56 self::SHA1 => $sha1Base36
57 ];
58 }
59
65 public function assumeFileDoesNotExist( string $path ) {
66 $this->fileStateByPath[$path] = [
67 self::EXISTS => false,
68 self::SIZE => false,
69 self::SHA1 => false
70 ];
71 }
72
80 public function resolveFileExistence( string $path, $curExistenceFunc ) {
81 return self::resolveFileProperty( $path, self::EXISTS, $curExistenceFunc );
82 }
83
91 public function resolveFileSize( string $path, $curSizeFunc ) {
92 return self::resolveFileProperty( $path, self::SIZE, $curSizeFunc );
93 }
94
102 public function resolveFileSha1Base36( string $path, $curSha1Func ) {
103 return self::resolveFileProperty( $path, self::SHA1, $curSha1Func );
104 }
105
112 private function resolveFileProperty( $path, $property, $curValueFunc ) {
113 if ( isset( $this->fileStateByPath[$path] ) ) {
114 // File is predicated to have a counterfactual state
115 $value = $this->fileStateByPath[$path][$property];
116 if ( $value instanceof Closure ) {
117 $value = $value();
118 $this->fileStateByPath[$path][$property] = $value;
119 }
120 } else {
121 // File is not predicated to have a counterfactual state; use the current state
122 $value = $curValueFunc( $path );
123 }
124
125 return $value;
126 }
127
132 public function snapshot( array $paths ) {
133 $snapshot = new self();
134 foreach ( $paths as $path ) {
135 if ( isset( $this->fileStateByPath[$path] ) ) {
136 $snapshot->fileStateByPath[$path] = $this->fileStateByPath[$path];
137 }
138 }
139
140 return $snapshot;
141 }
142}
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.
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.
assumeFileDoesNotExist(string $path)
Predicate that no file exists at the path.