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
27 * WebRequest clone which takes values from a provided array.
31 class FauxRequest
extends WebRequest
{
32 private $wasPosted = false;
33 private $session = array();
35 protected $cookies = array();
38 * @param array $data Array of *non*-urlencoded key => value pairs, the
39 * fake GET/POST values
40 * @param bool $wasPosted Whether to treat the data as POST
41 * @param array|null $session Session array or null
42 * @param string $protocol 'http' or 'https'
45 public function __construct( $data = array(), $wasPosted = false,
46 $session = null, $protocol = 'http'
48 $this->requestTime
= microtime( true );
50 if ( is_array( $data ) ) {
53 throw new MWException( "FauxRequest() got bogus data" );
55 $this->wasPosted
= $wasPosted;
57 $this->session
= $session;
59 $this->protocol
= $protocol;
63 * Initialise the header list
65 protected function initHeaders() {
71 * @param string $default
74 public function getText( $name, $default = '' ) {
75 # Override; don't recode since we're using internal data
76 return (string)$this->getVal( $name, $default );
82 public function getValues() {
89 public function getQueryValues() {
90 if ( $this->wasPosted
) {
97 public function getMethod() {
98 return $this->wasPosted ?
'POST' : 'GET';
104 public function wasPosted() {
105 return $this->wasPosted
;
108 public function getCookie( $key, $prefix = null, $default = null ) {
109 if ( $prefix === null ) {
110 global $wgCookiePrefix;
111 $prefix = $wgCookiePrefix;
113 $name = $prefix . $key;
114 return isset( $this->cookies
[$name] ) ?
$this->cookies
[$name] : $default;
119 * @param string $name Unprefixed name of the cookie to set
120 * @param string|null $value Value of the cookie to set
121 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
123 public function setCookie( $key, $value, $prefix = null ) {
124 $this->setCookies( array( $key => $value ), $prefix );
129 * @param array $cookies
130 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
132 public function setCookies( $cookies, $prefix = null ) {
133 if ( $prefix === null ) {
134 global $wgCookiePrefix;
135 $prefix = $wgCookiePrefix;
137 foreach ( $cookies as $key => $value ) {
138 $name = $prefix . $key;
139 $this->cookies
[$name] = $value;
143 public function checkSessionCookie() {
150 public function setRequestURL( $url ) {
151 $this->requestUrl
= $url;
155 * @since 1.25 MWException( "getRequestURL not implemented" )
158 public function getRequestURL() {
159 if ( $this->requestUrl
=== null ) {
160 throw new MWException( 'Request URL not set' );
162 return $this->requestUrl
;
165 public function getProtocol() {
166 return $this->protocol
;
170 * @param string $name
173 public function setHeader( $name, $val ) {
174 $this->setHeaders( array( $name => $val ) );
179 * @param array $headers
181 public function setHeaders( $headers ) {
182 foreach ( $headers as $name => $val ) {
183 $name = strtoupper( $name );
184 $this->headers
[$name] = $val;
192 public function getSessionData( $key ) {
193 if ( isset( $this->session
[$key] ) ) {
194 return $this->session
[$key];
203 public function setSessionData( $key, $data ) {
204 $this->session
[$key] = $data;
208 * @return array|mixed|null
210 public function getSessionArray() {
211 return $this->session
;
215 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
218 public function getRawQueryString() {
223 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
226 public function getRawPostString() {
231 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
234 public function getRawInput() {
239 * @param array $extWhitelist
242 public function checkUrlExtension( $extWhitelist = array() ) {
249 protected function getRawIP() {