MediaWiki REL1_40
FauxRequest.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Request;
27
28use MediaWiki;
32use MWException;
33use WebRequest;
34
42class FauxRequest extends WebRequest {
43 private $wasPosted = false;
44 private $requestUrl;
45 protected $cookies = [];
47 private $uploadData = [];
48
60 public function __construct( $data = [], $wasPosted = false,
61 $session = null, $protocol = 'http'
62 ) {
63 $this->requestTime = microtime( true );
64
65 if ( is_array( $data ) ) {
66 $this->data = $data;
67 } else {
68 throw new MWException( "MediaWiki\Request\FauxRequest() got bogus data" );
69 }
70 $this->wasPosted = $wasPosted;
71 if ( $session instanceof MediaWiki\Session\Session ) {
72 $this->sessionId = $session->getSessionId();
73 } elseif ( is_array( $session ) ) {
74 $mwsession = SessionManager::singleton()->getEmptySession( $this );
75 $this->sessionId = $mwsession->getSessionId();
76 foreach ( $session as $key => $value ) {
77 $mwsession->set( $key, $value );
78 }
79 } elseif ( $session !== null ) {
80 throw new MWException( "MediaWiki\Request\FauxRequest() got bogus session" );
81 }
82 $this->protocol = $protocol;
83 }
84
88 protected function initHeaders() {
89 // Nothing to init
90 }
91
97 public function getText( $name, $default = '' ) {
98 # Override; don't recode since we're using internal data
99 return (string)$this->getVal( $name, $default );
100 }
101
105 public function getQueryValues() {
106 if ( $this->wasPosted ) {
107 return [];
108 } else {
109 return $this->data;
110 }
111 }
112
113 public function getMethod() {
114 return $this->wasPosted ? 'POST' : 'GET';
115 }
116
120 public function wasPosted() {
121 return $this->wasPosted;
122 }
123
124 public function getCookie( $key, $prefix = null, $default = null ) {
125 if ( $prefix === null ) {
126 $cookiePrefix = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CookiePrefix );
127 $prefix = $cookiePrefix;
128 }
129 $name = $prefix . $key;
130 return $this->cookies[$name] ?? $default;
131 }
132
139 public function setCookie( $key, $value, $prefix = null ) {
140 $this->setCookies( [ $key => $value ], $prefix );
141 }
142
148 public function setCookies( $cookies, $prefix = null ) {
149 if ( $prefix === null ) {
150 $cookiePrefix = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CookiePrefix );
151 $prefix = $cookiePrefix;
152 }
153 foreach ( $cookies as $key => $value ) {
154 $name = $prefix . $key;
155 $this->cookies[$name] = $value;
156 }
157 }
158
165 public function setUploadData( $uploadData ) {
166 foreach ( $uploadData as $key => $data ) {
167 $this->setUpload( $key, $data );
168 }
169 }
170
178 public function setUpload( $key, $data ) {
179 if ( $data instanceof WebRequestUpload ) {
180 // cannot reuse MediaWiki\Request\WebRequestUpload, because it contains the original web request object
181 $data = [
182 'name' => $data->getName(),
183 'type' => $data->getType(),
184 'tmp_name' => $data->getTempName(),
185 'size' => $data->getSize(),
186 'error' => $data->getError(),
187 ];
188 }
189 // Check if everything is provided
190 if ( !is_array( $data ) ||
191 array_diff( WebRequestUpload::REQUIRED_FILEINFO_KEYS, array_keys( $data ) ) !== []
192 ) {
193 throw new MWException( __METHOD__ . ' got bogus data' );
194 }
195 $this->uploadData[$key] = $data;
196 }
197
204 public function getUpload( $key ) {
205 return new FauxRequestUpload( $this->uploadData, $this, $key );
206 }
207
212 public function setRequestURL( $url ) {
213 $this->requestUrl = $url;
214 }
215
221 public function getRequestURL() {
222 if ( $this->requestUrl === null ) {
223 throw new MWException( 'Request URL not set' );
224 }
225 return $this->requestUrl;
226 }
227
228 public function getProtocol() {
229 return $this->protocol;
230 }
231
236 public function setHeader( $name, $val ) {
237 $this->setHeaders( [ $name => $val ] );
238 }
239
244 public function setHeaders( $headers ) {
245 foreach ( $headers as $name => $val ) {
246 $name = strtoupper( $name );
247 $this->headers[$name] = $val;
248 }
249 }
250
254 public function getSessionArray() {
255 if ( $this->sessionId !== null ) {
256 return iterator_to_array( $this->getSession() );
257 }
258 return null;
259 }
260
261 public function getPostValues() {
262 return $this->wasPosted ? $this->data : [];
263 }
264
269 public function getRawQueryString() {
270 return '';
271 }
272
277 public function getRawPostString() {
278 return '';
279 }
280
285 public function getRawInput() {
286 return '';
287 }
288
293 protected function getRawIP() {
294 return '127.0.0.1';
295 }
296}
297
298class_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.
setUploadData( $uploadData)
Set fake upload data for all files.
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
__construct( $data=[], $wasPosted=false, $session=null, $protocol='http')
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.
This serves as the entry point to the MediaWiki session handling system.
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.