Followup r63914, function must match parent
[mediawiki.git] / includes / filerepo / FSRepo.php
blob0dd9d0f74e6803977f03c391b6f3abec273e1735
1 <?php
3 /**
4 * A repository for files accessible via the local filesystem. Does not support
5 * database access or registration.
6 * @ingroup FileRepo
7 */
8 class FSRepo extends FileRepo {
9 var $directory, $deletedDir, $deletedHashLevels, $fileMode;
10 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
11 var $oldFileFactory = false;
12 var $pathDisclosureProtection = 'simple';
14 function __construct( $info ) {
15 parent::__construct( $info );
17 // Required settings
18 $this->directory = $info['directory'];
19 $this->url = $info['url'];
21 // Optional settings
22 $this->hashLevels = isset( $info['hashLevels'] ) ? $info['hashLevels'] : 2;
23 $this->deletedHashLevels = isset( $info['deletedHashLevels'] ) ?
24 $info['deletedHashLevels'] : $this->hashLevels;
25 $this->deletedDir = isset( $info['deletedDir'] ) ? $info['deletedDir'] : false;
26 $this->fileMode = isset( $info['fileMode'] ) ? $info['fileMode'] : 0644;
27 if ( isset( $info['thumbDir'] ) ) {
28 $this->thumbDir = $info['thumbDir'];
29 } else {
30 $this->thumbDir = "{$this->directory}/thumb";
32 if ( isset( $info['thumbUrl'] ) ) {
33 $this->thumbUrl = $info['thumbUrl'];
34 } else {
35 $this->thumbUrl = "{$this->url}/thumb";
39 /**
40 * Get the public root directory of the repository.
42 function getRootDirectory() {
43 return $this->directory;
46 /**
47 * Get the public root URL of the repository
49 function getRootUrl() {
50 return $this->url;
53 /**
54 * Returns true if the repository uses a multi-level directory structure
56 function isHashed() {
57 return (bool)$this->hashLevels;
60 /**
61 * Get the local directory corresponding to one of the three basic zones
63 function getZonePath( $zone ) {
64 switch ( $zone ) {
65 case 'public':
66 return $this->directory;
67 case 'temp':
68 return "{$this->directory}/temp";
69 case 'deleted':
70 return $this->deletedDir;
71 case 'thumb':
72 return $this->thumbDir;
73 default:
74 return false;
78 /**
79 * @see FileRepo::getZoneUrl()
81 function getZoneUrl( $zone ) {
82 switch ( $zone ) {
83 case 'public':
84 return $this->url;
85 case 'temp':
86 return "{$this->url}/temp";
87 case 'deleted':
88 return parent::getZoneUrl( $zone ); // no public URL
89 case 'thumb':
90 return $this->thumbUrl;
91 default:
92 return parent::getZoneUrl( $zone );
96 /**
97 * Get a URL referring to this repository, with the private mwrepo protocol.
98 * The suffix, if supplied, is considered to be unencoded, and will be
99 * URL-encoded before being returned.
101 function getVirtualUrl( $suffix = false ) {
102 $path = 'mwrepo://' . $this->name;
103 if ( $suffix !== false ) {
104 $path .= '/' . rawurlencode( $suffix );
106 return $path;
110 * Get the local path corresponding to a virtual URL
112 function resolveVirtualUrl( $url ) {
113 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
114 throw new MWException( __METHOD__.': unknown protoocl' );
117 $bits = explode( '/', substr( $url, 9 ), 3 );
118 if ( count( $bits ) != 3 ) {
119 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
121 list( $repo, $zone, $rel ) = $bits;
122 if ( $repo !== $this->name ) {
123 throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
125 $base = $this->getZonePath( $zone );
126 if ( !$base ) {
127 throw new MWException( __METHOD__.": invalid zone: $zone" );
129 return $base . '/' . rawurldecode( $rel );
133 * Store a batch of files
135 * @param array $triplets (src,zone,dest) triplets as per store()
136 * @param integer $flags Bitwise combination of the following flags:
137 * self::DELETE_SOURCE Delete the source file after upload
138 * self::OVERWRITE Overwrite an existing destination file instead of failing
139 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
140 * same contents as the source
142 function storeBatch( $triplets, $flags = 0 ) {
143 if ( !wfMkdirParents( $this->directory ) ) {
144 return $this->newFatal( 'upload_directory_missing', $this->directory );
146 if ( !is_writable( $this->directory ) ) {
147 return $this->newFatal( 'upload_directory_read_only', $this->directory );
149 $status = $this->newGood();
150 foreach ( $triplets as $i => $triplet ) {
151 list( $srcPath, $dstZone, $dstRel ) = $triplet;
153 $root = $this->getZonePath( $dstZone );
154 if ( !$root ) {
155 throw new MWException( "Invalid zone: $dstZone" );
157 if ( !$this->validateFilename( $dstRel ) ) {
158 throw new MWException( 'Validation error in $dstRel' );
160 $dstPath = "$root/$dstRel";
161 $dstDir = dirname( $dstPath );
163 if ( !is_dir( $dstDir ) ) {
164 if ( !wfMkdirParents( $dstDir ) ) {
165 return $this->newFatal( 'directorycreateerror', $dstDir );
167 if ( $dstZone == 'deleted' ) {
168 $this->initDeletedDir( $dstDir );
172 if ( self::isVirtualUrl( $srcPath ) ) {
173 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
175 if ( !is_file( $srcPath ) ) {
176 // Make a list of files that don't exist for return to the caller
177 $status->fatal( 'filenotfound', $srcPath );
178 continue;
180 if ( !( $flags & self::OVERWRITE ) && file_exists( $dstPath ) ) {
181 if ( $flags & self::OVERWRITE_SAME ) {
182 $hashSource = sha1_file( $srcPath );
183 $hashDest = sha1_file( $dstPath );
184 if ( $hashSource != $hashDest ) {
185 $status->fatal( 'fileexistserror', $dstPath );
187 } else {
188 $status->fatal( 'fileexistserror', $dstPath );
193 $deleteDest = wfIsWindows() && ( $flags & self::OVERWRITE );
195 // Abort now on failure
196 if ( !$status->ok ) {
197 return $status;
200 foreach ( $triplets as $triplet ) {
201 list( $srcPath, $dstZone, $dstRel ) = $triplet;
202 $root = $this->getZonePath( $dstZone );
203 $dstPath = "$root/$dstRel";
204 $good = true;
206 if ( $flags & self::DELETE_SOURCE ) {
207 if ( $deleteDest ) {
208 unlink( $dstPath );
210 if ( !rename( $srcPath, $dstPath ) ) {
211 $status->error( 'filerenameerror', $srcPath, $dstPath );
212 $good = false;
214 } else {
215 if ( !copy( $srcPath, $dstPath ) ) {
216 $status->error( 'filecopyerror', $srcPath, $dstPath );
217 $good = false;
220 if ( $good ) {
221 $this->chmod( $dstPath );
222 $status->successCount++;
223 } else {
224 $status->failCount++;
227 return $status;
230 function append( $srcPath, $toAppendPath, $flags = 0 ) {
231 $status = $this->newGood();
233 // Resolve the virtual URL
234 if ( self::isVirtualUrl( $srcPath ) ) {
235 $srcPath = $this->resolveVirtualUrl( $srcPath );
237 // Make sure the files are there
238 if ( !is_file( $srcPath ) )
239 $status->fatal( 'filenotfound', $srcPath );
241 if ( !is_file( $toAppendPath ) )
242 $status->fatal( 'filenotfound', $toAppendPath );
244 if ( !$status->isOk() ) return $status;
246 // Do the append
247 $chunk = file_get_contents( $toAppendPath );
248 if( $chunk === false ) {
249 $status->fatal( 'fileappenderrorread', $toAppendPath );
252 if( $status->isOk() ) {
253 if ( file_put_contents( $srcPath, $chunk, FILE_APPEND ) ) {
254 $status->value = $srcPath;
255 } else {
256 $status->fatal( 'fileappenderror', $toAppendPath, $srcPath);
260 if ( $flags & self::DELETE_SOURCE ) {
261 unlink( $toAppendPath );
264 return $status;
268 * Checks existence of specified array of files.
270 * @param array $files URLs of files to check
271 * @param integer $flags Bitwise combination of the following flags:
272 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
273 * @return Either array of files and existence flags, or false
275 function fileExistsBatch( $files, $flags = 0 ) {
276 if ( !file_exists( $this->directory ) || !is_readable( $this->directory ) ) {
277 return false;
279 $result = array();
280 foreach ( $files as $key => $file ) {
281 if ( self::isVirtualUrl( $file ) ) {
282 $file = $this->resolveVirtualUrl( $file );
284 if( $flags & self::FILES_ONLY ) {
285 $result[$key] = is_file( $file );
286 } else {
287 $result[$key] = file_exists( $file );
291 return $result;
295 * Take all available measures to prevent web accessibility of new deleted
296 * directories, in case the user has not configured offline storage
298 protected function initDeletedDir( $dir ) {
299 // Add a .htaccess file to the root of the deleted zone
300 $root = $this->getZonePath( 'deleted' );
301 if ( !file_exists( "$root/.htaccess" ) ) {
302 file_put_contents( "$root/.htaccess", "Deny from all\n" );
304 // Seed new directories with a blank index.html, to prevent crawling
305 file_put_contents( "$dir/index.html", '' );
309 * Pick a random name in the temp zone and store a file to it.
310 * @param string $originalName The base name of the file as specified
311 * by the user. The file extension will be maintained.
312 * @param string $srcPath The current location of the file.
313 * @return FileRepoStatus object with the URL in the value.
315 function storeTemp( $originalName, $srcPath ) {
316 $date = gmdate( "YmdHis" );
317 $hashPath = $this->getHashPath( $originalName );
318 $dstRel = "$hashPath$date!$originalName";
319 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
321 $result = $this->store( $srcPath, 'temp', $dstRel );
322 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
323 return $result;
327 * Remove a temporary file or mark it for garbage collection
328 * @param string $virtualUrl The virtual URL returned by storeTemp
329 * @return boolean True on success, false on failure
331 function freeTemp( $virtualUrl ) {
332 $temp = "mwrepo://{$this->name}/temp";
333 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
334 wfDebug( __METHOD__.": Invalid virtual URL\n" );
335 return false;
337 $path = $this->resolveVirtualUrl( $virtualUrl );
338 wfSuppressWarnings();
339 $success = unlink( $path );
340 wfRestoreWarnings();
341 return $success;
345 * Publish a batch of files
346 * @param array $triplets (source,dest,archive) triplets as per publish()
347 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
348 * that the source files should be deleted if possible
350 function publishBatch( $triplets, $flags = 0 ) {
351 // Perform initial checks
352 if ( !wfMkdirParents( $this->directory ) ) {
353 return $this->newFatal( 'upload_directory_missing', $this->directory );
355 if ( !is_writable( $this->directory ) ) {
356 return $this->newFatal( 'upload_directory_read_only', $this->directory );
358 $status = $this->newGood( array() );
359 foreach ( $triplets as $i => $triplet ) {
360 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
362 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
363 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
365 if ( !$this->validateFilename( $dstRel ) ) {
366 throw new MWException( 'Validation error in $dstRel' );
368 if ( !$this->validateFilename( $archiveRel ) ) {
369 throw new MWException( 'Validation error in $archiveRel' );
371 $dstPath = "{$this->directory}/$dstRel";
372 $archivePath = "{$this->directory}/$archiveRel";
374 $dstDir = dirname( $dstPath );
375 $archiveDir = dirname( $archivePath );
376 // Abort immediately on directory creation errors since they're likely to be repetitive
377 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
378 return $this->newFatal( 'directorycreateerror', $dstDir );
380 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
381 return $this->newFatal( 'directorycreateerror', $archiveDir );
383 if ( !is_file( $srcPath ) ) {
384 // Make a list of files that don't exist for return to the caller
385 $status->fatal( 'filenotfound', $srcPath );
389 if ( !$status->ok ) {
390 return $status;
393 foreach ( $triplets as $i => $triplet ) {
394 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
395 $dstPath = "{$this->directory}/$dstRel";
396 $archivePath = "{$this->directory}/$archiveRel";
398 // Archive destination file if it exists
399 if( is_file( $dstPath ) ) {
400 // Check if the archive file exists
401 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
402 // unlinks the destination file if it exists. DB-based synchronisation in
403 // publishBatch's caller should prevent races. In Windows there's no
404 // problem because the rename primitive fails if the destination exists.
405 if ( is_file( $archivePath ) ) {
406 $success = false;
407 } else {
408 wfSuppressWarnings();
409 $success = rename( $dstPath, $archivePath );
410 wfRestoreWarnings();
413 if( !$success ) {
414 $status->error( 'filerenameerror',$dstPath, $archivePath );
415 $status->failCount++;
416 continue;
417 } else {
418 wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
420 $status->value[$i] = 'archived';
421 } else {
422 $status->value[$i] = 'new';
425 $good = true;
426 wfSuppressWarnings();
427 if ( $flags & self::DELETE_SOURCE ) {
428 if ( !rename( $srcPath, $dstPath ) ) {
429 $status->error( 'filerenameerror', $srcPath, $dstPath );
430 $good = false;
432 } else {
433 if ( !copy( $srcPath, $dstPath ) ) {
434 $status->error( 'filecopyerror', $srcPath, $dstPath );
435 $good = false;
438 wfRestoreWarnings();
440 if ( $good ) {
441 $status->successCount++;
442 wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
443 // Thread-safe override for umask
444 $this->chmod( $dstPath );
445 } else {
446 $status->failCount++;
449 return $status;
453 * Move a group of files to the deletion archive.
454 * If no valid deletion archive is configured, this may either delete the
455 * file or throw an exception, depending on the preference of the repository.
457 * @param array $sourceDestPairs Array of source/destination pairs. Each element
458 * is a two-element array containing the source file path relative to the
459 * public root in the first element, and the archive file path relative
460 * to the deleted zone root in the second element.
461 * @return FileRepoStatus
463 function deleteBatch( $sourceDestPairs ) {
464 $status = $this->newGood();
465 if ( !$this->deletedDir ) {
466 throw new MWException( __METHOD__.': no valid deletion archive directory' );
470 * Validate filenames and create archive directories
472 foreach ( $sourceDestPairs as $pair ) {
473 list( $srcRel, $archiveRel ) = $pair;
474 if ( !$this->validateFilename( $srcRel ) ) {
475 throw new MWException( __METHOD__.':Validation error in $srcRel' );
477 if ( !$this->validateFilename( $archiveRel ) ) {
478 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
480 $archivePath = "{$this->deletedDir}/$archiveRel";
481 $archiveDir = dirname( $archivePath );
482 if ( !is_dir( $archiveDir ) ) {
483 if ( !wfMkdirParents( $archiveDir ) ) {
484 $status->fatal( 'directorycreateerror', $archiveDir );
485 continue;
487 $this->initDeletedDir( $archiveDir );
489 // Check if the archive directory is writable
490 // This doesn't appear to work on NTFS
491 if ( !is_writable( $archiveDir ) ) {
492 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
495 if ( !$status->ok ) {
496 // Abort early
497 return $status;
501 * Move the files
502 * We're now committed to returning an OK result, which will lead to
503 * the files being moved in the DB also.
505 foreach ( $sourceDestPairs as $pair ) {
506 list( $srcRel, $archiveRel ) = $pair;
507 $srcPath = "{$this->directory}/$srcRel";
508 $archivePath = "{$this->deletedDir}/$archiveRel";
509 $good = true;
510 if ( file_exists( $archivePath ) ) {
511 # A file with this content hash is already archived
512 if ( !@unlink( $srcPath ) ) {
513 $status->error( 'filedeleteerror', $srcPath );
514 $good = false;
516 } else{
517 if ( !@rename( $srcPath, $archivePath ) ) {
518 $status->error( 'filerenameerror', $srcPath, $archivePath );
519 $good = false;
520 } else {
521 $this->chmod( $archivePath );
524 if ( $good ) {
525 $status->successCount++;
526 } else {
527 $status->failCount++;
530 return $status;
534 * Get a relative path for a deletion archive key,
535 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
537 function getDeletedHashPath( $key ) {
538 $path = '';
539 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
540 $path .= $key[$i] . '/';
542 return $path;
546 * Call a callback function for every file in the repository.
547 * Uses the filesystem even in child classes.
549 function enumFilesInFS( $callback ) {
550 $numDirs = 1 << ( $this->hashLevels * 4 );
551 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
552 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
553 $path = $this->directory;
554 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
555 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
557 if ( !file_exists( $path ) || !is_dir( $path ) ) {
558 continue;
560 $dir = opendir( $path );
561 while ( false !== ( $name = readdir( $dir ) ) ) {
562 call_user_func( $callback, $path . '/' . $name );
568 * Call a callback function for every file in the repository
569 * May use either the database or the filesystem
571 function enumFiles( $callback ) {
572 $this->enumFilesInFS( $callback );
576 * Get properties of a file with a given virtual URL
577 * The virtual URL must refer to this repo
579 function getFileProps( $virtualUrl ) {
580 $path = $this->resolveVirtualUrl( $virtualUrl );
581 return File::getPropsFromPath( $path );
585 * Path disclosure protection functions
587 * Get a callback function to use for cleaning error message parameters
589 function getErrorCleanupFunction() {
590 switch ( $this->pathDisclosureProtection ) {
591 case 'simple':
592 $callback = array( $this, 'simpleClean' );
593 break;
594 default:
595 $callback = parent::getErrorCleanupFunction();
597 return $callback;
600 function simpleClean( $param ) {
601 if ( !isset( $this->simpleCleanPairs ) ) {
602 global $IP;
603 $this->simpleCleanPairs = array(
604 $this->directory => 'public',
605 "{$this->directory}/temp" => 'temp',
606 $IP => '$IP',
607 dirname( __FILE__ ) => '$IP/extensions/WebStore',
609 if ( $this->deletedDir ) {
610 $this->simpleCleanPairs[$this->deletedDir] = 'deleted';
613 return strtr( $param, $this->simpleCleanPairs );
617 * Chmod a file, supressing the warnings.
618 * @param String $path The path to change
620 protected function chmod( $path ) {
621 wfSuppressWarnings();
622 chmod( $path, $this->fileMode );
623 wfRestoreWarnings();