| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Linker\LinkTarget; |
| 22 |
use MediaWiki\Page\PageIdentity; |
| 23 |
use MediaWiki\Title\Title; |
| 24 |
use Wikimedia\Mime\MimeAnalyzer; |
| 25 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class RepoGroup { |
| 33 |
|
| 34 |
protected $localRepo; |
| 35 |
|
| 36 |
|
| 37 |
protected $foreignRepos; |
| 38 |
|
| 39 |
|
| 40 |
protected $wanCache; |
| 41 |
|
| 42 |
|
| 43 |
protected $reposInitialised = false; |
| 44 |
|
| 45 |
|
| 46 |
protected $localInfo; |
| 47 |
|
| 48 |
|
| 49 |
protected $foreignInfo; |
| 50 |
|
| 51 |
|
| 52 |
protected $cache; |
| 53 |
|
| 54 |
|
| 55 |
private const MAX_CACHE_SIZE = 500; |
| 56 |
|
| 57 |
|
| 58 |
private $mimeAnalyzer; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function __construct( |
| 73 |
$localInfo, |
| 74 |
$foreignInfo, |
| 75 |
WANObjectCache $wanCache, |
| 76 |
MimeAnalyzer $mimeAnalyzer |
| 77 |
) { |
| 78 |
$this->localInfo = $localInfo; |
| 79 |
$this->foreignInfo = $foreignInfo; |
| 80 |
$this->cache = new MapCacheLRU( self::MAX_CACHE_SIZE ); |
| 81 |
$this->wanCache = $wanCache; |
| 82 |
$this->mimeAnalyzer = $mimeAnalyzer; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function findFile( $title, $options = [] ) { |
| 104 |
if ( !is_array( $options ) ) { |
| 105 |
|
| 106 |
$options = [ 'time' => $options ]; |
| 107 |
} |
| 108 |
if ( isset( $options['bypassCache'] ) ) { |
| 109 |
$options['latest'] = $options['bypassCache']; |
| 110 |
} |
| 111 |
if ( isset( $options['time'] ) && $options['time'] !== false ) { |
| 112 |
$options['time'] = wfTimestamp( TS_MW, $options['time'] ); |
| 113 |
} else { |
| 114 |
$options['time'] = false; |
| 115 |
} |
| 116 |
|
| 117 |
if ( !$this->reposInitialised ) { |
| 118 |
$this->initialiseRepos(); |
| 119 |
} |
| 120 |
|
| 121 |
$title = File::normalizeTitle( $title ); |
| 122 |
if ( !$title ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
$dbkey = $title->getDBkey(); |
| 128 |
$timeKey = is_string( $options['time'] ) ? $options['time'] : ''; |
| 129 |
if ( empty( $options['ignoreRedirect'] ) |
| 130 |
&& empty( $options['private'] ) |
| 131 |
&& empty( $options['latest'] ) |
| 132 |
) { |
| 133 |
if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) { |
| 134 |
return $this->cache->getField( $dbkey, $timeKey ); |
| 135 |
} |
| 136 |
$useCache = true; |
| 137 |
} else { |
| 138 |
$useCache = false; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
$image = $this->localRepo->findFile( $title, $options ); |
| 143 |
|
| 144 |
|
| 145 |
if ( !$image ) { |
| 146 |
foreach ( $this->foreignRepos as $repo ) { |
| 147 |
$image = $repo->findFile( $title, $options ); |
| 148 |
if ( $image ) { |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$image = $image instanceof File ? $image : false; |
| 155 |
|
| 156 |
if ( $useCache && ( !$image || $image->isCacheable() ) ) { |
| 157 |
$this->cache->setField( $dbkey, $timeKey, $image ); |
| 158 |
} |
| 159 |
|
| 160 |
return $image; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function findFiles( array $inputItems, $flags = 0 ) { |
| 181 |
if ( !$this->reposInitialised ) { |
| 182 |
$this->initialiseRepos(); |
| 183 |
} |
| 184 |
|
| 185 |
$items = []; |
| 186 |
foreach ( $inputItems as $item ) { |
| 187 |
if ( !is_array( $item ) ) { |
| 188 |
$item = [ 'title' => $item ]; |
| 189 |
} |
| 190 |
$item['title'] = File::normalizeTitle( $item['title'] ); |
| 191 |
if ( $item['title'] ) { |
| 192 |
$items[$item['title']->getDBkey()] = $item; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
$images = $this->localRepo->findFiles( $items, $flags ); |
| 197 |
|
| 198 |
foreach ( $this->foreignRepos as $repo ) { |
| 199 |
|
| 200 |
foreach ( $images as $name => $image ) { |
| 201 |
unset( $items[$name] ); |
| 202 |
} |
| 203 |
|
| 204 |
$images = array_merge( $images, $repo->findFiles( $items, $flags ) ); |
| 205 |
} |
| 206 |
|
| 207 |
return $images; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function checkRedirect( $title ) { |
| 216 |
if ( !$this->reposInitialised ) { |
| 217 |
$this->initialiseRepos(); |
| 218 |
} |
| 219 |
|
| 220 |
$title = File::normalizeTitle( $title ); |
| 221 |
|
| 222 |
$redir = $this->localRepo->checkRedirect( $title ); |
| 223 |
if ( $redir ) { |
| 224 |
return $redir; |
| 225 |
} |
| 226 |
|
| 227 |
foreach ( $this->foreignRepos as $repo ) { |
| 228 |
$redir = $repo->checkRedirect( $title ); |
| 229 |
if ( $redir ) { |
| 230 |
return $redir; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function findFileFromKey( $hash, $options = [] ) { |
| 246 |
if ( !$this->reposInitialised ) { |
| 247 |
$this->initialiseRepos(); |
| 248 |
} |
| 249 |
|
| 250 |
$file = $this->localRepo->findFileFromKey( $hash, $options ); |
| 251 |
if ( !$file ) { |
| 252 |
foreach ( $this->foreignRepos as $repo ) { |
| 253 |
$file = $repo->findFileFromKey( $hash, $options ); |
| 254 |
if ( $file ) { |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return $file; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public function findBySha1( $hash ) { |
| 270 |
if ( !$this->reposInitialised ) { |
| 271 |
$this->initialiseRepos(); |
| 272 |
} |
| 273 |
|
| 274 |
$result = $this->localRepo->findBySha1( $hash ); |
| 275 |
foreach ( $this->foreignRepos as $repo ) { |
| 276 |
$result = array_merge( $result, $repo->findBySha1( $hash ) ); |
| 277 |
} |
| 278 |
usort( $result, [ File::class, 'compare' ] ); |
| 279 |
|
| 280 |
return $result; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function findBySha1s( array $hashes ) { |
| 290 |
if ( !$this->reposInitialised ) { |
| 291 |
$this->initialiseRepos(); |
| 292 |
} |
| 293 |
|
| 294 |
$result = $this->localRepo->findBySha1s( $hashes ); |
| 295 |
foreach ( $this->foreignRepos as $repo ) { |
| 296 |
$result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) ); |
| 297 |
} |
| 298 |
|
| 299 |
foreach ( $result as $hash => $files ) { |
| 300 |
usort( $result[$hash], [ File::class, 'compare' ] ); |
| 301 |
} |
| 302 |
|
| 303 |
return $result; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public function getRepo( $index ) { |
| 312 |
if ( !$this->reposInitialised ) { |
| 313 |
$this->initialiseRepos(); |
| 314 |
} |
| 315 |
if ( $index === 'local' ) { |
| 316 |
return $this->localRepo; |
| 317 |
} |
| 318 |
return $this->foreignRepos[$index] ?? false; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function getRepoByName( $name ) { |
| 327 |
if ( !$this->reposInitialised ) { |
| 328 |
$this->initialiseRepos(); |
| 329 |
} |
| 330 |
foreach ( $this->foreignRepos as $repo ) { |
| 331 |
if ( $repo->name == $name ) { |
| 332 |
return $repo; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function getLocalRepo() { |
| 346 |
|
| 347 |
return $this->getRepo( 'local' ); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function forEachForeignRepo( $callback, $params = [] ) { |
| 359 |
if ( !$this->reposInitialised ) { |
| 360 |
$this->initialiseRepos(); |
| 361 |
} |
| 362 |
foreach ( $this->foreignRepos as $repo ) { |
| 363 |
if ( $callback( $repo, ...$params ) ) { |
| 364 |
return true; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function hasForeignRepos() { |
| 376 |
if ( !$this->reposInitialised ) { |
| 377 |
$this->initialiseRepos(); |
| 378 |
} |
| 379 |
return (bool)$this->foreignRepos; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function initialiseRepos() { |
| 386 |
if ( $this->reposInitialised ) { |
| 387 |
return; |
| 388 |
} |
| 389 |
$this->reposInitialised = true; |
| 390 |
|
| 391 |
$this->localRepo = $this->newRepo( $this->localInfo ); |
| 392 |
$this->foreignRepos = []; |
| 393 |
foreach ( $this->foreignInfo as $key => $info ) { |
| 394 |
$this->foreignRepos[$key] = $this->newRepo( $info ); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public function newCustomLocalRepo( $info = [] ) { |
| 405 |
|
| 406 |
return $this->newRepo( $info + $this->localInfo ); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
protected function newRepo( $info ) { |
| 415 |
$class = $info['class']; |
| 416 |
|
| 417 |
$info['wanCache'] = $this->wanCache; |
| 418 |
|
| 419 |
return new $class( $info ); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
private function splitVirtualUrl( $url ) { |
| 428 |
if ( !str_starts_with( $url, 'mwrepo://' ) ) { |
| 429 |
throw new InvalidArgumentException( __METHOD__ . ': unknown protocol' ); |
| 430 |
} |
| 431 |
|
| 432 |
$bits = explode( '/', substr( $url, 9 ), 3 ); |
| 433 |
if ( count( $bits ) != 3 ) { |
| 434 |
throw new InvalidArgumentException( __METHOD__ . ": invalid mwrepo URL: $url" ); |
| 435 |
} |
| 436 |
|
| 437 |
return $bits; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public function getFileProps( $fileName ) { |
| 445 |
if ( FileRepo::isVirtualUrl( $fileName ) ) { |
| 446 |
[ $repoName, , ] = $this->splitVirtualUrl( $fileName ); |
| 447 |
if ( $repoName === '' ) { |
| 448 |
$repoName = 'local'; |
| 449 |
} |
| 450 |
$repo = $this->getRepo( $repoName ); |
| 451 |
|
| 452 |
return $repo->getFileProps( $fileName ); |
| 453 |
} else { |
| 454 |
$mwProps = new MWFileProps( $this->mimeAnalyzer ); |
| 455 |
|
| 456 |
return $mwProps->getPropsFromPath( $fileName, true ); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
public function clearCache( $title = null ) { |
| 465 |
if ( $title == null ) { |
| 466 |
$this->cache->clear(); |
| 467 |
} elseif ( is_string( $title ) ) { |
| 468 |
$this->cache->clear( $title ); |
| 469 |
} else { |
| 470 |
$this->cache->clear( $title->getDBkey() ); |
| 471 |
} |
| 472 |
} |
| 473 |
} |