MediaWiki
REL1_35
UploadedFileStream.php
Go to the documentation of this file.
1
<?php
2
3
namespace
Wikimedia\ParamValidator\Util
;
4
5
use Psr\Http\Message\StreamInterface;
6
use RuntimeException;
7
use Throwable;
8
use Wikimedia\AtEase\AtEase;
9
20
class
UploadedFileStream
implements
StreamInterface {
21
23
private
$fp
;
24
26
private
$size
=
false
;
27
37
private
static
function
quietCall
( callable $func, array
$args
, $fail, $msg ) {
38
error_clear_last();
39
$ret = AtEase::quietCall( $func, ...
$args
);
40
if
( $ret === $fail ) {
41
$err = error_get_last();
42
throw
new
RuntimeException(
"$msg: "
. ( $err[
'message'
] ??
'Unknown error'
) );
43
}
44
return
$ret;
45
}
46
50
public
function
__construct
( $filename ) {
51
$this->fp =
self::quietCall
(
'fopen'
, [ $filename,
'r'
],
false
,
'Failed to open file'
);
52
}
53
58
private
function
checkOpen
() {
59
if
( !$this->fp ) {
60
throw
new
RuntimeException(
'Stream is not open'
);
61
}
62
}
63
64
public
function
__destruct
() {
65
$this->
close
();
66
}
67
68
public
function
__toString
() {
69
try
{
70
$this->
seek
( 0 );
71
return
$this->
getContents
();
72
}
catch
( Throwable $ex ) {
73
// Not allowed to throw
74
return
''
;
75
}
76
}
77
78
public
function
close
() {
79
if
( $this->fp ) {
80
// Spec doesn't care about close errors.
81
AtEase::suppressWarnings();
82
try
{
83
// PHP 7 emits warnings, suppress
84
fclose( $this->fp );
85
}
catch
( \TypeError $unused ) {
86
// While PHP 8 throws exceptions, ignore
87
}
finally
{
88
AtEase::restoreWarnings();
89
}
90
$this->fp =
null
;
91
}
92
}
93
94
public
function
detach
() {
95
$ret =
$this->fp
;
96
$this->fp =
null
;
97
return
$ret;
98
}
99
100
public
function
getSize
() {
101
if
( $this->size ===
false
) {
102
$this->size =
null
;
103
104
if
( $this->fp ) {
105
// Spec doesn't care about errors here.
106
AtEase::suppressWarnings();
107
try
{
108
$stat = fstat( $this->fp );
109
}
catch
( \TypeError $unused ) {
110
}
finally
{
111
AtEase::restoreWarnings();
112
}
113
$this->size = $stat[
'size'
] ??
null
;
114
}
115
}
116
117
return
$this->size
;
118
}
119
120
public
function
tell
() {
121
$this->
checkOpen
();
122
return
self::quietCall
(
'ftell'
, [ $this->fp ], -1,
'Cannot determine stream position'
);
123
}
124
125
public
function
eof
() {
126
// Spec doesn't care about errors here.
127
AtEase::suppressWarnings();
128
try
{
129
return
!$this->fp || feof( $this->fp );
130
}
catch
( \TypeError $unused ) {
131
return
true
;
132
}
finally
{
133
AtEase::restoreWarnings();
134
}
135
}
136
137
public
function
isSeekable
() {
138
return
(
bool
)
$this->fp
;
139
}
140
141
public
function
seek
( $offset, $whence = SEEK_SET ) {
142
$this->
checkOpen
();
143
self::quietCall
(
'fseek'
, [ $this->fp, $offset, $whence ], -1,
'Seek failed'
);
144
}
145
146
public
function
rewind
() {
147
$this->
seek
( 0 );
148
}
149
150
public
function
isWritable
() {
151
return
false
;
152
}
153
154
public
function
write
( $string ) {
155
$this->
checkOpen
();
156
throw
new
RuntimeException(
'Stream is read-only'
);
157
}
158
159
public
function
isReadable
() {
160
return
(
bool
)
$this->fp
;
161
}
162
163
public
function
read
( $length ) {
164
$this->
checkOpen
();
165
return
self::quietCall
(
'fread'
, [ $this->fp, $length ],
false
,
'Read failed'
);
166
}
167
168
public
function
getContents
() {
169
$this->
checkOpen
();
170
return
self::quietCall
(
'stream_get_contents'
, [ $this->fp ],
false
,
'Read failed'
);
171
}
172
173
public
function
getMetadata
( $key =
null
) {
174
$this->
checkOpen
();
175
$ret =
self::quietCall
(
'stream_get_meta_data'
, [ $this->fp ],
false
,
'Metadata fetch failed'
);
176
if
( $key !==
null
) {
177
$ret = $ret[$key] ??
null
;
178
}
179
return
$ret;
180
}
181
182
}
Wikimedia\ParamValidator\Util\UploadedFileStream
Implementation of StreamInterface for a file in $_FILES.
Definition
UploadedFileStream.php:20
Wikimedia\ParamValidator\Util\UploadedFileStream\eof
eof()
Definition
UploadedFileStream.php:125
Wikimedia\ParamValidator\Util\UploadedFileStream\detach
detach()
Definition
UploadedFileStream.php:94
Wikimedia\ParamValidator\Util\UploadedFileStream\isReadable
isReadable()
Definition
UploadedFileStream.php:159
Wikimedia\ParamValidator\Util\UploadedFileStream\getSize
getSize()
Definition
UploadedFileStream.php:100
Wikimedia\ParamValidator\Util\UploadedFileStream\getContents
getContents()
Definition
UploadedFileStream.php:168
Wikimedia\ParamValidator\Util\UploadedFileStream\__construct
__construct( $filename)
Definition
UploadedFileStream.php:50
Wikimedia\ParamValidator\Util\UploadedFileStream\read
read( $length)
Definition
UploadedFileStream.php:163
Wikimedia\ParamValidator\Util\UploadedFileStream\isSeekable
isSeekable()
Definition
UploadedFileStream.php:137
Wikimedia\ParamValidator\Util\UploadedFileStream\$size
int false null $size
File size.
Definition
UploadedFileStream.php:26
Wikimedia\ParamValidator\Util\UploadedFileStream\checkOpen
checkOpen()
Check if the stream is open.
Definition
UploadedFileStream.php:58
Wikimedia\ParamValidator\Util\UploadedFileStream\rewind
rewind()
Definition
UploadedFileStream.php:146
Wikimedia\ParamValidator\Util\UploadedFileStream\__toString
__toString()
Definition
UploadedFileStream.php:68
Wikimedia\ParamValidator\Util\UploadedFileStream\close
close()
Definition
UploadedFileStream.php:78
Wikimedia\ParamValidator\Util\UploadedFileStream\seek
seek( $offset, $whence=SEEK_SET)
Definition
UploadedFileStream.php:141
Wikimedia\ParamValidator\Util\UploadedFileStream\__destruct
__destruct()
Definition
UploadedFileStream.php:64
Wikimedia\ParamValidator\Util\UploadedFileStream\$fp
resource $fp
File handle.
Definition
UploadedFileStream.php:23
Wikimedia\ParamValidator\Util\UploadedFileStream\quietCall
static quietCall(callable $func, array $args, $fail, $msg)
Call, throwing on error.
Definition
UploadedFileStream.php:37
Wikimedia\ParamValidator\Util\UploadedFileStream\isWritable
isWritable()
Definition
UploadedFileStream.php:150
Wikimedia\ParamValidator\Util\UploadedFileStream\write
write( $string)
Definition
UploadedFileStream.php:154
Wikimedia\ParamValidator\Util\UploadedFileStream\getMetadata
getMetadata( $key=null)
Definition
UploadedFileStream.php:173
Wikimedia\ParamValidator\Util\UploadedFileStream\tell
tell()
Definition
UploadedFileStream.php:120
$args
if( $line===false) $args
Definition
mcc.php:124
Wikimedia\ParamValidator\Util
Definition
UploadedFile.php:3
includes
libs
ParamValidator
Util
UploadedFileStream.php
Generated on Sat Apr 6 2024 00:07:25 for MediaWiki by
1.9.8