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 = [];
39 
51  public function __construct( $data = [], $wasPosted = false,
52  $session = null, $protocol = 'http'
53  ) {
54  $this->requestTime = microtime( true );
55 
56  if ( is_array( $data ) ) {
57  $this->data = $data;
58  } else {
59  throw new MWException( "FauxRequest() got bogus data" );
60  }
61  $this->wasPosted = $wasPosted;
62  if ( $session instanceof MediaWiki\Session\Session ) {
63  $this->sessionId = $session->getSessionId();
64  } elseif ( is_array( $session ) ) {
65  $mwsession = SessionManager::singleton()->getEmptySession( $this );
66  $this->sessionId = $mwsession->getSessionId();
67  foreach ( $session as $key => $value ) {
68  $mwsession->set( $key, $value );
69  }
70  } elseif ( $session !== null ) {
71  throw new MWException( "FauxRequest() got bogus session" );
72  }
73  $this->protocol = $protocol;
74  }
75 
79  protected function initHeaders() {
80  // Nothing to init
81  }
82 
88  public function getText( $name, $default = '' ) {
89  # Override; don't recode since we're using internal data
90  return (string)$this->getVal( $name, $default );
91  }
92 
96  public function getQueryValues() {
97  if ( $this->wasPosted ) {
98  return [];
99  } else {
100  return $this->data;
101  }
102  }
103 
104  public function getMethod() {
105  return $this->wasPosted ? 'POST' : 'GET';
106  }
107 
111  public function wasPosted() {
112  return $this->wasPosted;
113  }
114 
115  public function getCookie( $key, $prefix = null, $default = null ) {
116  if ( $prefix === null ) {
117  global $wgCookiePrefix;
118  $prefix = $wgCookiePrefix;
119  }
120  $name = $prefix . $key;
121  return $this->cookies[$name] ?? $default;
122  }
123 
130  public function setCookie( $key, $value, $prefix = null ) {
131  $this->setCookies( [ $key => $value ], $prefix );
132  }
133 
139  public function setCookies( $cookies, $prefix = null ) {
140  if ( $prefix === null ) {
141  global $wgCookiePrefix;
142  $prefix = $wgCookiePrefix;
143  }
144  foreach ( $cookies as $key => $value ) {
145  $name = $prefix . $key;
146  $this->cookies[$name] = $value;
147  }
148  }
149 
154  public function setRequestURL( $url ) {
155  $this->requestUrl = $url;
156  }
157 
163  public function getRequestURL() {
164  if ( $this->requestUrl === null ) {
165  throw new MWException( 'Request URL not set' );
166  }
167  return $this->requestUrl;
168  }
169 
170  public function getProtocol() {
171  return $this->protocol;
172  }
173 
178  public function setHeader( $name, $val ) {
179  $this->setHeaders( [ $name => $val ] );
180  }
181 
186  public function setHeaders( $headers ) {
187  foreach ( $headers as $name => $val ) {
188  $name = strtoupper( $name );
189  $this->headers[$name] = $val;
190  }
191  }
192 
196  public function getSessionArray() {
197  if ( $this->sessionId !== null ) {
198  return iterator_to_array( $this->getSession() );
199  }
200  return null;
201  }
202 
207  public function getRawQueryString() {
208  return '';
209  }
210 
215  public function getRawPostString() {
216  return '';
217  }
218 
223  public function getRawInput() {
224  return '';
225  }
226 
231  protected function getRawIP() {
232  return '127.0.0.1';
233  }
234 }
FauxRequest\initHeaders
initHeaders()
Initialise the header list.
Definition: FauxRequest.php:79
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:35
WebRequest\$headers
array $headers
Lazy-initialized request headers indexed by upper-case header name.
Definition: WebRequest.php:65
FauxRequest\getRawQueryString
getRawQueryString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:207
FauxRequest\getRawPostString
getRawPostString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:215
WebRequest\$data
array $data
The parameters from $_GET, $_POST and the path router.
Definition: WebRequest.php:47
FauxRequest\wasPosted
wasPosted()
Definition: FauxRequest.php:111
FauxRequest\setHeaders
setHeaders( $headers)
Definition: FauxRequest.php:186
FauxRequest\getRawInput
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:223
FauxRequest\$cookies
$cookies
Definition: FauxRequest.php:38
FauxRequest\setRequestURL
setRequestURL( $url)
Definition: FauxRequest.php:154
WebRequest\$protocol
string $protocol
Cached URL protocol.
Definition: WebRequest.php:101
FauxRequest\getRawIP
getRawIP()
Definition: FauxRequest.php:231
FauxRequest\getProtocol
getProtocol()
Get the current URL protocol (http or https)
Definition: FauxRequest.php:170
MWException
MediaWiki exception.
Definition: MWException.php:29
FauxRequest\setHeader
setHeader( $name, $val)
Definition: FauxRequest.php:178
MediaWiki
A helper class for throttling authentication attempts.
FauxRequest\setCookie
setCookie( $key, $value, $prefix=null)
Definition: FauxRequest.php:130
FauxRequest\getText
getText( $name, $default='')
Definition: FauxRequest.php:88
FauxRequest\getQueryValues
getQueryValues()
Definition: FauxRequest.php:96
WebRequest\getSession
getSession()
Return the session for this request.
Definition: WebRequest.php:818
FauxRequest\__construct
__construct( $data=[], $wasPosted=false, $session=null, $protocol='http')
Stable to call.
Definition: FauxRequest.php:51
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:53
FauxRequest\getCookie
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
Definition: FauxRequest.php:115
FauxRequest\getRequestURL
getRequestURL()
Definition: FauxRequest.php:163
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:42
FauxRequest\getMethod
getMethod()
Get the HTTP method used for this request.
Definition: FauxRequest.php:104
FauxRequest\getSessionArray
getSessionArray()
Definition: FauxRequest.php:196
FauxRequest\$wasPosted
$wasPosted
Definition: FauxRequest.php:36
WebRequest\getVal
getVal( $name, $default=null)
Fetch a scalar from the input or return $default if it's not set.
Definition: WebRequest.php:503
FauxRequest\setCookies
setCookies( $cookies, $prefix=null)
Definition: FauxRequest.php:139
$wgCookiePrefix
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
Definition: DefaultSettings.php:6492
FauxRequest\$requestUrl
$requestUrl
Definition: FauxRequest.php:37