MediaWiki REL1_34
UploadedFileStream.php
Go to the documentation of this file.
1<?php
2
4
5use Exception;
6use Psr\Http\Message\StreamInterface;
7use RuntimeException;
8use Throwable;
9use Wikimedia\AtEase\AtEase;
10
21class UploadedFileStream implements StreamInterface {
22
24 private $fp;
25
27 private $size = false;
28
38 private static function quietCall( callable $func, array $args, $fail, $msg ) {
39 // TODO remove the function_exists check once we drop HHVM support
40 if ( function_exists( 'error_clear_last' ) ) {
41 error_clear_last();
42 }
43 $ret = AtEase::quietCall( $func, ...$args );
44 if ( $ret === $fail ) {
45 $err = error_get_last();
46 throw new RuntimeException( "$msg: " . ( $err['message'] ?? 'Unknown error' ) );
47 }
48 return $ret;
49 }
50
54 public function __construct( $filename ) {
55 $this->fp = self::quietCall( 'fopen', [ $filename, 'r' ], false, 'Failed to open file' );
56 }
57
62 private function checkOpen() {
63 if ( !$this->fp ) {
64 throw new RuntimeException( 'Stream is not open' );
65 }
66 }
67
68 public function __destruct() {
69 $this->close();
70 }
71
72 public function __toString() {
73 try {
74 $this->seek( 0 );
75 return $this->getContents();
76 } catch ( Exception $ex ) {
77 // Not allowed to throw
78 return '';
79 } catch ( Throwable $ex ) {
80 // Not allowed to throw
81 return '';
82 }
83 }
84
85 public function close() {
86 if ( $this->fp ) {
87 // Spec doesn't care about close errors.
88 AtEase::quietCall( 'fclose', $this->fp );
89 $this->fp = null;
90 }
91 }
92
93 public function detach() {
94 $ret = $this->fp;
95 $this->fp = null;
96 return $ret;
97 }
98
99 public function getSize() {
100 if ( $this->size === false ) {
101 $this->size = null;
102
103 if ( $this->fp ) {
104 // Spec doesn't care about errors here.
105 $stat = AtEase::quietCall( 'fstat', $this->fp );
106 $this->size = $stat['size'] ?? null;
107 }
108 }
109
110 return $this->size;
111 }
112
113 public function tell() {
114 $this->checkOpen();
115 return self::quietCall( 'ftell', [ $this->fp ], -1, 'Cannot determine stream position' );
116 }
117
118 public function eof() {
119 // Spec doesn't care about errors here.
120 return !$this->fp || AtEase::quietCall( 'feof', $this->fp );
121 }
122
123 public function isSeekable() {
124 return (bool)$this->fp;
125 }
126
127 public function seek( $offset, $whence = SEEK_SET ) {
128 $this->checkOpen();
129 self::quietCall( 'fseek', [ $this->fp, $offset, $whence ], -1, 'Seek failed' );
130 }
131
132 public function rewind() {
133 $this->seek( 0 );
134 }
135
136 public function isWritable() {
137 return false;
138 }
139
140 public function write( $string ) {
141 $this->checkOpen();
142 throw new RuntimeException( 'Stream is read-only' );
143 }
144
145 public function isReadable() {
146 return (bool)$this->fp;
147 }
148
149 public function read( $length ) {
150 $this->checkOpen();
151 return self::quietCall( 'fread', [ $this->fp, $length ], false, 'Read failed' );
152 }
153
154 public function getContents() {
155 $this->checkOpen();
156 return self::quietCall( 'stream_get_contents', [ $this->fp ], false, 'Read failed' );
157 }
158
159 public function getMetadata( $key = null ) {
160 $this->checkOpen();
161 $ret = self::quietCall( 'stream_get_meta_data', [ $this->fp ], false, 'Metadata fetch failed' );
162 if ( $key !== null ) {
163 $ret = $ret[$key] ?? null;
164 }
165 return $ret;
166 }
167
168}
if( $line===false) $args
Definition cdb.php:64
Implementation of StreamInterface for a file in $_FILES.
static quietCall(callable $func, array $args, $fail, $msg)
Call, throwing on error.