Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / filebackend / fsfile / FSFile.php
blobe17be3db82199bdcbc2ccbb81ae47b9d23e08545
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileBackend
24 namespace Wikimedia\FileBackend\FSFile;
26 use Wikimedia\AtEase\AtEase;
27 use Wikimedia\Timestamp\ConvertibleTimestamp;
29 /**
30 * Class representing a non-directory file on the file system
32 * @ingroup FileBackend
34 class FSFile {
35 /** @var string Path to file */
36 protected $path;
38 /** @var string File SHA-1 in base 36 */
39 protected $sha1Base36;
41 /**
42 * Sets up the file object
44 * @param string $path Path to temporary file on local disk
46 public function __construct( $path ) {
47 $this->path = $path;
50 /**
51 * Returns the file system path
53 * @return string
55 public function getPath() {
56 return $this->path;
59 /**
60 * Checks if the file exists
62 * @return bool
64 public function exists() {
65 return is_file( $this->path );
68 /**
69 * Get the file size in bytes
71 * @return int|bool
73 public function getSize() {
74 AtEase::suppressWarnings();
75 $size = filesize( $this->path );
76 AtEase::restoreWarnings();
78 return $size;
81 /**
82 * Get the file's last-modified timestamp
84 * @return string|bool TS_MW timestamp or false on failure
86 public function getTimestamp() {
87 AtEase::suppressWarnings();
88 $timestamp = filemtime( $this->path );
89 AtEase::restoreWarnings();
90 if ( $timestamp !== false ) {
91 $timestamp = ConvertibleTimestamp::convert( TS_MW, $timestamp );
94 return $timestamp;
97 /**
98 * Get an associative array containing information about
99 * a file with the given storage path.
101 * Resulting array fields include:
102 * - fileExists
103 * - size (filesize in bytes)
104 * - mime (as major/minor)
105 * - file-mime (as major/minor)
106 * - sha1 (in base 36)
107 * - major_mime
108 * - minor_mime
110 * @param string|bool $ext The file extension, or true to extract it from the filename.
111 * Set it to false to ignore the extension. Currently unused.
112 * @return array
114 public function getProps( $ext = true ) {
115 $info = self::placeholderProps();
116 $info['fileExists'] = $this->exists();
118 if ( $info['fileExists'] ) {
119 $info['size'] = $this->getSize(); // bytes
120 $info['sha1'] = $this->getSha1Base36();
122 $mime = mime_content_type( $this->path );
123 # MIME type according to file contents
124 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
125 # logical MIME type
126 $info['mime'] = $mime;
128 if ( strpos( $mime, '/' ) !== false ) {
129 [ $info['major_mime'], $info['minor_mime'] ] = explode( '/', $mime, 2 );
130 } else {
131 [ $info['major_mime'], $info['minor_mime'] ] = [ $mime, 'unknown' ];
135 return $info;
139 * Placeholder file properties to use for files that don't exist
141 * Resulting array fields include:
142 * - fileExists
143 * - size (filesize in bytes)
144 * - mime (as major/minor)
145 * - file-mime (as major/minor)
146 * - sha1 (in base 36)
147 * - major_mime
148 * - minor_mime
150 * @return array
152 public static function placeholderProps() {
153 $info = [];
154 $info['fileExists'] = false;
155 $info['size'] = 0;
156 $info['file-mime'] = null;
157 $info['major_mime'] = null;
158 $info['minor_mime'] = null;
159 $info['mime'] = null;
160 $info['sha1'] = '';
162 return $info;
166 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
167 * encoding, zero padded to 31 digits.
169 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
170 * fairly neatly.
172 * @param bool $recache
173 * @return bool|string False on failure
175 public function getSha1Base36( $recache = false ) {
176 if ( $this->sha1Base36 !== null && !$recache ) {
177 return $this->sha1Base36;
180 AtEase::suppressWarnings();
181 $this->sha1Base36 = sha1_file( $this->path );
182 AtEase::restoreWarnings();
184 if ( $this->sha1Base36 !== false ) {
185 $this->sha1Base36 = \Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
188 return $this->sha1Base36;
192 * Get the final file extension from a file system path
194 * @param string $path
195 * @return string
197 public static function extensionFromPath( $path ) {
198 $i = strrpos( $path, '.' );
200 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
204 * Get an associative array containing information about a file in the local filesystem.
206 * @param string $path Absolute local filesystem path
207 * @param string|bool $ext The file extension, or true to extract it from the filename.
208 * Set it to false to ignore the extension.
209 * @return array
211 public static function getPropsFromPath( $path, $ext = true ) {
212 $fsFile = new self( $path );
214 return $fsFile->getProps( $ext );
218 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
219 * encoding, zero padded to 31 digits.
221 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
222 * fairly neatly.
224 * @param string $path
225 * @return false|string False on failure
227 public static function getSha1Base36FromPath( $path ) {
228 $fsFile = new self( $path );
230 return $fsFile->getSha1Base36();
234 /** @deprecated class alias since 1.43 */
235 class_alias( FSFile::class, 'FSFile' );