Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / libs / filebackend / fsfile / FSFile.php
1 <?php
2 /**
3 * Non-directory file on the file system.
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 * @ingroup FileBackend
22 */
23
24 use Wikimedia\AtEase\AtEase;
25
26 /**
27 * Class representing a non-directory file on the file system
28 *
29 * @ingroup FileBackend
30 */
31 class FSFile {
32 /** @var string Path to file */
33 protected $path;
34
35 /** @var string File SHA-1 in base 36 */
36 protected $sha1Base36;
37
38 /**
39 * Sets up the file object
40 *
41 * @param string $path Path to temporary file on local disk
42 */
43 public function __construct( $path ) {
44 $this->path = $path;
45 }
46
47 /**
48 * Returns the file system path
49 *
50 * @return string
51 */
52 public function getPath() {
53 return $this->path;
54 }
55
56 /**
57 * Checks if the file exists
58 *
59 * @return bool
60 */
61 public function exists() {
62 return is_file( $this->path );
63 }
64
65 /**
66 * Get the file size in bytes
67 *
68 * @return int|bool
69 */
70 public function getSize() {
71 return filesize( $this->path );
72 }
73
74 /**
75 * Get the file's last-modified timestamp
76 *
77 * @return string|bool TS_MW timestamp or false on failure
78 */
79 public function getTimestamp() {
80 AtEase::suppressWarnings();
81 $timestamp = filemtime( $this->path );
82 AtEase::restoreWarnings();
83 if ( $timestamp !== false ) {
84 $timestamp = wfTimestamp( TS_MW, $timestamp );
85 }
86
87 return $timestamp;
88 }
89
90 /**
91 * Get an associative array containing information about
92 * a file with the given storage path.
93 *
94 * Resulting array fields include:
95 * - fileExists
96 * - size (filesize in bytes)
97 * - mime (as major/minor)
98 * - file-mime (as major/minor)
99 * - sha1 (in base 36)
100 * - major_mime
101 * - minor_mime
102 *
103 * @param string|bool $ext The file extension, or true to extract it from the filename.
104 * Set it to false to ignore the extension. Currently unused.
105 * @return array
106 */
107 public function getProps( $ext = true ) {
108 $info = self::placeholderProps();
109 $info['fileExists'] = $this->exists();
110
111 if ( $info['fileExists'] ) {
112 $info['size'] = $this->getSize(); // bytes
113 $info['sha1'] = $this->getSha1Base36();
114
115 $mime = mime_content_type( $this->path );
116 # MIME type according to file contents
117 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
118 # logical MIME type
119 $info['mime'] = $mime;
120
121 if ( strpos( $mime, '/' ) !== false ) {
122 list( $info['major_mime'], $info['minor_mime'] ) = explode( '/', $mime, 2 );
123 } else {
124 list( $info['major_mime'], $info['minor_mime'] ) = [ $mime, 'unknown' ];
125 }
126 }
127
128 return $info;
129 }
130
131 /**
132 * Placeholder file properties to use for files that don't exist
133 *
134 * Resulting array fields include:
135 * - fileExists
136 * - size (filesize in bytes)
137 * - mime (as major/minor)
138 * - file-mime (as major/minor)
139 * - sha1 (in base 36)
140 * - major_mime
141 * - minor_mime
142 *
143 * @return array
144 */
145 public static function placeholderProps() {
146 $info = [];
147 $info['fileExists'] = false;
148 $info['size'] = 0;
149 $info['file-mime'] = null;
150 $info['major_mime'] = null;
151 $info['minor_mime'] = null;
152 $info['mime'] = null;
153 $info['sha1'] = '';
154
155 return $info;
156 }
157
158 /**
159 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
160 * encoding, zero padded to 31 digits.
161 *
162 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
163 * fairly neatly.
164 *
165 * @param bool $recache
166 * @return bool|string False on failure
167 */
168 public function getSha1Base36( $recache = false ) {
169 if ( $this->sha1Base36 !== null && !$recache ) {
170 return $this->sha1Base36;
171 }
172
173 AtEase::suppressWarnings();
174 $this->sha1Base36 = sha1_file( $this->path );
175 AtEase::restoreWarnings();
176
177 if ( $this->sha1Base36 !== false ) {
178 $this->sha1Base36 = Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
179 }
180
181 return $this->sha1Base36;
182 }
183
184 /**
185 * Get the final file extension from a file system path
186 *
187 * @param string $path
188 * @return string
189 */
190 public static function extensionFromPath( $path ) {
191 $i = strrpos( $path, '.' );
192
193 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
194 }
195
196 /**
197 * Get an associative array containing information about a file in the local filesystem.
198 *
199 * @param string $path Absolute local filesystem path
200 * @param string|bool $ext The file extension, or true to extract it from the filename.
201 * Set it to false to ignore the extension.
202 * @return array
203 */
204 public static function getPropsFromPath( $path, $ext = true ) {
205 $fsFile = new self( $path );
206
207 return $fsFile->getProps( $ext );
208 }
209
210 /**
211 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
212 * encoding, zero padded to 31 digits.
213 *
214 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
215 * fairly neatly.
216 *
217 * @param string $path
218 * @return bool|string False on failure
219 */
220 public static function getSha1Base36FromPath( $path ) {
221 $fsFile = new self( $path );
222
223 return $fsFile->getSha1Base36();
224 }
225 }