MediaWiki REL1_37
FauxRequest.php
Go to the documentation of this file.
1<?php
27
35class FauxRequest extends WebRequest {
36 private $wasPosted = false;
37 private $requestUrl;
38 protected $cookies = [];
40 private $uploadData = [];
41
53 public function __construct( $data = [], $wasPosted = false,
54 $session = null, $protocol = 'http'
55 ) {
56 $this->requestTime = microtime( true );
57
58 if ( is_array( $data ) ) {
59 $this->data = $data;
60 } else {
61 throw new MWException( "FauxRequest() got bogus data" );
62 }
63 $this->wasPosted = $wasPosted;
64 if ( $session instanceof MediaWiki\Session\Session ) {
65 $this->sessionId = $session->getSessionId();
66 } elseif ( is_array( $session ) ) {
67 $mwsession = SessionManager::singleton()->getEmptySession( $this );
68 $this->sessionId = $mwsession->getSessionId();
69 foreach ( $session as $key => $value ) {
70 $mwsession->set( $key, $value );
71 }
72 } elseif ( $session !== null ) {
73 throw new MWException( "FauxRequest() got bogus session" );
74 }
75 $this->protocol = $protocol;
76 }
77
81 protected function initHeaders() {
82 // Nothing to init
83 }
84
90 public function getText( $name, $default = '' ) {
91 # Override; don't recode since we're using internal data
92 return (string)$this->getVal( $name, $default );
93 }
94
98 public function getQueryValues() {
99 if ( $this->wasPosted ) {
100 return [];
101 } else {
102 return $this->data;
103 }
104 }
105
106 public function getMethod() {
107 return $this->wasPosted ? 'POST' : 'GET';
108 }
109
113 public function wasPosted() {
114 return $this->wasPosted;
115 }
116
117 public function getCookie( $key, $prefix = null, $default = null ) {
118 if ( $prefix === null ) {
119 global $wgCookiePrefix;
120 $prefix = $wgCookiePrefix;
121 }
122 $name = $prefix . $key;
123 return $this->cookies[$name] ?? $default;
124 }
125
132 public function setCookie( $key, $value, $prefix = null ) {
133 $this->setCookies( [ $key => $value ], $prefix );
134 }
135
141 public function setCookies( $cookies, $prefix = null ) {
142 if ( $prefix === null ) {
143 global $wgCookiePrefix;
144 $prefix = $wgCookiePrefix;
145 }
146 foreach ( $cookies as $key => $value ) {
147 $name = $prefix . $key;
148 $this->cookies[$name] = $value;
149 }
150 }
151
158 public function setUploadData( $uploadData ) {
159 foreach ( $uploadData as $key => $data ) {
160 $this->setUpload( $key, $data );
161 }
162 }
163
171 public function setUpload( $key, $data ) {
172 if ( $data instanceof WebRequestUpload ) {
173 // cannot reuse WebRequestUpload, because it contains the original web request object
174 $data = [
175 'name' => $data->getName(),
176 'type' => $data->getType(),
177 'tmp_name' => $data->getTempName(),
178 'size' => $data->getSize(),
179 'error' => $data->getError(),
180 ];
181 }
182 // Check if everything is provided
183 if ( !is_array( $data ) ||
184 array_diff( WebRequestUpload::REQUIRED_FILEINFO_KEYS, array_keys( $data ) ) !== []
185 ) {
186 throw new MWException( __METHOD__ . ' got bogus data' );
187 }
188 $this->uploadData[$key] = $data;
189 }
190
197 public function getUpload( $key ) {
198 return new FauxRequestUpload( $this->uploadData, $this, $key );
199 }
200
205 public function setRequestURL( $url ) {
206 $this->requestUrl = $url;
207 }
208
214 public function getRequestURL() {
215 if ( $this->requestUrl === null ) {
216 throw new MWException( 'Request URL not set' );
217 }
218 return $this->requestUrl;
219 }
220
221 public function getProtocol() {
222 return $this->protocol;
223 }
224
229 public function setHeader( $name, $val ) {
230 $this->setHeaders( [ $name => $val ] );
231 }
232
237 public function setHeaders( $headers ) {
238 foreach ( $headers as $name => $val ) {
239 $name = strtoupper( $name );
240 $this->headers[$name] = $val;
241 }
242 }
243
247 public function getSessionArray() {
248 if ( $this->sessionId !== null ) {
249 return iterator_to_array( $this->getSession() );
250 }
251 return null;
252 }
253
254 public function getPostValues() {
255 return $this->wasPosted ? $this->data : [];
256 }
257
262 public function getRawQueryString() {
263 return '';
264 }
265
270 public function getRawPostString() {
271 return '';
272 }
273
278 public function getRawInput() {
279 return '';
280 }
281
286 protected function getRawIP() {
287 return '127.0.0.1';
288 }
289}
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
Object to fake the $_FILES array.
WebRequest clone which takes values from a provided array.
getPostValues()
Get the values passed via POST.
setCookie( $key, $value, $prefix=null)
setRequestURL( $url)
getText( $name, $default='')
array $uploadData
getRawPostString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
initHeaders()
Initialise the header list.
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
setCookies( $cookies, $prefix=null)
getProtocol()
Get the current URL protocol (http or https)
getUpload( $key)
Return a FauxRequestUpload object corresponding to the key.
getMethod()
Get the HTTP method used for this request.
setHeaders( $headers)
setUpload( $key, $data)
Set fake upload data for one file with specific key.
__construct( $data=[], $wasPosted=false, $session=null, $protocol='http')
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
setHeader( $name, $val)
getRawQueryString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
setUploadData( $uploadData)
Set fake upload data for all files.
MediaWiki exception.
This serves as the entry point to the MediaWiki session handling system.
Object to access the $_FILES array.
const REQUIRED_FILEINFO_KEYS
All keys a fileinfo has to specific to work with this class.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
string $protocol
Cached URL protocol.
getSession()
Return the session for this request.
getVal( $name, $default=null)
Fetch a text string and partially normalized it.
string[] $headers
Lazy-initialized request headers indexed by upper-case header name.
array $data
The parameters from $_GET, $_POST and the path router.
A helper class for throttling authentication attempts.