MediaWiki REL1_39
FauxRequest.php
Go to the documentation of this file.
1<?php
29
37class FauxRequest extends WebRequest {
38 private $wasPosted = false;
39 private $requestUrl;
40 protected $cookies = [];
42 private $uploadData = [];
43
55 public function __construct( $data = [], $wasPosted = false,
56 $session = null, $protocol = 'http'
57 ) {
58 $this->requestTime = microtime( true );
59
60 if ( is_array( $data ) ) {
61 $this->data = $data;
62 } else {
63 throw new MWException( "FauxRequest() got bogus data" );
64 }
65 $this->wasPosted = $wasPosted;
66 if ( $session instanceof MediaWiki\Session\Session ) {
67 $this->sessionId = $session->getSessionId();
68 } elseif ( is_array( $session ) ) {
69 $mwsession = SessionManager::singleton()->getEmptySession( $this );
70 $this->sessionId = $mwsession->getSessionId();
71 foreach ( $session as $key => $value ) {
72 $mwsession->set( $key, $value );
73 }
74 } elseif ( $session !== null ) {
75 throw new MWException( "FauxRequest() got bogus session" );
76 }
77 $this->protocol = $protocol;
78 }
79
83 protected function initHeaders() {
84 // Nothing to init
85 }
86
92 public function getText( $name, $default = '' ) {
93 # Override; don't recode since we're using internal data
94 return (string)$this->getVal( $name, $default );
95 }
96
100 public function getQueryValues() {
101 if ( $this->wasPosted ) {
102 return [];
103 } else {
104 return $this->data;
105 }
106 }
107
108 public function getMethod() {
109 return $this->wasPosted ? 'POST' : 'GET';
110 }
111
115 public function wasPosted() {
116 return $this->wasPosted;
117 }
118
119 public function getCookie( $key, $prefix = null, $default = null ) {
120 if ( $prefix === null ) {
121 $cookiePrefix = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CookiePrefix );
122 $prefix = $cookiePrefix;
123 }
124 $name = $prefix . $key;
125 return $this->cookies[$name] ?? $default;
126 }
127
134 public function setCookie( $key, $value, $prefix = null ) {
135 $this->setCookies( [ $key => $value ], $prefix );
136 }
137
143 public function setCookies( $cookies, $prefix = null ) {
144 if ( $prefix === null ) {
145 $cookiePrefix = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CookiePrefix );
146 $prefix = $cookiePrefix;
147 }
148 foreach ( $cookies as $key => $value ) {
149 $name = $prefix . $key;
150 $this->cookies[$name] = $value;
151 }
152 }
153
160 public function setUploadData( $uploadData ) {
161 foreach ( $uploadData as $key => $data ) {
162 $this->setUpload( $key, $data );
163 }
164 }
165
173 public function setUpload( $key, $data ) {
174 if ( $data instanceof WebRequestUpload ) {
175 // cannot reuse WebRequestUpload, because it contains the original web request object
176 $data = [
177 'name' => $data->getName(),
178 'type' => $data->getType(),
179 'tmp_name' => $data->getTempName(),
180 'size' => $data->getSize(),
181 'error' => $data->getError(),
182 ];
183 }
184 // Check if everything is provided
185 if ( !is_array( $data ) ||
186 array_diff( WebRequestUpload::REQUIRED_FILEINFO_KEYS, array_keys( $data ) ) !== []
187 ) {
188 throw new MWException( __METHOD__ . ' got bogus data' );
189 }
190 $this->uploadData[$key] = $data;
191 }
192
199 public function getUpload( $key ) {
200 return new FauxRequestUpload( $this->uploadData, $this, $key );
201 }
202
207 public function setRequestURL( $url ) {
208 $this->requestUrl = $url;
209 }
210
216 public function getRequestURL() {
217 if ( $this->requestUrl === null ) {
218 throw new MWException( 'Request URL not set' );
219 }
220 return $this->requestUrl;
221 }
222
223 public function getProtocol() {
224 return $this->protocol;
225 }
226
231 public function setHeader( $name, $val ) {
232 $this->setHeaders( [ $name => $val ] );
233 }
234
239 public function setHeaders( $headers ) {
240 foreach ( $headers as $name => $val ) {
241 $name = strtoupper( $name );
242 $this->headers[$name] = $val;
243 }
244 }
245
249 public function getSessionArray() {
250 if ( $this->sessionId !== null ) {
251 return iterator_to_array( $this->getSession() );
252 }
253 return null;
254 }
255
256 public function getPostValues() {
257 return $this->wasPosted ? $this->data : [];
258 }
259
264 public function getRawQueryString() {
265 return '';
266 }
267
272 public function getRawPostString() {
273 return '';
274 }
275
280 public function getRawInput() {
281 return '';
282 }
283
288 protected function getRawIP() {
289 return '127.0.0.1';
290 }
291}
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='')
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.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
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.