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' );
23 function newFileFromRow( $row ) {
24 if ( isset( $row->img_name ) ) {
25 return call_user_func( $this->fileFromRowFactory, $row, $this );
26 } elseif ( isset( $row->oi_name ) ) {
27 return call_user_func( $this->oldFileFromRowFactory, $row, $this );
29 throw new MWException( __METHOD__.': invalid row' );
33 function newFromArchiveName( $title, $archiveName ) {
34 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
38 * Delete files in the deleted directory if they are not referenced in the
39 * filearchive table. This needs to be done in the repo because it needs to
40 * interleave database locks with file operations, which is potentially a
42 * @return FileRepoStatus
44 function cleanupDeletedBatch( $storageKeys ) {
45 $root = $this->getZonePath( 'deleted' );
46 $dbw = $this->getMasterDB();
47 $status = $this->newGood();
48 $storageKeys = array_unique($storageKeys);
49 foreach ( $storageKeys as $key ) {
50 $hashPath = $this->getDeletedHashPath( $key );
51 $path = "$root/$hashPath$key";
53 $inuse = $dbw->selectField( 'filearchive', '1',
54 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
55 __METHOD__, array( 'FOR UPDATE' ) );
57 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
58 $ext = substr( $key, strcspn($key,'.') + 1 );
59 $ext = File::normalizeExtension($ext);
60 $inuse = $dbw->selectField( 'oldimage', '1',
61 array( 'oi_sha1' => $sha1,
62 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
63 $dbw->bitAnd('oi_deleted', File::DELETED_FILE) => File::DELETED_FILE ),
64 __METHOD__, array( 'FOR UPDATE' ) );
67 wfDebug( __METHOD__ . ": deleting $key\n" );
68 if ( !@unlink( $path ) ) {
69 $status->error( 'undelete-cleanup-error', $path );
73 wfDebug( __METHOD__ . ": $key still in use\n" );
74 $status->successCount++;
82 * Checks if there is a redirect named as $title
84 * @param $title Title of file
86 function checkRedirect( $title ) {
89 if( is_string( $title ) ) {
90 $title = Title::newFromTitle( $title );
92 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
93 $title = Title::makeTitle( NS_FILE, $title->getText() );
96 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
97 if ( $memcKey === false ) {
98 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
99 $expiry = 300; // no invalidation, 5 minutes
101 $expiry = 86400; // has invalidation, 1 day
103 $cachedValue = $wgMemc->get( $memcKey );
104 if ( $cachedValue === ' ' || $cachedValue === '' ) {
107 } elseif ( strval( $cachedValue ) !== '' ) {
108 return Title::newFromText( $cachedValue, NS_FILE );
109 } // else $cachedValue is false or null: cache miss
111 $id = $this->getArticleID( $title );
113 $wgMemc->set( $memcKey, " ", $expiry );
116 $dbr = $this->getSlaveDB();
117 $row = $dbr->selectRow(
119 array( 'rd_title', 'rd_namespace' ),
120 array( 'rd_from' => $id ),
124 if( $row && $row->rd_namespace == NS_FILE ) {
125 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
126 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
129 $wgMemc->set( $memcKey, '', $expiry );
136 * Function link Title::getArticleID().
137 * We can't say Title object, what database it should use, so we duplicate that function here.
139 protected function getArticleID( $title ) {
140 if( !$title instanceof Title ) {
143 $dbr = $this->getSlaveDB();
144 $id = $dbr->selectField(
148 'page_namespace' => $title->getNamespace(),
149 'page_title' => $title->getDBkey(),
151 __METHOD__ //Function name
157 * Get an array or iterator of file objects for files that have a given
158 * SHA-1 content hash.
160 function findBySha1( $hash ) {
161 $dbr = $this->getSlaveDB();
164 LocalFile::selectFields(),
165 array( 'img_sha1' => $hash )
169 foreach ( $res as $row ) {
170 $result[] = $this->newFileFromRow( $row );
178 * Get a connection to the slave DB
180 function getSlaveDB() {
181 return wfGetDB( DB_SLAVE );
185 * Get a connection to the master DB
187 function getMasterDB() {
188 return wfGetDB( DB_MASTER );
192 * Get a key on the primary cache for this repository.
193 * Returns false if the repository's cache is not accessible at this site.
194 * The parameters are the parts of the key, as for wfMemcKey().
196 function getSharedCacheKey( /*...*/ ) {
197 $args = func_get_args();
198 return call_user_func_array( 'wfMemcKey', $args );
202 * Invalidates image redirect cache related to that image
204 * @param $title Title of page
206 function invalidateImageRedirect( $title ) {
208 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
210 $wgMemc->delete( $memcKey );