3 * A repository that stores files in the local filesystem and registers them
4 * in the wiki's own database. This is the most commonly used repository class.
7 class LocalRepo
extends FSRepo
{
8 var $fileFactory = array( 'LocalFile', 'newFromTitle' );
9 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
10 var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
11 var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
13 function newFileFromRow( $row ) {
14 if ( isset( $row->img_name
) ) {
15 return call_user_func( $this->fileFromRowFactory
, $row, $this );
16 } elseif ( isset( $row->oi_name
) ) {
17 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
19 throw new MWException( __METHOD__
.': invalid row' );
23 function newFromArchiveName( $title, $archiveName ) {
24 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
28 * Delete files in the deleted directory if they are not referenced in the
29 * filearchive table. This needs to be done in the repo because it needs to
30 * interleave database locks with file operations, which is potentially a
32 * @return FileRepoStatus
34 function cleanupDeletedBatch( $storageKeys ) {
35 $root = $this->getZonePath( 'deleted' );
36 $dbw = $this->getMasterDB();
37 $status = $this->newGood();
38 $storageKeys = array_unique($storageKeys);
39 foreach ( $storageKeys as $key ) {
40 $hashPath = $this->getDeletedHashPath( $key );
41 $path = "$root/$hashPath$key";
43 $inuse = $dbw->selectField( 'filearchive', '1',
44 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
45 __METHOD__
, array( 'FOR UPDATE' ) );
47 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
48 $ext = substr( $key, strcspn($key,'.') +
1 );
49 $ext = File
::normalizeExtension($ext);
50 $inuse = $dbw->selectField( 'oldimage', '1',
51 array( 'oi_sha1' => $sha1,
52 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
53 $dbw->bitAnd('oi_deleted', File
::DELETED_FILE
) => File
::DELETED_FILE
),
54 __METHOD__
, array( 'FOR UPDATE' ) );
57 wfDebug( __METHOD__
. ": deleting $key\n" );
58 if ( !@unlink
( $path ) ) {
59 $status->error( 'undelete-cleanup-error', $path );
63 wfDebug( __METHOD__
. ": $key still in use\n" );
64 $status->successCount++
;
72 * Checks if there is a redirect named as $title
74 * @param $title Title of file
76 function checkRedirect( $title ) {
79 if( is_string( $title ) ) {
80 $title = Title
::newFromTitle( $title );
82 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
83 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
86 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
87 if ( $memcKey === false ) {
88 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
89 $expiry = 300; // no invalidation, 5 minutes
91 $expiry = 86400; // has invalidation, 1 day
93 $cachedValue = $wgMemc->get( $memcKey );
94 if ( $cachedValue === ' ' ||
$cachedValue === '' ) {
97 } elseif ( strval( $cachedValue ) !== '' ) {
98 return Title
::newFromText( $cachedValue, NS_FILE
);
99 } // else $cachedValue is false or null: cache miss
101 $id = $this->getArticleID( $title );
103 $wgMemc->set( $memcKey, " ", $expiry );
106 $dbr = $this->getSlaveDB();
107 $row = $dbr->selectRow(
109 array( 'rd_title', 'rd_namespace' ),
110 array( 'rd_from' => $id ),
114 if( $row && $row->rd_namespace
== NS_FILE
) {
115 $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
116 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
119 $wgMemc->set( $memcKey, '', $expiry );
126 * Function link Title::getArticleID().
127 * We can't say Title object, what database it should use, so we duplicate that function here.
129 protected function getArticleID( $title ) {
130 if( !$title instanceof Title
) {
133 $dbr = $this->getSlaveDB();
134 $id = $dbr->selectField(
138 'page_namespace' => $title->getNamespace(),
139 'page_title' => $title->getDBkey(),
141 __METHOD__
//Function name
147 * Get an array or iterator of file objects for files that have a given
148 * SHA-1 content hash.
150 function findBySha1( $hash ) {
151 $dbr = $this->getSlaveDB();
154 LocalFile
::selectFields(),
155 array( 'img_sha1' => $hash )
159 while ( $row = $res->fetchObject() )
160 $result[] = $this->newFileFromRow( $row );
166 * Get a connection to the slave DB
168 function getSlaveDB() {
169 return wfGetDB( DB_SLAVE
);
173 * Get a connection to the master DB
175 function getMasterDB() {
176 return wfGetDB( DB_MASTER
);
180 * Get a key on the primary cache for this repository.
181 * Returns false if the repository's cache is not accessible at this site.
182 * The parameters are the parts of the key, as for wfMemcKey().
184 function getSharedCacheKey( /*...*/ ) {
185 $args = func_get_args();
186 return call_user_func_array( 'wfMemcKey', $args );
190 * Invalidates image redirect cache related to that image
192 * @param $title Title of page
194 function invalidateImageRedirect( $title ) {
196 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
198 $wgMemc->delete( $memcKey );