Fix unused variables
[lhc/web/wiklou.git] / includes / filerepo / backend / FSFileBackend.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Class for a file system (FS) based file backend.
10 *
11 * All "containers" each map to a directory under the backend's base directory.
12 * For backwards-compatibility, some container paths can be set to custom paths.
13 * The wiki ID will not be used in any custom paths, so this should be avoided.
14 *
15 * Having directories with thousands of files will diminish performance.
16 * Sharding can be accomplished by using FileRepo-style hash paths.
17 *
18 * Status messages should avoid mentioning the internal FS paths.
19 * Likewise, error suppression should be used to avoid path disclosure.
20 *
21 * @ingroup FileBackend
22 * @since 1.19
23 */
24 class FSFileBackend extends FileBackend {
25 protected $basePath; // string; directory holding the container directories
26 /** @var Array Map of container names to root paths */
27 protected $containerPaths = array(); // for custom container paths
28 protected $fileMode; // integer; file permission mode
29
30 protected $hadWarningErrors = array();
31
32 /**
33 * @see FileBackend::__construct()
34 * Additional $config params include:
35 * basePath : File system directory that holds containers.
36 * containerPaths : Map of container names to custom file system directories.
37 * This should only be used for backwards-compatibility.
38 * fileMode : Octal UNIX file permissions to use on files stored.
39 */
40 public function __construct( array $config ) {
41 parent::__construct( $config );
42 if ( isset( $config['basePath'] ) ) {
43 if ( substr( $this->basePath, -1 ) === '/' ) {
44 $this->basePath = substr( $this->basePath, 0, -1 ); // remove trailing slash
45 }
46 } else {
47 $this->basePath = null; // none; containers must have explicit paths
48 }
49 $this->containerPaths = (array)$config['containerPaths'];
50 foreach ( $this->containerPaths as &$path ) {
51 if ( substr( $path, -1 ) === '/' ) {
52 $path = substr( $path, 0, -1 ); // remove trailing slash
53 }
54 }
55 $this->fileMode = isset( $config['fileMode'] )
56 ? $config['fileMode']
57 : 0644;
58 }
59
60 /**
61 * @see FileBackend::resolveContainerPath()
62 */
63 protected function resolveContainerPath( $container, $relStoragePath ) {
64 if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) {
65 return $relStoragePath; // container has a root directory
66 }
67 return null;
68 }
69
70 /**
71 * Given the short (unresolved) and full (resolved) name of
72 * a container, return the file system path of the container.
73 *
74 * @param $shortCont string
75 * @param $fullCont string
76 * @return string|null
77 */
78 protected function containerFSRoot( $shortCont, $fullCont ) {
79 if ( isset( $this->containerPaths[$shortCont] ) ) {
80 return $this->containerPaths[$shortCont];
81 } elseif ( isset( $this->basePath ) ) {
82 return "{$this->basePath}/{$fullCont}";
83 }
84 return null; // no container base path defined
85 }
86
87 /**
88 * Get the absolute file system path for a storage path
89 *
90 * @param $storagePath string Storage path
91 * @return string|null
92 */
93 protected function resolveToFSPath( $storagePath ) {
94 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
95 if ( $relPath === null ) {
96 return null; // invalid
97 }
98 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $storagePath );
99 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
100 if ( $relPath != '' ) {
101 $fsPath .= "/{$relPath}";
102 }
103 return $fsPath;
104 }
105
106 /**
107 * @see FileBackend::isPathUsableInternal()
108 */
109 public function isPathUsableInternal( $storagePath ) {
110 $fsPath = $this->resolveToFSPath( $storagePath );
111 if ( $fsPath === null ) {
112 return false; // invalid
113 }
114 $parentDir = dirname( $fsPath );
115
116 wfSuppressWarnings();
117 if ( file_exists( $fsPath ) ) {
118 $ok = is_file( $fsPath ) && is_writable( $fsPath );
119 } else {
120 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
121 }
122 wfRestoreWarnings();
123
124 return $ok;
125 }
126
127 /**
128 * @see FileBackend::doStoreInternal()
129 */
130 protected function doStoreInternal( array $params ) {
131 $status = Status::newGood();
132
133 $dest = $this->resolveToFSPath( $params['dst'] );
134 if ( $dest === null ) {
135 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
136 return $status;
137 }
138
139 if ( file_exists( $dest ) ) {
140 if ( !empty( $params['overwrite'] ) ) {
141 wfSuppressWarnings();
142 $ok = unlink( $dest );
143 wfRestoreWarnings();
144 if ( !$ok ) {
145 $status->fatal( 'backend-fail-delete', $params['dst'] );
146 return $status;
147 }
148 } else {
149 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
150 return $status;
151 }
152 }
153
154 wfSuppressWarnings();
155 $ok = copy( $params['src'], $dest );
156 wfRestoreWarnings();
157 if ( !$ok ) {
158 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
159 return $status;
160 }
161
162 $this->chmod( $dest );
163
164 return $status;
165 }
166
167 /**
168 * @see FileBackend::doCopyInternal()
169 */
170 protected function doCopyInternal( array $params ) {
171 $status = Status::newGood();
172
173 $source = $this->resolveToFSPath( $params['src'] );
174 if ( $source === null ) {
175 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
176 return $status;
177 }
178
179 $dest = $this->resolveToFSPath( $params['dst'] );
180 if ( $dest === null ) {
181 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
182 return $status;
183 }
184
185 if ( file_exists( $dest ) ) {
186 if ( !empty( $params['overwrite'] ) ) {
187 wfSuppressWarnings();
188 $ok = unlink( $dest );
189 wfRestoreWarnings();
190 if ( !$ok ) {
191 $status->fatal( 'backend-fail-delete', $params['dst'] );
192 return $status;
193 }
194 } else {
195 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
196 return $status;
197 }
198 }
199
200 wfSuppressWarnings();
201 $ok = copy( $source, $dest );
202 wfRestoreWarnings();
203 if ( !$ok ) {
204 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
205 return $status;
206 }
207
208 $this->chmod( $dest );
209
210 return $status;
211 }
212
213 /**
214 * @see FileBackend::doMoveInternal()
215 */
216 protected function doMoveInternal( array $params ) {
217 $status = Status::newGood();
218
219 $source = $this->resolveToFSPath( $params['src'] );
220 if ( $source === null ) {
221 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
222 return $status;
223 }
224
225 $dest = $this->resolveToFSPath( $params['dst'] );
226 if ( $dest === null ) {
227 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
228 return $status;
229 }
230
231 if ( file_exists( $dest ) ) {
232 if ( !empty( $params['overwrite'] ) ) {
233 // Windows does not support moving over existing files
234 if ( wfIsWindows() ) {
235 wfSuppressWarnings();
236 $ok = unlink( $dest );
237 wfRestoreWarnings();
238 if ( !$ok ) {
239 $status->fatal( 'backend-fail-delete', $params['dst'] );
240 return $status;
241 }
242 }
243 } else {
244 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
245 return $status;
246 }
247 }
248
249 wfSuppressWarnings();
250 $ok = rename( $source, $dest );
251 clearstatcache(); // file no longer at source
252 wfRestoreWarnings();
253 if ( !$ok ) {
254 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
255 return $status;
256 }
257
258 return $status;
259 }
260
261 /**
262 * @see FileBackend::doDeleteInternal()
263 */
264 protected function doDeleteInternal( array $params ) {
265 $status = Status::newGood();
266
267 $source = $this->resolveToFSPath( $params['src'] );
268 if ( $source === null ) {
269 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
270 return $status;
271 }
272
273 if ( !is_file( $source ) ) {
274 if ( empty( $params['ignoreMissingSource'] ) ) {
275 $status->fatal( 'backend-fail-delete', $params['src'] );
276 }
277 return $status; // do nothing; either OK or bad status
278 }
279
280 wfSuppressWarnings();
281 $ok = unlink( $source );
282 wfRestoreWarnings();
283 if ( !$ok ) {
284 $status->fatal( 'backend-fail-delete', $params['src'] );
285 return $status;
286 }
287
288 return $status;
289 }
290
291 /**
292 * @see FileBackend::doCreateInternal()
293 */
294 protected function doCreateInternal( array $params ) {
295 $status = Status::newGood();
296
297 $dest = $this->resolveToFSPath( $params['dst'] );
298 if ( $dest === null ) {
299 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
300 return $status;
301 }
302
303 if ( file_exists( $dest ) ) {
304 if ( !empty( $params['overwrite'] ) ) {
305 wfSuppressWarnings();
306 $ok = unlink( $dest );
307 wfRestoreWarnings();
308 if ( !$ok ) {
309 $status->fatal( 'backend-fail-delete', $params['dst'] );
310 return $status;
311 }
312 } else {
313 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
314 return $status;
315 }
316 }
317
318 wfSuppressWarnings();
319 $ok = file_put_contents( $dest, $params['content'] );
320 wfRestoreWarnings();
321 if ( !$ok ) {
322 $status->fatal( 'backend-fail-create', $params['dst'] );
323 return $status;
324 }
325
326 $this->chmod( $dest );
327
328 return $status;
329 }
330
331 /**
332 * @see FileBackend::doPrepareInternal()
333 */
334 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
335 $status = Status::newGood();
336 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
337 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
338 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
339 if ( !wfMkdirParents( $dir ) ) { // make directory and its parents
340 $status->fatal( 'directorycreateerror', $params['dir'] );
341 } elseif ( !is_writable( $dir ) ) {
342 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
343 } elseif ( !is_readable( $dir ) ) {
344 $status->fatal( 'directorynotreadableerror', $params['dir'] );
345 }
346 return $status;
347 }
348
349 /**
350 * @see FileBackend::doSecureInternal()
351 */
352 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
353 $status = Status::newGood();
354 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
355 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
356 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
357 // Seed new directories with a blank index.html, to prevent crawling...
358 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
359 wfSuppressWarnings();
360 $ok = file_put_contents( "{$dir}/index.html", '' );
361 wfRestoreWarnings();
362 if ( !$ok ) {
363 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
364 return $status;
365 }
366 }
367 // Add a .htaccess file to the root of the container...
368 if ( !empty( $params['noAccess'] ) ) {
369 if ( !file_exists( "{$contRoot}/.htaccess" ) ) {
370 wfSuppressWarnings();
371 $ok = file_put_contents( "{$contRoot}/.htaccess", "Deny from all\n" );
372 wfRestoreWarnings();
373 if ( !$ok ) {
374 $storeDir = "mwstore://{$this->name}/{$shortCont}";
375 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
376 return $status;
377 }
378 }
379 }
380 return $status;
381 }
382
383 /**
384 * @see FileBackend::doCleanInternal()
385 */
386 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
387 $status = Status::newGood();
388 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
389 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
390 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
391 wfSuppressWarnings();
392 if ( is_dir( $dir ) ) {
393 rmdir( $dir ); // remove directory if empty
394 }
395 wfRestoreWarnings();
396 return $status;
397 }
398
399 /**
400 * @see FileBackend::doFileExists()
401 */
402 protected function doGetFileStat( array $params ) {
403 $source = $this->resolveToFSPath( $params['src'] );
404 if ( $source === null ) {
405 return false; // invalid storage path
406 }
407
408 $this->trapWarnings();
409 $stat = is_file( $source ) ? stat( $source ) : false; // regular files only
410 $hadError = $this->untrapWarnings();
411
412 if ( $stat ) {
413 return array(
414 'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ),
415 'size' => $stat['size']
416 );
417 } elseif ( !$hadError ) {
418 return false; // file does not exist
419 } else {
420 return null; // failure
421 }
422 }
423
424 /**
425 * @see FileBackend::doClearCache()
426 */
427 protected function doClearCache( array $paths = null ) {
428 clearstatcache(); // clear the PHP file stat cache
429 }
430
431 /**
432 * @see FileBackend::getFileListInternal()
433 */
434 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
435 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
436 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
437 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
438 wfSuppressWarnings();
439 $exists = is_dir( $dir );
440 wfRestoreWarnings();
441 if ( !$exists ) {
442 return array(); // nothing under this dir
443 }
444 wfSuppressWarnings();
445 $readable = is_readable( $dir );
446 wfRestoreWarnings();
447 if ( !$readable ) {
448 return null; // bad permissions?
449 }
450 return new FSFileBackendFileList( $dir );
451 }
452
453 /**
454 * @see FileBackend::getLocalReference()
455 */
456 public function getLocalReference( array $params ) {
457 $source = $this->resolveToFSPath( $params['src'] );
458 if ( $source === null ) {
459 return null;
460 }
461 return new FSFile( $source );
462 }
463
464 /**
465 * @see FileBackend::getLocalCopy()
466 */
467 public function getLocalCopy( array $params ) {
468 $source = $this->resolveToFSPath( $params['src'] );
469 if ( $source === null ) {
470 return null;
471 }
472
473 // Create a new temporary file with the same extension...
474 $ext = FileBackend::extensionFromPath( $params['src'] );
475 $tmpFile = TempFSFile::factory( wfBaseName( $source ) . '_', $ext );
476 if ( !$tmpFile ) {
477 return null;
478 }
479 $tmpPath = $tmpFile->getPath();
480
481 // Copy the source file over the temp file
482 wfSuppressWarnings();
483 $ok = copy( $source, $tmpPath );
484 wfRestoreWarnings();
485 if ( !$ok ) {
486 return null;
487 }
488
489 $this->chmod( $tmpPath );
490
491 return $tmpFile;
492 }
493
494 /**
495 * Chmod a file, suppressing the warnings
496 *
497 * @param $path string Absolute file system path
498 * @return bool Success
499 */
500 protected function chmod( $path ) {
501 wfSuppressWarnings();
502 $ok = chmod( $path, $this->fileMode );
503 wfRestoreWarnings();
504
505 return $ok;
506 }
507
508 /**
509 * Suppress E_WARNING errors and track whether any happen
510 *
511 * @return void
512 */
513 protected function trapWarnings() {
514 $this->hadWarningErrors[] = false; // push to stack
515 set_error_handler( array( $this, 'handleWarning' ), E_WARNING );
516 }
517
518 /**
519 * Unsuppress E_WARNING errors and return true if any happened
520 *
521 * @return bool
522 */
523 protected function untrapWarnings() {
524 restore_error_handler(); // restore previous handler
525 return array_pop( $this->hadWarningErrors ); // pop from stack
526 }
527
528 private function handleWarning() {
529 $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true;
530 return true; // suppress from PHP handler
531 }
532 }
533
534 /**
535 * Wrapper around RecursiveDirectoryIterator that catches
536 * exception or does any custom behavoir that we may want.
537 * Do not use this class from places outside FSFileBackend.
538 *
539 * @ingroup FileBackend
540 */
541 class FSFileBackendFileList implements Iterator {
542 /** @var RecursiveIteratorIterator */
543 protected $iter;
544 protected $suffixStart; // integer
545 protected $pos = 0; // integer
546
547 /**
548 * @param $dir string file system directory
549 */
550 public function __construct( $dir ) {
551 $dir = realpath( $dir ); // normalize
552 $this->suffixStart = strlen( $dir ) + 1; // size of "path/to/dir/"
553 try {
554 # Get an iterator that will return leaf nodes (non-directories)
555 if ( MWInit::classExists( 'FilesystemIterator' ) ) { // PHP >= 5.3
556 # RecursiveDirectoryIterator extends FilesystemIterator.
557 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
558 $flags = FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS;
559 $this->iter = new RecursiveIteratorIterator(
560 new RecursiveDirectoryIterator( $dir, $flags ) );
561 } else { // PHP < 5.3
562 # RecursiveDirectoryIterator extends DirectoryIterator
563 $this->iter = new RecursiveIteratorIterator(
564 new RecursiveDirectoryIterator( $dir ) );
565 }
566 } catch ( UnexpectedValueException $e ) {
567 $this->iter = null; // bad permissions? deleted?
568 }
569 }
570
571 public function current() {
572 // Return only the relative path and normalize slashes to FileBackend-style
573 // Make sure to use the realpath since the suffix is based upon that
574 return str_replace( '\\', '/',
575 substr( realpath( $this->iter->current() ), $this->suffixStart ) );
576 }
577
578 public function key() {
579 return $this->pos;
580 }
581
582 public function next() {
583 try {
584 $this->iter->next();
585 } catch ( UnexpectedValueException $e ) {
586 $this->iter = null;
587 }
588 ++$this->pos;
589 }
590
591 public function rewind() {
592 $this->pos = 0;
593 try {
594 $this->iter->rewind();
595 } catch ( UnexpectedValueException $e ) {
596 $this->iter = null;
597 }
598 }
599
600 public function valid() {
601 return $this->iter && $this->iter->valid();
602 }
603 }