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
26 use MediaWiki\Session\SessionManager
;
29 * WebRequest clone which takes values from a provided array.
33 class FauxRequest
extends WebRequest
{
34 private $wasPosted = false;
36 protected $cookies = [];
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
44 * @param string $protocol 'http' or 'https'
47 public function __construct( $data = [], $wasPosted = false,
48 $session = null, $protocol = 'http'
50 $this->requestTime
= microtime( true );
52 if ( is_array( $data ) ) {
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;
73 * Initialise the header list
75 protected function initHeaders() {
81 * @param string $default
84 public function getText( $name, $default = '' ) {
85 # Override; don't recode since we're using internal data
86 return (string)$this->getVal( $name, $default );
92 public function getQueryValues() {
93 if ( $this->wasPosted
) {
100 public function getMethod() {
101 return $this->wasPosted ?
'POST' : 'GET';
107 public function wasPosted() {
108 return $this->wasPosted
;
111 public function getCookie( $key, $prefix = null, $default = null ) {
112 if ( $prefix === null ) {
113 global $wgCookiePrefix;
114 $prefix = $wgCookiePrefix;
116 $name = $prefix . $key;
117 return $this->cookies
[$name] ??
$default;
122 * @param string $key Unprefixed name of the cookie to set
123 * @param string|null $value Value of the cookie to set
124 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
126 public function setCookie( $key, $value, $prefix = null ) {
127 $this->setCookies( [ $key => $value ], $prefix );
132 * @param array $cookies
133 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
135 public function setCookies( $cookies, $prefix = null ) {
136 if ( $prefix === null ) {
137 global $wgCookiePrefix;
138 $prefix = $wgCookiePrefix;
140 foreach ( $cookies as $key => $value ) {
141 $name = $prefix . $key;
142 $this->cookies
[$name] = $value;
150 public function setRequestURL( $url ) {
151 $this->requestUrl
= $url;
155 * @since 1.25 MWException( "getRequestURL not implemented" )
159 public function getRequestURL() {
160 if ( $this->requestUrl
=== null ) {
161 throw new MWException( 'Request URL not set' );
163 return $this->requestUrl
;
166 public function getProtocol() {
167 return $this->protocol
;
171 * @param string $name
174 public function setHeader( $name, $val ) {
175 $this->setHeaders( [ $name => $val ] );
180 * @param array $headers
182 public function setHeaders( $headers ) {
183 foreach ( $headers as $name => $val ) {
184 $name = strtoupper( $name );
185 $this->headers
[$name] = $val;
192 public function getSessionArray() {
193 if ( $this->sessionId
!== null ) {
194 return iterator_to_array( $this->getSession() );
200 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
203 public function getRawQueryString() {
208 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
211 public function getRawPostString() {
216 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
219 public function getRawInput() {
224 * @codeCoverageIgnore
225 * @param array $extWhitelist
228 public function checkUrlExtension( $extWhitelist = [] ) {
233 * @codeCoverageIgnore
236 protected function getRawIP() {