X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Ffilebackend%2FFSFileBackend.php;h=993e2bac49420e04301374a7a5bdcb3d60b21135;hb=34a4bcbd48bf7af458eb110cc383c5bbb6989a31;hp=11a5ac961a2b5bc3eb9f698bb86b2a2a12d1db7a;hpb=ea4340e126eb2657cc878af74d53b9991844fb6b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/filebackend/FSFileBackend.php b/includes/filebackend/FSFileBackend.php index 11a5ac961a..993e2bac49 100644 --- a/includes/filebackend/FSFileBackend.php +++ b/includes/filebackend/FSFileBackend.php @@ -39,14 +39,22 @@ * @since 1.19 */ class FSFileBackend extends FileBackendStore { - protected $basePath; // string; directory holding the container directories - /** @var Array Map of container names to root paths */ - protected $containerPaths = array(); // for custom container paths - protected $fileMode; // integer; file permission mode - protected $fileOwner; // string; required OS username to own files - protected $currentUser; // string; OS username running this script - - /** @var Array */ + /** @var string Directory holding the container directories */ + protected $basePath; + + /** @var array Map of container names to root paths for custom container paths */ + protected $containerPaths = array(); + + /** @var int File permission mode */ + protected $fileMode; + + /** @var string Required OS username to own files */ + protected $fileOwner; + + /** @var string OS username running this script */ + protected $currentUser; + + /** @var array */ protected $hadWarningErrors = array(); /** @@ -82,12 +90,6 @@ class FSFileBackend extends FileBackendStore { } } - /** - * @see FileBackendStore::resolveContainerPath() - * @param $container string - * @param $relStoragePath string - * @return null|string - */ protected function resolveContainerPath( $container, $relStoragePath ) { // Check that container has a root directory if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) { @@ -96,6 +98,7 @@ class FSFileBackend extends FileBackendStore { return $relStoragePath; } } + return null; } @@ -121,8 +124,8 @@ class FSFileBackend extends FileBackendStore { * Given the short (unresolved) and full (resolved) name of * a container, return the file system path of the container. * - * @param $shortCont string - * @param $fullCont string + * @param string $shortCont + * @param string $fullCont * @return string|null */ protected function containerFSRoot( $shortCont, $fullCont ) { @@ -131,6 +134,7 @@ class FSFileBackend extends FileBackendStore { } elseif ( isset( $this->basePath ) ) { return "{$this->basePath}/{$fullCont}"; } + return null; // no container base path defined } @@ -150,13 +154,10 @@ class FSFileBackend extends FileBackendStore { if ( $relPath != '' ) { $fsPath .= "/{$relPath}"; } + return $fsPath; } - /** - * @see FileBackendStore::isPathUsableInternal() - * @return bool - */ public function isPathUsableInternal( $storagePath ) { $fsPath = $this->resolveToFSPath( $storagePath ); if ( $fsPath === null ) { @@ -178,16 +179,13 @@ class FSFileBackend extends FileBackendStore { return $ok; } - /** - * @see FileBackendStore::doCreateInternal() - * @return Status - */ protected function doCreateInternal( array $params ) { $status = Status::newGood(); $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -195,6 +193,7 @@ class FSFileBackend extends FileBackendStore { $tempFile = TempFSFile::factory( 'create_', 'tmp' ); if ( !$tempFile ) { $status->fatal( 'backend-fail-create', $params['dst'] ); + return $status; } $this->trapWarnings(); @@ -202,6 +201,7 @@ class FSFileBackend extends FileBackendStore { $this->untrapWarnings(); if ( $bytes === false ) { $status->fatal( 'backend-fail-create', $params['dst'] ); + return $status; } $cmd = implode( ' ', array( @@ -217,6 +217,7 @@ class FSFileBackend extends FileBackendStore { $this->untrapWarnings(); if ( $bytes === false ) { $status->fatal( 'backend-fail-create', $params['dst'] ); + return $status; } $this->chmod( $dest ); @@ -235,16 +236,13 @@ class FSFileBackend extends FileBackendStore { } } - /** - * @see FileBackendStore::doStoreInternal() - * @return Status - */ protected function doStoreInternal( array $params ) { $status = Status::newGood(); $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -266,6 +264,7 @@ class FSFileBackend extends FileBackendStore { trigger_error( __METHOD__ . ": copy() failed but returned true." ); } $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); + return $status; } $this->chmod( $dest ); @@ -284,22 +283,20 @@ class FSFileBackend extends FileBackendStore { } } - /** - * @see FileBackendStore::doCopyInternal() - * @return Status - */ protected function doCopyInternal( array $params ) { $status = Status::newGood(); $source = $this->resolveToFSPath( $params['src'] ); if ( $source === null ) { $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + return $status; } $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -307,6 +304,7 @@ class FSFileBackend extends FileBackendStore { if ( empty( $params['ignoreMissingSource'] ) ) { $status->fatal( 'backend-fail-copy', $params['src'] ); } + return $status; // do nothing; either OK or bad status } @@ -330,6 +328,7 @@ class FSFileBackend extends FileBackendStore { trigger_error( __METHOD__ . ": copy() failed but returned true." ); } $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); + return $status; } $this->chmod( $dest ); @@ -348,22 +347,20 @@ class FSFileBackend extends FileBackendStore { } } - /** - * @see FileBackendStore::doMoveInternal() - * @return Status - */ protected function doMoveInternal( array $params ) { $status = Status::newGood(); $source = $this->resolveToFSPath( $params['src'] ); if ( $source === null ) { $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + return $status; } $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -371,6 +368,7 @@ class FSFileBackend extends FileBackendStore { if ( empty( $params['ignoreMissingSource'] ) ) { $status->fatal( 'backend-fail-move', $params['src'] ); } + return $status; // do nothing; either OK or bad status } @@ -388,6 +386,7 @@ class FSFileBackend extends FileBackendStore { clearstatcache(); // file no longer at source if ( !$ok ) { $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] ); + return $status; } } @@ -405,16 +404,13 @@ class FSFileBackend extends FileBackendStore { } } - /** - * @see FileBackendStore::doDeleteInternal() - * @return Status - */ protected function doDeleteInternal( array $params ) { $status = Status::newGood(); $source = $this->resolveToFSPath( $params['src'] ); if ( $source === null ) { $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + return $status; } @@ -422,6 +418,7 @@ class FSFileBackend extends FileBackendStore { if ( empty( $params['ignoreMissingSource'] ) ) { $status->fatal( 'backend-fail-delete', $params['src'] ); } + return $status; // do nothing; either OK or bad status } @@ -437,6 +434,7 @@ class FSFileBackend extends FileBackendStore { $this->untrapWarnings(); if ( !$ok ) { $status->fatal( 'backend-fail-delete', $params['src'] ); + return $status; } } @@ -455,7 +453,9 @@ class FSFileBackend extends FileBackendStore { } /** - * @see FileBackendStore::doPrepareInternal() + * @param string $fullCont + * @param $dirRel + * @param array $params * @return Status */ protected function doPrepareInternal( $fullCont, $dirRel, array $params ) { @@ -478,13 +478,10 @@ class FSFileBackend extends FileBackendStore { if ( is_dir( $dir ) && !$existed ) { $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) ); } + return $status; } - /** - * @see FileBackendStore::doSecureInternal() - * @return Status - */ protected function doSecureInternal( $fullCont, $dirRel, array $params ) { $status = Status::newGood(); list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -509,13 +506,10 @@ class FSFileBackend extends FileBackendStore { $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" ); } } + return $status; } - /** - * @see FileBackendStore::doPublishInternal() - * @return Status - */ protected function doPublishInternal( $fullCont, $dirRel, array $params ) { $status = Status::newGood(); list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -540,13 +534,10 @@ class FSFileBackend extends FileBackendStore { } $this->untrapWarnings(); } + return $status; } - /** - * @see FileBackendStore::doCleanInternal() - * @return Status - */ protected function doCleanInternal( $fullCont, $dirRel, array $params ) { $status = Status::newGood(); list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -557,13 +548,10 @@ class FSFileBackend extends FileBackendStore { rmdir( $dir ); // remove directory if empty } $this->untrapWarnings(); + return $status; } - /** - * @see FileBackendStore::doFileExists() - * @return array|bool|null - */ protected function doGetFileStat( array $params ) { $source = $this->resolveToFSPath( $params['src'] ); if ( $source === null ) { @@ -593,10 +581,6 @@ class FSFileBackend extends FileBackendStore { clearstatcache(); // clear the PHP file stat cache } - /** - * @see FileBackendStore::doDirectoryExists() - * @return bool|null - */ protected function doDirectoryExists( $fullCont, $dirRel, array $params ) { list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid @@ -611,7 +595,10 @@ class FSFileBackend extends FileBackendStore { /** * @see FileBackendStore::getDirectoryListInternal() - * @return Array|null + * @param string $fullCont + * @param string $dirRel + * @param array $params + * @return array|null */ public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) { list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -620,17 +607,23 @@ class FSFileBackend extends FileBackendStore { $exists = is_dir( $dir ); if ( !$exists ) { wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" ); + return array(); // nothing under this dir } elseif ( !is_readable( $dir ) ) { wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); + return null; // bad permissions? } + return new FSFileBackendDirList( $dir, $params ); } /** * @see FileBackendStore::getFileListInternal() - * @return Array|FSFileBackendFileList|null + * @param string $fullCont + * @param string $dirRel + * @param array $params + * @return array|FSFileBackendFileList|null */ public function getFileListInternal( $fullCont, $dirRel, array $params ) { list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -639,18 +632,17 @@ class FSFileBackend extends FileBackendStore { $exists = is_dir( $dir ); if ( !$exists ) { wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" ); + return array(); // nothing under this dir } elseif ( !is_readable( $dir ) ) { wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); + return null; // bad permissions? } + return new FSFileBackendFileList( $dir, $params ); } - /** - * @see FileBackendStore::doGetLocalReferenceMulti() - * @return Array - */ protected function doGetLocalReferenceMulti( array $params ) { $fsFiles = array(); // (path => FSFile) @@ -666,10 +658,6 @@ class FSFileBackend extends FileBackendStore { return $fsFiles; } - /** - * @see FileBackendStore::doGetLocalCopyMulti() - * @return Array - */ protected function doGetLocalCopyMulti( array $params ) { $tmpFiles = array(); // (path => TempFSFile) @@ -702,18 +690,10 @@ class FSFileBackend extends FileBackendStore { return $tmpFiles; } - /** - * @see FileBackendStore::directoriesAreVirtual() - * @return bool - */ protected function directoriesAreVirtual() { return false; } - /** - * @see FileBackendStore::doExecuteOpHandlesInternal() - * @return Array List of corresponding Status objects - */ protected function doExecuteOpHandlesInternal( array $fileOpHandles ) { $statuses = array(); @@ -788,8 +768,6 @@ class FSFileBackend extends FileBackendStore { /** * Listen for E_WARNING errors and track whether any happen - * - * @return void */ protected function trapWarnings() { $this->hadWarningErrors[] = false; // push to stack @@ -807,14 +785,15 @@ class FSFileBackend extends FileBackendStore { } /** - * @param $errno integer - * @param $errstr string + * @param int $errno + * @param string $errstr * @return bool * @access private */ public function handleWarning( $errno, $errstr ) { wfDebugLog( 'FSFileBackend', $errstr ); // more detailed error logging $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true; + return true; // suppress from PHP handler } } @@ -827,13 +806,15 @@ class FSFileOpHandle extends FileBackendStoreOpHandle { public $chmodPath; // string; file to chmod /** - * @param $backend - * @param $params array - * @param $call - * @param $cmd - * @param $chmodPath null + * @param FSFileBackend $backend + * @param array $params + * @param string $call + * @param string $cmd + * @param int|null $chmodPath */ - public function __construct( $backend, array $params, $call, $cmd, $chmodPath = null ) { + public function __construct( + FSFileBackend $backend, array $params, $call, $cmd, $chmodPath = null + ) { $this->backend = $backend; $this->params = $params; $this->call = $call; @@ -852,14 +833,19 @@ class FSFileOpHandle extends FileBackendStoreOpHandle { abstract class FSFileBackendList implements Iterator { /** @var Iterator */ protected $iter; - protected $suffixStart; // integer - protected $pos = 0; // integer - /** @var Array */ + + /** @var int */ + protected $suffixStart; + + /** @var int */ + protected $pos = 0; + + /** @var array */ protected $params = array(); /** * @param string $dir file system directory - * @param $params array + * @param array $params */ public function __construct( $dir, array $params ) { $path = realpath( $dir ); // normalize @@ -891,6 +877,7 @@ abstract class FSFileBackendList implements Iterator { # RecursiveDirectoryIterator extends FilesystemIterator. # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x. $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS; + return new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, $flags ), RecursiveIteratorIterator::CHILD_FIRST // include dirs @@ -900,7 +887,7 @@ abstract class FSFileBackendList implements Iterator { /** * @see Iterator::key() - * @return integer + * @return int */ public function key() { return $this->pos; @@ -916,29 +903,29 @@ abstract class FSFileBackendList implements Iterator { /** * @see Iterator::next() - * @return void + * @throws FileBackendError */ public function next() { try { $this->iter->next(); $this->filterViaNext(); - } catch ( UnexpectedValueException $e ) { - $this->iter = null; + } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? + throw new FileBackendError( "File iterator gave UnexpectedValueException." ); } ++$this->pos; } /** * @see Iterator::rewind() - * @return void + * @throws FileBackendError */ public function rewind() { $this->pos = 0; try { $this->iter->rewind(); $this->filterViaNext(); - } catch ( UnexpectedValueException $e ) { - $this->iter = null; + } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? + throw new FileBackendError( "File iterator gave UnexpectedValueException." ); } } @@ -953,13 +940,14 @@ abstract class FSFileBackendList implements Iterator { /** * Filter out items by advancing to the next ones */ - protected function filterViaNext() {} + protected function filterViaNext() { + } /** * Return only the relative path and normalize slashes to FileBackend-style. * Uses the "real path" since the suffix is based upon that. * - * @param $path string + * @param string $dir * @return string */ protected function getRelPath( $dir ) { @@ -967,6 +955,7 @@ abstract class FSFileBackendList implements Iterator { if ( $path === false ) { $path = $dir; } + return strtr( substr( $path, $this->suffixStart ), '\\', '/' ); } }