FileContentsHasher.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Generate hash digests of file contents to help with cache invalidation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. class FileContentsHasher {
  23. /** @var BagOStuff */
  24. protected $cache;
  25. /** @var FileContentsHasher */
  26. private static $instance;
  27. public function __construct() {
  28. $this->cache = ObjectCache::getLocalServerInstance( 'hash' );
  29. }
  30. /**
  31. * Get the singleton instance of this class.
  32. *
  33. * @return FileContentsHasher
  34. */
  35. public static function singleton() {
  36. if ( !self::$instance ) {
  37. self::$instance = new self;
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * Get a hash of a file's contents, either by retrieving a previously-
  43. * computed hash from the cache, or by computing a hash from the file.
  44. *
  45. * @private
  46. * @param string $filePath Full path to the file.
  47. * @param string $algo Name of selected hashing algorithm.
  48. * @return string|bool Hash of file contents, or false if the file could not be read.
  49. */
  50. public function getFileContentsHashInternal( $filePath, $algo = 'md4' ) {
  51. $mtime = filemtime( $filePath );
  52. if ( $mtime === false ) {
  53. return false;
  54. }
  55. $cacheKey = $this->cache->makeGlobalKey( __CLASS__, $filePath, $mtime, $algo );
  56. $hash = $this->cache->get( $cacheKey );
  57. if ( $hash ) {
  58. return $hash;
  59. }
  60. $contents = file_get_contents( $filePath );
  61. if ( $contents === false ) {
  62. return false;
  63. }
  64. $hash = hash( $algo, $contents );
  65. $this->cache->set( $cacheKey, $hash, 60 * 60 * 24 ); // 24h
  66. return $hash;
  67. }
  68. /**
  69. * Get a hash of the combined contents of one or more files, either by
  70. * retrieving a previously-computed hash from the cache, or by computing
  71. * a hash from the files.
  72. *
  73. * @param string|string[] $filePaths One or more file paths.
  74. * @param string $algo Name of selected hashing algorithm.
  75. * @return string|bool Hash of files' contents, or false if no file could not be read.
  76. */
  77. public static function getFileContentsHash( $filePaths, $algo = 'md4' ) {
  78. $instance = self::singleton();
  79. if ( !is_array( $filePaths ) ) {
  80. $filePaths = (array)$filePaths;
  81. }
  82. Wikimedia\suppressWarnings();
  83. if ( count( $filePaths ) === 1 ) {
  84. $hash = $instance->getFileContentsHashInternal( $filePaths[0], $algo );
  85. Wikimedia\restoreWarnings();
  86. return $hash;
  87. }
  88. sort( $filePaths );
  89. $hashes = array_map( function ( $filePath ) use ( $instance, $algo ) {
  90. return $instance->getFileContentsHashInternal( $filePath, $algo ) ?: '';
  91. }, $filePaths );
  92. Wikimedia\restoreWarnings();
  93. $hashes = implode( '', $hashes );
  94. return $hashes ? hash( $algo, $hashes ) : false;
  95. }
  96. }