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' );
41 * @return OldLocalFile
43 function newFromArchiveName( $title, $archiveName ) {
44 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
48 * Delete files in the deleted directory if they are not referenced in the
49 * filearchive table. This needs to be done in the repo because it needs to
50 * interleave database locks with file operations, which is potentially a
53 * @param $storageKeys array
55 * @return FileRepoStatus
57 function cleanupDeletedBatch( $storageKeys ) {
58 $root = $this->getZonePath( 'deleted' );
59 $dbw = $this->getMasterDB();
60 $status = $this->newGood();
61 $storageKeys = array_unique( $storageKeys );
62 foreach ( $storageKeys as $key ) {
63 $hashPath = $this->getDeletedHashPath( $key );
64 $path = "$root/$hashPath$key";
66 $inuse = $dbw->selectField( 'filearchive', '1',
67 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
68 __METHOD__
, array( 'FOR UPDATE' ) );
70 $sha1 = self
::getHashFromKey( $key );
71 $ext = substr( $key, strcspn( $key, '.' ) +
1 );
72 $ext = File
::normalizeExtension($ext);
73 $inuse = $dbw->selectField( 'oldimage', '1',
74 array( 'oi_sha1' => $sha1,
75 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
76 $dbw->bitAnd('oi_deleted', File
::DELETED_FILE
) => File
::DELETED_FILE
),
77 __METHOD__
, array( 'FOR UPDATE' ) );
80 wfDebug( __METHOD__
. ": deleting $key\n" );
82 $unlink = unlink( $path );
85 $status->error( 'undelete-cleanup-error', $path );
89 wfDebug( __METHOD__
. ": $key still in use\n" );
90 $status->successCount++
;
98 * Gets the SHA1 hash from a storage key
103 public static function getHashFromKey( $key ) {
104 return strtok( $key, '.' );
108 * Checks if there is a redirect named as $title
110 * @param $title Title of file
112 function checkRedirect( $title ) {
115 if( is_string( $title ) ) {
116 $title = Title
::newFromText( $title );
118 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
119 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
122 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
123 if ( $memcKey === false ) {
124 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
125 $expiry = 300; // no invalidation, 5 minutes
127 $expiry = 86400; // has invalidation, 1 day
129 $cachedValue = $wgMemc->get( $memcKey );
130 if ( $cachedValue === ' ' ||
$cachedValue === '' ) {
133 } elseif ( strval( $cachedValue ) !== '' ) {
134 return Title
::newFromText( $cachedValue, NS_FILE
);
135 } // else $cachedValue is false or null: cache miss
137 $id = $this->getArticleID( $title );
139 $wgMemc->set( $memcKey, " ", $expiry );
142 $dbr = $this->getSlaveDB();
143 $row = $dbr->selectRow(
145 array( 'rd_title', 'rd_namespace' ),
146 array( 'rd_from' => $id ),
150 if( $row && $row->rd_namespace
== NS_FILE
) {
151 $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
152 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
155 $wgMemc->set( $memcKey, '', $expiry );
162 * Function link Title::getArticleID().
163 * We can't say Title object, what database it should use, so we duplicate that function here.
164 * @param $title Title
166 protected function getArticleID( $title ) {
167 if( !$title instanceof Title
) {
170 $dbr = $this->getSlaveDB();
171 $id = $dbr->selectField(
175 'page_namespace' => $title->getNamespace(),
176 'page_title' => $title->getDBkey(),
178 __METHOD__
//Function name
184 * Get an array or iterator of file objects for files that have a given
185 * SHA-1 content hash.
187 function findBySha1( $hash ) {
188 $dbr = $this->getSlaveDB();
191 LocalFile
::selectFields(),
192 array( 'img_sha1' => $hash )
196 foreach ( $res as $row ) {
197 $result[] = $this->newFileFromRow( $row );
205 * Get a connection to the slave DB
207 function getSlaveDB() {
208 return wfGetDB( DB_SLAVE
);
212 * Get a connection to the master DB
214 function getMasterDB() {
215 return wfGetDB( DB_MASTER
);
219 * Get a key on the primary cache for this repository.
220 * Returns false if the repository's cache is not accessible at this site.
221 * The parameters are the parts of the key, as for wfMemcKey().
223 function getSharedCacheKey( /*...*/ ) {
224 $args = func_get_args();
225 return call_user_func_array( 'wfMemcKey', $args );
229 * Invalidates image redirect cache related to that image
231 * @param $title Title of page
233 function invalidateImageRedirect( $title ) {
235 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
237 $wgMemc->delete( $memcKey );