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 $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
18 var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
19 var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
21 function newFileFromRow( $row ) {
22 if ( isset( $row->img_name
) ) {
23 return call_user_func( $this->fileFromRowFactory
, $row, $this );
24 } elseif ( isset( $row->oi_name
) ) {
25 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
27 throw new MWException( __METHOD__
.': invalid row' );
31 function newFromArchiveName( $title, $archiveName ) {
32 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
36 * Delete files in the deleted directory if they are not referenced in the
37 * filearchive table. This needs to be done in the repo because it needs to
38 * interleave database locks with file operations, which is potentially a
40 * @return FileRepoStatus
42 function cleanupDeletedBatch( $storageKeys ) {
43 $root = $this->getZonePath( 'deleted' );
44 $dbw = $this->getMasterDB();
45 $status = $this->newGood();
46 $storageKeys = array_unique($storageKeys);
47 foreach ( $storageKeys as $key ) {
48 $hashPath = $this->getDeletedHashPath( $key );
49 $path = "$root/$hashPath$key";
51 $inuse = $dbw->selectField( 'filearchive', '1',
52 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
53 __METHOD__
, array( 'FOR UPDATE' ) );
55 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
56 $ext = substr( $key, strcspn($key,'.') +
1 );
57 $ext = File
::normalizeExtension($ext);
58 $inuse = $dbw->selectField( 'oldimage', '1',
59 array( 'oi_sha1' => $sha1,
60 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
61 $dbw->bitAnd('oi_deleted', File
::DELETED_FILE
) => File
::DELETED_FILE
),
62 __METHOD__
, array( 'FOR UPDATE' ) );
65 wfDebug( __METHOD__
. ": deleting $key\n" );
66 if ( !@unlink
( $path ) ) {
67 $status->error( 'undelete-cleanup-error', $path );
71 wfDebug( __METHOD__
. ": $key still in use\n" );
72 $status->successCount++
;
80 * Checks if there is a redirect named as $title
82 * @param $title Title of file
84 function checkRedirect( $title ) {
87 if( is_string( $title ) ) {
88 $title = Title
::newFromTitle( $title );
90 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
91 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
94 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
95 if ( $memcKey === false ) {
96 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
97 $expiry = 300; // no invalidation, 5 minutes
99 $expiry = 86400; // has invalidation, 1 day
101 $cachedValue = $wgMemc->get( $memcKey );
102 if ( $cachedValue === ' ' ||
$cachedValue === '' ) {
105 } elseif ( strval( $cachedValue ) !== '' ) {
106 return Title
::newFromText( $cachedValue, NS_FILE
);
107 } // else $cachedValue is false or null: cache miss
109 $id = $this->getArticleID( $title );
111 $wgMemc->set( $memcKey, " ", $expiry );
114 $dbr = $this->getSlaveDB();
115 $row = $dbr->selectRow(
117 array( 'rd_title', 'rd_namespace' ),
118 array( 'rd_from' => $id ),
122 if( $row && $row->rd_namespace
== NS_FILE
) {
123 $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
124 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
127 $wgMemc->set( $memcKey, '', $expiry );
134 * Function link Title::getArticleID().
135 * We can't say Title object, what database it should use, so we duplicate that function here.
137 protected function getArticleID( $title ) {
138 if( !$title instanceof Title
) {
141 $dbr = $this->getSlaveDB();
142 $id = $dbr->selectField(
146 'page_namespace' => $title->getNamespace(),
147 'page_title' => $title->getDBkey(),
149 __METHOD__
//Function name
155 * Get an array or iterator of file objects for files that have a given
156 * SHA-1 content hash.
158 function findBySha1( $hash ) {
159 $dbr = $this->getSlaveDB();
162 LocalFile
::selectFields(),
163 array( 'img_sha1' => $hash )
167 foreach ( $res as $row ) {
168 $result[] = $this->newFileFromRow( $row );
176 * Get a connection to the slave DB
178 function getSlaveDB() {
179 return wfGetDB( DB_SLAVE
);
183 * Get a connection to the master DB
185 function getMasterDB() {
186 return wfGetDB( DB_MASTER
);
190 * Get a key on the primary cache for this repository.
191 * Returns false if the repository's cache is not accessible at this site.
192 * The parameters are the parts of the key, as for wfMemcKey().
194 function getSharedCacheKey( /*...*/ ) {
195 $args = func_get_args();
196 return call_user_func_array( 'wfMemcKey', $args );
200 * Invalidates image redirect cache related to that image
202 * @param $title Title of page
204 function invalidateImageRedirect( $title ) {
206 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
208 $wgMemc->delete( $memcKey );