diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2021-12-13 20:33:15 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2021-12-13 20:33:15 +0000 |
commit | 9f303dc3988814db2d3f278f7a927fecf8d56017 (patch) | |
tree | a0dfc3b971e70089c739bcdbb8e181a63c267034 | |
parent | f4991297805021036d9052c45e1571e315a9250b (diff) | |
parent | 66102bd54c1f55c0d42fd064085e163780424424 (diff) |
Merge "resourceloader: Remove support for $algo param in FileContentsHasher"
-rw-r--r-- | includes/utils/FileContentsHasher.php | 33 | ||||
-rw-r--r-- | tests/phpunit/includes/utils/FileContentsHasherTest.php | 34 |
2 files changed, 32 insertions, 35 deletions
diff --git a/includes/utils/FileContentsHasher.php b/includes/utils/FileContentsHasher.php index 6177b402fbad..cbeca3d4c775 100644 --- a/includes/utils/FileContentsHasher.php +++ b/includes/utils/FileContentsHasher.php @@ -20,6 +20,7 @@ * @file */ class FileContentsHasher { + private const ALGO = 'md4'; /** @var BagOStuff */ protected $cache; @@ -49,26 +50,28 @@ class FileContentsHasher { * computed hash from the cache, or by computing a hash from the file. * * @param string $filePath Full path to the file. - * @param string $algo Name of selected hashing algorithm. * @return string|bool Hash of file contents, or false if the file could not be read. */ - private function getFileContentsHashInternal( $filePath, $algo = 'md4' ) { - $mtime = filemtime( $filePath ); + private function getFileContentsHashInternal( $filePath ) { + // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged + $mtime = @filemtime( $filePath ); if ( $mtime === false ) { return false; } - $cacheKey = $this->cache->makeGlobalKey( __CLASS__, $filePath, $mtime, $algo ); + $cacheKey = $this->cache->makeGlobalKey( __CLASS__, $filePath, $mtime, self::ALGO ); return $this->cache->getWithSetCallback( $cacheKey, - BagOStuff::TTL_DAY, - static function () use ( $filePath, $algo ) { - $contents = file_get_contents( $filePath ); + $this->cache::TTL_DAY, + static function () use ( $filePath ) { + // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged + $contents = @file_get_contents( $filePath ); if ( $contents === false ) { + // Don't cache false return false; } - return hash( $algo, $contents ); + return hash( self::ALGO, $contents ); } ); } @@ -79,33 +82,27 @@ class FileContentsHasher { * a hash from the files. * * @param string|string[] $filePaths One or more file paths. - * @param string $algo Name of selected hashing algorithm. * @return string|bool Hash of files' contents, or false if no file could not be read. */ - public static function getFileContentsHash( $filePaths, $algo = 'md4' ) { + public static function getFileContentsHash( $filePaths ) { $instance = self::singleton(); if ( !is_array( $filePaths ) ) { $filePaths = (array)$filePaths; } - Wikimedia\suppressWarnings(); - if ( count( $filePaths ) === 1 ) { - $hash = $instance->getFileContentsHashInternal( $filePaths[0], $algo ); - Wikimedia\restoreWarnings(); + $hash = $instance->getFileContentsHashInternal( $filePaths[0] ); return $hash; } sort( $filePaths ); $hashes = []; foreach ( $filePaths as $filePath ) { - $hashes[] = $instance->getFileContentsHashInternal( $filePath, $algo ) ?: ''; + $hashes[] = $instance->getFileContentsHashInternal( $filePath ) ?: ''; } - Wikimedia\restoreWarnings(); - $hashes = implode( '', $hashes ); - return $hashes ? hash( $algo, $hashes ) : false; + return $hashes ? hash( self::ALGO, $hashes ) : false; } } diff --git a/tests/phpunit/includes/utils/FileContentsHasherTest.php b/tests/phpunit/includes/utils/FileContentsHasherTest.php index afefb26b5ac6..c239b10fd062 100644 --- a/tests/phpunit/includes/utils/FileContentsHasherTest.php +++ b/tests/phpunit/includes/utils/FileContentsHasherTest.php @@ -13,25 +13,24 @@ class FileContentsHasherTest extends PHPUnit\Framework\TestCase { }, glob( __DIR__ . '/../../data/filecontentshasher/*.*' ) ); } - public function provideMultipleFiles() { - return [ - [ $this->provideSingleFile() ] - ]; - } - /** * @covers FileContentsHasher::getFileContentsHash * @covers FileContentsHasher::getFileContentsHashInternal * @dataProvider provideSingleFile */ public function testSingleFileHash( $fileName, $contents ) { - foreach ( [ 'md4', 'md5' ] as $algo ) { - $expectedHash = hash( $algo, $contents ); - $actualHash = FileContentsHasher::getFileContentsHash( $fileName, $algo ); - $this->assertEquals( $expectedHash, $actualHash ); - $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileName, $algo ); - $this->assertEquals( $expectedHash, $actualHashRepeat ); - } + $expected = hash( 'md4', $contents ); + $actualHash = FileContentsHasher::getFileContentsHash( $fileName ); + $this->assertEquals( $expected, $actualHash ); + + $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileName ); + $this->assertEquals( $expected, $actualHashRepeat ); + } + + public function provideMultipleFiles() { + return [ + [ $this->provideSingleFile() ] + ]; } /** @@ -44,13 +43,14 @@ class FileContentsHasherTest extends PHPUnit\Framework\TestCase { $hashes = []; foreach ( $files as [ $fileName, $contents ] ) { $fileNames[] = $fileName; - $hashes[] = md5( $contents ); + $hashes[] = hash( 'md4', $contents ); } - $expectedHash = md5( implode( '', $hashes ) ); - $actualHash = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' ); + $expectedHash = hash( 'md4', implode( '', $hashes ) ); + $actualHash = FileContentsHasher::getFileContentsHash( $fileNames ); $this->assertEquals( $expectedHash, $actualHash ); - $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' ); + + $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileNames ); $this->assertEquals( $expectedHash, $actualHashRepeat ); } } |