MediaWiki REL1_41
FauxRequest.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Request;
27
28use InvalidArgumentException;
29use MediaWiki;
33use MWException;
34
42class FauxRequest extends WebRequest {
43 private $wasPosted;
44 private $requestUrl;
45 protected $cookies = [];
47 private $uploadData = [];
48
59 public function __construct( array $data = [], $wasPosted = false,
60 $session = null, $protocol = 'http'
61 ) {
62 $this->requestTime = microtime( true );
63
64 $this->data = $data;
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 InvalidArgumentException( "MediaWiki\Request\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 MediaWiki\Request\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 InvalidArgumentException( __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
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}
290
294class_alias( FauxRequest::class, 'FauxRequest' );
MediaWiki exception.
A class containing constants representing the names of configuration variables.
const CookiePrefix
Name constant for the CookiePrefix setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Object to fake the $_FILES array.
WebRequest clone which takes values from a provided array.
getRawQueryString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
setCookie( $key, $value, $prefix=null)
setUpload( $key, $data)
Set fake upload data for one file with specific key.
getMethod()
Get the HTTP method used for this request.
setCookies( $cookies, $prefix=null)
getText( $name, $default='')
getRawPostString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
getUpload( $key)
Return a MediaWiki\Request\FauxRequestUpload object corresponding to the key.
getProtocol()
Get the current URL protocol (http or https)
getPostValues()
Get the values passed via POST.
__construct(array $data=[], $wasPosted=false, $session=null, $protocol='http')
setUploadData( $uploadData)
Set fake upload data for all files.
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
initHeaders()
Initialise the header list.
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...
getVal( $name, $default=null)
Fetch a text string and partially normalize it.
string[] $headers
Lazy-initialized request headers indexed by upper-case header name.
getSession()
Return the session for this request.
string $protocol
Cached URL protocol.
array $data
The parameters from $_GET, $_POST and the path router.
This serves as the entry point to the MediaWiki session handling system.
Manages data for an authenticated session.
Definition Session.php:50
A helper class for throttling authentication attempts.