| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Settings\Source; |
| 4 |
|
| 5 |
use MediaWiki\Settings\Cache\CacheableSource; |
| 6 |
use MediaWiki\Settings\SettingsBuilderException; |
| 7 |
use MediaWiki\Settings\Source\Format\JsonFormat; |
| 8 |
use MediaWiki\Settings\Source\Format\SettingsFormat; |
| 9 |
use MediaWiki\Settings\Source\Format\YamlFormat; |
| 10 |
use Stringable; |
| 11 |
use UnexpectedValueException; |
| 12 |
use Wikimedia\AtEase\AtEase; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class FileSource implements Stringable, CacheableSource, SettingsIncludeLocator { |
| 20 |
private const BUILT_IN_FORMATS = [ |
| 21 |
JsonFormat::class, |
| 22 |
YamlFormat::class, |
| 23 |
]; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private const EXPIRY_TTL = 60 * 60 * 24; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private const EXPIRY_WEIGHT = 1.0; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private $format; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private $path; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function __construct( string $path, ?SettingsFormat $format = null ) { |
| 89 |
$this->path = $path; |
| 90 |
$this->format = $format; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function allowsStaleLoad(): bool { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function load(): array { |
| 112 |
$ext = pathinfo( $this->path, PATHINFO_EXTENSION ); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if ( $this->format ) { |
| 117 |
return $this->readAndDecode( $this->format ); |
| 118 |
} |
| 119 |
|
| 120 |
foreach ( self::BUILT_IN_FORMATS as $format ) { |
| 121 |
if ( call_user_func( [ $format, 'supportsFileExtension' ], $ext ) ) { |
| 122 |
return $this->readAndDecode( new $format() ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
throw new SettingsBuilderException( |
| 127 |
"None of the built-in formats are suitable for '{path}'", |
| 128 |
[ |
| 129 |
'path' => $this->path, |
| 130 |
] |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getExpiryTtl(): int { |
| 140 |
return self::EXPIRY_TTL; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function getExpiryWeight(): float { |
| 150 |
return self::EXPIRY_WEIGHT; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function getHashKey(): string { |
| 160 |
$stat = stat( $this->path ); |
| 161 |
|
| 162 |
if ( $stat === false ) { |
| 163 |
throw new SettingsBuilderException( |
| 164 |
"Failed to stat file '{path}'", |
| 165 |
[ 'path' => $this->path ] |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
return sprintf( '%x-%x-%x', $stat['ino'], $stat['size'], $stat['mtime'] ); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function __toString(): string { |
| 178 |
return $this->path; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private function readAndDecode( SettingsFormat $format ): array { |
| 190 |
$contents = AtEase::quietCall( 'file_get_contents', $this->path ); |
| 191 |
|
| 192 |
if ( $contents === false ) { |
| 193 |
if ( !is_readable( $this->path ) ) { |
| 194 |
throw new SettingsBuilderException( |
| 195 |
"File '{path}' is not readable", |
| 196 |
[ 'path' => $this->path ] |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
if ( is_dir( $this->path ) ) { |
| 201 |
throw new SettingsBuilderException( |
| 202 |
"'{path}' is a directory, not a file", |
| 203 |
[ 'path' => $this->path ] |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
throw new SettingsBuilderException( |
| 208 |
"Failed to read file '{path}'", |
| 209 |
[ 'path' => $this->path ] |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
try { |
| 214 |
return $format->decode( $contents ); |
| 215 |
} catch ( UnexpectedValueException $e ) { |
| 216 |
throw new SettingsBuilderException( |
| 217 |
"Failed to decode file '{path}': {message}", |
| 218 |
[ |
| 219 |
'path' => $this->path, |
| 220 |
'message' => $e->getMessage() |
| 221 |
] |
| 222 |
); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
public function locateInclude( string $location ): string { |
| 227 |
return SettingsFileUtils::resolveRelativeLocation( $location, dirname( $this->path ) ); |
| 228 |
} |
| 229 |
} |