3 * Local repository that stores files in the local filesystem and registers them
4 * in the wiki's own database.
11 * A repository that stores files in the local filesystem and registers them
12 * in the wiki's own database. This is the most commonly used repository class.
15 class LocalRepo
extends FSRepo
{
16 var $fileFactory = array( 'LocalFile', 'newFromTitle' );
17 var $fileFactoryKey = array( 'LocalFile', 'newFromKey' );
18 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
19 var $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
20 var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
21 var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
28 function newFileFromRow( $row ) {
29 if ( isset( $row->img_name
) ) {
30 return call_user_func( $this->fileFromRowFactory
, $row, $this );
31 } elseif ( isset( $row->oi_name
) ) {
32 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
34 throw new MWException( __METHOD__
.': invalid row' );
38 function newFromArchiveName( $title, $archiveName ) {
39 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
43 * Delete files in the deleted directory if they are not referenced in the
44 * filearchive table. This needs to be done in the repo because it needs to
45 * interleave database locks with file operations, which is potentially a
47 * @return FileRepoStatus
49 function cleanupDeletedBatch( $storageKeys ) {
50 $root = $this->getZonePath( 'deleted' );
51 $dbw = $this->getMasterDB();
52 $status = $this->newGood();
53 $storageKeys = array_unique($storageKeys);
54 foreach ( $storageKeys as $key ) {
55 $hashPath = $this->getDeletedHashPath( $key );
56 $path = "$root/$hashPath$key";
58 $inuse = $dbw->selectField( 'filearchive', '1',
59 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
60 __METHOD__
, array( 'FOR UPDATE' ) );
62 $sha1 = self
::getHashFromKey( $key );
63 $ext = substr( $key, strcspn( $key, '.' ) +
1 );
64 $ext = File
::normalizeExtension($ext);
65 $inuse = $dbw->selectField( 'oldimage', '1',
66 array( 'oi_sha1' => $sha1,
67 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
68 $dbw->bitAnd('oi_deleted', File
::DELETED_FILE
) => File
::DELETED_FILE
),
69 __METHOD__
, array( 'FOR UPDATE' ) );
72 wfDebug( __METHOD__
. ": deleting $key\n" );
73 if ( !@unlink
( $path ) ) {
74 $status->error( 'undelete-cleanup-error', $path );
78 wfDebug( __METHOD__
. ": $key still in use\n" );
79 $status->successCount++
;
87 * Gets the SHA1 hash from a storage key
92 public static function getHashFromKey( $key ) {
93 return strtok( $key, '.' );
97 * Checks if there is a redirect named as $title
99 * @param $title Title of file
101 function checkRedirect( $title ) {
104 if( is_string( $title ) ) {
105 $title = Title
::newFromText( $title );
107 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
108 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
111 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
112 if ( $memcKey === false ) {
113 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
114 $expiry = 300; // no invalidation, 5 minutes
116 $expiry = 86400; // has invalidation, 1 day
118 $cachedValue = $wgMemc->get( $memcKey );
119 if ( $cachedValue === ' ' ||
$cachedValue === '' ) {
122 } elseif ( strval( $cachedValue ) !== '' ) {
123 return Title
::newFromText( $cachedValue, NS_FILE
);
124 } // else $cachedValue is false or null: cache miss
126 $id = $this->getArticleID( $title );
128 $wgMemc->set( $memcKey, " ", $expiry );
131 $dbr = $this->getSlaveDB();
132 $row = $dbr->selectRow(
134 array( 'rd_title', 'rd_namespace' ),
135 array( 'rd_from' => $id ),
139 if( $row && $row->rd_namespace
== NS_FILE
) {
140 $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
141 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
144 $wgMemc->set( $memcKey, '', $expiry );
151 * Function link Title::getArticleID().
152 * We can't say Title object, what database it should use, so we duplicate that function here.
153 * @param $title Title
155 protected function getArticleID( $title ) {
156 if( !$title instanceof Title
) {
159 $dbr = $this->getSlaveDB();
160 $id = $dbr->selectField(
164 'page_namespace' => $title->getNamespace(),
165 'page_title' => $title->getDBkey(),
167 __METHOD__
//Function name
173 * Get an array or iterator of file objects for files that have a given
174 * SHA-1 content hash.
176 function findBySha1( $hash ) {
177 $dbr = $this->getSlaveDB();
180 LocalFile
::selectFields(),
181 array( 'img_sha1' => $hash )
185 foreach ( $res as $row ) {
186 $result[] = $this->newFileFromRow( $row );
194 * Get a connection to the slave DB
196 function getSlaveDB() {
197 return wfGetDB( DB_SLAVE
);
201 * Get a connection to the master DB
203 function getMasterDB() {
204 return wfGetDB( DB_MASTER
);
208 * Get a key on the primary cache for this repository.
209 * Returns false if the repository's cache is not accessible at this site.
210 * The parameters are the parts of the key, as for wfMemcKey().
212 function getSharedCacheKey( /*...*/ ) {
213 $args = func_get_args();
214 return call_user_func_array( 'wfMemcKey', $args );
218 * Invalidates image redirect cache related to that image
220 * @param $title Title of page
222 function invalidateImageRedirect( $title ) {
224 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
226 $wgMemc->delete( $memcKey );