MediaWiki  1.34.0
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 }
FauxRequest\initHeaders
initHeaders()
Initialise the header list.
Definition: FauxRequest.php:75
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
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:203
FauxRequest\getRawPostString
getRawPostString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:211
WebRequest\$data
array $data
The parameters from $_GET, $_POST and the path router.
Definition: WebRequest.php:47
FauxRequest\wasPosted
wasPosted()
Definition: FauxRequest.php:107
FauxRequest\setHeaders
setHeaders( $headers)
Definition: FauxRequest.php:182
FauxRequest\getRawInput
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:219
FauxRequest\$cookies
$cookies
Definition: FauxRequest.php:36
FauxRequest\setRequestURL
setRequestURL( $url)
Definition: FauxRequest.php:150
WebRequest\$protocol
string $protocol
Cached URL protocol.
Definition: WebRequest.php:101
FauxRequest\getRawIP
getRawIP()
Definition: FauxRequest.php:236
FauxRequest\getProtocol
getProtocol()
Get the current URL protocol (http or https)
Definition: FauxRequest.php:166
MWException
MediaWiki exception.
Definition: MWException.php:26
FauxRequest\setHeader
setHeader( $name, $val)
Definition: FauxRequest.php:174
MediaWiki
This class serves as a utility class for this extension.
FauxRequest\setCookie
setCookie( $key, $value, $prefix=null)
Definition: FauxRequest.php:126
FauxRequest\getText
getText( $name, $default='')
Definition: FauxRequest.php:84
FauxRequest\getQueryValues
getQueryValues()
Definition: FauxRequest.php:92
WebRequest\getSession
getSession()
Return the session for this request.
Definition: WebRequest.php:798
FauxRequest\__construct
__construct( $data=[], $wasPosted=false, $session=null, $protocol='http')
Definition: FauxRequest.php:47
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:50
FauxRequest\getCookie
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
Definition: FauxRequest.php:111
FauxRequest\getRequestURL
getRequestURL()
Definition: FauxRequest.php:159
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:100
FauxRequest\getSessionArray
getSessionArray()
Definition: FauxRequest.php:192
FauxRequest\$wasPosted
$wasPosted
Definition: FauxRequest.php:34
WebRequest\getVal
getVal( $name, $default=null)
Fetch a scalar from the input or return $default if it's not set.
Definition: WebRequest.php:483
FauxRequest\setCookies
setCookies( $cookies, $prefix=null)
Definition: FauxRequest.php:135
FauxRequest\checkUrlExtension
checkUrlExtension( $extWhitelist=[])
Definition: FauxRequest.php:228
$wgCookiePrefix
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
Definition: DefaultSettings.php:6054
FauxRequest\$requestUrl
$requestUrl
Definition: FauxRequest.php:35