3 * Local repository that stores files in the local filesystem and registers them
4 * in the wiki's own database.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 use MediaWiki\MediaWikiServices
;
26 use Wikimedia\Rdbms\IResultWrapper
;
27 use Wikimedia\Rdbms\Database
;
28 use Wikimedia\Rdbms\IDatabase
;
31 * A repository that stores files in the local filesystem and registers them
32 * in the wiki's own database. This is the most commonly used repository class.
36 class LocalRepo
extends FileRepo
{
38 protected $fileFactory = [ LocalFile
::class, 'newFromTitle' ];
40 protected $fileFactoryKey = [ LocalFile
::class, 'newFromKey' ];
42 protected $fileFromRowFactory = [ LocalFile
::class, 'newFromRow' ];
44 protected $oldFileFromRowFactory = [ OldLocalFile
::class, 'newFromRow' ];
46 protected $oldFileFactory = [ OldLocalFile
::class, 'newFromTitle' ];
48 protected $oldFileFactoryKey = [ OldLocalFile
::class, 'newFromKey' ];
50 function __construct( array $info = null ) {
51 parent
::__construct( $info );
53 $this->hasSha1Storage
= isset( $info['storageLayout'] )
54 && $info['storageLayout'] === 'sha1';
56 if ( $this->hasSha1Storage() ) {
57 $this->backend
= new FileBackendDBRepoWrapper( [
58 'backend' => $this->backend
,
59 'repoName' => $this->name
,
60 'dbHandleFactory' => $this->getDBFactory()
67 * @param stdClass $row
70 function newFileFromRow( $row ) {
71 if ( isset( $row->img_name
) ) {
72 return call_user_func( $this->fileFromRowFactory
, $row, $this );
73 } elseif ( isset( $row->oi_name
) ) {
74 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
76 throw new MWException( __METHOD__
. ': invalid row' );
82 * @param string $archiveName
83 * @return OldLocalFile
85 function newFromArchiveName( $title, $archiveName ) {
86 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
90 * Delete files in the deleted directory if they are not referenced in the
91 * filearchive table. This needs to be done in the repo because it needs to
92 * interleave database locks with file operations, which is potentially a
95 * @param string[] $storageKeys
99 function cleanupDeletedBatch( array $storageKeys ) {
100 if ( $this->hasSha1Storage() ) {
101 wfDebug( __METHOD__
. ": skipped because storage uses sha1 paths\n" );
102 return Status
::newGood();
105 $backend = $this->backend
; // convenience
106 $root = $this->getZonePath( 'deleted' );
107 $dbw = $this->getMasterDB();
108 $status = $this->newGood();
109 $storageKeys = array_unique( $storageKeys );
110 foreach ( $storageKeys as $key ) {
111 $hashPath = $this->getDeletedHashPath( $key );
112 $path = "$root/$hashPath$key";
113 $dbw->startAtomic( __METHOD__
);
114 // Check for usage in deleted/hidden files and preemptively
115 // lock the key to avoid any future use until we are finished.
116 $deleted = $this->deletedFileHasKey( $key, 'lock' );
117 $hidden = $this->hiddenFileHasKey( $key, 'lock' );
118 if ( !$deleted && !$hidden ) { // not in use now
119 wfDebug( __METHOD__
. ": deleting $key\n" );
120 $op = [ 'op' => 'delete', 'src' => $path ];
121 if ( !$backend->doOperation( $op )->isOK() ) {
122 $status->error( 'undelete-cleanup-error', $path );
123 $status->failCount++
;
126 wfDebug( __METHOD__
. ": $key still in use\n" );
127 $status->successCount++
;
129 $dbw->endAtomic( __METHOD__
);
136 * Check if a deleted (filearchive) file has this sha1 key
138 * @param string $key File storage key (base-36 sha1 key with file extension)
139 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
140 * @return bool File with this key is in use
142 protected function deletedFileHasKey( $key, $lock = null ) {
143 $options = ( $lock === 'lock' ) ?
[ 'FOR UPDATE' ] : [];
145 $dbw = $this->getMasterDB();
147 return (bool)$dbw->selectField( 'filearchive', '1',
148 [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ],
154 * Check if a hidden (revision delete) file has this sha1 key
156 * @param string $key File storage key (base-36 sha1 key with file extension)
157 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
158 * @return bool File with this key is in use
160 protected function hiddenFileHasKey( $key, $lock = null ) {
161 $options = ( $lock === 'lock' ) ?
[ 'FOR UPDATE' ] : [];
163 $sha1 = self
::getHashFromKey( $key );
164 $ext = File
::normalizeExtension( substr( $key, strcspn( $key, '.' ) +
1 ) );
166 $dbw = $this->getMasterDB();
168 return (bool)$dbw->selectField( 'oldimage', '1',
169 [ 'oi_sha1' => $sha1,
170 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
171 $dbw->bitAnd( 'oi_deleted', File
::DELETED_FILE
) => File
::DELETED_FILE
],
177 * Gets the SHA1 hash from a storage key
182 public static function getHashFromKey( $key ) {
183 return strtok( $key, '.' );
187 * Checks if there is a redirect named as $title
189 * @param Title $title Title of file
192 function checkRedirect( Title
$title ) {
193 $title = File
::normalizeTitle( $title, 'exception' );
195 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
196 if ( $memcKey === false ) {
197 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
198 $expiry = 300; // no invalidation, 5 minutes
200 $expiry = 86400; // has invalidation, 1 day
203 $method = __METHOD__
;
204 $redirDbKey = $this->wanCache
->getWithSetCallback(
207 function ( $oldValue, &$ttl, array &$setOpts ) use ( $method, $title ) {
208 $dbr = $this->getReplicaDB(); // possibly remote DB
210 $setOpts +
= Database
::getCacheSetOptions( $dbr );
212 $row = $dbr->selectRow(
213 [ 'page', 'redirect' ],
214 [ 'rd_namespace', 'rd_title' ],
216 'page_namespace' => $title->getNamespace(),
217 'page_title' => $title->getDBkey(),
223 return ( $row && $row->rd_namespace
== NS_FILE
)
224 ? Title
::makeTitle( $row->rd_namespace
, $row->rd_title
)->getDBkey()
225 : ''; // negative cache
227 [ 'pcTTL' => WANObjectCache
::TTL_PROC_LONG
]
230 // @note: also checks " " for b/c
231 if ( $redirDbKey !== ' ' && strval( $redirDbKey ) !== '' ) {
232 // Page is a redirect to another file
233 return Title
::newFromText( $redirDbKey, NS_FILE
);
236 return false; // no redirect
239 public function findFiles( array $items, $flags = 0 ) {
240 $finalFiles = []; // map of (DB key => corresponding File) for matches
242 $searchSet = []; // map of (normalized DB key => search params)
243 foreach ( $items as $item ) {
244 if ( is_array( $item ) ) {
245 $title = File
::normalizeTitle( $item['title'] );
247 $searchSet[$title->getDBkey()] = $item;
250 $title = File
::normalizeTitle( $item );
252 $searchSet[$title->getDBkey()] = [];
257 $fileMatchesSearch = function ( File
$file, array $search ) {
258 // Note: file name comparison done elsewhere (to handle redirects)
259 $user = ( !empty( $search['private'] ) && $search['private'] instanceof User
)
266 ( empty( $search['time'] ) && !$file->isOld() ) ||
267 ( !empty( $search['time'] ) && $search['time'] === $file->getTimestamp() )
269 ( !empty( $search['private'] ) ||
!$file->isDeleted( File
::DELETED_FILE
) ) &&
270 $file->userCan( File
::DELETED_FILE
, $user )
274 $applyMatchingFiles = function ( IResultWrapper
$res, &$searchSet, &$finalFiles )
275 use ( $fileMatchesSearch, $flags )
277 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
278 $info = $this->getInfo();
279 foreach ( $res as $row ) {
280 $file = $this->newFileFromRow( $row );
281 // There must have been a search for this DB key, but this has to handle the
282 // cases were title capitalization is different on the client and repo wikis.
283 $dbKeysLook = [ strtr( $file->getName(), ' ', '_' ) ];
284 if ( !empty( $info['initialCapital'] ) ) {
285 // Search keys for "hi.png" and "Hi.png" should use the "Hi.png file"
286 $dbKeysLook[] = $contLang->lcfirst( $file->getName() );
288 foreach ( $dbKeysLook as $dbKey ) {
289 if ( isset( $searchSet[$dbKey] )
290 && $fileMatchesSearch( $file, $searchSet[$dbKey] )
292 $finalFiles[$dbKey] = ( $flags & FileRepo
::NAME_AND_TIME_ONLY
)
293 ?
[ 'title' => $dbKey, 'timestamp' => $file->getTimestamp() ]
295 unset( $searchSet[$dbKey] );
301 $dbr = $this->getReplicaDB();
305 foreach ( array_keys( $searchSet ) as $dbKey ) {
306 $imgNames[] = $this->getNameFromTitle( File
::normalizeTitle( $dbKey ) );
309 if ( count( $imgNames ) ) {
310 $fileQuery = LocalFile
::getQueryInfo();
311 $res = $dbr->select( $fileQuery['tables'], $fileQuery['fields'], [ 'img_name' => $imgNames ],
312 __METHOD__
, [], $fileQuery['joins'] );
313 $applyMatchingFiles( $res, $searchSet, $finalFiles );
316 // Query old image table
317 $oiConds = []; // WHERE clause array for each file
318 foreach ( $searchSet as $dbKey => $search ) {
319 if ( isset( $search['time'] ) ) {
320 $oiConds[] = $dbr->makeList(
322 'oi_name' => $this->getNameFromTitle( File
::normalizeTitle( $dbKey ) ),
323 'oi_timestamp' => $dbr->timestamp( $search['time'] )
330 if ( count( $oiConds ) ) {
331 $fileQuery = OldLocalFile
::getQueryInfo();
332 $res = $dbr->select( $fileQuery['tables'], $fileQuery['fields'],
333 $dbr->makeList( $oiConds, LIST_OR
),
334 __METHOD__
, [], $fileQuery['joins'] );
335 $applyMatchingFiles( $res, $searchSet, $finalFiles );
338 // Check for redirects...
339 foreach ( $searchSet as $dbKey => $search ) {
340 if ( !empty( $search['ignoreRedirect'] ) ) {
344 $title = File
::normalizeTitle( $dbKey );
345 $redir = $this->checkRedirect( $title ); // hopefully hits memcached
347 if ( $redir && $redir->getNamespace() == NS_FILE
) {
348 $file = $this->newFile( $redir );
349 if ( $file && $fileMatchesSearch( $file, $search ) ) {
350 $file->redirectedFrom( $title->getDBkey() );
351 if ( $flags & FileRepo
::NAME_AND_TIME_ONLY
) {
352 $finalFiles[$dbKey] = [
353 'title' => $file->getTitle()->getDBkey(),
354 'timestamp' => $file->getTimestamp()
357 $finalFiles[$dbKey] = $file;
367 * Get an array or iterator of file objects for files that have a given
368 * SHA-1 content hash.
370 * @param string $hash A sha1 hash to look for
371 * @return LocalFile[]
373 function findBySha1( $hash ) {
374 $dbr = $this->getReplicaDB();
375 $fileQuery = LocalFile
::getQueryInfo();
377 $fileQuery['tables'],
378 $fileQuery['fields'],
379 [ 'img_sha1' => $hash ],
381 [ 'ORDER BY' => 'img_name' ],
386 foreach ( $res as $row ) {
387 $result[] = $this->newFileFromRow( $row );
395 * Get an array of arrays or iterators of file objects for files that
396 * have the given SHA-1 content hashes.
398 * Overrides generic implementation in FileRepo for performance reason
400 * @param string[] $hashes An array of hashes
401 * @return array[] An Array of arrays or iterators of file objects and the hash as key
403 function findBySha1s( array $hashes ) {
404 if ( $hashes === [] ) {
405 return []; // empty parameter
408 $dbr = $this->getReplicaDB();
409 $fileQuery = LocalFile
::getQueryInfo();
411 $fileQuery['tables'],
412 $fileQuery['fields'],
413 [ 'img_sha1' => $hashes ],
415 [ 'ORDER BY' => 'img_name' ],
420 foreach ( $res as $row ) {
421 $file = $this->newFileFromRow( $row );
422 $result[$file->getSha1()][] = $file;
430 * Return an array of files where the name starts with $prefix.
432 * @param string $prefix The prefix to search for
433 * @param int $limit The maximum amount of files to return
434 * @return LocalFile[]
436 public function findFilesByPrefix( $prefix, $limit ) {
437 $selectOptions = [ 'ORDER BY' => 'img_name', 'LIMIT' => intval( $limit ) ];
440 $dbr = $this->getReplicaDB();
441 $fileQuery = LocalFile
::getQueryInfo();
443 $fileQuery['tables'],
444 $fileQuery['fields'],
445 'img_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ),
451 // Build file objects
453 foreach ( $res as $row ) {
454 $files[] = $this->newFileFromRow( $row );
461 * Get a connection to the replica DB
464 function getReplicaDB() {
465 return wfGetDB( DB_REPLICA
);
469 * Alias for getReplicaDB()
472 * @deprecated Since 1.29
474 function getSlaveDB() {
475 return $this->getReplicaDB();
479 * Get a connection to the master DB
482 function getMasterDB() {
483 return wfGetDB( DB_MASTER
);
487 * Get a callback to get a DB handle given an index (DB_REPLICA/DB_MASTER)
490 protected function getDBFactory() {
491 return function ( $index ) {
492 return wfGetDB( $index );
497 * Get a key on the primary cache for this repository.
498 * Returns false if the repository's cache is not accessible at this site.
499 * The parameters are the parts of the key.
503 function getSharedCacheKey( /*...*/ ) {
504 $args = func_get_args();
506 return $this->wanCache
->makeKey( ...$args );
510 * Invalidates image redirect cache related to that image
512 * @param Title $title Title of page
515 function invalidateImageRedirect( Title
$title ) {
516 $key = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
518 $this->getMasterDB()->onTransactionPreCommitOrIdle(
519 function () use ( $key ) {
520 $this->wanCache
->delete( $key );
528 * Return information about the repository.
536 return array_merge( parent
::getInfo(), [
537 'favicon' => wfExpandUrl( $wgFavicon ),
541 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
542 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
545 public function storeBatch( array $triplets, $flags = 0 ) {
546 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
549 public function cleanupBatch( array $files, $flags = 0 ) {
550 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
553 public function publish(
560 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
563 public function publishBatch( array $ntuples, $flags = 0 ) {
564 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
567 public function delete( $srcRel, $archiveRel ) {
568 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
571 public function deleteBatch( array $sourceDestPairs ) {
572 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
576 * Skips the write operation if storage is sha1-based, executes it normally otherwise
578 * @param string $function
582 protected function skipWriteOperationIfSha1( $function, array $args ) {
583 $this->assertWritableRepo(); // fail out if read-only
585 if ( $this->hasSha1Storage() ) {
586 wfDebug( __METHOD__
. ": skipped because storage uses sha1 paths\n" );
587 return Status
::newGood();
589 return parent
::$function( ...$args );