Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / filerepo / LocalRepo.php
blob549be40807cbc14bcf96afc447a6c79bc5a35129
1 <?php
2 /**
3 * Local repository that stores files in the local filesystem and registers them
4 * in the wiki's own database.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup FileRepo
25 /**
26 * A repository that stores files in the local filesystem and registers them
27 * in the wiki's own database. This is the most commonly used repository class.
29 * @ingroup FileRepo
31 class LocalRepo extends FileRepo {
32 var $fileFactory = array( 'LocalFile' , 'newFromTitle' );
33 var $fileFactoryKey = array( 'LocalFile' , 'newFromKey' );
34 var $fileFromRowFactory = array( 'LocalFile' , 'newFromRow' );
35 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
36 var $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
37 var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
39 /**
40 * @throws MWException
41 * @param $row
42 * @return LocalFile
44 function newFileFromRow( $row ) {
45 if ( isset( $row->img_name ) ) {
46 return call_user_func( $this->fileFromRowFactory, $row, $this );
47 } elseif ( isset( $row->oi_name ) ) {
48 return call_user_func( $this->oldFileFromRowFactory, $row, $this );
49 } else {
50 throw new MWException( __METHOD__ . ': invalid row' );
54 /**
55 * @param $title
56 * @param $archiveName
57 * @return OldLocalFile
59 function newFromArchiveName( $title, $archiveName ) {
60 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
63 /**
64 * Delete files in the deleted directory if they are not referenced in the
65 * filearchive table. This needs to be done in the repo because it needs to
66 * interleave database locks with file operations, which is potentially a
67 * remote operation.
69 * @param $storageKeys array
71 * @return FileRepoStatus
73 function cleanupDeletedBatch( array $storageKeys ) {
74 $backend = $this->backend; // convenience
75 $root = $this->getZonePath( 'deleted' );
76 $dbw = $this->getMasterDB();
77 $status = $this->newGood();
78 $storageKeys = array_unique( $storageKeys );
79 foreach ( $storageKeys as $key ) {
80 $hashPath = $this->getDeletedHashPath( $key );
81 $path = "$root/$hashPath$key";
82 $dbw->begin( __METHOD__ );
83 // Check for usage in deleted/hidden files and pre-emptively
84 // lock the key to avoid any future use until we are finished.
85 $deleted = $this->deletedFileHasKey( $key, 'lock' );
86 $hidden = $this->hiddenFileHasKey( $key, 'lock' );
87 if ( !$deleted && !$hidden ) { // not in use now
88 wfDebug( __METHOD__ . ": deleting $key\n" );
89 $op = array( 'op' => 'delete', 'src' => $path );
90 if ( !$backend->doOperation( $op )->isOK() ) {
91 $status->error( 'undelete-cleanup-error', $path );
92 $status->failCount++;
94 } else {
95 wfDebug( __METHOD__ . ": $key still in use\n" );
96 $status->successCount++;
98 $dbw->commit( __METHOD__ );
100 return $status;
104 * Check if a deleted (filearchive) file has this sha1 key
106 * @param string $key File storage key (base-36 sha1 key with file extension)
107 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
108 * @return bool File with this key is in use
110 protected function deletedFileHasKey( $key, $lock = null ) {
111 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
113 $dbw = $this->getMasterDB();
114 return (bool)$dbw->selectField( 'filearchive', '1',
115 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
116 __METHOD__, $options
121 * Check if a hidden (revision delete) file has this sha1 key
123 * @param string $key File storage key (base-36 sha1 key with file extension)
124 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
125 * @return bool File with this key is in use
127 protected function hiddenFileHasKey( $key, $lock = null ) {
128 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
130 $sha1 = self::getHashFromKey( $key );
131 $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
133 $dbw = $this->getMasterDB();
134 return (bool)$dbw->selectField( 'oldimage', '1',
135 array( 'oi_sha1' => $sha1,
136 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
137 $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
138 __METHOD__, $options
143 * Gets the SHA1 hash from a storage key
145 * @param string $key
146 * @return string
148 public static function getHashFromKey( $key ) {
149 return strtok( $key, '.' );
153 * Checks if there is a redirect named as $title
155 * @param $title Title of file
156 * @return bool
158 function checkRedirect( Title $title ) {
159 global $wgMemc;
161 $title = File::normalizeTitle( $title, 'exception' );
163 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
164 if ( $memcKey === false ) {
165 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
166 $expiry = 300; // no invalidation, 5 minutes
167 } else {
168 $expiry = 86400; // has invalidation, 1 day
170 $cachedValue = $wgMemc->get( $memcKey );
171 if ( $cachedValue === ' ' || $cachedValue === '' ) {
172 // Does not exist
173 return false;
174 } elseif ( strval( $cachedValue ) !== '' ) {
175 return Title::newFromText( $cachedValue, NS_FILE );
176 } // else $cachedValue is false or null: cache miss
178 $id = $this->getArticleID( $title );
179 if ( !$id ) {
180 $wgMemc->set( $memcKey, " ", $expiry );
181 return false;
183 $dbr = $this->getSlaveDB();
184 $row = $dbr->selectRow(
185 'redirect',
186 array( 'rd_title', 'rd_namespace' ),
187 array( 'rd_from' => $id ),
188 __METHOD__
191 if ( $row && $row->rd_namespace == NS_FILE ) {
192 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
193 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
194 return $targetTitle;
195 } else {
196 $wgMemc->set( $memcKey, '', $expiry );
197 return false;
202 * Function link Title::getArticleID().
203 * We can't say Title object, what database it should use, so we duplicate that function here.
205 * @param $title Title
206 * @return bool|int|mixed
208 protected function getArticleID( $title ) {
209 if ( !$title instanceof Title ) {
210 return 0;
212 $dbr = $this->getSlaveDB();
213 $id = $dbr->selectField(
214 'page', // Table
215 'page_id', //Field
216 array( //Conditions
217 'page_namespace' => $title->getNamespace(),
218 'page_title' => $title->getDBkey(),
220 __METHOD__ //Function name
222 return $id;
226 * Get an array or iterator of file objects for files that have a given
227 * SHA-1 content hash.
229 * @param string $hash a sha1 hash to look for
230 * @return Array
232 function findBySha1( $hash ) {
233 $dbr = $this->getSlaveDB();
234 $res = $dbr->select(
235 'image',
236 LocalFile::selectFields(),
237 array( 'img_sha1' => $hash ),
238 __METHOD__,
239 array( 'ORDER BY' => 'img_name' )
242 $result = array();
243 foreach ( $res as $row ) {
244 $result[] = $this->newFileFromRow( $row );
246 $res->free();
248 return $result;
252 * Get an array of arrays or iterators of file objects for files that
253 * have the given SHA-1 content hashes.
255 * Overrides generic implementation in FileRepo for performance reason
257 * @param array $hashes An array of hashes
258 * @return array An Array of arrays or iterators of file objects and the hash as key
260 function findBySha1s( array $hashes ) {
261 if ( !count( $hashes ) ) {
262 return array(); //empty parameter
265 $dbr = $this->getSlaveDB();
266 $res = $dbr->select(
267 'image',
268 LocalFile::selectFields(),
269 array( 'img_sha1' => $hashes ),
270 __METHOD__,
271 array( 'ORDER BY' => 'img_name' )
274 $result = array();
275 foreach ( $res as $row ) {
276 $file = $this->newFileFromRow( $row );
277 $result[$file->getSha1()][] = $file;
279 $res->free();
281 return $result;
285 * Return an array of files where the name starts with $prefix.
287 * @param string $prefix The prefix to search for
288 * @param int $limit The maximum amount of files to return
289 * @return array
291 public function findFilesByPrefix( $prefix, $limit ) {
292 $selectOptions = array( 'ORDER BY' => 'img_name', 'LIMIT' => intval( $limit ) );
294 // Query database
295 $dbr = $this->getSlaveDB();
296 $res = $dbr->select(
297 'image',
298 LocalFile::selectFields(),
299 'img_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ),
300 __METHOD__,
301 $selectOptions
304 // Build file objects
305 $files = array();
306 foreach ( $res as $row ) {
307 $files[] = $this->newFileFromRow( $row );
309 return $files;
313 * Get a connection to the slave DB
314 * @return DatabaseBase
316 function getSlaveDB() {
317 return wfGetDB( DB_SLAVE );
321 * Get a connection to the master DB
322 * @return DatabaseBase
324 function getMasterDB() {
325 return wfGetDB( DB_MASTER );
329 * Get a key on the primary cache for this repository.
330 * Returns false if the repository's cache is not accessible at this site.
331 * The parameters are the parts of the key, as for wfMemcKey().
333 * @return string
335 function getSharedCacheKey( /*...*/ ) {
336 $args = func_get_args();
337 return call_user_func_array( 'wfMemcKey', $args );
341 * Invalidates image redirect cache related to that image
343 * @param $title Title of page
344 * @return void
346 function invalidateImageRedirect( Title $title ) {
347 global $wgMemc;
348 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
349 if ( $memcKey ) {
350 $wgMemc->delete( $memcKey );