3 * Non-directory file on the file system.
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.
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.
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
21 * @ingroup FileBackend
24 use Wikimedia\AtEase\AtEase
;
25 use Wikimedia\Timestamp\ConvertibleTimestamp
;
28 * Class representing a non-directory file on the file system
30 * @ingroup FileBackend
33 /** @var string Path to file */
36 /** @var string File SHA-1 in base 36 */
37 protected $sha1Base36;
40 * Sets up the file object
42 * @param string $path Path to temporary file on local disk
44 public function __construct( $path ) {
49 * Returns the file system path
53 public function getPath() {
58 * Checks if the file exists
62 public function exists() {
63 return is_file( $this->path
);
67 * Get the file size in bytes
71 public function getSize() {
72 AtEase
::suppressWarnings();
73 $size = filesize( $this->path
);
74 AtEase
::restoreWarnings();
80 * Get the file's last-modified timestamp
82 * @return string|bool TS_MW timestamp or false on failure
84 public function getTimestamp() {
85 AtEase
::suppressWarnings();
86 $timestamp = filemtime( $this->path
);
87 AtEase
::restoreWarnings();
88 if ( $timestamp !== false ) {
89 $timestamp = ConvertibleTimestamp
::convert( TS_MW
, $timestamp );
96 * Get an associative array containing information about
97 * a file with the given storage path.
99 * Resulting array fields include:
101 * - size (filesize in bytes)
102 * - mime (as major/minor)
103 * - file-mime (as major/minor)
104 * - sha1 (in base 36)
108 * @param string|bool $ext The file extension, or true to extract it from the filename.
109 * Set it to false to ignore the extension. Currently unused.
112 public function getProps( $ext = true ) {
113 $info = self
::placeholderProps();
114 $info['fileExists'] = $this->exists();
116 if ( $info['fileExists'] ) {
117 $info['size'] = $this->getSize(); // bytes
118 $info['sha1'] = $this->getSha1Base36();
120 $mime = mime_content_type( $this->path
);
121 # MIME type according to file contents
122 $info['file-mime'] = ( $mime === false ) ?
'unknown/unknown' : $mime;
124 $info['mime'] = $mime;
126 if ( strpos( $mime, '/' ) !== false ) {
127 list( $info['major_mime'], $info['minor_mime'] ) = explode( '/', $mime, 2 );
129 list( $info['major_mime'], $info['minor_mime'] ) = [ $mime, 'unknown' ];
137 * Placeholder file properties to use for files that don't exist
139 * Resulting array fields include:
141 * - size (filesize in bytes)
142 * - mime (as major/minor)
143 * - file-mime (as major/minor)
144 * - sha1 (in base 36)
150 public static function placeholderProps() {
152 $info['fileExists'] = false;
154 $info['file-mime'] = null;
155 $info['major_mime'] = null;
156 $info['minor_mime'] = null;
157 $info['mime'] = null;
164 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
165 * encoding, zero padded to 31 digits.
167 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
170 * @param bool $recache
171 * @return bool|string False on failure
173 public function getSha1Base36( $recache = false ) {
174 if ( $this->sha1Base36
!== null && !$recache ) {
175 return $this->sha1Base36
;
178 AtEase
::suppressWarnings();
179 $this->sha1Base36
= sha1_file( $this->path
);
180 AtEase
::restoreWarnings();
182 if ( $this->sha1Base36
!== false ) {
183 $this->sha1Base36
= Wikimedia\base_convert
( $this->sha1Base36
, 16, 36, 31 );
186 return $this->sha1Base36
;
190 * Get the final file extension from a file system path
192 * @param string $path
195 public static function extensionFromPath( $path ) {
196 $i = strrpos( $path, '.' );
198 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
202 * Get an associative array containing information about a file in the local filesystem.
204 * @param string $path Absolute local filesystem path
205 * @param string|bool $ext The file extension, or true to extract it from the filename.
206 * Set it to false to ignore the extension.
209 public static function getPropsFromPath( $path, $ext = true ) {
210 $fsFile = new self( $path );
212 return $fsFile->getProps( $ext );
216 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
217 * encoding, zero padded to 31 digits.
219 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
222 * @param string $path
223 * @return bool|string False on failure
225 public static function getSha1Base36FromPath( $path ) {
226 $fsFile = new self( $path );
228 return $fsFile->getSha1Base36();