Fix use of RawMessage in Status::getMessage()
[mediawiki.git] / includes / FauxRequest.php
blobc2108f207a52bdfe291949d20dcd13b810922e9c
1 <?php
2 /**
3 * Deal with importing all those nasty globals and things
5 * Copyright © 2003 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
26 use MediaWiki\Session\SessionManager;
28 /**
29 * WebRequest clone which takes values from a provided array.
31 * @ingroup HTTP
33 class FauxRequest extends WebRequest {
34 private $wasPosted = false;
35 private $requestUrl;
36 protected $cookies = [];
38 /**
39 * @param array $data Array of *non*-urlencoded key => value pairs, the
40 * fake GET/POST values
41 * @param bool $wasPosted Whether to treat the data as POST
42 * @param MediaWiki\\Session\\Session|array|null $session Session, session
43 * data array, or null
44 * @param string $protocol 'http' or 'https'
45 * @throws MWException
47 public function __construct( $data = [], $wasPosted = false,
48 $session = null, $protocol = 'http'
49 ) {
50 $this->requestTime = microtime( true );
52 if ( is_array( $data ) ) {
53 $this->data = $data;
54 } else {
55 throw new MWException( "FauxRequest() got bogus data" );
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 );
66 } elseif ( $session !== null ) {
67 throw new MWException( "FauxRequest() got bogus session" );
69 $this->protocol = $protocol;
72 /**
73 * Initialise the header list
75 protected function initHeaders() {
76 // Nothing to init
79 /**
80 * @param string $name
81 * @param string $default
82 * @return string
84 public function getText( $name, $default = '' ) {
85 # Override; don't recode since we're using internal data
86 return (string)$this->getVal( $name, $default );
89 /**
90 * @return array
92 public function getValues() {
93 return $this->data;
96 /**
97 * @return array
99 public function getQueryValues() {
100 if ( $this->wasPosted ) {
101 return [];
102 } else {
103 return $this->data;
107 public function getMethod() {
108 return $this->wasPosted ? 'POST' : 'GET';
112 * @return bool
114 public function wasPosted() {
115 return $this->wasPosted;
118 public function getCookie( $key, $prefix = null, $default = null ) {
119 if ( $prefix === null ) {
120 global $wgCookiePrefix;
121 $prefix = $wgCookiePrefix;
123 $name = $prefix . $key;
124 return isset( $this->cookies[$name] ) ? $this->cookies[$name] : $default;
128 * @since 1.26
129 * @param string $name Unprefixed name of the cookie to set
130 * @param string|null $value Value of the cookie to set
131 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
133 public function setCookie( $key, $value, $prefix = null ) {
134 $this->setCookies( [ $key => $value ], $prefix );
138 * @since 1.26
139 * @param array $cookies
140 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
142 public function setCookies( $cookies, $prefix = null ) {
143 if ( $prefix === null ) {
144 global $wgCookiePrefix;
145 $prefix = $wgCookiePrefix;
147 foreach ( $cookies as $key => $value ) {
148 $name = $prefix . $key;
149 $this->cookies[$name] = $value;
154 * @since 1.25
156 public function setRequestURL( $url ) {
157 $this->requestUrl = $url;
161 * @since 1.25 MWException( "getRequestURL not implemented" )
162 * no longer thrown.
164 public function getRequestURL() {
165 if ( $this->requestUrl === null ) {
166 throw new MWException( 'Request URL not set' );
168 return $this->requestUrl;
171 public function getProtocol() {
172 return $this->protocol;
176 * @param string $name
177 * @param string $val
179 public function setHeader( $name, $val ) {
180 $this->setHeaders( [ $name => $val ] );
184 * @since 1.26
185 * @param array $headers
187 public function setHeaders( $headers ) {
188 foreach ( $headers as $name => $val ) {
189 $name = strtoupper( $name );
190 $this->headers[$name] = $val;
195 * @return array|null
197 public function getSessionArray() {
198 if ( $this->sessionId !== null ) {
199 return iterator_to_array( $this->getSession() );
201 return null;
205 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
206 * @return string
208 public function getRawQueryString() {
209 return '';
213 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
214 * @return string
216 public function getRawPostString() {
217 return '';
221 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
222 * @return string
224 public function getRawInput() {
225 return '';
229 * @param array $extWhitelist
230 * @return bool
232 public function checkUrlExtension( $extWhitelist = [] ) {
233 return true;
237 * @return string
239 protected function getRawIP() {
240 return '127.0.0.1';