MediaWiki REL1_31
FauxRequest.php
Go to the documentation of this file.
1<?php
27
33class 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 ) {
120 global $wgCookiePrefix;
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 ) {
144 global $wgCookiePrefix;
145 $prefix = $wgCookiePrefix;
146 }
147 foreach ( $cookies as $key => $value ) {
148 $name = $prefix . $key;
149 $this->cookies[$name] = $value;
150 }
151 }
152
157 public function setRequestURL( $url ) {
158 $this->requestUrl = $url;
159 }
160
166 public function getRequestURL() {
167 if ( $this->requestUrl === null ) {
168 throw new MWException( 'Request URL not set' );
169 }
170 return $this->requestUrl;
171 }
172
173 public function getProtocol() {
174 return $this->protocol;
175 }
176
181 public function setHeader( $name, $val ) {
182 $this->setHeaders( [ $name => $val ] );
183 }
184
189 public function setHeaders( $headers ) {
190 foreach ( $headers as $name => $val ) {
191 $name = strtoupper( $name );
192 $this->headers[$name] = $val;
193 }
194 }
195
199 public function getSessionArray() {
200 if ( $this->sessionId !== null ) {
201 return iterator_to_array( $this->getSession() );
202 }
203 return null;
204 }
205
210 public function getRawQueryString() {
211 return '';
212 }
213
218 public function getRawPostString() {
219 return '';
220 }
221
226 public function getRawInput() {
227 return '';
228 }
229
235 public function checkUrlExtension( $extWhitelist = [] ) {
236 return true;
237 }
238
243 protected function getRawIP() {
244 return '127.0.0.1';
245 }
246}
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
WebRequest clone which takes values from a provided array.
setCookie( $key, $value, $prefix=null)
setRequestURL( $url)
getText( $name, $default='')
getRawPostString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
initHeaders()
Initialise the header list.
checkUrlExtension( $extWhitelist=[])
getRawInput()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
setCookies( $cookies, $prefix=null)
getProtocol()
Get the current URL protocol (http or https)
getMethod()
Get the HTTP method used for this request.
setHeaders( $headers)
__construct( $data=[], $wasPosted=false, $session=null, $protocol='http')
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
setHeader( $name, $val)
getRawQueryString()
FauxRequests shouldn't depend on raw request data (but that could be implemented here)
MediaWiki exception.
This serves as the entry point to the MediaWiki session handling system.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
string $protocol
Cached URL protocol.
getSession()
Return the session for this request.
getVal( $name, $default=null)
Fetch a scalar from the input or return $default if it's not set.
it sets a lot of them automatically from cookies
Definition design.txt:93
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
A helper class for throttling authentication attempts.