MediaWiki fundraising/REL1_35
FauxRequest.php
Go to the documentation of this file.
1<?php
27
35class FauxRequest extends WebRequest {
36 private $wasPosted = false;
37 private $requestUrl;
38 protected $cookies = [];
39
51 public function __construct( $data = [], $wasPosted = false,
52 $session = null, $protocol = 'http'
53 ) {
54 $this->requestTime = microtime( true );
55
56 if ( is_array( $data ) ) {
57 $this->data = $data;
58 } else {
59 throw new MWException( "FauxRequest() got bogus data" );
60 }
61 $this->wasPosted = $wasPosted;
62 if ( $session instanceof MediaWiki\Session\Session ) {
63 $this->sessionId = $session->getSessionId();
64 } elseif ( is_array( $session ) ) {
65 $mwsession = SessionManager::singleton()->getEmptySession( $this );
66 $this->sessionId = $mwsession->getSessionId();
67 foreach ( $session as $key => $value ) {
68 $mwsession->set( $key, $value );
69 }
70 } elseif ( $session !== null ) {
71 throw new MWException( "FauxRequest() got bogus session" );
72 }
73 $this->protocol = $protocol;
74 }
75
79 protected function initHeaders() {
80 // Nothing to init
81 }
82
88 public function getText( $name, $default = '' ) {
89 # Override; don't recode since we're using internal data
90 return (string)$this->getVal( $name, $default );
91 }
92
96 public function getQueryValues() {
97 if ( $this->wasPosted ) {
98 return [];
99 } else {
100 return $this->data;
101 }
102 }
103
104 public function getMethod() {
105 return $this->wasPosted ? 'POST' : 'GET';
106 }
107
111 public function wasPosted() {
112 return $this->wasPosted;
113 }
114
115 public function getCookie( $key, $prefix = null, $default = null ) {
116 if ( $prefix === null ) {
117 global $wgCookiePrefix;
118 $prefix = $wgCookiePrefix;
119 }
120 $name = $prefix . $key;
121 return $this->cookies[$name] ?? $default;
122 }
123
130 public function setCookie( $key, $value, $prefix = null ) {
131 $this->setCookies( [ $key => $value ], $prefix );
132 }
133
139 public function setCookies( $cookies, $prefix = null ) {
140 if ( $prefix === null ) {
141 global $wgCookiePrefix;
142 $prefix = $wgCookiePrefix;
143 }
144 foreach ( $cookies as $key => $value ) {
145 $name = $prefix . $key;
146 $this->cookies[$name] = $value;
147 }
148 }
149
154 public function setRequestURL( $url ) {
155 $this->requestUrl = $url;
156 }
157
163 public function getRequestURL() {
164 if ( $this->requestUrl === null ) {
165 throw new MWException( 'Request URL not set' );
166 }
167 return $this->requestUrl;
168 }
169
170 public function getProtocol() {
171 return $this->protocol;
172 }
173
178 public function setHeader( $name, $val ) {
179 $this->setHeaders( [ $name => $val ] );
180 }
181
186 public function setHeaders( $headers ) {
187 foreach ( $headers as $name => $val ) {
188 $name = strtoupper( $name );
189 $this->headers[$name] = $val;
190 }
191 }
192
196 public function getSessionArray() {
197 if ( $this->sessionId !== null ) {
198 return iterator_to_array( $this->getSession() );
199 }
200 return null;
201 }
202
207 public function getRawQueryString() {
208 return '';
209 }
210
215 public function getRawPostString() {
216 return '';
217 }
218
223 public function getRawInput() {
224 return '';
225 }
226
231 protected function getRawIP() {
232 return '127.0.0.1';
233 }
234}
$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.
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')
Stable to call.
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...
array $headers
Lazy-initialized request headers indexed by upper-case header name.
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.
array $data
The parameters from $_GET, $_POST and the path router.
A helper class for throttling authentication attempts.