From: Marius Hoch Date: Thu, 28 Feb 2013 00:11:12 +0000 (+0100) Subject: Only run sha1_file once per file in FSFile X-Git-Tag: 1.31.0-rc.0~20536^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22auteur_infos%22%2C%20%22id_auteur=%24id%22%29%20.%20%22?a=commitdiff_plain;h=13ae6bfb4fe54b0798e9be985951797605edf144;p=lhc%2Fweb%2Fwiklou.git Only run sha1_file once per file in FSFile This shouldn't harm as we don't use this and then alter the file. But it can kick in useful with extension that need the sha1 of files (like AbuseFilter). Change-Id: Ied2ac0397266a5ffd27da02ef6448b64c12fad86 --- diff --git a/includes/filebackend/FSFile.php b/includes/filebackend/FSFile.php index acbc4a996d..a6b6200756 100644 --- a/includes/filebackend/FSFile.php +++ b/includes/filebackend/FSFile.php @@ -28,6 +28,7 @@ */ class FSFile { protected $path; // path to file + private $sha1Base36 = null; // File Sha1Base36 /** * Sets up the file object @@ -193,20 +194,26 @@ class FSFile { * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36 * fairly neatly. * + * @param $recache bool * @return bool|string False on failure */ - public function getSha1Base36() { + public function getSha1Base36( $recache = false ) { wfProfileIn( __METHOD__ ); + if ( $this->sha1Base36 !== null && !$recache ) { + return $this->sha1Base36; + } + wfSuppressWarnings(); - $hash = sha1_file( $this->path ); + $this->sha1Base36 = sha1_file( $this->path ); wfRestoreWarnings(); - if ( $hash !== false ) { - $hash = wfBaseConvert( $hash, 16, 36, 31 ); + + if ( $this->sha1Base36 !== false ) { + $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 ); } wfProfileOut( __METHOD__ ); - return $hash; + return $this->sha1Base36; } /** @@ -242,11 +249,18 @@ class FSFile { * fairly neatly. * * @param $path string + * @param $recache bool * * @return bool|string False on failure */ - public static function getSha1Base36FromPath( $path ) { - $fsFile = new self( $path ); - return $fsFile->getSha1Base36(); + public static function getSha1Base36FromPath( $path, $recache = false ) { + static $sha1Base36 = array(); + + if ( !isset( $sha1Base36[$path] ) || $recache ) { + $fsFile = new self( $path ); + $sha1Base36[$path] = $fsFile->getSha1Base36(); + } + + return $sha1Base36[$path]; } }