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 
92  public function getQueryValues() {
93  if ( $this->wasPosted ) {
94  return [];
95  } else {
96  return $this->data;
97  }
98  }
99 
100  public function getMethod() {
101  return $this->wasPosted ? 'POST' : 'GET';
102  }
103 
107  public function wasPosted() {
108  return $this->wasPosted;
109  }
110 
111  public function getCookie( $key, $prefix = null, $default = null ) {
112  if ( $prefix === null ) {
113  global $wgCookiePrefix;
114  $prefix = $wgCookiePrefix;
115  }
116  $name = $prefix . $key;
117  return $this->cookies[$name] ?? $default;
118  }
119 
126  public function setCookie( $key, $value, $prefix = null ) {
127  $this->setCookies( [ $key => $value ], $prefix );
128  }
129 
135  public function setCookies( $cookies, $prefix = null ) {
136  if ( $prefix === null ) {
137  global $wgCookiePrefix;
138  $prefix = $wgCookiePrefix;
139  }
140  foreach ( $cookies as $key => $value ) {
141  $name = $prefix . $key;
142  $this->cookies[$name] = $value;
143  }
144  }
145 
150  public function setRequestURL( $url ) {
151  $this->requestUrl = $url;
152  }
153 
159  public function getRequestURL() {
160  if ( $this->requestUrl === null ) {
161  throw new MWException( 'Request URL not set' );
162  }
163  return $this->requestUrl;
164  }
165 
166  public function getProtocol() {
167  return $this->protocol;
168  }
169 
174  public function setHeader( $name, $val ) {
175  $this->setHeaders( [ $name => $val ] );
176  }
177 
182  public function setHeaders( $headers ) {
183  foreach ( $headers as $name => $val ) {
184  $name = strtoupper( $name );
185  $this->headers[$name] = $val;
186  }
187  }
188 
192  public function getSessionArray() {
193  if ( $this->sessionId !== null ) {
194  return iterator_to_array( $this->getSession() );
195  }
196  return null;
197  }
198 
203  public function getRawQueryString() {
204  return '';
205  }
206 
211  public function getRawPostString() {
212  return '';
213  }
214 
219  public function getRawInput() {
220  return '';
221  }
222 
228  public function checkUrlExtension( $extWhitelist = [] ) {
229  return true;
230  }
231 
236  protected function getRawIP() {
237  return '127.0.0.1';
238  }
239 }
getVal( $name, $default=null)
Fetch a scalar from the input or return $default if it&#39;s not set.
Definition: WebRequest.php:489
$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:804
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=[])