MediaWiki  master
FauxRequest.php
Go to the documentation of this file.
1 <?php
27 
33 class FauxRequest extends WebRequest {
34  private $wasPosted = false;
35  private $requestUrl;
36  protected $cookies = [];
37 
47  public function __construct( $data = [], $wasPosted = false,
48  $session = null, $protocol = 'http'
49  ) {
50  $this->requestTime = microtime( true );
51 
52  if ( is_array( $data ) ) {
53  $this->data = $data;
54  } else {
55  throw new MWException( "FauxRequest() got bogus data" );
56  }
57  $this->wasPosted = $wasPosted;
58  if ( $session instanceof MediaWiki\Session\Session ) {
59  $this->sessionId = $session->getSessionId();
60  } elseif ( is_array( $session ) ) {
61  $mwsession = SessionManager::singleton()->getEmptySession( $this );
62  $this->sessionId = $mwsession->getSessionId();
63  foreach ( $session as $key => $value ) {
64  $mwsession->set( $key, $value );
65  }
66  } elseif ( $session !== null ) {
67  throw new MWException( "FauxRequest() got bogus session" );
68  }
69  $this->protocol = $protocol;
70  }
71 
75  protected function initHeaders() {
76  // Nothing to init
77  }
78 
84  public function getText( $name, $default = '' ) {
85  # Override; don't recode since we're using internal data
86  return (string)$this->getVal( $name, $default );
87  }
88 
93  public function getValues() {
94  return $this->data;
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  global $wgCookiePrefix;
122  $prefix = $wgCookiePrefix;
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  global $wgCookiePrefix;
146  $prefix = $wgCookiePrefix;
147  }
148  foreach ( $cookies as $key => $value ) {
149  $name = $prefix . $key;
150  $this->cookies[$name] = $value;
151  }
152  }
153 
158  public function setRequestURL( $url ) {
159  $this->requestUrl = $url;
160  }
161 
167  public function getRequestURL() {
168  if ( $this->requestUrl === null ) {
169  throw new MWException( 'Request URL not set' );
170  }
171  return $this->requestUrl;
172  }
173 
174  public function getProtocol() {
175  return $this->protocol;
176  }
177 
182  public function setHeader( $name, $val ) {
183  $this->setHeaders( [ $name => $val ] );
184  }
185 
190  public function setHeaders( $headers ) {
191  foreach ( $headers as $name => $val ) {
192  $name = strtoupper( $name );
193  $this->headers[$name] = $val;
194  }
195  }
196 
200  public function getSessionArray() {
201  if ( $this->sessionId !== null ) {
202  return iterator_to_array( $this->getSession() );
203  }
204  return null;
205  }
206 
211  public function getRawQueryString() {
212  return '';
213  }
214 
219  public function getRawPostString() {
220  return '';
221  }
222 
227  public function getRawInput() {
228  return '';
229  }
230 
236  public function checkUrlExtension( $extWhitelist = [] ) {
237  return true;
238  }
239 
244  protected function getRawIP() {
245  return '127.0.0.1';
246  }
247 }
getVal( $name, $default=null)
Fetch a scalar from the input or return $default if it&#39;s not set.
Definition: WebRequest.php:483
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
setHeaders( $headers)
getRawInput()
FauxRequests shouldn&#39;t depend on raw request data (but that could be implemented here) ...
getRawPostString()
FauxRequests shouldn&#39;t depend on raw request data (but that could be implemented here) ...
string $protocol
Cached URL protocol.
Definition: WebRequest.php:101
getSession()
Return the session for this request.
Definition: WebRequest.php:798
getRawQueryString()
FauxRequests shouldn&#39;t depend on raw request data (but that could be implemented here) ...
A helper class for throttling authentication attempts.
getText( $name, $default='')
Definition: FauxRequest.php:84
setHeader( $name, $val)
array $headers
Lazy-initialized request headers indexed by upper-case header name.
Definition: WebRequest.php:65
setRequestURL( $url)
getCookie( $key, $prefix=null, $default=null)
setCookie( $key, $value, $prefix=null)
array $data
The parameters from $_GET, $_POST and the path router.
Definition: WebRequest.php:47
setCookies( $cookies, $prefix=null)
__construct( $data=[], $wasPosted=false, $session=null, $protocol='http')
Definition: FauxRequest.php:47
initHeaders()
Initialise the header list.
Definition: FauxRequest.php:75
checkUrlExtension( $extWhitelist=[])