1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
<?php
/**
* Deal with importing all those nasty globals and things
*
* Copyright © 2003 Brooke Vibber <bvibber@wikimedia.org>
* https://www.mediawiki.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Request;
use InvalidArgumentException;
use MediaWiki;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Session\SessionManager;
use MWException;
/**
* WebRequest clone which takes values from a provided array.
*
* @newable
*
* @ingroup HTTP
*/
class FauxRequest extends WebRequest {
private bool $wasPosted;
private ?string $requestUrl = null;
private array $serverInfo;
protected array $cookies = [];
private array $uploadData = [];
/**
* @stable to call
*
* @param array $data Array of *non*-urlencoded key => value pairs, the
* fake GET/POST values
* @param bool $wasPosted Whether to treat the data as POST
* @param MediaWiki\Session\Session|array|null $session Session, session
* data array, or null
* @param string $protocol 'http' or 'https'
*/
public function __construct( array $data = [], $wasPosted = false,
$session = null, $protocol = 'http'
) {
$this->requestTime = microtime( true );
$this->serverInfo = $_SERVER;
$this->data = $data;
$this->wasPosted = $wasPosted;
if ( $session instanceof MediaWiki\Session\Session ) {
$this->sessionId = $session->getSessionId();
} elseif ( is_array( $session ) ) {
$mwsession = SessionManager::singleton()->getEmptySession( $this );
$this->sessionId = $mwsession->getSessionId();
foreach ( $session as $key => $value ) {
$mwsession->set( $key, $value );
}
} elseif ( $session !== null ) {
throw new InvalidArgumentException( "MediaWiki\Request\FauxRequest() got bogus session" );
}
$this->protocol = $protocol;
}
public function response(): FauxResponse {
/* Lazy initialization of response object for this request */
if ( !$this->response ) {
$this->response = new FauxResponse();
}
return $this->response;
}
/**
* Initialise the header list
*/
protected function initHeaders() {
// Nothing to init
}
/**
* @param string $name
* @param string $default
* @return string
*/
public function getText( $name, $default = '' ) {
# Override; don't recode since we're using internal data
return (string)$this->getVal( $name, $default );
}
/**
* @return array
*/
public function getQueryValues() {
if ( $this->wasPosted ) {
return [];
} else {
return $this->data;
}
}
public function getQueryValuesOnly() {
return $this->getQueryValues();
}
public function getMethod() {
return $this->wasPosted ? 'POST' : 'GET';
}
/**
* @return bool
*/
public function wasPosted() {
return $this->wasPosted;
}
public function getCookie( $key, $prefix = null, $default = null ) {
if ( $prefix === null ) {
$cookiePrefix = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CookiePrefix );
$prefix = $cookiePrefix;
}
$name = $prefix . $key;
return $this->cookies[$name] ?? $default;
}
/**
* @param string $key Unprefixed name of the cookie to set
* @param string|null $value Value of the cookie to set
* @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
* @since 1.26
*/
public function setCookie( $key, $value, $prefix = null ) {
$this->setCookies( [ $key => $value ], $prefix );
}
/**
* @param array $cookies
* @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
* @since 1.26
*/
public function setCookies( $cookies, $prefix = null ) {
if ( $prefix === null ) {
$cookiePrefix = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CookiePrefix );
$prefix = $cookiePrefix;
}
foreach ( $cookies as $key => $value ) {
$name = $prefix . $key;
$this->cookies[$name] = $value;
}
}
/**
* Set fake upload data for all files
*
* @param (array|WebRequestUpload)[] $uploadData
* @since 1.37
*/
public function setUploadData( $uploadData ) {
foreach ( $uploadData as $key => $data ) {
$this->setUpload( $key, $data );
}
}
/**
* Set fake upload data for one file with specific key
*
* @param string $key
* @param array|WebRequestUpload $data
* @since 1.37
*/
public function setUpload( $key, $data ) {
if ( $data instanceof WebRequestUpload ) {
// cannot reuse MediaWiki\Request\WebRequestUpload, because it contains the original web request object
$data = [
'name' => $data->getName(),
'type' => $data->getType(),
'tmp_name' => $data->getTempName(),
'size' => $data->getSize(),
'error' => $data->getError(),
];
}
// Check if everything is provided
if ( !is_array( $data ) ||
array_diff( WebRequestUpload::REQUIRED_FILEINFO_KEYS, array_keys( $data ) ) !== []
) {
throw new InvalidArgumentException( __METHOD__ . ' got bogus data' );
}
$this->uploadData[$key] = $data;
}
/**
* Return a MediaWiki\Request\FauxRequestUpload object corresponding to the key
*
* @param string $key
* @return FauxRequestUpload
*/
public function getUpload( $key ) {
return new FauxRequestUpload( $this->uploadData, $this, $key );
}
/**
* @param string $url
*
* @since 1.25
*/
public function setRequestURL( string $url ) {
$this->requestUrl = $url;
if ( preg_match( '@^(.*)://@', $url, $m ) ) {
$this->protocol = $m[1];
}
}
/**
* @since 1.42
* @return bool
*/
public function hasRequestURL(): bool {
return $this->requestUrl !== null;
}
protected function getServerInfo( $name, $default = null ): ?string {
return $this->serverInfo[$name] ?? $default;
}
/**
* @see $_SERVER
* @param array $info
*/
public function setServerInfo( array $info ) {
$this->serverInfo = $info;
}
/**
* @return string
*/
public function getRequestURL() {
if ( $this->requestUrl === null ) {
throw new MWException( 'Request URL not set' );
}
return $this->requestUrl;
}
public function getProtocol() {
return $this->protocol;
}
/**
* @param string $name
* @param string $val
*/
public function setHeader( $name, $val ) {
$this->setHeaders( [ $name => $val ] );
}
/**
* @param array $headers
* @since 1.26
*/
public function setHeaders( $headers ) {
foreach ( $headers as $name => $val ) {
$name = strtoupper( $name );
$this->headers[$name] = $val;
}
}
/**
* @return array|null
*/
public function getSessionArray() {
if ( $this->sessionId !== null ) {
return iterator_to_array( $this->getSession() );
}
return null;
}
public function getPostValues() {
return $this->wasPosted ? $this->data : [];
}
/**
* FauxRequests shouldn't depend on raw request data (but that could be implemented here)
* @return string
*/
public function getRawQueryString() {
return '';
}
/**
* FauxRequests shouldn't depend on raw request data (but that could be implemented here)
* @return string
*/
public function getRawPostString() {
return '';
}
/**
* FauxRequests shouldn't depend on raw request data (but that could be implemented here)
* @return string
*/
public function getRawInput() {
return '';
}
/**
* @codeCoverageIgnore
* @return string
*/
protected function getRawIP() {
return '127.0.0.1';
}
}
/** @deprecated class alias since 1.40 */
class_alias( FauxRequest::class, 'FauxRequest' );
|