| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use MediaWiki\Context\RequestContext; |
| 11 |
use MediaWiki\Linker\LinkTarget; |
| 12 |
use MediaWiki\MainConfigNames; |
| 13 |
use MediaWiki\MediaWikiServices; |
| 14 |
use MediaWiki\Page\PageIdentity; |
| 15 |
use MediaWiki\Permissions\Authority; |
| 16 |
use MediaWiki\Status\Status; |
| 17 |
use MediaWiki\Title\Title; |
| 18 |
use MediaWiki\User\UserIdentity; |
| 19 |
use MediaWiki\Utils\MWTimestamp; |
| 20 |
use Shellbox\Command\BoxedCommand; |
| 21 |
use Wikimedia\AtEase\AtEase; |
| 22 |
use Wikimedia\FileBackend\FileBackend; |
| 23 |
use Wikimedia\FileBackend\FSFile\FSFile; |
| 24 |
use Wikimedia\FileBackend\FSFile\TempFSFile; |
| 25 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 26 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
class FileRepo { |
| 58 |
public const DELETE_SOURCE = 1; |
| 59 |
public const OVERWRITE = 2; |
| 60 |
public const OVERWRITE_SAME = 4; |
| 61 |
public const SKIP_LOCKING = 8; |
| 62 |
|
| 63 |
public const NAME_AND_TIME_ONLY = 1; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public $fetchDescription; |
| 69 |
|
| 70 |
|
| 71 |
public $descriptionCacheExpiry; |
| 72 |
|
| 73 |
|
| 74 |
protected $hasSha1Storage = false; |
| 75 |
|
| 76 |
|
| 77 |
protected $supportsSha1URLs = false; |
| 78 |
|
| 79 |
|
| 80 |
protected $backend; |
| 81 |
|
| 82 |
|
| 83 |
protected $zones = []; |
| 84 |
|
| 85 |
|
| 86 |
protected $thumbScriptUrl; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected $transformVia404; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
protected $descBaseUrl; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected $scriptDirUrl; |
| 102 |
|
| 103 |
|
| 104 |
protected $articleUrl; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
protected $initialCapital; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
protected $pathDisclosureProtection = 'simple'; |
| 119 |
|
| 120 |
|
| 121 |
protected $url; |
| 122 |
|
| 123 |
|
| 124 |
protected $thumbUrl; |
| 125 |
|
| 126 |
|
| 127 |
protected $hashLevels; |
| 128 |
|
| 129 |
|
| 130 |
protected $deletedHashLevels; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
protected $abbrvThreshold; |
| 137 |
|
| 138 |
|
| 139 |
protected $favicon = null; |
| 140 |
|
| 141 |
|
| 142 |
protected $isPrivate; |
| 143 |
|
| 144 |
|
| 145 |
protected $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ]; |
| 146 |
|
| 147 |
protected $oldFileFactory = false; |
| 148 |
|
| 149 |
protected $fileFactoryKey = false; |
| 150 |
|
| 151 |
protected $oldFileFactoryKey = false; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
protected $thumbProxyUrl; |
| 157 |
|
| 158 |
protected $thumbProxySecret; |
| 159 |
|
| 160 |
|
| 161 |
protected $disableLocalTransform = false; |
| 162 |
|
| 163 |
|
| 164 |
protected $wanCache; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public $name; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function __construct( ?array $info = null ) { |
| 179 |
|
| 180 |
if ( |
| 181 |
$info === null |
| 182 |
|| !array_key_exists( 'name', $info ) |
| 183 |
|| !array_key_exists( 'backend', $info ) |
| 184 |
) { |
| 185 |
throw new InvalidArgumentException( __CLASS__ . |
| 186 |
" requires an array of options having both 'name' and 'backend' keys.\n" ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
$this->name = $info['name']; |
| 191 |
if ( $info['backend'] instanceof FileBackend ) { |
| 192 |
$this->backend = $info['backend']; |
| 193 |
} else { |
| 194 |
$this->backend = |
| 195 |
MediaWikiServices::getInstance()->getFileBackendGroup()->get( $info['backend'] ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
$optionalSettings = [ |
| 200 |
'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription', |
| 201 |
'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry', |
| 202 |
'favicon', 'thumbProxyUrl', 'thumbProxySecret', 'disableLocalTransform' |
| 203 |
]; |
| 204 |
foreach ( $optionalSettings as $var ) { |
| 205 |
if ( isset( $info[$var] ) ) { |
| 206 |
$this->$var = $info[$var]; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
$localCapitalLinks = |
| 212 |
MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE ); |
| 213 |
$this->initialCapital = $info['initialCapital'] ?? $localCapitalLinks; |
| 214 |
if ( $localCapitalLinks && !$this->initialCapital ) { |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
throw new InvalidArgumentException( |
| 223 |
'File repos with initial capital false are not allowed on wikis where the File ' . |
| 224 |
'namespace has initial capital true' ); |
| 225 |
} |
| 226 |
|
| 227 |
$this->url = $info['url'] ?? false; |
| 228 |
$defaultThumbUrl = $this->url ? $this->url . '/thumb' : false; |
| 229 |
$this->thumbUrl = $info['thumbUrl'] ?? $defaultThumbUrl; |
| 230 |
$this->hashLevels = $info['hashLevels'] ?? 2; |
| 231 |
$this->deletedHashLevels = $info['deletedHashLevels'] ?? $this->hashLevels; |
| 232 |
$this->transformVia404 = !empty( $info['transformVia404'] ); |
| 233 |
$this->abbrvThreshold = $info['abbrvThreshold'] ?? 255; |
| 234 |
$this->isPrivate = !empty( $info['isPrivate'] ); |
| 235 |
|
| 236 |
$this->zones = $info['zones'] ?? []; |
| 237 |
foreach ( [ 'public', 'thumb', 'transcoded', 'temp', 'deleted' ] as $zone ) { |
| 238 |
if ( !isset( $this->zones[$zone]['container'] ) ) { |
| 239 |
$this->zones[$zone]['container'] = "{$this->name}-{$zone}"; |
| 240 |
} |
| 241 |
if ( !isset( $this->zones[$zone]['directory'] ) ) { |
| 242 |
$this->zones[$zone]['directory'] = ''; |
| 243 |
} |
| 244 |
if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) { |
| 245 |
$this->zones[$zone]['urlsByExt'] = []; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
$this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] ); |
| 250 |
|
| 251 |
$this->wanCache = $info['wanCache'] ?? WANObjectCache::newEmpty(); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function getBackend() { |
| 260 |
return $this->backend; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public function getReadOnlyReason() { |
| 270 |
return $this->backend->getReadOnlyReason(); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
protected function initZones( $doZones = [] ): void { |
| 279 |
foreach ( (array)$doZones as $zone ) { |
| 280 |
$root = $this->getZonePath( $zone ); |
| 281 |
if ( $root === null ) { |
| 282 |
throw new RuntimeException( "No '$zone' zone defined in the {$this->name} repo." ); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public static function isVirtualUrl( $url ) { |
| 294 |
return str_starts_with( $url, 'mwrepo://' ); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public function getVirtualUrl( $suffix = false ) { |
| 306 |
$path = 'mwrepo://' . $this->name; |
| 307 |
if ( $suffix !== false ) { |
| 308 |
$path .= '/' . rawurlencode( $suffix ); |
| 309 |
} |
| 310 |
|
| 311 |
return $path; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public function getZoneUrl( $zone, $ext = null ) { |
| 322 |
if ( in_array( $zone, [ 'public', 'thumb', 'transcoded' ] ) ) { |
| 323 |
|
| 324 |
if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) { |
| 325 |
|
| 326 |
|
| 327 |
return $this->zones[$zone]['urlsByExt'][$ext]; |
| 328 |
} elseif ( isset( $this->zones[$zone]['url'] ) ) { |
| 329 |
|
| 330 |
return $this->zones[$zone]['url']; |
| 331 |
} |
| 332 |
} |
| 333 |
switch ( $zone ) { |
| 334 |
case 'public': |
| 335 |
return $this->url; |
| 336 |
case 'temp': |
| 337 |
case 'deleted': |
| 338 |
return false; |
| 339 |
case 'thumb': |
| 340 |
return $this->thumbUrl; |
| 341 |
case 'transcoded': |
| 342 |
return "{$this->url}/transcoded"; |
| 343 |
default: |
| 344 |
return false; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function backendSupportsUnicodePaths() { |
| 352 |
return (bool)( $this->getBackend()->getFeatures() & FileBackend::ATTR_UNICODE_PATHS ); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function resolveVirtualUrl( $url ) { |
| 364 |
if ( !str_starts_with( $url, 'mwrepo://' ) ) { |
| 365 |
throw new InvalidArgumentException( __METHOD__ . ': unknown protocol' ); |
| 366 |
} |
| 367 |
$bits = explode( '/', substr( $url, 9 ), 3 ); |
| 368 |
if ( count( $bits ) != 3 ) { |
| 369 |
throw new InvalidArgumentException( __METHOD__ . ": invalid mwrepo URL: $url" ); |
| 370 |
} |
| 371 |
[ $repo, $zone, $rel ] = $bits; |
| 372 |
if ( $repo !== $this->name ) { |
| 373 |
throw new InvalidArgumentException( __METHOD__ . ": fetching from a foreign repo is not supported" ); |
| 374 |
} |
| 375 |
$base = $this->getZonePath( $zone ); |
| 376 |
if ( !$base ) { |
| 377 |
throw new InvalidArgumentException( __METHOD__ . ": invalid zone: $zone" ); |
| 378 |
} |
| 379 |
|
| 380 |
return $base . '/' . rawurldecode( $rel ); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
protected function getZoneLocation( $zone ) { |
| 390 |
if ( !isset( $this->zones[$zone] ) ) { |
| 391 |
return [ null, null ]; |
| 392 |
} |
| 393 |
|
| 394 |
return [ $this->zones[$zone]['container'], $this->zones[$zone]['directory'] ]; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public function getZonePath( $zone ) { |
| 404 |
[ $container, $base ] = $this->getZoneLocation( $zone ); |
| 405 |
if ( $container === null || $base === null ) { |
| 406 |
return null; |
| 407 |
} |
| 408 |
$backendName = $this->backend->getName(); |
| 409 |
if ( $base != '' ) { |
| 410 |
$base = "/{$base}"; |
| 411 |
} |
| 412 |
|
| 413 |
return "mwstore://$backendName/{$container}{$base}"; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function newFile( $title, $time = false ) { |
| 428 |
$title = File::normalizeTitle( $title ); |
| 429 |
if ( !$title ) { |
| 430 |
return null; |
| 431 |
} |
| 432 |
if ( $time ) { |
| 433 |
if ( $this->oldFileFactory ) { |
| 434 |
return call_user_func( $this->oldFileFactory, $title, $this, $time ); |
| 435 |
} else { |
| 436 |
return null; |
| 437 |
} |
| 438 |
} else { |
| 439 |
return call_user_func( $this->fileFactory, $title, $this ); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
public function findFile( $title, $options = [] ) { |
| 463 |
if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) { |
| 464 |
throw new InvalidArgumentException( |
| 465 |
__METHOD__ . ' called with the `private` option set to something ' . |
| 466 |
'other than an Authority object' |
| 467 |
); |
| 468 |
} |
| 469 |
|
| 470 |
$title = File::normalizeTitle( $title ); |
| 471 |
if ( !$title ) { |
| 472 |
return false; |
| 473 |
} |
| 474 |
if ( isset( $options['bypassCache'] ) ) { |
| 475 |
$options['latest'] = $options['bypassCache']; |
| 476 |
} |
| 477 |
$time = $options['time'] ?? false; |
| 478 |
$flags = !empty( $options['latest'] ) ? IDBAccessObject::READ_LATEST : 0; |
| 479 |
|
| 480 |
$img = $this->newFile( $title ); |
| 481 |
if ( !$img ) { |
| 482 |
return false; |
| 483 |
} |
| 484 |
$img->load( $flags ); |
| 485 |
if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) { |
| 486 |
return $img; |
| 487 |
} |
| 488 |
|
| 489 |
if ( $time !== false ) { |
| 490 |
$img = $this->newFile( $title, $time ); |
| 491 |
if ( $img ) { |
| 492 |
$img->load( $flags ); |
| 493 |
if ( $img->exists() ) { |
| 494 |
if ( !$img->isDeleted( File::DELETED_FILE ) ) { |
| 495 |
return $img; |
| 496 |
} elseif ( |
| 497 |
|
| 498 |
!empty( $options['private'] ) && |
| 499 |
$img->userCan( File::DELETED_FILE, $options['private'] ) |
| 500 |
) { |
| 501 |
return $img; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
if ( !empty( $options['ignoreRedirect'] ) ) { |
| 509 |
return false; |
| 510 |
} |
| 511 |
$redir = $this->checkRedirect( $title ); |
| 512 |
if ( $redir && $title->getNamespace() === NS_FILE ) { |
| 513 |
$img = $this->newFile( $redir ); |
| 514 |
if ( !$img ) { |
| 515 |
return false; |
| 516 |
} |
| 517 |
$img->load( $flags ); |
| 518 |
if ( $img->exists() ) { |
| 519 |
$img->redirectedFrom( $title->getDBkey() ); |
| 520 |
|
| 521 |
return $img; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
return false; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public function findFiles( array $items, $flags = 0 ) { |
| 546 |
$result = []; |
| 547 |
foreach ( $items as $item ) { |
| 548 |
if ( is_array( $item ) ) { |
| 549 |
$title = $item['title']; |
| 550 |
$options = $item; |
| 551 |
unset( $options['title'] ); |
| 552 |
|
| 553 |
if ( |
| 554 |
!empty( $options['private'] ) && |
| 555 |
!( $options['private'] instanceof Authority ) |
| 556 |
) { |
| 557 |
$options['private'] = RequestContext::getMain()->getAuthority(); |
| 558 |
} |
| 559 |
} else { |
| 560 |
$title = $item; |
| 561 |
$options = []; |
| 562 |
} |
| 563 |
$file = $this->findFile( $title, $options ); |
| 564 |
if ( $file ) { |
| 565 |
$searchName = File::normalizeTitle( $title )->getDBkey(); |
| 566 |
if ( $flags & self::NAME_AND_TIME_ONLY ) { |
| 567 |
$result[$searchName] = [ |
| 568 |
'title' => $file->getTitle()->getDBkey(), |
| 569 |
'timestamp' => $file->getTimestamp() |
| 570 |
]; |
| 571 |
} else { |
| 572 |
$result[$searchName] = $file; |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
return $result; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public function findFileFromKey( $sha1, $options = [] ) { |
| 591 |
if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) { |
| 592 |
throw new InvalidArgumentException( |
| 593 |
__METHOD__ . ' called with the `private` option set to something ' . |
| 594 |
'other than an Authority object' |
| 595 |
); |
| 596 |
} |
| 597 |
|
| 598 |
$time = $options['time'] ?? false; |
| 599 |
|
| 600 |
if ( !$this->fileFactoryKey ) { |
| 601 |
return false; |
| 602 |
} |
| 603 |
$img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time ); |
| 604 |
if ( $img && $img->exists() ) { |
| 605 |
return $img; |
| 606 |
} |
| 607 |
|
| 608 |
if ( $time !== false && $this->oldFileFactoryKey ) { |
| 609 |
$img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time ); |
| 610 |
if ( $img && $img->exists() ) { |
| 611 |
if ( !$img->isDeleted( File::DELETED_FILE ) ) { |
| 612 |
return $img; |
| 613 |
} elseif ( |
| 614 |
|
| 615 |
!empty( $options['private'] ) && |
| 616 |
$img->userCan( File::DELETED_FILE, $options['private'] ) |
| 617 |
) { |
| 618 |
return $img; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
return false; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public function findBySha1( $hash ) { |
| 635 |
return []; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
public function findBySha1s( array $hashes ) { |
| 646 |
$result = []; |
| 647 |
foreach ( $hashes as $hash ) { |
| 648 |
$files = $this->findBySha1( $hash ); |
| 649 |
if ( count( $files ) ) { |
| 650 |
$result[$hash] = $files; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
return $result; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public function findFilesByPrefix( $prefix, $limit ) { |
| 666 |
return []; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
public function getThumbScriptUrl() { |
| 675 |
return $this->thumbScriptUrl; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
public function getThumbProxyUrl() { |
| 684 |
return $this->thumbProxyUrl; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
public function getThumbProxySecret() { |
| 693 |
return $this->thumbProxySecret; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
public function canTransformVia404() { |
| 702 |
return $this->transformVia404; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
public function canTransformLocally() { |
| 712 |
return !$this->disableLocalTransform; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
public function getNameFromTitle( $title ) { |
| 722 |
if ( |
| 723 |
$this->initialCapital != |
| 724 |
MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE ) |
| 725 |
) { |
| 726 |
$name = $title->getDBkey(); |
| 727 |
if ( $this->initialCapital ) { |
| 728 |
$name = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $name ); |
| 729 |
} |
| 730 |
} else { |
| 731 |
$name = $title->getDBkey(); |
| 732 |
} |
| 733 |
|
| 734 |
return $name; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
public function getRootDirectory() { |
| 743 |
return $this->getZonePath( 'public' ); |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
public function getHashPath( $name ) { |
| 754 |
return self::getHashPathForLevel( $name, $this->hashLevels ); |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
public function getTempHashPath( $suffix ) { |
| 765 |
$parts = explode( '!', $suffix, 2 ); |
| 766 |
$name = $parts[1] ?? $suffix; |
| 767 |
return self::getHashPathForLevel( $name, $this->hashLevels ); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
protected static function getHashPathForLevel( $name, $levels ) { |
| 776 |
if ( $levels == 0 ) { |
| 777 |
return ''; |
| 778 |
} else { |
| 779 |
$hash = md5( $name ); |
| 780 |
$path = ''; |
| 781 |
for ( $i = 1; $i <= $levels; $i++ ) { |
| 782 |
$path .= substr( $hash, 0, $i ) . '/'; |
| 783 |
} |
| 784 |
|
| 785 |
return $path; |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
public function getHashLevels() { |
| 795 |
return $this->hashLevels; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
public function getName() { |
| 804 |
return $this->name; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
public function makeUrl( $query = '', $entry = 'index' ) { |
| 815 |
if ( isset( $this->scriptDirUrl ) ) { |
| 816 |
return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}.php", $query ); |
| 817 |
} |
| 818 |
|
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
public function getDescriptionUrl( $name ) { |
| 835 |
$encName = wfUrlencode( $name ); |
| 836 |
if ( $this->descBaseUrl !== null ) { |
| 837 |
|
| 838 |
return $this->descBaseUrl . $encName; |
| 839 |
} |
| 840 |
if ( $this->articleUrl !== null ) { |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
return str_replace( '$1', |
| 845 |
"Image:$encName", $this->articleUrl ); |
| 846 |
} |
| 847 |
if ( $this->scriptDirUrl !== null ) { |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
return $this->makeUrl( "title=Image:$encName" ); |
| 853 |
} |
| 854 |
|
| 855 |
return false; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
public function getDescriptionRenderUrl( $name, $lang = null ) { |
| 869 |
$query = 'action=render'; |
| 870 |
if ( $lang !== null ) { |
| 871 |
$query .= '&uselang=' . urlencode( $lang ); |
| 872 |
} |
| 873 |
if ( isset( $this->scriptDirUrl ) ) { |
| 874 |
return $this->makeUrl( |
| 875 |
'title=' . |
| 876 |
wfUrlencode( 'Image:' . $name ) . |
| 877 |
"&$query" ); |
| 878 |
} else { |
| 879 |
$descUrl = $this->getDescriptionUrl( $name ); |
| 880 |
if ( $descUrl ) { |
| 881 |
return wfAppendQuery( $descUrl, $query ); |
| 882 |
} else { |
| 883 |
return false; |
| 884 |
} |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
public function getDescriptionStylesheetUrl() { |
| 894 |
if ( isset( $this->scriptDirUrl ) ) { |
| 895 |
|
| 896 |
|
| 897 |
return $this->makeUrl( 'title=MediaWiki:Filepage.css&action=raw&ctype=text/css' ); |
| 898 |
} |
| 899 |
|
| 900 |
return false; |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) { |
| 921 |
$this->assertWritableRepo(); |
| 922 |
|
| 923 |
$status = $this->storeBatch( [ [ $srcPath, $dstZone, $dstRel ] ], $flags ); |
| 924 |
if ( $status->successCount == 0 ) { |
| 925 |
$status->setOK( false ); |
| 926 |
} |
| 927 |
|
| 928 |
return $status; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
public function storeBatch( array $triplets, $flags = 0 ) { |
| 945 |
$this->assertWritableRepo(); |
| 946 |
|
| 947 |
if ( $flags & self::DELETE_SOURCE ) { |
| 948 |
throw new InvalidArgumentException( "DELETE_SOURCE not supported in " . __METHOD__ ); |
| 949 |
} |
| 950 |
|
| 951 |
$status = $this->newGood(); |
| 952 |
$backend = $this->backend; |
| 953 |
|
| 954 |
$operations = []; |
| 955 |
|
| 956 |
foreach ( $triplets as [ $src, $dstZone, $dstRel ] ) { |
| 957 |
$srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src; |
| 958 |
wfDebug( __METHOD__ |
| 959 |
. "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )" |
| 960 |
); |
| 961 |
|
| 962 |
if ( $src instanceof FSFile ) { |
| 963 |
$op = 'store'; |
| 964 |
} else { |
| 965 |
$src = $this->resolveToStoragePathIfVirtual( $src ); |
| 966 |
$op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store'; |
| 967 |
} |
| 968 |
|
| 969 |
$root = $this->getZonePath( $dstZone ); |
| 970 |
if ( !$root ) { |
| 971 |
throw new RuntimeException( "Invalid zone: $dstZone" ); |
| 972 |
} |
| 973 |
if ( !$this->validateFilename( $dstRel ) ) { |
| 974 |
throw new RuntimeException( 'Validation error in $dstRel' ); |
| 975 |
} |
| 976 |
$dstPath = "$root/$dstRel"; |
| 977 |
$dstDir = dirname( $dstPath ); |
| 978 |
|
| 979 |
if ( !$this->initDirectory( $dstDir )->isOK() ) { |
| 980 |
return $this->newFatal( 'directorycreateerror', $dstDir ); |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
$operations[] = [ |
| 985 |
'op' => $op, |
| 986 |
'src' => $src, |
| 987 |
'dst' => $dstPath, |
| 988 |
'overwrite' => (bool)( $flags & self::OVERWRITE ), |
| 989 |
'overwriteSame' => (bool)( $flags & self::OVERWRITE_SAME ), |
| 990 |
]; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
$opts = [ 'force' => true ]; |
| 995 |
if ( $flags & self::SKIP_LOCKING ) { |
| 996 |
$opts['nonLocking'] = true; |
| 997 |
} |
| 998 |
|
| 999 |
return $status->merge( $backend->doOperations( $operations, $opts ) ); |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
public function cleanupBatch( array $files, $flags = 0 ) { |
| 1013 |
$this->assertWritableRepo(); |
| 1014 |
|
| 1015 |
$status = $this->newGood(); |
| 1016 |
|
| 1017 |
$operations = []; |
| 1018 |
foreach ( $files as $path ) { |
| 1019 |
if ( is_array( $path ) ) { |
| 1020 |
|
| 1021 |
[ $zone, $rel ] = $path; |
| 1022 |
$path = $this->getZonePath( $zone ) . "/$rel"; |
| 1023 |
} else { |
| 1024 |
|
| 1025 |
$path = $this->resolveToStoragePathIfVirtual( $path ); |
| 1026 |
} |
| 1027 |
$operations[] = [ 'op' => 'delete', 'src' => $path ]; |
| 1028 |
} |
| 1029 |
|
| 1030 |
$opts = [ 'force' => true ]; |
| 1031 |
if ( $flags & self::SKIP_LOCKING ) { |
| 1032 |
$opts['nonLocking'] = true; |
| 1033 |
} |
| 1034 |
|
| 1035 |
return $status->merge( $this->backend->doOperations( $operations, $opts ) ); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
final public function quickImport( $src, $dst, $options = null ) { |
| 1056 |
return $this->quickImportBatch( [ [ $src, $dst, $options ] ] ); |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
public function quickImportBatch( array $triples ) { |
| 1074 |
$status = $this->newGood(); |
| 1075 |
$operations = []; |
| 1076 |
foreach ( $triples as $triple ) { |
| 1077 |
[ $src, $dst ] = $triple; |
| 1078 |
if ( $src instanceof FSFile ) { |
| 1079 |
$op = 'store'; |
| 1080 |
} else { |
| 1081 |
$src = $this->resolveToStoragePathIfVirtual( $src ); |
| 1082 |
$op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store'; |
| 1083 |
} |
| 1084 |
$dst = $this->resolveToStoragePathIfVirtual( $dst ); |
| 1085 |
|
| 1086 |
if ( !isset( $triple[2] ) ) { |
| 1087 |
$headers = []; |
| 1088 |
} elseif ( is_string( $triple[2] ) ) { |
| 1089 |
|
| 1090 |
$headers = [ 'Content-Disposition' => $triple[2] ]; |
| 1091 |
} elseif ( is_array( $triple[2] ) && isset( $triple[2]['headers'] ) ) { |
| 1092 |
$headers = $triple[2]['headers']; |
| 1093 |
} else { |
| 1094 |
$headers = []; |
| 1095 |
} |
| 1096 |
|
| 1097 |
$operations[] = [ |
| 1098 |
'op' => $op, |
| 1099 |
'src' => $src, |
| 1100 |
'dst' => $dst, |
| 1101 |
'headers' => $headers |
| 1102 |
]; |
| 1103 |
$status->merge( $this->initDirectory( dirname( $dst ) ) ); |
| 1104 |
} |
| 1105 |
|
| 1106 |
return $status->merge( $this->backend->doQuickOperations( $operations ) ); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
final public function quickPurge( $path ) { |
| 1118 |
return $this->quickPurgeBatch( [ $path ] ); |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
public function quickCleanDir( $dir ) { |
| 1129 |
return $this->newGood()->merge( |
| 1130 |
$this->backend->clean( |
| 1131 |
[ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ] |
| 1132 |
) |
| 1133 |
); |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
public function quickPurgeBatch( array $paths ) { |
| 1145 |
$status = $this->newGood(); |
| 1146 |
$operations = []; |
| 1147 |
foreach ( $paths as $path ) { |
| 1148 |
$operations[] = [ |
| 1149 |
'op' => 'delete', |
| 1150 |
'src' => $this->resolveToStoragePathIfVirtual( $path ), |
| 1151 |
'ignoreMissingSource' => true |
| 1152 |
]; |
| 1153 |
} |
| 1154 |
$status->merge( $this->backend->doQuickOperations( $operations ) ); |
| 1155 |
|
| 1156 |
return $status; |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
public function storeTemp( $originalName, $srcPath ) { |
| 1170 |
$this->assertWritableRepo(); |
| 1171 |
|
| 1172 |
$date = MWTimestamp::getInstance()->format( 'YmdHis' ); |
| 1173 |
$hashPath = $this->getHashPath( $originalName ); |
| 1174 |
$dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName ); |
| 1175 |
$virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel; |
| 1176 |
|
| 1177 |
$result = $this->quickImport( $srcPath, $virtualUrl ); |
| 1178 |
$result->value = $virtualUrl; |
| 1179 |
|
| 1180 |
return $result; |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
public function freeTemp( $virtualUrl ) { |
| 1190 |
$this->assertWritableRepo(); |
| 1191 |
|
| 1192 |
$temp = $this->getVirtualUrl( 'temp' ); |
| 1193 |
if ( !str_starts_with( $virtualUrl, $temp ) ) { |
| 1194 |
wfDebug( __METHOD__ . ": Invalid temp virtual URL" ); |
| 1195 |
|
| 1196 |
return false; |
| 1197 |
} |
| 1198 |
|
| 1199 |
return $this->quickPurge( $virtualUrl )->isOK(); |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) { |
| 1212 |
$this->assertWritableRepo(); |
| 1213 |
|
| 1214 |
$status = $this->newGood(); |
| 1215 |
|
| 1216 |
$sources = []; |
| 1217 |
foreach ( $srcPaths as $srcPath ) { |
| 1218 |
|
| 1219 |
$source = $this->resolveToStoragePathIfVirtual( $srcPath ); |
| 1220 |
$sources[] = $source; |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
$params = [ 'srcs' => $sources, 'dst' => $dstPath ]; |
| 1225 |
$status->merge( $this->backend->concatenate( $params ) ); |
| 1226 |
if ( !$status->isOK() ) { |
| 1227 |
return $status; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
if ( $flags & self::DELETE_SOURCE ) { |
| 1232 |
$status->merge( $this->quickPurgeBatch( $srcPaths ) ); |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
$status->setResult( true ); |
| 1237 |
|
| 1238 |
return $status; |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
public function publish( |
| 1265 |
$src, $dstRel, $archiveRel, $flags = 0, array $options = [] |
| 1266 |
) { |
| 1267 |
$this->assertWritableRepo(); |
| 1268 |
|
| 1269 |
$status = $this->publishBatch( |
| 1270 |
[ [ $src, $dstRel, $archiveRel, $options ] ], $flags ); |
| 1271 |
if ( $status->successCount == 0 ) { |
| 1272 |
$status->setOK( false ); |
| 1273 |
} |
| 1274 |
$status->value = $status->value[0] ?? false; |
| 1275 |
|
| 1276 |
return $status; |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
public function publishBatch( array $ntuples, $flags = 0 ) { |
| 1291 |
$this->assertWritableRepo(); |
| 1292 |
|
| 1293 |
$backend = $this->backend; |
| 1294 |
|
| 1295 |
$this->initZones( 'public' ); |
| 1296 |
|
| 1297 |
$status = $this->newGood( [] ); |
| 1298 |
|
| 1299 |
$operations = []; |
| 1300 |
$sourceFSFilesToDelete = []; |
| 1301 |
|
| 1302 |
foreach ( $ntuples as $ntuple ) { |
| 1303 |
[ $src, $dstRel, $archiveRel ] = $ntuple; |
| 1304 |
$srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src; |
| 1305 |
|
| 1306 |
$options = $ntuple[3] ?? []; |
| 1307 |
|
| 1308 |
$srcPath = $this->resolveToStoragePathIfVirtual( $srcPath ); |
| 1309 |
if ( !$this->validateFilename( $dstRel ) ) { |
| 1310 |
throw new RuntimeException( 'Validation error in $dstRel' ); |
| 1311 |
} |
| 1312 |
if ( !$this->validateFilename( $archiveRel ) ) { |
| 1313 |
throw new RuntimeException( 'Validation error in $archiveRel' ); |
| 1314 |
} |
| 1315 |
|
| 1316 |
$publicRoot = $this->getZonePath( 'public' ); |
| 1317 |
$dstPath = "$publicRoot/$dstRel"; |
| 1318 |
$archivePath = "$publicRoot/$archiveRel"; |
| 1319 |
|
| 1320 |
$dstDir = dirname( $dstPath ); |
| 1321 |
$archiveDir = dirname( $archivePath ); |
| 1322 |
|
| 1323 |
if ( !$this->initDirectory( $dstDir )->isOK() ) { |
| 1324 |
return $this->newFatal( 'directorycreateerror', $dstDir ); |
| 1325 |
} |
| 1326 |
if ( !$this->initDirectory( $archiveDir )->isOK() ) { |
| 1327 |
return $this->newFatal( 'directorycreateerror', $archiveDir ); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
$headers = $options['headers'] ?? []; |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
$operations[] = [ |
| 1340 |
'op' => 'copy', |
| 1341 |
'src' => $dstPath, |
| 1342 |
'dst' => $archivePath, |
| 1343 |
'ignoreMissingSource' => true |
| 1344 |
]; |
| 1345 |
|
| 1346 |
|
| 1347 |
if ( FileBackend::isStoragePath( $srcPath ) ) { |
| 1348 |
$operations[] = [ |
| 1349 |
'op' => ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy', |
| 1350 |
'src' => $srcPath, |
| 1351 |
'dst' => $dstPath, |
| 1352 |
'overwrite' => true, |
| 1353 |
'headers' => $headers |
| 1354 |
]; |
| 1355 |
} else { |
| 1356 |
$operations[] = [ |
| 1357 |
'op' => 'store', |
| 1358 |
'src' => $src, |
| 1359 |
'dst' => $dstPath, |
| 1360 |
'overwrite' => true, |
| 1361 |
'headers' => $headers |
| 1362 |
]; |
| 1363 |
if ( $flags & self::DELETE_SOURCE ) { |
| 1364 |
$sourceFSFilesToDelete[] = $srcPath; |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
$status->merge( $backend->doOperations( $operations ) ); |
| 1371 |
|
| 1372 |
foreach ( $ntuples as $i => $ntuple ) { |
| 1373 |
[ , , $archiveRel ] = $ntuple; |
| 1374 |
$archivePath = $this->getZonePath( 'public' ) . "/$archiveRel"; |
| 1375 |
if ( $this->fileExists( $archivePath ) ) { |
| 1376 |
$status->value[$i] = 'archived'; |
| 1377 |
} else { |
| 1378 |
$status->value[$i] = 'new'; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
foreach ( $sourceFSFilesToDelete as $file ) { |
| 1383 |
AtEase::suppressWarnings(); |
| 1384 |
unlink( $file ); |
| 1385 |
AtEase::restoreWarnings(); |
| 1386 |
} |
| 1387 |
|
| 1388 |
return $status; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
protected function initDirectory( $dir ) { |
| 1399 |
$path = $this->resolveToStoragePathIfVirtual( $dir ); |
| 1400 |
[ , $container, ] = FileBackend::splitStoragePath( $path ); |
| 1401 |
|
| 1402 |
$params = [ 'dir' => $path ]; |
| 1403 |
if ( $this->isPrivate |
| 1404 |
|| $container === $this->zones['deleted']['container'] |
| 1405 |
|| $container === $this->zones['temp']['container'] |
| 1406 |
) { |
| 1407 |
|
| 1408 |
|
| 1409 |
$params = [ 'noAccess' => true, 'noListing' => true ] + $params; |
| 1410 |
} |
| 1411 |
|
| 1412 |
return $this->newGood()->merge( $this->backend->prepare( $params ) ); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
public function cleanDir( $dir ) { |
| 1422 |
$this->assertWritableRepo(); |
| 1423 |
|
| 1424 |
return $this->newGood()->merge( |
| 1425 |
$this->backend->clean( |
| 1426 |
[ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ] |
| 1427 |
) |
| 1428 |
); |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
public function fileExists( $file ) { |
| 1438 |
$result = $this->fileExistsBatch( [ $file ] ); |
| 1439 |
|
| 1440 |
return $result[0]; |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
public function fileExistsBatch( array $files ) { |
| 1451 |
$paths = array_map( [ $this, 'resolveToStoragePathIfVirtual' ], $files ); |
| 1452 |
$this->backend->preloadFileStat( [ 'srcs' => $paths ] ); |
| 1453 |
|
| 1454 |
$result = []; |
| 1455 |
foreach ( $paths as $key => $path ) { |
| 1456 |
$result[$key] = $this->backend->fileExists( [ 'src' => $path ] ); |
| 1457 |
} |
| 1458 |
|
| 1459 |
return $result; |
| 1460 |
} |
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
public function delete( $srcRel, $archiveRel ) { |
| 1473 |
$this->assertWritableRepo(); |
| 1474 |
|
| 1475 |
return $this->deleteBatch( [ [ $srcRel, $archiveRel ] ] ); |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
public function deleteBatch( array $sourceDestPairs ) { |
| 1495 |
$this->assertWritableRepo(); |
| 1496 |
|
| 1497 |
|
| 1498 |
$this->initZones( [ 'public', 'deleted' ] ); |
| 1499 |
|
| 1500 |
$status = $this->newGood(); |
| 1501 |
|
| 1502 |
$backend = $this->backend; |
| 1503 |
$operations = []; |
| 1504 |
|
| 1505 |
foreach ( $sourceDestPairs as [ $srcRel, $archiveRel ] ) { |
| 1506 |
if ( !$this->validateFilename( $srcRel ) ) { |
| 1507 |
throw new RuntimeException( __METHOD__ . ':Validation error in $srcRel' ); |
| 1508 |
} elseif ( !$this->validateFilename( $archiveRel ) ) { |
| 1509 |
throw new RuntimeException( __METHOD__ . ':Validation error in $archiveRel' ); |
| 1510 |
} |
| 1511 |
|
| 1512 |
$publicRoot = $this->getZonePath( 'public' ); |
| 1513 |
$srcPath = "{$publicRoot}/$srcRel"; |
| 1514 |
|
| 1515 |
$deletedRoot = $this->getZonePath( 'deleted' ); |
| 1516 |
$archivePath = "{$deletedRoot}/{$archiveRel}"; |
| 1517 |
$archiveDir = dirname( $archivePath ); |
| 1518 |
|
| 1519 |
|
| 1520 |
if ( !$this->initDirectory( $archiveDir )->isGood() ) { |
| 1521 |
return $this->newFatal( 'directorycreateerror', $archiveDir ); |
| 1522 |
} |
| 1523 |
|
| 1524 |
$operations[] = [ |
| 1525 |
'op' => 'move', |
| 1526 |
'src' => $srcPath, |
| 1527 |
'dst' => $archivePath, |
| 1528 |
|
| 1529 |
|
| 1530 |
'overwriteSame' => true |
| 1531 |
]; |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
$opts = [ 'force' => true ]; |
| 1538 |
return $status->merge( $backend->doOperations( $operations, $opts ) ); |
| 1539 |
} |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
public function cleanupDeletedBatch( array $storageKeys ) { |
| 1548 |
$this->assertWritableRepo(); |
| 1549 |
} |
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
public function getDeletedHashPath( $key ) { |
| 1559 |
if ( strlen( $key ) < 31 ) { |
| 1560 |
throw new InvalidArgumentException( "Invalid storage key '$key'." ); |
| 1561 |
} |
| 1562 |
$path = ''; |
| 1563 |
for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) { |
| 1564 |
$path .= $key[$i] . '/'; |
| 1565 |
} |
| 1566 |
|
| 1567 |
return $path; |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
protected function resolveToStoragePathIfVirtual( $path ) { |
| 1578 |
if ( self::isVirtualUrl( $path ) ) { |
| 1579 |
return $this->resolveVirtualUrl( $path ); |
| 1580 |
} |
| 1581 |
|
| 1582 |
return $path; |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
public function getLocalCopy( $virtualUrl ) { |
| 1593 |
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl ); |
| 1594 |
|
| 1595 |
return $this->backend->getLocalCopy( [ 'src' => $path ] ); |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
public function getLocalReference( $virtualUrl ) { |
| 1607 |
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl ); |
| 1608 |
|
| 1609 |
return $this->backend->getLocalReference( [ 'src' => $path ] ); |
| 1610 |
} |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
public function addShellboxInputFile( BoxedCommand $command, string $boxedName, |
| 1622 |
string $virtualUrl |
| 1623 |
) { |
| 1624 |
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl ); |
| 1625 |
|
| 1626 |
return $this->backend->addShellboxInputFile( $command, $boxedName, [ 'src' => $path ] ); |
| 1627 |
} |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
public function getFileProps( $virtualUrl ) { |
| 1637 |
$fsFile = $this->getLocalReference( $virtualUrl ); |
| 1638 |
$mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() ); |
| 1639 |
if ( $fsFile ) { |
| 1640 |
$props = $mwProps->getPropsFromPath( $fsFile->getPath(), true ); |
| 1641 |
} else { |
| 1642 |
$props = $mwProps->newPlaceholderProps(); |
| 1643 |
} |
| 1644 |
|
| 1645 |
return $props; |
| 1646 |
} |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
public function getFileTimestamp( $virtualUrl ) { |
| 1655 |
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl ); |
| 1656 |
|
| 1657 |
return $this->backend->getFileTimestamp( [ 'src' => $path ] ); |
| 1658 |
} |
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
public function getFileSize( $virtualUrl ) { |
| 1667 |
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl ); |
| 1668 |
|
| 1669 |
return $this->backend->getFileSize( [ 'src' => $path ] ); |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
public function getFileSha1( $virtualUrl ) { |
| 1679 |
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl ); |
| 1680 |
|
| 1681 |
return $this->backend->getFileSha1Base36( [ 'src' => $path ] ); |
| 1682 |
} |
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) { |
| 1694 |
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl ); |
| 1695 |
$params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ]; |
| 1696 |
|
| 1697 |
|
| 1698 |
ob_start( null, 1_048_576 ); |
| 1699 |
ob_implicit_flush( true ); |
| 1700 |
|
| 1701 |
$status = $this->newGood()->merge( $this->backend->streamFile( $params ) ); |
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
if ( ob_get_status() ) { |
| 1706 |
ob_end_flush(); |
| 1707 |
} |
| 1708 |
|
| 1709 |
return $status; |
| 1710 |
} |
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
public function enumFiles( $callback ) { |
| 1721 |
$this->enumFilesInStorage( $callback ); |
| 1722 |
} |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
protected function enumFilesInStorage( $callback ) { |
| 1732 |
$publicRoot = $this->getZonePath( 'public' ); |
| 1733 |
$numDirs = 1 << ( $this->hashLevels * 4 ); |
| 1734 |
|
| 1735 |
|
| 1736 |
for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) { |
| 1737 |
$hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex ); |
| 1738 |
$path = $publicRoot; |
| 1739 |
for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) { |
| 1740 |
$path .= '/' . substr( $hexString, 0, $hexPos + 1 ); |
| 1741 |
} |
| 1742 |
$iterator = $this->backend->getFileList( [ 'dir' => $path ] ); |
| 1743 |
if ( $iterator === null ) { |
| 1744 |
throw new RuntimeException( __METHOD__ . ': could not get file listing for ' . $path ); |
| 1745 |
} |
| 1746 |
foreach ( $iterator as $name ) { |
| 1747 |
|
| 1748 |
call_user_func( $callback, "{$path}/{$name}" ); |
| 1749 |
} |
| 1750 |
} |
| 1751 |
} |
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
public function validateFilename( $filename ) { |
| 1760 |
if ( strval( $filename ) == '' ) { |
| 1761 |
return false; |
| 1762 |
} |
| 1763 |
|
| 1764 |
return FileBackend::isPathTraversalFree( $filename ); |
| 1765 |
} |
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
private function getErrorCleanupFunction() { |
| 1773 |
switch ( $this->pathDisclosureProtection ) { |
| 1774 |
case 'none': |
| 1775 |
case 'simple': |
| 1776 |
$callback = [ $this, 'passThrough' ]; |
| 1777 |
break; |
| 1778 |
default: |
| 1779 |
$callback = [ $this, 'paranoidClean' ]; |
| 1780 |
} |
| 1781 |
return $callback; |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
public function paranoidClean( $param ) { |
| 1791 |
return '[hidden]'; |
| 1792 |
} |
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
public function passThrough( $param ) { |
| 1801 |
return $param; |
| 1802 |
} |
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
public function newFatal( $message, ...$parameters ) { |
| 1812 |
$status = Status::newFatal( $message, ...$parameters ); |
| 1813 |
$status->cleanCallback = $this->getErrorCleanupFunction(); |
| 1814 |
|
| 1815 |
return $status; |
| 1816 |
} |
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
public function newGood( $value = null ) { |
| 1825 |
$status = Status::newGood( $value ); |
| 1826 |
$status->cleanCallback = $this->getErrorCleanupFunction(); |
| 1827 |
|
| 1828 |
return $status; |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
public function checkRedirect( $title ) { |
| 1840 |
return false; |
| 1841 |
} |
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
public function invalidateImageRedirect( $title ) { |
| 1851 |
} |
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
public function getDisplayName() { |
| 1859 |
$sitename = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Sitename ); |
| 1860 |
|
| 1861 |
if ( $this->isLocal() ) { |
| 1862 |
return $sitename; |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text(); |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
public function nameForThumb( $name ) { |
| 1877 |
if ( strlen( $name ) > $this->abbrvThreshold ) { |
| 1878 |
$ext = FileBackend::extensionFromPath( $name ); |
| 1879 |
$name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext"; |
| 1880 |
} |
| 1881 |
|
| 1882 |
return $name; |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
public function isLocal() { |
| 1891 |
return $this->getName() == 'local'; |
| 1892 |
} |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
public function getSharedCacheKey( $kClassSuffix, ...$components ) { |
| 1906 |
return false; |
| 1907 |
} |
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
public function getLocalCacheKey( $kClassSuffix, ...$components ) { |
| 1921 |
return $this->wanCache->makeKey( |
| 1922 |
'filerepo-' . $kClassSuffix, |
| 1923 |
$this->getName(), |
| 1924 |
...$components |
| 1925 |
); |
| 1926 |
} |
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
public function getTempRepo() { |
| 1937 |
return new TempFileRepo( [ |
| 1938 |
'name' => "{$this->name}-temp", |
| 1939 |
'backend' => $this->backend, |
| 1940 |
'zones' => [ |
| 1941 |
'public' => [ |
| 1942 |
|
| 1943 |
|
| 1944 |
'container' => $this->zones['temp']['container'], |
| 1945 |
'directory' => $this->zones['temp']['directory'] |
| 1946 |
], |
| 1947 |
'thumb' => [ |
| 1948 |
'container' => $this->zones['temp']['container'], |
| 1949 |
'directory' => $this->zones['temp']['directory'] == '' |
| 1950 |
? 'thumb' |
| 1951 |
: $this->zones['temp']['directory'] . '/thumb' |
| 1952 |
], |
| 1953 |
'transcoded' => [ |
| 1954 |
'container' => $this->zones['temp']['container'], |
| 1955 |
'directory' => $this->zones['temp']['directory'] == '' |
| 1956 |
? 'transcoded' |
| 1957 |
: $this->zones['temp']['directory'] . '/transcoded' |
| 1958 |
] |
| 1959 |
], |
| 1960 |
'hashLevels' => $this->hashLevels, |
| 1961 |
'isPrivate' => true |
| 1962 |
] ); |
| 1963 |
} |
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
public function getUploadStash( ?UserIdentity $user = null ) { |
| 1972 |
return new UploadStash( $this, $user ); |
| 1973 |
} |
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
protected function assertWritableRepo() { |
| 1982 |
} |
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
public function getInfo() { |
| 1991 |
$ret = [ |
| 1992 |
'name' => $this->getName(), |
| 1993 |
'displayname' => $this->getDisplayName(), |
| 1994 |
'rootUrl' => $this->getZoneUrl( 'public' ), |
| 1995 |
'local' => $this->isLocal(), |
| 1996 |
]; |
| 1997 |
|
| 1998 |
$optionalSettings = [ |
| 1999 |
'url', |
| 2000 |
'thumbUrl', |
| 2001 |
'initialCapital', |
| 2002 |
'descBaseUrl', |
| 2003 |
'scriptDirUrl', |
| 2004 |
'articleUrl', |
| 2005 |
'fetchDescription', |
| 2006 |
'descriptionCacheExpiry', |
| 2007 |
]; |
| 2008 |
foreach ( $optionalSettings as $k ) { |
| 2009 |
if ( isset( $this->$k ) ) { |
| 2010 |
$ret[$k] = $this->$k; |
| 2011 |
} |
| 2012 |
} |
| 2013 |
if ( isset( $this->favicon ) ) { |
| 2014 |
|
| 2015 |
$ret['favicon'] = MediaWikiServices::getInstance()->getUrlUtils() |
| 2016 |
->expand( $this->favicon ); |
| 2017 |
} |
| 2018 |
|
| 2019 |
return $ret; |
| 2020 |
} |
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
public function hasSha1Storage() { |
| 2027 |
return $this->hasSha1Storage; |
| 2028 |
} |
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
public function supportsSha1URLs() { |
| 2035 |
return $this->supportsSha1URLs; |
| 2036 |
} |
| 2037 |
} |