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
26 * A repository that stores files in the local filesystem and registers them
27 * in the wiki's own database. This is the most commonly used repository class.
31 class LocalRepo
extends FileRepo
{
33 protected $hasSha1Storage = false;
36 protected $fileFactory = array( 'LocalFile', 'newFromTitle' );
39 protected $fileFactoryKey = array( 'LocalFile', 'newFromKey' );
42 protected $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
45 protected $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
48 protected $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
51 protected $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
53 function __construct( array $info = null ) {
54 parent
::__construct( $info );
56 $this->hasSha1Storage
= isset( $info['storageLayout'] ) && $info['storageLayout'] === 'sha1';
58 if ( $this->hasSha1Storage() ) {
59 $this->backend
= new FileBackendDBRepoWrapper( array(
60 'backend' => $this->backend
,
61 'repoName' => $this->name
,
62 'dbHandleFactory' => $this->getDBFactory()
69 * @param stdClass $row
72 function newFileFromRow( $row ) {
73 if ( isset( $row->img_name
) ) {
74 return call_user_func( $this->fileFromRowFactory
, $row, $this );
75 } elseif ( isset( $row->oi_name
) ) {
76 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
78 throw new MWException( __METHOD__
. ': invalid row' );
84 * @param string $archiveName
85 * @return OldLocalFile
87 function newFromArchiveName( $title, $archiveName ) {
88 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
92 * Delete files in the deleted directory if they are not referenced in the
93 * filearchive table. This needs to be done in the repo because it needs to
94 * interleave database locks with file operations, which is potentially a
97 * @param array $storageKeys
99 * @return FileRepoStatus
101 function cleanupDeletedBatch( array $storageKeys ) {
102 if ( $this->hasSha1Storage() ) {
103 wfDebug( __METHOD__
. ": skipped because storage uses sha1 paths\n" );
104 return Status
::newGood();
107 $backend = $this->backend
; // convenience
108 $root = $this->getZonePath( 'deleted' );
109 $dbw = $this->getMasterDB();
110 $status = $this->newGood();
111 $storageKeys = array_unique( $storageKeys );
112 foreach ( $storageKeys as $key ) {
113 $hashPath = $this->getDeletedHashPath( $key );
114 $path = "$root/$hashPath$key";
115 $dbw->startAtomic( __METHOD__
);
116 // Check for usage in deleted/hidden files and preemptively
117 // lock the key to avoid any future use until we are finished.
118 $deleted = $this->deletedFileHasKey( $key, 'lock' );
119 $hidden = $this->hiddenFileHasKey( $key, 'lock' );
120 if ( !$deleted && !$hidden ) { // not in use now
121 wfDebug( __METHOD__
. ": deleting $key\n" );
122 $op = array( 'op' => 'delete', 'src' => $path );
123 if ( !$backend->doOperation( $op )->isOK() ) {
124 $status->error( 'undelete-cleanup-error', $path );
125 $status->failCount++
;
128 wfDebug( __METHOD__
. ": $key still in use\n" );
129 $status->successCount++
;
131 $dbw->endAtomic( __METHOD__
);
138 * Check if a deleted (filearchive) file has this sha1 key
140 * @param string $key File storage key (base-36 sha1 key with file extension)
141 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
142 * @return bool File with this key is in use
144 protected function deletedFileHasKey( $key, $lock = null ) {
145 $options = ( $lock === 'lock' ) ?
array( 'FOR UPDATE' ) : array();
147 $dbw = $this->getMasterDB();
149 return (bool)$dbw->selectField( 'filearchive', '1',
150 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
156 * Check if a hidden (revision delete) file has this sha1 key
158 * @param string $key File storage key (base-36 sha1 key with file extension)
159 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
160 * @return bool File with this key is in use
162 protected function hiddenFileHasKey( $key, $lock = null ) {
163 $options = ( $lock === 'lock' ) ?
array( 'FOR UPDATE' ) : array();
165 $sha1 = self
::getHashFromKey( $key );
166 $ext = File
::normalizeExtension( substr( $key, strcspn( $key, '.' ) +
1 ) );
168 $dbw = $this->getMasterDB();
170 return (bool)$dbw->selectField( 'oldimage', '1',
171 array( 'oi_sha1' => $sha1,
172 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
173 $dbw->bitAnd( 'oi_deleted', File
::DELETED_FILE
) => File
::DELETED_FILE
),
179 * Gets the SHA1 hash from a storage key
184 public static function getHashFromKey( $key ) {
185 return strtok( $key, '.' );
189 * Checks if there is a redirect named as $title
191 * @param Title $title Title of file
194 function checkRedirect( Title
$title ) {
195 $cache = ObjectCache
::getMainWANInstance();
197 $title = File
::normalizeTitle( $title, 'exception' );
199 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
200 if ( $memcKey === false ) {
201 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
202 $expiry = 300; // no invalidation, 5 minutes
204 $expiry = 86400; // has invalidation, 1 day
206 $cachedValue = $cache->get( $memcKey );
207 if ( $cachedValue === ' ' ||
$cachedValue === '' ) {
210 } elseif ( strval( $cachedValue ) !== '' && $cachedValue !== ' PURGED' ) {
211 return Title
::newFromText( $cachedValue, NS_FILE
);
212 } // else $cachedValue is false or null: cache miss
214 $id = $this->getArticleID( $title );
216 $cache->set( $memcKey, " ", $expiry );
220 $dbr = $this->getSlaveDB();
221 $row = $dbr->selectRow(
223 array( 'rd_title', 'rd_namespace' ),
224 array( 'rd_from' => $id ),
228 if ( $row && $row->rd_namespace
== NS_FILE
) {
229 $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
230 $cache->set( $memcKey, $targetTitle->getDBkey(), $expiry );
234 $cache->set( $memcKey, '', $expiry );
241 * Function link Title::getArticleID().
242 * We can't say Title object, what database it should use, so we duplicate that function here.
244 * @param Title $title
245 * @return bool|int|mixed
247 protected function getArticleID( $title ) {
248 if ( !$title instanceof Title
) {
251 $dbr = $this->getSlaveDB();
252 $id = $dbr->selectField(
256 'page_namespace' => $title->getNamespace(),
257 'page_title' => $title->getDBkey(),
259 __METHOD__
//Function name
265 public function findFiles( array $items, $flags = 0 ) {
266 $finalFiles = array(); // map of (DB key => corresponding File) for matches
268 $searchSet = array(); // map of (normalized DB key => search params)
269 foreach ( $items as $item ) {
270 if ( is_array( $item ) ) {
271 $title = File
::normalizeTitle( $item['title'] );
273 $searchSet[$title->getDBkey()] = $item;
276 $title = File
::normalizeTitle( $item );
278 $searchSet[$title->getDBkey()] = array();
283 $fileMatchesSearch = function ( File
$file, array $search ) {
284 // Note: file name comparison done elsewhere (to handle redirects)
285 $user = ( !empty( $search['private'] ) && $search['private'] instanceof User
)
292 ( empty( $search['time'] ) && !$file->isOld() ) ||
293 ( !empty( $search['time'] ) && $search['time'] === $file->getTimestamp() )
295 ( !empty( $search['private'] ) ||
!$file->isDeleted( File
::DELETED_FILE
) ) &&
296 $file->userCan( File
::DELETED_FILE
, $user )
301 $applyMatchingFiles = function ( ResultWrapper
$res, &$searchSet, &$finalFiles )
302 use ( $that, $fileMatchesSearch, $flags )
305 $info = $that->getInfo();
306 foreach ( $res as $row ) {
307 $file = $that->newFileFromRow( $row );
308 // There must have been a search for this DB key, but this has to handle the
309 // cases were title capitalization is different on the client and repo wikis.
310 $dbKeysLook = array( strtr( $file->getName(), ' ', '_' ) );
311 if ( !empty( $info['initialCapital'] ) ) {
312 // Search keys for "hi.png" and "Hi.png" should use the "Hi.png file"
313 $dbKeysLook[] = $wgContLang->lcfirst( $file->getName() );
315 foreach ( $dbKeysLook as $dbKey ) {
316 if ( isset( $searchSet[$dbKey] )
317 && $fileMatchesSearch( $file, $searchSet[$dbKey] )
319 $finalFiles[$dbKey] = ( $flags & FileRepo
::NAME_AND_TIME_ONLY
)
320 ?
array( 'title' => $dbKey, 'timestamp' => $file->getTimestamp() )
322 unset( $searchSet[$dbKey] );
328 $dbr = $this->getSlaveDB();
332 foreach ( array_keys( $searchSet ) as $dbKey ) {
333 $imgNames[] = $this->getNameFromTitle( File
::normalizeTitle( $dbKey ) );
336 if ( count( $imgNames ) ) {
337 $res = $dbr->select( 'image',
338 LocalFile
::selectFields(), array( 'img_name' => $imgNames ), __METHOD__
);
339 $applyMatchingFiles( $res, $searchSet, $finalFiles );
342 // Query old image table
343 $oiConds = array(); // WHERE clause array for each file
344 foreach ( $searchSet as $dbKey => $search ) {
345 if ( isset( $search['time'] ) ) {
346 $oiConds[] = $dbr->makeList(
348 'oi_name' => $this->getNameFromTitle( File
::normalizeTitle( $dbKey ) ),
349 'oi_timestamp' => $dbr->timestamp( $search['time'] )
356 if ( count( $oiConds ) ) {
357 $res = $dbr->select( 'oldimage',
358 OldLocalFile
::selectFields(), $dbr->makeList( $oiConds, LIST_OR
), __METHOD__
);
359 $applyMatchingFiles( $res, $searchSet, $finalFiles );
362 // Check for redirects...
363 foreach ( $searchSet as $dbKey => $search ) {
364 if ( !empty( $search['ignoreRedirect'] ) ) {
368 $title = File
::normalizeTitle( $dbKey );
369 $redir = $this->checkRedirect( $title ); // hopefully hits memcached
371 if ( $redir && $redir->getNamespace() == NS_FILE
) {
372 $file = $this->newFile( $redir );
373 if ( $file && $fileMatchesSearch( $file, $search ) ) {
374 $file->redirectedFrom( $title->getDBkey() );
375 if ( $flags & FileRepo
::NAME_AND_TIME_ONLY
) {
376 $finalFiles[$dbKey] = array(
377 'title' => $file->getTitle()->getDBkey(),
378 'timestamp' => $file->getTimestamp()
381 $finalFiles[$dbKey] = $file;
391 * Get an array or iterator of file objects for files that have a given
392 * SHA-1 content hash.
394 * @param string $hash A sha1 hash to look for
397 function findBySha1( $hash ) {
398 $dbr = $this->getSlaveDB();
401 LocalFile
::selectFields(),
402 array( 'img_sha1' => $hash ),
404 array( 'ORDER BY' => 'img_name' )
408 foreach ( $res as $row ) {
409 $result[] = $this->newFileFromRow( $row );
417 * Get an array of arrays or iterators of file objects for files that
418 * have the given SHA-1 content hashes.
420 * Overrides generic implementation in FileRepo for performance reason
422 * @param array $hashes An array of hashes
423 * @return array An Array of arrays or iterators of file objects and the hash as key
425 function findBySha1s( array $hashes ) {
426 if ( !count( $hashes ) ) {
427 return array(); //empty parameter
430 $dbr = $this->getSlaveDB();
433 LocalFile
::selectFields(),
434 array( 'img_sha1' => $hashes ),
436 array( 'ORDER BY' => 'img_name' )
440 foreach ( $res as $row ) {
441 $file = $this->newFileFromRow( $row );
442 $result[$file->getSha1()][] = $file;
450 * Return an array of files where the name starts with $prefix.
452 * @param string $prefix The prefix to search for
453 * @param int $limit The maximum amount of files to return
456 public function findFilesByPrefix( $prefix, $limit ) {
457 $selectOptions = array( 'ORDER BY' => 'img_name', 'LIMIT' => intval( $limit ) );
460 $dbr = $this->getSlaveDB();
463 LocalFile
::selectFields(),
464 'img_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ),
469 // Build file objects
471 foreach ( $res as $row ) {
472 $files[] = $this->newFileFromRow( $row );
479 * Get a connection to the slave DB
480 * @return DatabaseBase
482 function getSlaveDB() {
483 return wfGetDB( DB_SLAVE
);
487 * Get a connection to the master DB
488 * @return DatabaseBase
490 function getMasterDB() {
491 return wfGetDB( DB_MASTER
);
495 * Get a callback to get a DB handle given an index (DB_SLAVE/DB_MASTER)
498 protected function getDBFactory() {
499 return function( $index ) {
500 return wfGetDB( $index );
505 * Get a key on the primary cache for this repository.
506 * Returns false if the repository's cache is not accessible at this site.
507 * The parameters are the parts of the key, as for wfMemcKey().
511 function getSharedCacheKey( /*...*/ ) {
512 $args = func_get_args();
514 return call_user_func_array( 'wfMemcKey', $args );
518 * Invalidates image redirect cache related to that image
520 * @param Title $title Title of page
523 function invalidateImageRedirect( Title
$title ) {
524 $cache = ObjectCache
::getMainWANInstance();
526 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
528 // Set a temporary value for the cache key, to ensure
529 // that this value stays purged long enough so that
530 // it isn't refreshed with a stale value due to a
532 $cache->delete( $memcKey, 12 );
537 * Return information about the repository.
545 return array_merge( parent
::getInfo(), array(
546 'favicon' => wfExpandUrl( $wgFavicon ),
550 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
551 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
554 public function storeBatch( array $triplets, $flags = 0 ) {
555 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
558 public function cleanupBatch( array $files, $flags = 0 ) {
559 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
562 public function publish(
567 array $options = array()
569 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
572 public function publishBatch( array $ntuples, $flags = 0 ) {
573 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
576 public function delete( $srcRel, $archiveRel ) {
577 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
580 public function deleteBatch( array $sourceDestPairs ) {
581 return $this->skipWriteOperationIfSha1( __FUNCTION__
, func_get_args() );
585 * Skips the write operation if storage is sha1-based, executes it normally otherwise
587 * @param string $function
589 * @return FileRepoStatus
591 protected function skipWriteOperationIfSha1( $function, array $args ) {
592 $this->assertWritableRepo(); // fail out if read-only
594 if ( $this->hasSha1Storage() ) {
595 wfDebug( __METHOD__
. ": skipped because storage uses sha1 paths\n" );
596 return Status
::newGood();
598 return call_user_func_array('parent::' . $function, $args );