Small optimization for FileContentsHasher
authorOri Livneh <ori@wikimedia.org>
Mon, 5 Oct 2015 17:56:32 +0000 (10:56 -0700)
committerOri Livneh <ori@wikimedia.org>
Mon, 5 Oct 2015 18:13:41 +0000 (11:13 -0700)
Make sure that PHP warnings are suppressed and restored once per invocation of
FileContentsHasher::getFileContentsHash(), rather than once or twice per file.

Change-Id: I814093f226d62e5e479411d0c3a7bbbe4998255a

includes/utils/FileContentsHasher.php

index 655c1d0..c866919 100644 (file)
@@ -57,7 +57,7 @@ class FileContentsHasher {
         * @return string|bool Hash of file contents, or false if the file could not be read.
         */
        public function getFileContentsHashInternal( $filePath, $algo = 'md4' ) {
-               $mtime = MediaWiki\quietCall( 'filemtime', $filePath );
+               $mtime = filemtime( $filePath );
                if ( $mtime === false ) {
                        return false;
                }
@@ -69,7 +69,7 @@ class FileContentsHasher {
                        return $hash;
                }
 
-               $contents = MediaWiki\quietCall( 'file_get_contents', $filePath );
+               $contents = file_get_contents( $filePath );
                if ( $contents === false ) {
                        return false;
                }
@@ -96,8 +96,12 @@ class FileContentsHasher {
                        $filePaths = (array)$filePaths;
                }
 
+               MediaWiki\suppressWarnings();
+
                if ( count( $filePaths ) === 1 ) {
-                       return $instance->getFileContentsHashInternal( $filePaths[0], $algo );
+                       $hash = $instance->getFileContentsHashInternal( $filePaths[0], $algo );
+                       MediaWiki\restoreWarnings();
+                       return $hash;
                }
 
                sort( $filePaths );
@@ -105,6 +109,8 @@ class FileContentsHasher {
                        return $instance->getFileContentsHashInternal( $filePath, $algo ) ?: '';
                }, $filePaths );
 
+               MediaWiki\restoreWarnings();
+
                $hashes = implode( '', $hashes );
                return $hashes ? hash( $algo, $hashes ) : false;
        }