Merge "filebackend: cleaned up the FileBackend constructor"
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
1 <?php
2 /**
3 * Local repository that stores files in the local filesystem and registers them
4 * in the wiki's own database.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup FileRepo
23 */
24
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.
28 *
29 * @ingroup FileRepo
30 */
31 class LocalRepo extends FileRepo {
32 protected $fileFactory = array( 'LocalFile', 'newFromTitle' );
33 protected $fileFactoryKey = array( 'LocalFile', 'newFromKey' );
34 protected $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
35 protected $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
36 protected $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
37 protected $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
38
39 /**
40 * @throws MWException
41 * @param $row
42 * @return LocalFile
43 */
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' );
51 }
52 }
53
54 /**
55 * @param $title
56 * @param $archiveName
57 * @return OldLocalFile
58 */
59 function newFromArchiveName( $title, $archiveName ) {
60 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
61 }
62
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.
68 *
69 * @param $storageKeys array
70 *
71 * @return FileRepoStatus
72 */
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++;
93 }
94 } else {
95 wfDebug( __METHOD__ . ": $key still in use\n" );
96 $status->successCount++;
97 }
98 $dbw->commit( __METHOD__ );
99 }
100
101 return $status;
102 }
103
104 /**
105 * Check if a deleted (filearchive) file has this sha1 key
106 *
107 * @param string $key File storage key (base-36 sha1 key with file extension)
108 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
109 * @return bool File with this key is in use
110 */
111 protected function deletedFileHasKey( $key, $lock = null ) {
112 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
113
114 $dbw = $this->getMasterDB();
115
116 return (bool)$dbw->selectField( 'filearchive', '1',
117 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
118 __METHOD__, $options
119 );
120 }
121
122 /**
123 * Check if a hidden (revision delete) file has this sha1 key
124 *
125 * @param string $key File storage key (base-36 sha1 key with file extension)
126 * @param string|null $lock Use "lock" to lock the row via FOR UPDATE
127 * @return bool File with this key is in use
128 */
129 protected function hiddenFileHasKey( $key, $lock = null ) {
130 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
131
132 $sha1 = self::getHashFromKey( $key );
133 $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
134
135 $dbw = $this->getMasterDB();
136
137 return (bool)$dbw->selectField( 'oldimage', '1',
138 array( 'oi_sha1' => $sha1,
139 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
140 $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
141 __METHOD__, $options
142 );
143 }
144
145 /**
146 * Gets the SHA1 hash from a storage key
147 *
148 * @param string $key
149 * @return string
150 */
151 public static function getHashFromKey( $key ) {
152 return strtok( $key, '.' );
153 }
154
155 /**
156 * Checks if there is a redirect named as $title
157 *
158 * @param $title Title of file
159 * @return bool
160 */
161 function checkRedirect( Title $title ) {
162 global $wgMemc;
163
164 $title = File::normalizeTitle( $title, 'exception' );
165
166 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
167 if ( $memcKey === false ) {
168 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
169 $expiry = 300; // no invalidation, 5 minutes
170 } else {
171 $expiry = 86400; // has invalidation, 1 day
172 }
173 $cachedValue = $wgMemc->get( $memcKey );
174 if ( $cachedValue === ' ' || $cachedValue === '' ) {
175 // Does not exist
176 return false;
177 } elseif ( strval( $cachedValue ) !== '' && $cachedValue !== ' PURGED' ) {
178 return Title::newFromText( $cachedValue, NS_FILE );
179 } // else $cachedValue is false or null: cache miss
180
181 $id = $this->getArticleID( $title );
182 if ( !$id ) {
183 $wgMemc->add( $memcKey, " ", $expiry );
184
185 return false;
186 }
187 $dbr = $this->getSlaveDB();
188 $row = $dbr->selectRow(
189 'redirect',
190 array( 'rd_title', 'rd_namespace' ),
191 array( 'rd_from' => $id ),
192 __METHOD__
193 );
194
195 if ( $row && $row->rd_namespace == NS_FILE ) {
196 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
197 $wgMemc->add( $memcKey, $targetTitle->getDBkey(), $expiry );
198
199 return $targetTitle;
200 } else {
201 $wgMemc->add( $memcKey, '', $expiry );
202
203 return false;
204 }
205 }
206
207 /**
208 * Function link Title::getArticleID().
209 * We can't say Title object, what database it should use, so we duplicate that function here.
210 *
211 * @param $title Title
212 * @return bool|int|mixed
213 */
214 protected function getArticleID( $title ) {
215 if ( !$title instanceof Title ) {
216 return 0;
217 }
218 $dbr = $this->getSlaveDB();
219 $id = $dbr->selectField(
220 'page', // Table
221 'page_id', //Field
222 array( //Conditions
223 'page_namespace' => $title->getNamespace(),
224 'page_title' => $title->getDBkey(),
225 ),
226 __METHOD__ //Function name
227 );
228
229 return $id;
230 }
231
232 /**
233 * Get an array or iterator of file objects for files that have a given
234 * SHA-1 content hash.
235 *
236 * @param string $hash a sha1 hash to look for
237 * @return Array
238 */
239 function findBySha1( $hash ) {
240 $dbr = $this->getSlaveDB();
241 $res = $dbr->select(
242 'image',
243 LocalFile::selectFields(),
244 array( 'img_sha1' => $hash ),
245 __METHOD__,
246 array( 'ORDER BY' => 'img_name' )
247 );
248
249 $result = array();
250 foreach ( $res as $row ) {
251 $result[] = $this->newFileFromRow( $row );
252 }
253 $res->free();
254
255 return $result;
256 }
257
258 /**
259 * Get an array of arrays or iterators of file objects for files that
260 * have the given SHA-1 content hashes.
261 *
262 * Overrides generic implementation in FileRepo for performance reason
263 *
264 * @param array $hashes An array of hashes
265 * @return array An Array of arrays or iterators of file objects and the hash as key
266 */
267 function findBySha1s( array $hashes ) {
268 if ( !count( $hashes ) ) {
269 return array(); //empty parameter
270 }
271
272 $dbr = $this->getSlaveDB();
273 $res = $dbr->select(
274 'image',
275 LocalFile::selectFields(),
276 array( 'img_sha1' => $hashes ),
277 __METHOD__,
278 array( 'ORDER BY' => 'img_name' )
279 );
280
281 $result = array();
282 foreach ( $res as $row ) {
283 $file = $this->newFileFromRow( $row );
284 $result[$file->getSha1()][] = $file;
285 }
286 $res->free();
287
288 return $result;
289 }
290
291 /**
292 * Return an array of files where the name starts with $prefix.
293 *
294 * @param string $prefix The prefix to search for
295 * @param int $limit The maximum amount of files to return
296 * @return array
297 */
298 public function findFilesByPrefix( $prefix, $limit ) {
299 $selectOptions = array( 'ORDER BY' => 'img_name', 'LIMIT' => intval( $limit ) );
300
301 // Query database
302 $dbr = $this->getSlaveDB();
303 $res = $dbr->select(
304 'image',
305 LocalFile::selectFields(),
306 'img_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ),
307 __METHOD__,
308 $selectOptions
309 );
310
311 // Build file objects
312 $files = array();
313 foreach ( $res as $row ) {
314 $files[] = $this->newFileFromRow( $row );
315 }
316
317 return $files;
318 }
319
320 /**
321 * Get a connection to the slave DB
322 * @return DatabaseBase
323 */
324 function getSlaveDB() {
325 return wfGetDB( DB_SLAVE );
326 }
327
328 /**
329 * Get a connection to the master DB
330 * @return DatabaseBase
331 */
332 function getMasterDB() {
333 return wfGetDB( DB_MASTER );
334 }
335
336 /**
337 * Get a key on the primary cache for this repository.
338 * Returns false if the repository's cache is not accessible at this site.
339 * The parameters are the parts of the key, as for wfMemcKey().
340 *
341 * @return string
342 */
343 function getSharedCacheKey( /*...*/ ) {
344 $args = func_get_args();
345
346 return call_user_func_array( 'wfMemcKey', $args );
347 }
348
349 /**
350 * Invalidates image redirect cache related to that image
351 *
352 * @param $title Title of page
353 * @return void
354 */
355 function invalidateImageRedirect( Title $title ) {
356 global $wgMemc;
357 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
358 if ( $memcKey ) {
359 // Set a temporary value for the cache key, to ensure
360 // that this value stays purged long enough so that
361 // it isn't refreshed with a stale value due to a
362 // lagged slave.
363 $wgMemc->set( $memcKey, ' PURGED', 12 );
364 }
365 }
366 }