From f8450b63cbb5ceaab3d8dd909f9c43c62406ba40 Mon Sep 17 00:00:00 2001 From: Justin Du Date: Mon, 4 Jan 2016 07:55:15 -0600 Subject: [PATCH] Split WebRequest.php classes Change autoload.php file to accomodate the split Bug: T122633 Change-Id: I0b2260da7cc086659fdd0894f7bb94798dff1163 --- autoload.php | 4 +- includes/DerivativeRequest.php | 87 ++++++++++ includes/FauxRequest.php | 252 ++++++++++++++++++++++++++++ includes/WebRequest.php | 291 --------------------------------- 4 files changed, 341 insertions(+), 293 deletions(-) create mode 100644 includes/DerivativeRequest.php create mode 100644 includes/FauxRequest.php diff --git a/autoload.php b/autoload.php index dbeecebea2..429a46ae97 100644 --- a/autoload.php +++ b/autoload.php @@ -331,7 +331,7 @@ $wgAutoloadLocalClasses = array( 'DeprecatedGlobal' => __DIR__ . '/includes/DeprecatedGlobal.php', 'DeprecatedInterfaceFinder' => __DIR__ . '/maintenance/findDeprecated.php', 'DerivativeContext' => __DIR__ . '/includes/context/DerivativeContext.php', - 'DerivativeRequest' => __DIR__ . '/includes/WebRequest.php', + 'DerivativeRequest' => __DIR__ . '/includes/DerivativeRequest.php', 'DerivativeResourceLoaderContext' => __DIR__ . '/includes/resourceloader/DerivativeResourceLoaderContext.php', 'DescribeFileOp' => __DIR__ . '/includes/filebackend/FileOp.php', 'Diff' => __DIR__ . '/includes/diff/DairikiDiff.php', @@ -417,7 +417,7 @@ $wgAutoloadLocalClasses = array( 'FakeResultWrapper' => __DIR__ . '/includes/db/DatabaseUtility.php', 'Fallback' => __DIR__ . '/includes/Fallback.php', 'FatalError' => __DIR__ . '/includes/exception/FatalError.php', - 'FauxRequest' => __DIR__ . '/includes/WebRequest.php', + 'FauxRequest' => __DIR__ . '/includes/FauxRequest.php', 'FauxResponse' => __DIR__ . '/includes/WebResponse.php', 'FeedItem' => __DIR__ . '/includes/Feed.php', 'FeedUtils' => __DIR__ . '/includes/FeedUtils.php', diff --git a/includes/DerivativeRequest.php b/includes/DerivativeRequest.php new file mode 100644 index 0000000000..dda1358f11 --- /dev/null +++ b/includes/DerivativeRequest.php @@ -0,0 +1,87 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Similar to FauxRequest, but only fakes URL parameters and method + * (POST or GET) and use the base request for the remaining stuff + * (cookies, session and headers). + * + * @ingroup HTTP + * @since 1.19 + */ +class DerivativeRequest extends FauxRequest { + private $base; + + /** + * @param WebRequest $base + * @param array $data Array of *non*-urlencoded key => value pairs, the + * fake GET/POST values + * @param bool $wasPosted Whether to treat the data as POST + */ + public function __construct( WebRequest $base, $data, $wasPosted = false ) { + $this->base = $base; + parent::__construct( $data, $wasPosted ); + } + + public function getCookie( $key, $prefix = null, $default = null ) { + return $this->base->getCookie( $key, $prefix, $default ); + } + + public function checkSessionCookie() { + return $this->base->checkSessionCookie(); + } + + public function getHeader( $name, $flags = 0 ) { + return $this->base->getHeader( $name, $flags ); + } + + public function getAllHeaders() { + return $this->base->getAllHeaders(); + } + + public function getSessionData( $key ) { + return $this->base->getSessionData( $key ); + } + + public function setSessionData( $key, $data ) { + $this->base->setSessionData( $key, $data ); + } + + public function getAcceptLang() { + return $this->base->getAcceptLang(); + } + + public function getIP() { + return $this->base->getIP(); + } + + public function getProtocol() { + return $this->base->getProtocol(); + } + + public function getElapsedTime() { + return $this->base->getElapsedTime(); + } +} diff --git a/includes/FauxRequest.php b/includes/FauxRequest.php new file mode 100644 index 0000000000..888f853a4d --- /dev/null +++ b/includes/FauxRequest.php @@ -0,0 +1,252 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * WebRequest clone which takes values from a provided array. + * + * @ingroup HTTP + */ +class FauxRequest extends WebRequest { + private $wasPosted = false; + private $session = array(); + private $requestUrl; + protected $cookies = array(); + + /** + * @param array $data Array of *non*-urlencoded key => value pairs, the + * fake GET/POST values + * @param bool $wasPosted Whether to treat the data as POST + * @param array|null $session Session array or null + * @param string $protocol 'http' or 'https' + * @throws MWException + */ + public function __construct( $data = array(), $wasPosted = false, + $session = null, $protocol = 'http' + ) { + $this->requestTime = microtime( true ); + + if ( is_array( $data ) ) { + $this->data = $data; + } else { + throw new MWException( "FauxRequest() got bogus data" ); + } + $this->wasPosted = $wasPosted; + if ( $session ) { + $this->session = $session; + } + $this->protocol = $protocol; + } + + /** + * Initialise the header list + */ + protected function initHeaders() { + // Nothing to init + } + + /** + * @param string $name + * @param string $default + * @return string + */ + public function getText( $name, $default = '' ) { + # Override; don't recode since we're using internal data + return (string)$this->getVal( $name, $default ); + } + + /** + * @return array + */ + public function getValues() { + return $this->data; + } + + /** + * @return array + */ + public function getQueryValues() { + if ( $this->wasPosted ) { + return array(); + } else { + return $this->data; + } + } + + public function getMethod() { + return $this->wasPosted ? 'POST' : 'GET'; + } + + /** + * @return bool + */ + public function wasPosted() { + return $this->wasPosted; + } + + public function getCookie( $key, $prefix = null, $default = null ) { + if ( $prefix === null ) { + global $wgCookiePrefix; + $prefix = $wgCookiePrefix; + } + $name = $prefix . $key; + return isset( $this->cookies[$name] ) ? $this->cookies[$name] : $default; + } + + /** + * @since 1.26 + * @param string $name Unprefixed name of the cookie to set + * @param string|null $value Value of the cookie to set + * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix + */ + public function setCookie( $key, $value, $prefix = null ) { + $this->setCookies( array( $key => $value ), $prefix ); + } + + /** + * @since 1.26 + * @param array $cookies + * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix + */ + public function setCookies( $cookies, $prefix = null ) { + if ( $prefix === null ) { + global $wgCookiePrefix; + $prefix = $wgCookiePrefix; + } + foreach ( $cookies as $key => $value ) { + $name = $prefix . $key; + $this->cookies[$name] = $value; + } + } + + public function checkSessionCookie() { + return false; + } + + /** + * @since 1.25 + */ + public function setRequestURL( $url ) { + $this->requestUrl = $url; + } + + /** + * @since 1.25 MWException( "getRequestURL not implemented" ) + * no longer thrown. + */ + public function getRequestURL() { + if ( $this->requestUrl === null ) { + throw new MWException( 'Request URL not set' ); + } + return $this->requestUrl; + } + + public function getProtocol() { + return $this->protocol; + } + + /** + * @param string $name + * @param string $val + */ + public function setHeader( $name, $val ) { + $this->setHeaders( array( $name => $val ) ); + } + + /** + * @since 1.26 + * @param array $headers + */ + public function setHeaders( $headers ) { + foreach ( $headers as $name => $val ) { + $name = strtoupper( $name ); + $this->headers[$name] = $val; + } + } + + /** + * @param string $key + * @return array|null + */ + public function getSessionData( $key ) { + if ( isset( $this->session[$key] ) ) { + return $this->session[$key]; + } + return null; + } + + /** + * @param string $key + * @param array $data + */ + public function setSessionData( $key, $data ) { + $this->session[$key] = $data; + } + + /** + * @return array|mixed|null + */ + public function getSessionArray() { + return $this->session; + } + + /** + * FauxRequests shouldn't depend on raw request data (but that could be implemented here) + * @return string + */ + public function getRawQueryString() { + return ''; + } + + /** + * FauxRequests shouldn't depend on raw request data (but that could be implemented here) + * @return string + */ + public function getRawPostString() { + return ''; + } + + /** + * FauxRequests shouldn't depend on raw request data (but that could be implemented here) + * @return string + */ + public function getRawInput() { + return ''; + } + + /** + * @param array $extWhitelist + * @return bool + */ + public function checkUrlExtension( $extWhitelist = array() ) { + return true; + } + + /** + * @return string + */ + protected function getRawIP() { + return '127.0.0.1'; + } +} diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 23eb63b4fa..aa55e00cf6 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -1175,294 +1175,3 @@ HTML; $this->ip = $ip; } } - -/** - * WebRequest clone which takes values from a provided array. - * - * @ingroup HTTP - */ -class FauxRequest extends WebRequest { - private $wasPosted = false; - private $session = array(); - private $requestUrl; - protected $cookies = array(); - - /** - * @param array $data Array of *non*-urlencoded key => value pairs, the - * fake GET/POST values - * @param bool $wasPosted Whether to treat the data as POST - * @param array|null $session Session array or null - * @param string $protocol 'http' or 'https' - * @throws MWException - */ - public function __construct( $data = array(), $wasPosted = false, - $session = null, $protocol = 'http' - ) { - $this->requestTime = microtime( true ); - - if ( is_array( $data ) ) { - $this->data = $data; - } else { - throw new MWException( "FauxRequest() got bogus data" ); - } - $this->wasPosted = $wasPosted; - if ( $session ) { - $this->session = $session; - } - $this->protocol = $protocol; - } - - /** - * Initialise the header list - */ - protected function initHeaders() { - // Nothing to init - } - - /** - * @param string $name - * @param string $default - * @return string - */ - public function getText( $name, $default = '' ) { - # Override; don't recode since we're using internal data - return (string)$this->getVal( $name, $default ); - } - - /** - * @return array - */ - public function getValues() { - return $this->data; - } - - /** - * @return array - */ - public function getQueryValues() { - if ( $this->wasPosted ) { - return array(); - } else { - return $this->data; - } - } - - public function getMethod() { - return $this->wasPosted ? 'POST' : 'GET'; - } - - /** - * @return bool - */ - public function wasPosted() { - return $this->wasPosted; - } - - public function getCookie( $key, $prefix = null, $default = null ) { - if ( $prefix === null ) { - global $wgCookiePrefix; - $prefix = $wgCookiePrefix; - } - $name = $prefix . $key; - return isset( $this->cookies[$name] ) ? $this->cookies[$name] : $default; - } - - /** - * @since 1.26 - * @param string $name Unprefixed name of the cookie to set - * @param string|null $value Value of the cookie to set - * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix - */ - public function setCookie( $key, $value, $prefix = null ) { - $this->setCookies( array( $key => $value ), $prefix ); - } - - /** - * @since 1.26 - * @param array $cookies - * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix - */ - public function setCookies( $cookies, $prefix = null ) { - if ( $prefix === null ) { - global $wgCookiePrefix; - $prefix = $wgCookiePrefix; - } - foreach ( $cookies as $key => $value ) { - $name = $prefix . $key; - $this->cookies[$name] = $value; - } - } - - public function checkSessionCookie() { - return false; - } - - /** - * @since 1.25 - */ - public function setRequestURL( $url ) { - $this->requestUrl = $url; - } - - /** - * @since 1.25 MWException( "getRequestURL not implemented" ) - * no longer thrown. - */ - public function getRequestURL() { - if ( $this->requestUrl === null ) { - throw new MWException( 'Request URL not set' ); - } - return $this->requestUrl; - } - - public function getProtocol() { - return $this->protocol; - } - - /** - * @param string $name - * @param string $val - */ - public function setHeader( $name, $val ) { - $this->setHeaders( array( $name => $val ) ); - } - - /** - * @since 1.26 - * @param array $headers - */ - public function setHeaders( $headers ) { - foreach ( $headers as $name => $val ) { - $name = strtoupper( $name ); - $this->headers[$name] = $val; - } - } - - /** - * @param string $key - * @return array|null - */ - public function getSessionData( $key ) { - if ( isset( $this->session[$key] ) ) { - return $this->session[$key]; - } - return null; - } - - /** - * @param string $key - * @param array $data - */ - public function setSessionData( $key, $data ) { - $this->session[$key] = $data; - } - - /** - * @return array|mixed|null - */ - public function getSessionArray() { - return $this->session; - } - - /** - * FauxRequests shouldn't depend on raw request data (but that could be implemented here) - * @return string - */ - public function getRawQueryString() { - return ''; - } - - /** - * FauxRequests shouldn't depend on raw request data (but that could be implemented here) - * @return string - */ - public function getRawPostString() { - return ''; - } - - /** - * FauxRequests shouldn't depend on raw request data (but that could be implemented here) - * @return string - */ - public function getRawInput() { - return ''; - } - - /** - * @param array $extWhitelist - * @return bool - */ - public function checkUrlExtension( $extWhitelist = array() ) { - return true; - } - - /** - * @return string - */ - protected function getRawIP() { - return '127.0.0.1'; - } -} - -/** - * Similar to FauxRequest, but only fakes URL parameters and method - * (POST or GET) and use the base request for the remaining stuff - * (cookies, session and headers). - * - * @ingroup HTTP - * @since 1.19 - */ -class DerivativeRequest extends FauxRequest { - private $base; - - /** - * @param WebRequest $base - * @param array $data Array of *non*-urlencoded key => value pairs, the - * fake GET/POST values - * @param bool $wasPosted Whether to treat the data as POST - */ - public function __construct( WebRequest $base, $data, $wasPosted = false ) { - $this->base = $base; - parent::__construct( $data, $wasPosted ); - } - - public function getCookie( $key, $prefix = null, $default = null ) { - return $this->base->getCookie( $key, $prefix, $default ); - } - - public function checkSessionCookie() { - return $this->base->checkSessionCookie(); - } - - public function getHeader( $name, $flags = 0 ) { - return $this->base->getHeader( $name, $flags ); - } - - public function getAllHeaders() { - return $this->base->getAllHeaders(); - } - - public function getSessionData( $key ) { - return $this->base->getSessionData( $key ); - } - - public function setSessionData( $key, $data ) { - $this->base->setSessionData( $key, $data ); - } - - public function getAcceptLang() { - return $this->base->getAcceptLang(); - } - - public function getIP() { - return $this->base->getIP(); - } - - public function getProtocol() { - return $this->base->getProtocol(); - } - - public function getElapsedTime() { - return $this->base->getElapsedTime(); - } -} -- 2.20.1