Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
34.87% |
91 / 261 |
|
48.48% |
16 / 33 |
CRAP | |
0.00% |
0 / 1 |
| LocalRepo | |
35.00% |
91 / 260 |
|
48.48% |
16 / 33 |
2520.59 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
| newFileFromRow | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| newFromArchiveName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| cleanupDeletedBatch | |
12.50% |
3 / 24 |
|
0.00% |
0 / 1 |
30.12 | |||
| deletedFileHasKey | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| hiddenFileHasKey | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| getHashFromKey | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| checkRedirect | |
92.86% |
26 / 28 |
|
0.00% |
0 / 1 |
6.01 | |||
| findFiles | |
0.00% |
0 / 78 |
|
0.00% |
0 / 1 |
1056 | |||
| findBySha1 | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| findBySha1s | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| findFilesByPrefix | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| getReplicaDB | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPrimaryDB | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDBFactory | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
2.86 | |||
| hasAcessibleSharedCache | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSharedCacheKey | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| invalidateImageRedirect | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| store | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| storeBatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| cleanupBatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| publish | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| publishBatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteBatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| skipWriteOperationIfSha1 | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| isJsonMetadataEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isSplitMetadataEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getSplitMetadataThreshold | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isMetadataUpdateEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isMetadataReserializeEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBlobStore | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getUploadStash | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\FileRepo; |
| 8 | |
| 9 | use Closure; |
| 10 | use InvalidArgumentException; |
| 11 | use MediaWiki\Context\RequestContext; |
| 12 | use MediaWiki\FileRepo\File\File; |
| 13 | use MediaWiki\FileRepo\File\FileSelectQueryBuilder; |
| 14 | use MediaWiki\FileRepo\File\LocalFile; |
| 15 | use MediaWiki\FileRepo\File\OldLocalFile; |
| 16 | use MediaWiki\Linker\LinkTarget; |
| 17 | use MediaWiki\MediaWikiServices; |
| 18 | use MediaWiki\Page\PageIdentity; |
| 19 | use MediaWiki\Permissions\Authority; |
| 20 | use MediaWiki\Status\Status; |
| 21 | use MediaWiki\Storage\BlobStore; |
| 22 | use MediaWiki\Title\Title; |
| 23 | use MediaWiki\Upload\UploadStash; |
| 24 | use MediaWiki\User\UserIdentity; |
| 25 | use MediaWiki\WikiMap\WikiMap; |
| 26 | use stdClass; |
| 27 | use Wikimedia\ObjectCache\WANObjectCache; |
| 28 | use Wikimedia\Rdbms\Database; |
| 29 | use Wikimedia\Rdbms\IConnectionProvider; |
| 30 | use Wikimedia\Rdbms\IDatabase; |
| 31 | use Wikimedia\Rdbms\IExpression; |
| 32 | use Wikimedia\Rdbms\IReadableDatabase; |
| 33 | use Wikimedia\Rdbms\IResultWrapper; |
| 34 | use Wikimedia\Rdbms\LikeValue; |
| 35 | |
| 36 | /** |
| 37 | * Local repository that stores files in the local filesystem and registers them |
| 38 | * in the wiki's own database. |
| 39 | * |
| 40 | * This is the most commonly used file repository class. |
| 41 | * |
| 42 | * @ingroup FileRepo |
| 43 | * @method LocalFile|null newFile( $title, $time = false ) |
| 44 | */ |
| 45 | class LocalRepo extends FileRepo { |
| 46 | /** @var callable */ |
| 47 | protected $fileFactory = [ LocalFile::class, 'newFromTitle' ]; |
| 48 | /** @var callable */ |
| 49 | protected $fileFactoryKey = [ LocalFile::class, 'newFromKey' ]; |
| 50 | /** @var callable */ |
| 51 | protected $fileFromRowFactory = [ LocalFile::class, 'newFromRow' ]; |
| 52 | /** @var callable */ |
| 53 | protected $oldFileFromRowFactory = [ OldLocalFile::class, 'newFromRow' ]; |
| 54 | /** @var callable */ |
| 55 | protected $oldFileFactory = [ OldLocalFile::class, 'newFromTitle' ]; |
| 56 | /** @var callable */ |
| 57 | protected $oldFileFactoryKey = [ OldLocalFile::class, 'newFromKey' ]; |
| 58 | |
| 59 | /** @var string DB domain of the repo wiki */ |
| 60 | protected $dbDomain; |
| 61 | protected IConnectionProvider $dbProvider; |
| 62 | /** @var bool Whether shared cache keys are exposed/accessible */ |
| 63 | protected $hasAccessibleSharedCache; |
| 64 | |
| 65 | /** @var BlobStore */ |
| 66 | protected $blobStore; |
| 67 | |
| 68 | /** @var bool */ |
| 69 | protected $useJsonMetadata = true; |
| 70 | |
| 71 | /** @var bool */ |
| 72 | protected $useSplitMetadata = false; |
| 73 | |
| 74 | /** @var int|null */ |
| 75 | protected $splitMetadataThreshold = 1000; |
| 76 | |
| 77 | /** @var bool */ |
| 78 | protected $updateCompatibleMetadata = false; |
| 79 | |
| 80 | /** @var bool */ |
| 81 | protected $reserializeMetadata = false; |
| 82 | |
| 83 | public function __construct( ?array $info = null ) { |
| 84 | parent::__construct( $info ); |
| 85 | |
| 86 | $this->dbDomain = WikiMap::getCurrentWikiDbDomain()->getId(); |
| 87 | $this->hasAccessibleSharedCache = true; |
| 88 | |
| 89 | $this->hasSha1Storage = ( $info['storageLayout'] ?? null ) === 'sha1'; |
| 90 | $this->dbProvider = MediaWikiServices::getInstance()->getConnectionProvider(); |
| 91 | |
| 92 | if ( $this->hasSha1Storage() ) { |
| 93 | $this->backend = new FileBackendDBRepoWrapper( [ |
| 94 | 'backend' => $this->backend, |
| 95 | 'repoName' => $this->name, |
| 96 | 'dbHandleFactory' => $this->getDBFactory() |
| 97 | ] ); |
| 98 | } |
| 99 | |
| 100 | foreach ( |
| 101 | [ |
| 102 | 'useJsonMetadata', |
| 103 | 'useSplitMetadata', |
| 104 | 'splitMetadataThreshold', |
| 105 | 'updateCompatibleMetadata', |
| 106 | 'reserializeMetadata', |
| 107 | ] as $option |
| 108 | ) { |
| 109 | if ( isset( $info[$option] ) ) { |
| 110 | $this->$option = $info[$option]; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param stdClass $row |
| 117 | * @return LocalFile |
| 118 | */ |
| 119 | public function newFileFromRow( $row ) { |
| 120 | if ( isset( $row->img_name ) ) { |
| 121 | return ( $this->fileFromRowFactory )( $row, $this ); |
| 122 | } elseif ( isset( $row->oi_name ) ) { |
| 123 | return ( $this->oldFileFromRowFactory )( $row, $this ); |
| 124 | } else { |
| 125 | throw new InvalidArgumentException( __METHOD__ . ': invalid row' ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param PageIdentity|LinkTarget|string $title |
| 131 | * @param string $archiveName |
| 132 | * @return OldLocalFile |
| 133 | */ |
| 134 | public function newFromArchiveName( $title, $archiveName ) { |
| 135 | $title = File::normalizeTitle( $title ); |
| 136 | return OldLocalFile::newFromArchiveName( $title, $this, $archiveName ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Delete files in the deleted directory if they are not referenced in the |
| 141 | * filearchive table. This needs to be done in the repo because it needs to |
| 142 | * interleave database locks with file operations, which is potentially a |
| 143 | * remote operation. |
| 144 | * |
| 145 | * @param string[] $storageKeys |
| 146 | * |
| 147 | * @return Status |
| 148 | */ |
| 149 | public function cleanupDeletedBatch( array $storageKeys ) { |
| 150 | if ( $this->hasSha1Storage() ) { |
| 151 | wfDebug( __METHOD__ . ": skipped because storage uses sha1 paths" ); |
| 152 | return Status::newGood(); |
| 153 | } |
| 154 | |
| 155 | $backend = $this->backend; // convenience |
| 156 | $root = $this->getZonePath( 'deleted' ); |
| 157 | $dbw = $this->getPrimaryDB(); |
| 158 | $status = $this->newGood(); |
| 159 | $storageKeys = array_unique( $storageKeys ); |
| 160 | foreach ( $storageKeys as $key ) { |
| 161 | $hashPath = $this->getDeletedHashPath( $key ); |
| 162 | $path = "$root/$hashPath$key"; |
| 163 | $dbw->startAtomic( __METHOD__ ); |
| 164 | // Check for usage in deleted/hidden files and preemptively |
| 165 | // lock the key to avoid any future use until we are finished. |
| 166 | $deleted = $this->deletedFileHasKey( $key, 'lock' ); |
| 167 | $hidden = $this->hiddenFileHasKey( $key, 'lock' ); |
| 168 | if ( !$deleted && !$hidden ) { // not in use now |
| 169 | wfDebug( __METHOD__ . ": deleting $key" ); |
| 170 | $op = [ 'op' => 'delete', 'src' => $path ]; |
| 171 | if ( !$backend->doOperation( $op )->isOK() ) { |
| 172 | $status->error( 'undelete-cleanup-error', $path ); |
| 173 | $status->failCount++; |
| 174 | } |
| 175 | } else { |
| 176 | wfDebug( __METHOD__ . ": $key still in use" ); |
| 177 | $status->successCount++; |
| 178 | } |
| 179 | $dbw->endAtomic( __METHOD__ ); |
| 180 | } |
| 181 | |
| 182 | return $status; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Check if a deleted (filearchive) file has this sha1 key |
| 187 | * |
| 188 | * @param string $key File storage key (base-36 sha1 key with file extension) |
| 189 | * @param string|null $lock Use "lock" to lock the row via FOR UPDATE |
| 190 | * @return bool File with this key is in use |
| 191 | */ |
| 192 | protected function deletedFileHasKey( $key, $lock = null ) { |
| 193 | $queryBuilder = $this->getPrimaryDB()->newSelectQueryBuilder() |
| 194 | ->select( '1' ) |
| 195 | ->from( 'filearchive' ) |
| 196 | ->where( [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ] ); |
| 197 | if ( $lock === 'lock' ) { |
| 198 | $queryBuilder->forUpdate(); |
| 199 | } |
| 200 | return (bool)$queryBuilder->caller( __METHOD__ )->fetchField(); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Check if a hidden (revision delete) file has this sha1 key |
| 205 | * |
| 206 | * @param string $key File storage key (base-36 sha1 key with file extension) |
| 207 | * @param string|null $lock Use "lock" to lock the row via FOR UPDATE |
| 208 | * @return bool File with this key is in use |
| 209 | */ |
| 210 | protected function hiddenFileHasKey( $key, $lock = null ) { |
| 211 | $sha1 = self::getHashFromKey( $key ); |
| 212 | $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) ); |
| 213 | |
| 214 | $dbw = $this->getPrimaryDB(); |
| 215 | $queryBuilder = $dbw->newSelectQueryBuilder() |
| 216 | ->select( '1' ) |
| 217 | ->from( 'oldimage' ) |
| 218 | ->where( [ |
| 219 | 'oi_sha1' => $sha1, |
| 220 | $dbw->expr( 'oi_archive_name', IExpression::LIKE, new LikeValue( $dbw->anyString(), ".$ext" ) ), |
| 221 | $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE, |
| 222 | ] ); |
| 223 | if ( $lock === 'lock' ) { |
| 224 | $queryBuilder->forUpdate(); |
| 225 | } |
| 226 | |
| 227 | return (bool)$queryBuilder->caller( __METHOD__ )->fetchField(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Gets the SHA1 hash from a storage key |
| 232 | * |
| 233 | * @param string $key |
| 234 | * @return string |
| 235 | */ |
| 236 | public static function getHashFromKey( $key ) { |
| 237 | $sha1 = strtok( $key, '.' ); |
| 238 | if ( is_string( $sha1 ) && strlen( $sha1 ) === 32 && $sha1[0] === '0' ) { |
| 239 | $sha1 = substr( $sha1, 1 ); |
| 240 | } |
| 241 | return $sha1; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Checks if there is a redirect named as $title |
| 246 | * |
| 247 | * @param PageIdentity|LinkTarget $title Title of file |
| 248 | * @return Title|false |
| 249 | */ |
| 250 | public function checkRedirect( $title ) { |
| 251 | $title = File::normalizeTitle( $title, 'exception' ); |
| 252 | |
| 253 | $memcKey = $this->getSharedCacheKey( 'file-redirect', md5( $title->getDBkey() ) ); |
| 254 | if ( $memcKey === false ) { |
| 255 | $memcKey = $this->getLocalCacheKey( 'file-redirect', md5( $title->getDBkey() ) ); |
| 256 | $expiry = 300; // no invalidation, 5 minutes |
| 257 | } else { |
| 258 | $expiry = 86400; // has invalidation, 1 day |
| 259 | } |
| 260 | |
| 261 | $method = __METHOD__; |
| 262 | $redirDbKey = $this->wanCache->getWithSetCallback( |
| 263 | $memcKey, |
| 264 | $expiry, |
| 265 | function ( $oldValue, &$ttl, array &$setOpts ) use ( $method, $title ) { |
| 266 | $dbr = $this->getReplicaDB(); // possibly remote DB |
| 267 | |
| 268 | $setOpts += Database::getCacheSetOptions( $dbr ); |
| 269 | |
| 270 | $row = $dbr->newSelectQueryBuilder() |
| 271 | ->select( [ 'rd_namespace', 'rd_title' ] ) |
| 272 | ->from( 'page' ) |
| 273 | ->join( 'redirect', null, 'rd_from = page_id' ) |
| 274 | ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] ) |
| 275 | ->caller( $method )->fetchRow(); |
| 276 | |
| 277 | return ( $row && $row->rd_namespace == NS_FILE ) |
| 278 | ? Title::makeTitle( $row->rd_namespace, $row->rd_title )->getDBkey() |
| 279 | : ''; // negative cache |
| 280 | }, |
| 281 | [ 'pcTTL' => WANObjectCache::TTL_PROC_LONG ] |
| 282 | ); |
| 283 | |
| 284 | // @note: also checks " " for b/c |
| 285 | if ( $redirDbKey !== ' ' && strval( $redirDbKey ) !== '' ) { |
| 286 | // Page is a redirect to another file |
| 287 | return Title::newFromText( $redirDbKey, NS_FILE ); |
| 288 | } |
| 289 | |
| 290 | return false; // no redirect |
| 291 | } |
| 292 | |
| 293 | /** @inheritDoc */ |
| 294 | public function findFiles( array $items, $flags = 0 ) { |
| 295 | $finalFiles = []; // map of (DB key => corresponding File) for matches |
| 296 | |
| 297 | $searchSet = []; // map of (normalized DB key => search params) |
| 298 | foreach ( $items as $item ) { |
| 299 | if ( is_array( $item ) ) { |
| 300 | $title = File::normalizeTitle( $item['title'] ); |
| 301 | if ( $title ) { |
| 302 | $searchSet[$title->getDBkey()] = $item; |
| 303 | } |
| 304 | } else { |
| 305 | $title = File::normalizeTitle( $item ); |
| 306 | if ( $title ) { |
| 307 | $searchSet[$title->getDBkey()] = []; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | $fileMatchesSearch = static function ( File $file, array $search ) { |
| 313 | // Note: file name comparison done elsewhere (to handle redirects) |
| 314 | |
| 315 | // Fallback to RequestContext::getMain should be replaced with a better |
| 316 | // way of setting the user that should be used; currently it needs to be |
| 317 | // set for each file individually. See T263033#6477586 |
| 318 | $contextPerformer = RequestContext::getMain()->getAuthority(); |
| 319 | $performer = ( !empty( $search['private'] ) && $search['private'] instanceof Authority ) |
| 320 | ? $search['private'] |
| 321 | : $contextPerformer; |
| 322 | |
| 323 | return ( |
| 324 | $file->exists() && |
| 325 | ( |
| 326 | ( empty( $search['time'] ) && !$file->isOld() ) || |
| 327 | ( !empty( $search['time'] ) && $search['time'] === $file->getTimestamp() ) |
| 328 | ) && |
| 329 | ( !empty( $search['private'] ) || !$file->isDeleted( File::DELETED_FILE ) ) && |
| 330 | $file->userCan( File::DELETED_FILE, $performer ) |
| 331 | ); |
| 332 | }; |
| 333 | |
| 334 | $applyMatchingFiles = function ( IResultWrapper $res, &$searchSet, &$finalFiles ) |
| 335 | use ( $fileMatchesSearch, $flags ) |
| 336 | { |
| 337 | $contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 338 | $info = $this->getInfo(); |
| 339 | foreach ( $res as $row ) { |
| 340 | $file = $this->newFileFromRow( $row ); |
| 341 | // There must have been a search for this DB key, but this has to handle the |
| 342 | // cases were title capitalization is different on the client and repo wikis. |
| 343 | $dbKeysLook = [ strtr( $file->getName(), ' ', '_' ) ]; |
| 344 | if ( !empty( $info['initialCapital'] ) ) { |
| 345 | // Search keys for "hi.png" and "Hi.png" should use the "Hi.png file" |
| 346 | $dbKeysLook[] = $contLang->lcfirst( $file->getName() ); |
| 347 | } |
| 348 | foreach ( $dbKeysLook as $dbKey ) { |
| 349 | if ( isset( $searchSet[$dbKey] ) |
| 350 | && $fileMatchesSearch( $file, $searchSet[$dbKey] ) |
| 351 | ) { |
| 352 | $finalFiles[$dbKey] = ( $flags & FileRepo::NAME_AND_TIME_ONLY ) |
| 353 | ? [ 'title' => $dbKey, 'timestamp' => $file->getTimestamp() ] |
| 354 | : $file; |
| 355 | unset( $searchSet[$dbKey] ); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | }; |
| 360 | |
| 361 | $dbr = $this->getReplicaDB(); |
| 362 | |
| 363 | // Query image table |
| 364 | $imgNames = []; |
| 365 | foreach ( $searchSet as $dbKey => $_ ) { |
| 366 | $imgNames[] = $this->getNameFromTitle( File::normalizeTitle( $dbKey ) ); |
| 367 | } |
| 368 | |
| 369 | if ( count( $imgNames ) ) { |
| 370 | $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr ); |
| 371 | $res = $queryBuilder->where( [ 'img_name' => $imgNames ] )->caller( __METHOD__ )->fetchResultSet(); |
| 372 | $applyMatchingFiles( $res, $searchSet, $finalFiles ); |
| 373 | } |
| 374 | |
| 375 | // Query old image table |
| 376 | $oiConds = []; // WHERE clause array for each file |
| 377 | foreach ( $searchSet as $dbKey => $search ) { |
| 378 | if ( isset( $search['time'] ) ) { |
| 379 | $oiConds[] = $dbr |
| 380 | ->expr( 'oi_name', '=', $this->getNameFromTitle( File::normalizeTitle( $dbKey ) ) ) |
| 381 | ->and( 'oi_timestamp', '=', $dbr->timestamp( $search['time'] ) ); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if ( count( $oiConds ) ) { |
| 386 | $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr ); |
| 387 | |
| 388 | $res = $queryBuilder->where( $dbr->orExpr( $oiConds ) ) |
| 389 | ->caller( __METHOD__ )->fetchResultSet(); |
| 390 | $applyMatchingFiles( $res, $searchSet, $finalFiles ); |
| 391 | } |
| 392 | |
| 393 | // Check for redirects... |
| 394 | foreach ( $searchSet as $dbKey => $search ) { |
| 395 | if ( !empty( $search['ignoreRedirect'] ) ) { |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | $title = File::normalizeTitle( $dbKey ); |
| 400 | $redir = $this->checkRedirect( $title ); // hopefully hits memcached |
| 401 | |
| 402 | if ( $redir && $redir->getNamespace() === NS_FILE ) { |
| 403 | $file = $this->newFile( $redir ); |
| 404 | if ( $file && $fileMatchesSearch( $file, $search ) ) { |
| 405 | $file->redirectedFrom( $title->getDBkey() ); |
| 406 | if ( $flags & FileRepo::NAME_AND_TIME_ONLY ) { |
| 407 | $finalFiles[$dbKey] = [ |
| 408 | 'title' => $file->getTitle()->getDBkey(), |
| 409 | 'timestamp' => $file->getTimestamp() |
| 410 | ]; |
| 411 | } else { |
| 412 | $finalFiles[$dbKey] = $file; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return $finalFiles; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Get an array or iterator of file objects for files that have a given |
| 423 | * SHA-1 content hash. |
| 424 | * |
| 425 | * @param string $hash A sha1 hash to look for |
| 426 | * @return LocalFile[] |
| 427 | */ |
| 428 | public function findBySha1( $hash ) { |
| 429 | $queryBuilder = FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ); |
| 430 | $res = $queryBuilder->where( [ 'img_sha1' => $hash ] ) |
| 431 | ->orderBy( 'img_name' ) |
| 432 | ->caller( __METHOD__ )->fetchResultSet(); |
| 433 | |
| 434 | $result = []; |
| 435 | foreach ( $res as $row ) { |
| 436 | $result[] = $this->newFileFromRow( $row ); |
| 437 | } |
| 438 | $res->free(); |
| 439 | |
| 440 | return $result; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Get an array of arrays or iterators of file objects for files that |
| 445 | * have the given SHA-1 content hashes. |
| 446 | * |
| 447 | * Overrides generic implementation in FileRepo for performance reason |
| 448 | * |
| 449 | * @param string[] $hashes An array of hashes |
| 450 | * @return File[][] An Array of arrays or iterators of file objects and the hash as key |
| 451 | */ |
| 452 | public function findBySha1s( array $hashes ) { |
| 453 | if ( $hashes === [] ) { |
| 454 | return []; // empty parameter |
| 455 | } |
| 456 | |
| 457 | $dbr = $this->getReplicaDB(); |
| 458 | $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr ); |
| 459 | |
| 460 | $queryBuilder->where( [ 'img_sha1' => $hashes ] ) |
| 461 | ->orderBy( 'img_name' ); |
| 462 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 463 | |
| 464 | $result = []; |
| 465 | foreach ( $res as $row ) { |
| 466 | $file = $this->newFileFromRow( $row ); |
| 467 | $result[$file->getSha1()][] = $file; |
| 468 | } |
| 469 | $res->free(); |
| 470 | |
| 471 | return $result; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Return an array of files where the name starts with $prefix. |
| 476 | * |
| 477 | * @param string $prefix The prefix to search for |
| 478 | * @param int $limit The maximum amount of files to return |
| 479 | * @return LocalFile[] |
| 480 | */ |
| 481 | public function findFilesByPrefix( $prefix, $limit ) { |
| 482 | $dbr = $this->getReplicaDB(); |
| 483 | $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr ); |
| 484 | |
| 485 | $queryBuilder |
| 486 | ->where( $dbr->expr( 'img_name', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ) ) |
| 487 | ->orderBy( 'img_name' ) |
| 488 | ->limit( intval( $limit ) ); |
| 489 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 490 | |
| 491 | // Build file objects |
| 492 | $files = []; |
| 493 | foreach ( $res as $row ) { |
| 494 | $files[] = $this->newFileFromRow( $row ); |
| 495 | } |
| 496 | |
| 497 | return $files; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Get a connection to the replica DB |
| 502 | * @return IReadableDatabase |
| 503 | */ |
| 504 | public function getReplicaDB() { |
| 505 | return $this->dbProvider->getReplicaDatabase(); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Get a connection to the primary DB |
| 510 | * @return IDatabase |
| 511 | * @since 1.37 |
| 512 | */ |
| 513 | public function getPrimaryDB() { |
| 514 | return $this->dbProvider->getPrimaryDatabase(); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Get a callback to get a DB handle given an index (DB_REPLICA/DB_PRIMARY) |
| 519 | * @return Closure |
| 520 | */ |
| 521 | protected function getDBFactory() { |
| 522 | // TODO: DB_REPLICA/DB_PRIMARY shouldn't be passed around |
| 523 | return static function ( $index ) { |
| 524 | if ( $index === DB_PRIMARY ) { |
| 525 | return MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 526 | } else { |
| 527 | return MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 528 | } |
| 529 | }; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Check whether the repo has a shared cache, accessible from the current site context |
| 534 | * |
| 535 | * @return bool |
| 536 | * @since 1.35 |
| 537 | */ |
| 538 | protected function hasAcessibleSharedCache() { |
| 539 | return $this->hasAccessibleSharedCache; |
| 540 | } |
| 541 | |
| 542 | /** @inheritDoc */ |
| 543 | public function getSharedCacheKey( $kClassSuffix, ...$components ) { |
| 544 | // T267668: do not include the repo name in the key |
| 545 | return $this->hasAcessibleSharedCache() |
| 546 | ? $this->wanCache->makeGlobalKey( |
| 547 | 'filerepo-' . $kClassSuffix, |
| 548 | $this->dbDomain, |
| 549 | ...$components |
| 550 | ) |
| 551 | : false; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Invalidates image redirect cache related to that image |
| 556 | * |
| 557 | * @param PageIdentity|LinkTarget $title Title of page |
| 558 | * @return void |
| 559 | */ |
| 560 | public function invalidateImageRedirect( $title ) { |
| 561 | $key = $this->getSharedCacheKey( 'file-redirect', md5( $title->getDBkey() ) ); |
| 562 | if ( $key ) { |
| 563 | $this->getPrimaryDB()->onTransactionPreCommitOrIdle( |
| 564 | function () use ( $key ) { |
| 565 | $this->wanCache->delete( $key ); |
| 566 | }, |
| 567 | __METHOD__ |
| 568 | ); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /** @inheritDoc */ |
| 573 | public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) { |
| 574 | return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() ); |
| 575 | } |
| 576 | |
| 577 | /** @inheritDoc */ |
| 578 | public function storeBatch( array $triplets, $flags = 0 ) { |
| 579 | return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() ); |
| 580 | } |
| 581 | |
| 582 | /** @inheritDoc */ |
| 583 | public function cleanupBatch( array $files, $flags = 0 ) { |
| 584 | return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() ); |
| 585 | } |
| 586 | |
| 587 | /** @inheritDoc */ |
| 588 | public function publish( |
| 589 | $src, |
| 590 | $dstRel, |
| 591 | $archiveRel, |
| 592 | $flags = 0, |
| 593 | array $options = [] |
| 594 | ) { |
| 595 | return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() ); |
| 596 | } |
| 597 | |
| 598 | /** @inheritDoc */ |
| 599 | public function publishBatch( array $ntuples, $flags = 0 ) { |
| 600 | return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() ); |
| 601 | } |
| 602 | |
| 603 | /** @inheritDoc */ |
| 604 | public function delete( $srcRel, $archiveRel ) { |
| 605 | return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() ); |
| 606 | } |
| 607 | |
| 608 | /** @inheritDoc */ |
| 609 | public function deleteBatch( array $sourceDestPairs ) { |
| 610 | return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() ); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Skips the write operation if storage is sha1-based, executes it normally otherwise |
| 615 | * |
| 616 | * @param string $function |
| 617 | * @param array $args |
| 618 | * @return Status |
| 619 | */ |
| 620 | protected function skipWriteOperationIfSha1( $function, array $args ) { |
| 621 | $this->assertWritableRepo(); // fail out if read-only |
| 622 | |
| 623 | if ( $this->hasSha1Storage() ) { |
| 624 | wfDebug( __METHOD__ . ": skipped because storage uses sha1 paths" ); |
| 625 | return Status::newGood(); |
| 626 | } else { |
| 627 | return parent::$function( ...$args ); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Returns true if files should store metadata in JSON format. This |
| 633 | * requires metadata from all handlers to be JSON-serializable. |
| 634 | * |
| 635 | * To avoid breaking existing metadata, reading JSON metadata is always |
| 636 | * enabled regardless of this setting. |
| 637 | * |
| 638 | * @return bool |
| 639 | */ |
| 640 | public function isJsonMetadataEnabled() { |
| 641 | return $this->useJsonMetadata; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Returns true if files should split up large metadata, storing parts of |
| 646 | * it in the BlobStore. |
| 647 | * |
| 648 | * @return bool |
| 649 | */ |
| 650 | public function isSplitMetadataEnabled() { |
| 651 | return $this->isJsonMetadataEnabled() && $this->useSplitMetadata; |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Get the threshold above which metadata items should be split into |
| 656 | * separate storage, or null if no splitting should be done. |
| 657 | * |
| 658 | * @return int |
| 659 | */ |
| 660 | public function getSplitMetadataThreshold() { |
| 661 | return $this->splitMetadataThreshold; |
| 662 | } |
| 663 | |
| 664 | /** @inheritDoc */ |
| 665 | public function isMetadataUpdateEnabled() { |
| 666 | return $this->updateCompatibleMetadata; |
| 667 | } |
| 668 | |
| 669 | /** @inheritDoc */ |
| 670 | public function isMetadataReserializeEnabled() { |
| 671 | return $this->reserializeMetadata; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Get a BlobStore for storing and retrieving large metadata, or null if |
| 676 | * that can't be done. |
| 677 | */ |
| 678 | public function getBlobStore(): ?BlobStore { |
| 679 | if ( !$this->blobStore ) { |
| 680 | $this->blobStore = MediaWikiServices::getInstance()->getBlobStoreFactory() |
| 681 | ->newBlobStore( $this->dbDomain ); |
| 682 | } |
| 683 | return $this->blobStore; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Get an UploadStash associated with this repo. |
| 688 | * |
| 689 | * @param UserIdentity|null $user |
| 690 | * @return UploadStash |
| 691 | */ |
| 692 | public function getUploadStash( ?UserIdentity $user = null ) { |
| 693 | return new UploadStash( $this, $user ); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /** @deprecated class alias since 1.44 */ |
| 698 | class_alias( LocalRepo::class, 'LocalRepo' ); |