MediaWiki REL1_41
LocalRepo.php
Go to the documentation of this file.
1<?php
35
45class LocalRepo extends FileRepo {
47 protected $fileFactory = [ LocalFile::class, 'newFromTitle' ];
49 protected $fileFactoryKey = [ LocalFile::class, 'newFromKey' ];
51 protected $fileFromRowFactory = [ LocalFile::class, 'newFromRow' ];
53 protected $oldFileFromRowFactory = [ OldLocalFile::class, 'newFromRow' ];
55 protected $oldFileFactory = [ OldLocalFile::class, 'newFromTitle' ];
57 protected $oldFileFactoryKey = [ OldLocalFile::class, 'newFromKey' ];
58
60 protected $dbDomain;
64
66 protected $blobStore;
67
69 protected $useJsonMetadata = true;
70
72 protected $useSplitMetadata = false;
73
75 protected $splitMetadataThreshold = 1000;
76
78 protected $updateCompatibleMetadata = false;
79
81 protected $reserializeMetadata = false;
82
83 public function __construct( array $info = null ) {
84 parent::__construct( $info );
85
86 $this->dbDomain = WikiMap::getCurrentWikiDbDomain();
87 $this->hasAccessibleSharedCache = true;
88
89 $this->hasSha1Storage = ( $info['storageLayout'] ?? null ) === 'sha1';
90 $this->dbProvider = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
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
120 public function newFileFromRow( $row ) {
121 if ( isset( $row->img_name ) ) {
122 return call_user_func( $this->fileFromRowFactory, $row, $this );
123 } elseif ( isset( $row->oi_name ) ) {
124 return call_user_func( $this->oldFileFromRowFactory, $row, $this );
125 } else {
126 throw new MWException( __METHOD__ . ': invalid row' );
127 }
128 }
129
135 public function newFromArchiveName( $title, $archiveName ) {
136 $title = File::normalizeTitle( $title );
137 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
138 }
139
150 public function cleanupDeletedBatch( array $storageKeys ) {
151 if ( $this->hasSha1Storage() ) {
152 wfDebug( __METHOD__ . ": skipped because storage uses sha1 paths" );
153 return Status::newGood();
154 }
155
156 $backend = $this->backend; // convenience
157 $root = $this->getZonePath( 'deleted' );
158 $dbw = $this->getPrimaryDB();
159 $status = $this->newGood();
160 $storageKeys = array_unique( $storageKeys );
161 foreach ( $storageKeys as $key ) {
162 $hashPath = $this->getDeletedHashPath( $key );
163 $path = "$root/$hashPath$key";
164 $dbw->startAtomic( __METHOD__ );
165 // Check for usage in deleted/hidden files and preemptively
166 // lock the key to avoid any future use until we are finished.
167 $deleted = $this->deletedFileHasKey( $key, 'lock' );
168 $hidden = $this->hiddenFileHasKey( $key, 'lock' );
169 if ( !$deleted && !$hidden ) { // not in use now
170 wfDebug( __METHOD__ . ": deleting $key" );
171 $op = [ 'op' => 'delete', 'src' => $path ];
172 if ( !$backend->doOperation( $op )->isOK() ) {
173 $status->error( 'undelete-cleanup-error', $path );
174 $status->failCount++;
175 }
176 } else {
177 wfDebug( __METHOD__ . ": $key still in use" );
178 $status->successCount++;
179 }
180 $dbw->endAtomic( __METHOD__ );
181 }
182
183 return $status;
184 }
185
193 protected function deletedFileHasKey( $key, $lock = null ) {
194 $queryBuilder = $this->getPrimaryDB()->newSelectQueryBuilder()
195 ->select( '1' )
196 ->from( 'filearchive' )
197 ->where( [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ] );
198 if ( $lock === 'lock' ) {
199 $queryBuilder->forUpdate();
200 }
201 return (bool)$queryBuilder->caller( __METHOD__ )->fetchField();
202 }
203
211 protected function hiddenFileHasKey( $key, $lock = null ) {
212 $sha1 = self::getHashFromKey( $key );
213 $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
214
215 $dbw = $this->getPrimaryDB();
216 $queryBuilder = $dbw->newSelectQueryBuilder()
217 ->select( '1' )
218 ->from( 'oldimage' )
219 ->where( [
220 'oi_sha1' => $sha1,
221 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
222 $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE,
223 ] );
224 if ( $lock === 'lock' ) {
225 $queryBuilder->forUpdate();
226 }
227
228 return (bool)$queryBuilder->caller( __METHOD__ )->fetchField();
229 }
230
237 public static function getHashFromKey( $key ) {
238 $sha1 = strtok( $key, '.' );
239 if ( is_string( $sha1 ) && strlen( $sha1 ) === 32 && $sha1[0] === '0' ) {
240 $sha1 = substr( $sha1, 1 );
241 }
242 return $sha1;
243 }
244
251 public function checkRedirect( $title ) {
252 $title = File::normalizeTitle( $title, 'exception' );
253
254 $memcKey = $this->getSharedCacheKey( 'file-redirect', md5( $title->getDBkey() ) );
255 if ( $memcKey === false ) {
256 $memcKey = $this->getLocalCacheKey( 'file-redirect', md5( $title->getDBkey() ) );
257 $expiry = 300; // no invalidation, 5 minutes
258 } else {
259 $expiry = 86400; // has invalidation, 1 day
260 }
261
262 $method = __METHOD__;
263 $redirDbKey = $this->wanCache->getWithSetCallback(
264 $memcKey,
265 $expiry,
266 function ( $oldValue, &$ttl, array &$setOpts ) use ( $method, $title ) {
267 $dbr = $this->getReplicaDB(); // possibly remote DB
268
269 $setOpts += Database::getCacheSetOptions( $dbr );
270
271 $row = $dbr->newSelectQueryBuilder()
272 ->select( [ 'rd_namespace', 'rd_title' ] )
273 ->from( 'page' )
274 ->join( 'redirect', null, 'rd_from = page_id' )
275 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] )
276 ->caller( $method )->fetchRow();
277
278 return ( $row && $row->rd_namespace == NS_FILE )
279 ? Title::makeTitle( $row->rd_namespace, $row->rd_title )->getDBkey()
280 : ''; // negative cache
281 },
282 [ 'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
283 );
284
285 // @note: also checks " " for b/c
286 if ( $redirDbKey !== ' ' && strval( $redirDbKey ) !== '' ) {
287 // Page is a redirect to another file
288 return Title::newFromText( $redirDbKey, NS_FILE );
289 }
290
291 return false; // no redirect
292 }
293
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->makeList(
380 [
381 'oi_name' => $this->getNameFromTitle( File::normalizeTitle( $dbKey ) ),
382 'oi_timestamp' => $dbr->timestamp( $search['time'] )
383 ],
385 );
386 }
387 }
388
389 if ( count( $oiConds ) ) {
390 $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr );
391
392 $res = $queryBuilder->where( $dbr->makeList( $oiConds, LIST_OR ) )
393 ->caller( __METHOD__ )->fetchResultSet();
394 $applyMatchingFiles( $res, $searchSet, $finalFiles );
395 }
396
397 // Check for redirects...
398 foreach ( $searchSet as $dbKey => $search ) {
399 if ( !empty( $search['ignoreRedirect'] ) ) {
400 continue;
401 }
402
403 $title = File::normalizeTitle( $dbKey );
404 $redir = $this->checkRedirect( $title ); // hopefully hits memcached
405
406 if ( $redir && $redir->getNamespace() === NS_FILE ) {
407 $file = $this->newFile( $redir );
408 if ( $file && $fileMatchesSearch( $file, $search ) ) {
409 $file->redirectedFrom( $title->getDBkey() );
410 if ( $flags & FileRepo::NAME_AND_TIME_ONLY ) {
411 $finalFiles[$dbKey] = [
412 'title' => $file->getTitle()->getDBkey(),
413 'timestamp' => $file->getTimestamp()
414 ];
415 } else {
416 $finalFiles[$dbKey] = $file;
417 }
418 }
419 }
420 }
421
422 return $finalFiles;
423 }
424
432 public function findBySha1( $hash ) {
433 $queryBuilder = FileSelectQueryBuilder::newForFile( $this->getReplicaDB() );
434 $res = $queryBuilder->where( [ 'img_sha1' => $hash ] )
435 ->orderBy( 'img_name' )
436 ->caller( __METHOD__ )->fetchResultSet();
437
438 $result = [];
439 foreach ( $res as $row ) {
440 $result[] = $this->newFileFromRow( $row );
441 }
442 $res->free();
443
444 return $result;
445 }
446
456 public function findBySha1s( array $hashes ) {
457 if ( $hashes === [] ) {
458 return []; // empty parameter
459 }
460
461 $dbr = $this->getReplicaDB();
462 $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr );
463
464 $queryBuilder->where( [ 'img_sha1' => $hashes ] )
465 ->orderBy( 'img_name' );
466 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
467
468 $result = [];
469 foreach ( $res as $row ) {
470 $file = $this->newFileFromRow( $row );
471 $result[$file->getSha1()][] = $file;
472 }
473 $res->free();
474
475 return $result;
476 }
477
485 public function findFilesByPrefix( $prefix, $limit ) {
486 $dbr = $this->getReplicaDB();
487 $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr );
488
489 $queryBuilder->where( 'img_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) )
490 ->orderBy( 'img_name' )
491 ->limit( intval( $limit ) );
492 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
493
494 // Build file objects
495 $files = [];
496 foreach ( $res as $row ) {
497 $files[] = $this->newFileFromRow( $row );
498 }
499
500 return $files;
501 }
502
507 public function getReplicaDB() {
508 return $this->dbProvider->getReplicaDatabase();
509 }
510
516 public function getPrimaryDB() {
517 return $this->dbProvider->getPrimaryDatabase();
518 }
519
524 protected function getDBFactory() {
525 return static function ( $index ) {
526 return wfGetDB( $index );
527 };
528 }
529
536 protected function hasAcessibleSharedCache() {
537 return $this->hasAccessibleSharedCache;
538 }
539
540 public function getSharedCacheKey( $kClassSuffix, ...$components ) {
541 // T267668: do not include the repo name in the key
542 return $this->hasAcessibleSharedCache()
543 ? $this->wanCache->makeGlobalKey(
544 'filerepo-' . $kClassSuffix,
545 $this->dbDomain,
546 ...$components
547 )
548 : false;
549 }
550
557 public function invalidateImageRedirect( $title ) {
558 $key = $this->getSharedCacheKey( 'file-redirect', md5( $title->getDBkey() ) );
559 if ( $key ) {
560 $this->getPrimaryDB()->onTransactionPreCommitOrIdle(
561 function () use ( $key ) {
562 $this->wanCache->delete( $key );
563 },
564 __METHOD__
565 );
566 }
567 }
568
569 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
570 return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
571 }
572
573 public function storeBatch( array $triplets, $flags = 0 ) {
574 return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
575 }
576
577 public function cleanupBatch( array $files, $flags = 0 ) {
578 return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
579 }
580
581 public function publish(
582 $src,
583 $dstRel,
584 $archiveRel,
585 $flags = 0,
586 array $options = []
587 ) {
588 return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
589 }
590
591 public function publishBatch( array $ntuples, $flags = 0 ) {
592 return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
593 }
594
595 public function delete( $srcRel, $archiveRel ) {
596 return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
597 }
598
599 public function deleteBatch( array $sourceDestPairs ) {
600 return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
601 }
602
610 protected function skipWriteOperationIfSha1( $function, array $args ) {
611 $this->assertWritableRepo(); // fail out if read-only
612
613 if ( $this->hasSha1Storage() ) {
614 wfDebug( __METHOD__ . ": skipped because storage uses sha1 paths" );
615 return Status::newGood();
616 } else {
617 return parent::$function( ...$args );
618 }
619 }
620
630 public function isJsonMetadataEnabled() {
631 return $this->useJsonMetadata;
632 }
633
640 public function isSplitMetadataEnabled() {
641 return $this->isJsonMetadataEnabled() && $this->useSplitMetadata;
642 }
643
650 public function getSplitMetadataThreshold() {
651 return $this->splitMetadataThreshold;
652 }
653
654 public function isMetadataUpdateEnabled() {
655 return $this->updateCompatibleMetadata;
656 }
657
659 return $this->reserializeMetadata;
660 }
661
668 public function getBlobStore(): ?BlobStore {
669 if ( !$this->blobStore ) {
670 $this->blobStore = MediaWikiServices::getInstance()->getBlobStoreFactory()
671 ->newBlobStore( $this->dbDomain );
672 }
673 return $this->blobStore;
674 }
675}
const NS_FILE
Definition Defines.php:70
const LIST_OR
Definition Defines.php:46
const LIST_AND
Definition Defines.php:43
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:88
Proxy backend that manages file layout rewriting for FileRepo.
doOperation(array $op, array $opts=[])
Same as doOperations() except it takes a single operation.
Base class for file repositories.
Definition FileRepo.php:50
assertWritableRepo()
Throw an exception if this repo is read-only by design.
newGood( $value=null)
Create a new good result.
const NAME_AND_TIME_ONLY
Definition FileRepo.php:56
getLocalCacheKey( $kClassSuffix,... $components)
Get a site-local, repository-qualified, WAN cache key.
hasSha1Storage()
Returns whether or not storage is SHA-1 based.
FileBackend $backend
Definition FileRepo.php:73
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition FileRepo.php:398
getDeletedHashPath( $key)
Get a relative path for a deletion archive key, e.g.
getNameFromTitle( $title)
Get the name of a file from its title.
Definition FileRepo.php:716
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition FileRepo.php:422
getInfo()
Return information about the repository.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:70
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:45
skipWriteOperationIfSha1( $function, array $args)
Skips the write operation if storage is sha1-based, executes it normally otherwise.
int null $splitMetadataThreshold
Definition LocalRepo.php:75
getDBFactory()
Get a callback to get a DB handle given an index (DB_REPLICA/DB_PRIMARY)
getSharedCacheKey( $kClassSuffix,... $components)
Get a global, repository-qualified, WAN cache key.
isMetadataUpdateEnabled()
newFileFromRow( $row)
isSplitMetadataEnabled()
Returns true if files should split up large metadata, storing parts of it in the BlobStore.
deletedFileHasKey( $key, $lock=null)
Check if a deleted (filearchive) file has this sha1 key.
callable $oldFileFactoryKey
Definition LocalRepo.php:57
callable $oldFileFactory
Definition LocalRepo.php:55
isJsonMetadataEnabled()
Returns true if files should store metadata in JSON format.
cleanupBatch(array $files, $flags=0)
Deletes a batch of files.
publishBatch(array $ntuples, $flags=0)
Publish a batch of files.
findFiles(array $items, $flags=0)
Find many files at once.
findFilesByPrefix( $prefix, $limit)
Return an array of files where the name starts with $prefix.
findBySha1s(array $hashes)
Get an array of arrays or iterators of file objects for files that have the given SHA-1 content hashe...
getBlobStore()
Get a BlobStore for storing and retrieving large metadata, or null if that can't be done.
IConnectionProvider $dbProvider
Definition LocalRepo.php:61
callable $oldFileFromRowFactory
Definition LocalRepo.php:53
string $dbDomain
DB domain of the repo wiki.
Definition LocalRepo.php:60
BlobStore $blobStore
Definition LocalRepo.php:66
bool $useJsonMetadata
Definition LocalRepo.php:69
invalidateImageRedirect( $title)
Invalidates image redirect cache related to that image.
cleanupDeletedBatch(array $storageKeys)
Delete files in the deleted directory if they are not referenced in the filearchive table.
bool $updateCompatibleMetadata
Definition LocalRepo.php:78
getPrimaryDB()
Get a connection to the primary DB.
checkRedirect( $title)
Checks if there is a redirect named as $title.
hasAcessibleSharedCache()
Check whether the repo has a shared cache, accessible from the current site context.
bool $hasAccessibleSharedCache
Whether shared cache keys are exposed/accessible.
Definition LocalRepo.php:63
callable $fileFactoryKey
Definition LocalRepo.php:49
getReplicaDB()
Get a connection to the replica DB.
store( $srcPath, $dstZone, $dstRel, $flags=0)
Store a file to a given destination.
publish( $src, $dstRel, $archiveRel, $flags=0, array $options=[])
Copy or move a file either from a storage path, virtual URL, or file system path, into this repositor...
storeBatch(array $triplets, $flags=0)
Store a batch of files.
getSplitMetadataThreshold()
Get the threshold above which metadata items should be split into separate storage,...
callable $fileFromRowFactory
Definition LocalRepo.php:51
__construct(array $info=null)
Definition LocalRepo.php:83
deleteBatch(array $sourceDestPairs)
Move a group of files to the deletion archive.
hiddenFileHasKey( $key, $lock=null)
Check if a hidden (revision delete) file has this sha1 key.
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
newFromArchiveName( $title, $archiveName)
bool $reserializeMetadata
Definition LocalRepo.php:81
isMetadataReserializeEnabled()
callable $fileFactory
Definition LocalRepo.php:47
bool $useSplitMetadata
Definition LocalRepo.php:72
findBySha1( $hash)
Get an array or iterator of file objects for files that have a given SHA-1 content hash.
MediaWiki exception.
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:58
Represents a title within MediaWiki.
Definition Title.php:76
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Represents the target of a wiki link.
Interface for objects (potentially) representing an editable wiki page.
This interface represents the authority associated the current execution context, such as a web reque...
Definition Authority.php:37
Service for loading and storing data blobs.
Definition BlobStore.php:33
Provide primary and replica IDatabase connections.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
A database connection without write operations.
Result wrapper for grabbing data queried from an IDatabase object.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!is_readable( $file)) $ext
Definition router.php:48