MediaWiki  1.29.2
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 getValues() {
93  return $this->data;
94  }
95 
99  public function getQueryValues() {
100  if ( $this->wasPosted ) {
101  return [];
102  } else {
103  return $this->data;
104  }
105  }
106 
107  public function getMethod() {
108  return $this->wasPosted ? 'POST' : 'GET';
109  }
110 
114  public function wasPosted() {
115  return $this->wasPosted;
116  }
117 
118  public function getCookie( $key, $prefix = null, $default = null ) {
119  if ( $prefix === null ) {
121  $prefix = $wgCookiePrefix;
122  }
123  $name = $prefix . $key;
124  return isset( $this->cookies[$name] ) ? $this->cookies[$name] : $default;
125  }
126 
133  public function setCookie( $key, $value, $prefix = null ) {
134  $this->setCookies( [ $key => $value ], $prefix );
135  }
136 
142  public function setCookies( $cookies, $prefix = null ) {
143  if ( $prefix === null ) {
145  $prefix = $wgCookiePrefix;
146  }
147  foreach ( $cookies as $key => $value ) {
148  $name = $prefix . $key;
149  $this->cookies[$name] = $value;
150  }
151  }
152 
156  public function setRequestURL( $url ) {
157  $this->requestUrl = $url;
158  }
159 
164  public function getRequestURL() {
165  if ( $this->requestUrl === null ) {
166  throw new MWException( 'Request URL not set' );
167  }
168  return $this->requestUrl;
169  }
170 
171  public function getProtocol() {
172  return $this->protocol;
173  }
174 
179  public function setHeader( $name, $val ) {
180  $this->setHeaders( [ $name => $val ] );
181  }
182 
187  public function setHeaders( $headers ) {
188  foreach ( $headers as $name => $val ) {
189  $name = strtoupper( $name );
190  $this->headers[$name] = $val;
191  }
192  }
193 
197  public function getSessionArray() {
198  if ( $this->sessionId !== null ) {
199  return iterator_to_array( $this->getSession() );
200  }
201  return null;
202  }
203 
208  public function getRawQueryString() {
209  return '';
210  }
211 
216  public function getRawPostString() {
217  return '';
218  }
219 
224  public function getRawInput() {
225  return '';
226  }
227 
233  public function checkUrlExtension( $extWhitelist = [] ) {
234  return true;
235  }
236 
241  protected function getRawIP() {
242  return '127.0.0.1';
243  }
244 }
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
cookies
it sets a lot of them automatically from cookies
Definition: design.txt:93
$wgCookiePrefix
if( $wgLocalInterwiki) if( $wgSharedPrefix===false) if( $wgSharedSchema===false) if(! $wgCookiePrefix) $wgCookiePrefix
Definition: Setup.php:326
FauxRequest\getRawQueryString
getRawQueryString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:208
FauxRequest\getRawPostString
getRawPostString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:216
FauxRequest\wasPosted
wasPosted()
Definition: FauxRequest.php:114
FauxRequest\setHeaders
setHeaders( $headers)
Definition: FauxRequest.php:187
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
FauxRequest\getRawInput
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
Definition: FauxRequest.php:224
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
FauxRequest\$cookies
$cookies
Definition: FauxRequest.php:36
WebRequest\$headers
$headers
Definition: WebRequest.php:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
FauxRequest\setRequestURL
setRequestURL( $url)
Definition: FauxRequest.php:156
WebRequest\$protocol
string $protocol
Cached URL protocol.
Definition: WebRequest.php:75
FauxRequest\getRawIP
getRawIP()
Definition: FauxRequest.php:241
FauxRequest\getProtocol
getProtocol()
Get the current URL protocol (http or https)
Definition: FauxRequest.php:171
MWException
MediaWiki exception.
Definition: MWException.php:26
FauxRequest\setHeader
setHeader( $name, $val)
Definition: FauxRequest.php:179
MediaWiki
A helper class for throttling authentication attempts.
FauxRequest\setCookie
setCookie( $key, $value, $prefix=null)
Definition: FauxRequest.php:133
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
FauxRequest\getText
getText( $name, $default='')
Definition: FauxRequest.php:84
FauxRequest\getQueryValues
getQueryValues()
Definition: FauxRequest.php:99
WebRequest\$data
$data
Definition: WebRequest.php:39
WebRequest\getSession
getSession()
Return the session for this request.
Definition: WebRequest.php:727
$value
$value
Definition: styleTest.css.php:45
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:49
FauxRequest\getCookie
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
Definition: FauxRequest.php:118
FauxRequest\getRequestURL
getRequestURL()
Definition: FauxRequest.php:164
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:38
FauxRequest\getMethod
getMethod()
Get the HTTP method used for this request.
Definition: FauxRequest.php:107
FauxRequest\getSessionArray
getSessionArray()
Definition: FauxRequest.php:197
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:437
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
FauxRequest\setCookies
setCookies( $cookies, $prefix=null)
Definition: FauxRequest.php:142
FauxRequest\getValues
getValues()
Definition: FauxRequest.php:92
FauxRequest\checkUrlExtension
checkUrlExtension( $extWhitelist=[])
Definition: FauxRequest.php:233
headers
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add headers
Definition: design.txt:12
FauxRequest\$requestUrl
$requestUrl
Definition: FauxRequest.php:35
data
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6