MediaWiki  master
FauxRequest.php
Go to the documentation of this file.
1 <?php
27 
35 class 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 }
FauxRequest\setUploadData
setUploadData( $uploadData)
Set fake upload data for all files.
Definition: FauxRequest.php:158
FauxRequest\initHeaders
initHeaders()
Initialise the header list.
Definition: FauxRequest.php:81
WebRequest\$headers
string[] $headers
Lazy-initialized request headers indexed by upper-case header name.
Definition: WebRequest.php:67
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:35
FauxRequest\getRawQueryString
getRawQueryString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:262
FauxRequest\getRawPostString
getRawPostString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:270
WebRequest\$data
array $data
The parameters from $_GET, $_POST and the path router.
Definition: WebRequest.php:48
FauxRequest\wasPosted
wasPosted()
Definition: FauxRequest.php:113
FauxRequest\setHeaders
setHeaders( $headers)
Definition: FauxRequest.php:237
FauxRequest\getRawInput
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:278
FauxRequest\$cookies
$cookies
Definition: FauxRequest.php:38
FauxRequest\setRequestURL
setRequestURL( $url)
Definition: FauxRequest.php:205
WebRequest\$protocol
string $protocol
Cached URL protocol.
Definition: WebRequest.php:103
FauxRequest\getRawIP
getRawIP()
Definition: FauxRequest.php:286
FauxRequest\getProtocol
getProtocol()
Get the current URL protocol (http or https)
Definition: FauxRequest.php:221
MWException
MediaWiki exception.
Definition: MWException.php:29
FauxRequest\setHeader
setHeader( $name, $val)
Definition: FauxRequest.php:229
MediaWiki
A helper class for throttling authentication attempts.
FauxRequest\getUpload
getUpload( $key)
Return a FauxRequestUpload object corresponding to the key.
Definition: FauxRequest.php:197
FauxRequest\setCookie
setCookie( $key, $value, $prefix=null)
Definition: FauxRequest.php:132
WebRequestUpload
Object to access the $_FILES array.
Definition: WebRequestUpload.php:33
FauxRequest\getText
getText( $name, $default='')
Definition: FauxRequest.php:90
FauxRequest\getQueryValues
getQueryValues()
Definition: FauxRequest.php:98
WebRequest\getSession
getSession()
Return the session for this request.
Definition: WebRequest.php:822
FauxRequest\__construct
__construct( $data=[], $wasPosted=false, $session=null, $protocol='http')
Definition: FauxRequest.php:53
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:83
FauxRequest\getCookie
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
Definition: FauxRequest.php:117
FauxRequest\getRequestURL
getRequestURL()
Definition: FauxRequest.php:214
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:43
FauxRequest\getMethod
getMethod()
Get the HTTP method used for this request.
Definition: FauxRequest.php:106
FauxRequest\getSessionArray
getSessionArray()
Definition: FauxRequest.php:247
FauxRequest\$wasPosted
$wasPosted
Definition: FauxRequest.php:36
FauxRequest\getPostValues
getPostValues()
Get the values passed via POST.
Definition: FauxRequest.php:254
WebRequest\getVal
getVal( $name, $default=null)
Fetch a text string and partially normalized it.
Definition: WebRequest.php:505
FauxRequestUpload
Object to fake the $_FILES array.
Definition: FauxRequestUpload.php:29
FauxRequest\setCookies
setCookies( $cookies, $prefix=null)
Definition: FauxRequest.php:141
FauxRequest\$uploadData
array $uploadData
Definition: FauxRequest.php:40
$wgCookiePrefix
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
Definition: DefaultSettings.php:7171
FauxRequest\$requestUrl
$requestUrl
Definition: FauxRequest.php:37
FauxRequest\setUpload
setUpload( $key, $data)
Set fake upload data for one file with specific key.
Definition: FauxRequest.php:171
WebRequestUpload\REQUIRED_FILEINFO_KEYS
const REQUIRED_FILEINFO_KEYS
All keys a fileinfo has to specific to work with this class.
Definition: WebRequestUpload.php:35