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