| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Output\StreamFile; |
| 22 |
use Shellbox\Command\BoxedCommand; |
| 23 |
use Wikimedia\FileBackend\FileBackend; |
| 24 |
use Wikimedia\Rdbms\IDatabase; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class FileBackendDBRepoWrapper extends FileBackend { |
| 41 |
|
| 42 |
protected $backend; |
| 43 |
|
| 44 |
protected $repoName; |
| 45 |
|
| 46 |
protected $dbHandleFunc; |
| 47 |
|
| 48 |
protected $resolvedPathCache; |
| 49 |
|
| 50 |
protected $dbs; |
| 51 |
|
| 52 |
public function __construct( array $config ) { |
| 53 |
|
| 54 |
$backend = $config['backend']; |
| 55 |
$config['name'] = $backend->getName(); |
| 56 |
$config['domainId'] = $backend->getDomainId(); |
| 57 |
parent::__construct( $config ); |
| 58 |
$this->backend = $config['backend']; |
| 59 |
$this->repoName = $config['repoName']; |
| 60 |
$this->dbHandleFunc = $config['dbHandleFactory']; |
| 61 |
$this->resolvedPathCache = new MapCacheLRU( 100 ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function getInternalBackend() { |
| 70 |
return $this->backend; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function getBackendPath( $path, $latest = true ) { |
| 84 |
$paths = $this->getBackendPaths( [ $path ], $latest ); |
| 85 |
return current( $paths ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function getBackendPaths( array $paths, $latest = true ) { |
| 99 |
$db = $this->getDB( $latest ? DB_PRIMARY : DB_REPLICA ); |
| 100 |
|
| 101 |
|
| 102 |
$resolved = []; |
| 103 |
foreach ( $paths as $i => $path ) { |
| 104 |
if ( !$latest && $this->resolvedPathCache->hasField( $path, 'target', 10 ) ) { |
| 105 |
$resolved[$i] = $this->resolvedPathCache->getField( $path, 'target' ); |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
[ , $container ] = FileBackend::splitStoragePath( $path ); |
| 110 |
|
| 111 |
if ( $container === "{$this->repoName}-public" ) { |
| 112 |
$name = basename( $path ); |
| 113 |
if ( str_contains( $path, '!' ) ) { |
| 114 |
$sha1 = $db->newSelectQueryBuilder() |
| 115 |
->select( 'oi_sha1' ) |
| 116 |
->from( 'oldimage' ) |
| 117 |
->where( [ 'oi_archive_name' => $name ] ) |
| 118 |
->caller( __METHOD__ )->fetchField(); |
| 119 |
} else { |
| 120 |
$sha1 = $db->newSelectQueryBuilder() |
| 121 |
->select( 'img_sha1' ) |
| 122 |
->from( 'image' ) |
| 123 |
->where( [ 'img_name' => $name ] ) |
| 124 |
->caller( __METHOD__ )->fetchField(); |
| 125 |
} |
| 126 |
if ( $sha1 === null || !strlen( $sha1 ) ) { |
| 127 |
$resolved[$i] = $path; |
| 128 |
continue; |
| 129 |
} |
| 130 |
$resolved[$i] = $this->getPathForSHA1( $sha1 ); |
| 131 |
$this->resolvedPathCache->setField( $path, 'target', $resolved[$i] ); |
| 132 |
} elseif ( $container === "{$this->repoName}-deleted" ) { |
| 133 |
$name = basename( $path ); |
| 134 |
$sha1 = substr( $name, 0, strpos( $name, '.' ) ); |
| 135 |
$resolved[$i] = $this->getPathForSHA1( $sha1 ); |
| 136 |
$this->resolvedPathCache->setField( $path, 'target', $resolved[$i] ); |
| 137 |
} else { |
| 138 |
$resolved[$i] = $path; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$res = []; |
| 143 |
foreach ( $paths as $i => $path ) { |
| 144 |
$res[$i] = $resolved[$i]; |
| 145 |
} |
| 146 |
|
| 147 |
return $res; |
| 148 |
} |
| 149 |
|
| 150 |
protected function doOperationsInternal( array $ops, array $opts ) { |
| 151 |
return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts ); |
| 152 |
} |
| 153 |
|
| 154 |
protected function doQuickOperationsInternal( array $ops, array $opts ) { |
| 155 |
return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ), $opts ); |
| 156 |
} |
| 157 |
|
| 158 |
protected function doPrepare( array $params ) { |
| 159 |
return $this->backend->doPrepare( $params ); |
| 160 |
} |
| 161 |
|
| 162 |
protected function doSecure( array $params ) { |
| 163 |
return $this->backend->doSecure( $params ); |
| 164 |
} |
| 165 |
|
| 166 |
protected function doPublish( array $params ) { |
| 167 |
return $this->backend->doPublish( $params ); |
| 168 |
} |
| 169 |
|
| 170 |
protected function doClean( array $params ) { |
| 171 |
return $this->backend->doClean( $params ); |
| 172 |
} |
| 173 |
|
| 174 |
public function concatenate( array $params ) { |
| 175 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 176 |
} |
| 177 |
|
| 178 |
public function fileExists( array $params ) { |
| 179 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 180 |
} |
| 181 |
|
| 182 |
public function getFileTimestamp( array $params ) { |
| 183 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 184 |
} |
| 185 |
|
| 186 |
public function getFileSize( array $params ) { |
| 187 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 188 |
} |
| 189 |
|
| 190 |
public function getFileStat( array $params ) { |
| 191 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 192 |
} |
| 193 |
|
| 194 |
public function getFileXAttributes( array $params ) { |
| 195 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 196 |
} |
| 197 |
|
| 198 |
public function getFileSha1Base36( array $params ) { |
| 199 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 200 |
} |
| 201 |
|
| 202 |
public function getFileProps( array $params ) { |
| 203 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 204 |
} |
| 205 |
|
| 206 |
public function streamFile( array $params ) { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
$type = StreamFile::contentTypeFromPath( $params['src'] ); |
| 212 |
if ( $type && $type != 'unknown/unknown' ) { |
| 213 |
$params['headers'][] = "Content-type: $type"; |
| 214 |
} |
| 215 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 216 |
} |
| 217 |
|
| 218 |
public function getFileContentsMulti( array $params ) { |
| 219 |
return $this->translateArrayResults( __FUNCTION__, $params ); |
| 220 |
} |
| 221 |
|
| 222 |
public function getLocalReferenceMulti( array $params ) { |
| 223 |
return $this->translateArrayResults( __FUNCTION__, $params ); |
| 224 |
} |
| 225 |
|
| 226 |
public function getLocalCopyMulti( array $params ) { |
| 227 |
return $this->translateArrayResults( __FUNCTION__, $params ); |
| 228 |
} |
| 229 |
|
| 230 |
public function getFileHttpUrl( array $params ) { |
| 231 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 232 |
} |
| 233 |
|
| 234 |
public function addShellboxInputFile( BoxedCommand $command, string $boxedName, |
| 235 |
array $params |
| 236 |
) { |
| 237 |
$params['src'] = $this->getBackendPath( $params['src'], !empty( $params['latest'] ) ); |
| 238 |
return $this->backend->addShellboxInputFile( $command, $boxedName, $params ); |
| 239 |
} |
| 240 |
|
| 241 |
public function directoryExists( array $params ) { |
| 242 |
return $this->backend->directoryExists( $params ); |
| 243 |
} |
| 244 |
|
| 245 |
public function getDirectoryList( array $params ) { |
| 246 |
return $this->backend->getDirectoryList( $params ); |
| 247 |
} |
| 248 |
|
| 249 |
public function getFileList( array $params ) { |
| 250 |
return $this->backend->getFileList( $params ); |
| 251 |
} |
| 252 |
|
| 253 |
public function getFeatures() { |
| 254 |
return $this->backend->getFeatures(); |
| 255 |
} |
| 256 |
|
| 257 |
public function clearCache( ?array $paths = null ) { |
| 258 |
$this->backend->clearCache( null ); |
| 259 |
} |
| 260 |
|
| 261 |
public function preloadCache( array $paths ) { |
| 262 |
$paths = $this->getBackendPaths( $paths ); |
| 263 |
$this->backend->preloadCache( $paths ); |
| 264 |
} |
| 265 |
|
| 266 |
public function preloadFileStat( array $params ) { |
| 267 |
return $this->translateSrcParams( __FUNCTION__, $params ); |
| 268 |
} |
| 269 |
|
| 270 |
public function getScopedLocksForOps( array $ops, StatusValue $status ) { |
| 271 |
return $this->backend->getScopedLocksForOps( $ops, $status ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function getPathForSHA1( $sha1 ) { |
| 283 |
if ( strlen( $sha1 ) < 3 ) { |
| 284 |
throw new InvalidArgumentException( "Invalid file SHA-1." ); |
| 285 |
} |
| 286 |
return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) . |
| 287 |
"/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}"; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
protected function getDB( $index ) { |
| 297 |
if ( !isset( $this->dbs[$index] ) ) { |
| 298 |
$func = $this->dbHandleFunc; |
| 299 |
$this->dbs[$index] = $func( $index ); |
| 300 |
} |
| 301 |
return $this->dbs[$index]; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
protected function translateSrcParams( $function, array $params ) { |
| 312 |
$latest = !empty( $params['latest'] ); |
| 313 |
|
| 314 |
if ( isset( $params['src'] ) ) { |
| 315 |
$params['src'] = $this->getBackendPath( $params['src'], $latest ); |
| 316 |
} |
| 317 |
|
| 318 |
if ( isset( $params['srcs'] ) ) { |
| 319 |
$params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest ); |
| 320 |
} |
| 321 |
|
| 322 |
return $this->backend->$function( $params ); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
protected function translateArrayResults( $function, array $params ) { |
| 333 |
$origPaths = $params['srcs']; |
| 334 |
$params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) ); |
| 335 |
$pathMap = array_combine( $params['srcs'], $origPaths ); |
| 336 |
|
| 337 |
$results = $this->backend->$function( $params ); |
| 338 |
|
| 339 |
$contents = []; |
| 340 |
foreach ( $results as $path => $result ) { |
| 341 |
$contents[$pathMap[$path]] = $result; |
| 342 |
} |
| 343 |
|
| 344 |
return $contents; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
protected function mungeOpPaths( array $ops ) { |
| 356 |
|
| 357 |
static $srcRefOps = [ 'store', 'copy', 'describe' ]; |
| 358 |
foreach ( $ops as &$op ) { |
| 359 |
if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) { |
| 360 |
$op['src'] = $this->getBackendPath( $op['src'], true ); |
| 361 |
} |
| 362 |
if ( isset( $op['srcs'] ) ) { |
| 363 |
$op['srcs'] = $this->getBackendPaths( $op['srcs'], true ); |
| 364 |
} |
| 365 |
} |
| 366 |
return $ops; |
| 367 |
} |
| 368 |
} |