Revert r101492, broken, see CR. Also revert followup r101496.
[mediawiki.git] / includes / filerepo / LocalRepo.php
blobe29bca0fc7458b54b7e98f24a51385bafe4c6b04
1 <?php
2 /**
3 * Local repository that stores files in the local filesystem and registers them
4 * in the wiki's own database.
6 * @file
7 * @ingroup FileRepo
8 */
10 /**
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.
13 * @ingroup FileRepo
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 /**
24 * @throws MWException
25 * @param $row
26 * @return File
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 );
33 } else {
34 throw new MWException( __METHOD__.': invalid row' );
38 /**
39 * @param $title
40 * @param $archiveName
41 * @return OldLocalFile
43 function newFromArchiveName( $title, $archiveName ) {
44 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
47 /**
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
51 * remote operation.
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";
65 $dbw->begin();
66 // Check for usage in deleted/hidden files and pre-emptively
67 // lock the key to avoid any future use until we are finished.
68 $deleted = $this->deletedFileHasKey( $key, 'lock' );
69 $hidden = $this->hiddenFileHasKey( $key, 'lock' );
70 if ( !$deleted && !$hidden ) { // not in use now
71 wfDebug( __METHOD__ . ": deleting $key\n" );
72 wfSuppressWarnings();
73 $unlink = unlink( $path );
74 wfRestoreWarnings();
75 if ( !$unlink ) {
76 $status->error( 'undelete-cleanup-error', $path );
77 $status->failCount++;
79 } else {
80 wfDebug( __METHOD__ . ": $key still in use\n" );
81 $status->successCount++;
83 $dbw->commit();
85 return $status;
88 /**
89 * Check if a deleted (filearchive) file has this sha1 key
90 * @param $key String File storage key (base-36 sha1 key with file extension)
91 * @param $lock String|null Use "lock" to lock the row via FOR UPDATE
92 * @return bool File with this key is in use
94 protected function deletedFileHasKey( $key, $lock = null ) {
95 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
97 $dbw = $this->getMasterDB();
98 return (bool)$dbw->selectField( 'filearchive', '1',
99 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
100 __METHOD__, $options
105 * Check if a hidden (revision delete) file has this sha1 key
106 * @param $key String File storage key (base-36 sha1 key with file extension)
107 * @param $lock String|null Use "lock" to lock the row via FOR UPDATE
108 * @return bool File with this key is in use
110 protected function hiddenFileHasKey( $key, $lock = null ) {
111 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
113 $sha1 = self::getHashFromKey( $key );
114 $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
116 $dbw = $this->getMasterDB();
117 return (bool)$dbw->selectField( 'oldimage', '1',
118 array( 'oi_sha1' => $sha1,
119 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
120 $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
121 __METHOD__, array( 'FOR UPDATE' )
126 * Gets the SHA1 hash from a storage key
128 * @param string $key
129 * @return string
131 public static function getHashFromKey( $key ) {
132 return strtok( $key, '.' );
136 * Checks if there is a redirect named as $title
138 * @param $title Title of file
140 function checkRedirect( $title ) {
141 global $wgMemc;
143 if( is_string( $title ) ) {
144 $title = Title::newFromText( $title );
146 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
147 $title = Title::makeTitle( NS_FILE, $title->getText() );
150 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
151 if ( $memcKey === false ) {
152 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
153 $expiry = 300; // no invalidation, 5 minutes
154 } else {
155 $expiry = 86400; // has invalidation, 1 day
157 $cachedValue = $wgMemc->get( $memcKey );
158 if ( $cachedValue === ' ' || $cachedValue === '' ) {
159 // Does not exist
160 return false;
161 } elseif ( strval( $cachedValue ) !== '' ) {
162 return Title::newFromText( $cachedValue, NS_FILE );
163 } // else $cachedValue is false or null: cache miss
165 $id = $this->getArticleID( $title );
166 if( !$id ) {
167 $wgMemc->set( $memcKey, " ", $expiry );
168 return false;
170 $dbr = $this->getSlaveDB();
171 $row = $dbr->selectRow(
172 'redirect',
173 array( 'rd_title', 'rd_namespace' ),
174 array( 'rd_from' => $id ),
175 __METHOD__
178 if( $row && $row->rd_namespace == NS_FILE ) {
179 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
180 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
181 return $targetTitle;
182 } else {
183 $wgMemc->set( $memcKey, '', $expiry );
184 return false;
190 * Function link Title::getArticleID().
191 * We can't say Title object, what database it should use, so we duplicate that function here.
192 * @param $title Title
194 protected function getArticleID( $title ) {
195 if( !$title instanceof Title ) {
196 return 0;
198 $dbr = $this->getSlaveDB();
199 $id = $dbr->selectField(
200 'page', // Table
201 'page_id', //Field
202 array( //Conditions
203 'page_namespace' => $title->getNamespace(),
204 'page_title' => $title->getDBkey(),
206 __METHOD__ //Function name
208 return $id;
212 * Get an array or iterator of file objects for files that have a given
213 * SHA-1 content hash.
215 function findBySha1( $hash ) {
216 $dbr = $this->getSlaveDB();
217 $res = $dbr->select(
218 'image',
219 LocalFile::selectFields(),
220 array( 'img_sha1' => $hash )
223 $result = array();
224 foreach ( $res as $row ) {
225 $result[] = $this->newFileFromRow( $row );
227 $res->free();
229 return $result;
233 * Get a connection to the slave DB
235 function getSlaveDB() {
236 return wfGetDB( DB_SLAVE );
240 * Get a connection to the master DB
242 function getMasterDB() {
243 return wfGetDB( DB_MASTER );
247 * Get a key on the primary cache for this repository.
248 * Returns false if the repository's cache is not accessible at this site.
249 * The parameters are the parts of the key, as for wfMemcKey().
251 function getSharedCacheKey( /*...*/ ) {
252 $args = func_get_args();
253 return call_user_func_array( 'wfMemcKey', $args );
257 * Invalidates image redirect cache related to that image
259 * @param $title Title of page
261 function invalidateImageRedirect( $title ) {
262 global $wgMemc;
263 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
264 if ( $memcKey ) {
265 $wgMemc->delete( $memcKey );