2019-05-09 01:36:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Rest;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\LazyOpenStream;
|
|
|
|
|
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
|
|
use GuzzleHttp\Psr7\Uri;
|
2024-01-05 00:37:59 +00:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use MediaWiki\Request\WebRequest;
|
2019-05-09 01:36:18 +00:00
|
|
|
|
|
|
|
|
// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is a request class that gets data directly from the superglobals and
|
|
|
|
|
* other global PHP state, notably php://input.
|
|
|
|
|
*/
|
|
|
|
|
class RequestFromGlobals extends RequestBase {
|
2024-09-07 19:49:56 +00:00
|
|
|
/** @var Uri|null */
|
2019-05-09 01:36:18 +00:00
|
|
|
private $uri;
|
2024-09-07 19:49:56 +00:00
|
|
|
/** @var string|null */
|
2019-05-09 01:36:18 +00:00
|
|
|
private $protocol;
|
2024-09-07 19:49:56 +00:00
|
|
|
/** @var array|null */
|
2019-05-09 01:36:18 +00:00
|
|
|
private $uploadedFiles;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $params Associative array of parameters:
|
|
|
|
|
* - cookiePrefix: The prefix for cookie names used by getCookie()
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $params = [] ) {
|
|
|
|
|
parent::__construct( $params['cookiePrefix'] ?? '' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RequestInterface
|
|
|
|
|
|
|
|
|
|
public function getMethod() {
|
2024-03-06 12:08:08 +00:00
|
|
|
// Even though the spec says that method names should always be
|
|
|
|
|
// upper case, some clients may send lower case method names (T359306).
|
|
|
|
|
return strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' );
|
2019-05-09 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUri() {
|
|
|
|
|
if ( $this->uri === null ) {
|
2024-01-05 00:37:59 +00:00
|
|
|
$requestUrl = WebRequest::getGlobalRequestURL();
|
2020-07-01 11:22:33 +00:00
|
|
|
|
2020-08-26 19:33:30 +00:00
|
|
|
try {
|
2020-07-01 11:22:33 +00:00
|
|
|
$uriInstance = new Uri( $requestUrl );
|
2024-01-05 00:37:59 +00:00
|
|
|
} catch ( InvalidArgumentException $e ) {
|
2020-08-26 19:33:30 +00:00
|
|
|
// Uri constructor will throw exception if the URL is
|
|
|
|
|
// relative and contains colon-number pattern that
|
|
|
|
|
// looks like a port.
|
|
|
|
|
//
|
|
|
|
|
// Since $requestUrl here is absolute-path references
|
|
|
|
|
// so all titles that contain colon followed by a
|
2022-05-09 09:09:00 +00:00
|
|
|
// number would be inaccessible if the exception occurs.
|
2020-08-26 19:33:30 +00:00
|
|
|
$uriInstance = (
|
|
|
|
|
new Uri( '//HOST:80' . $requestUrl )
|
|
|
|
|
)->withScheme( '' )->withHost( '' )->withPort( null );
|
2020-07-01 11:22:33 +00:00
|
|
|
}
|
2020-08-26 19:33:30 +00:00
|
|
|
$this->uri = $uriInstance;
|
2019-05-09 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
return $this->uri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MessageInterface
|
|
|
|
|
|
|
|
|
|
public function getProtocolVersion() {
|
|
|
|
|
if ( $this->protocol === null ) {
|
|
|
|
|
$serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
|
2022-04-13 13:44:41 +00:00
|
|
|
$prefix = 'HTTP/';
|
|
|
|
|
if ( str_starts_with( $serverProtocol, $prefix ) ) {
|
|
|
|
|
$this->protocol = substr( $serverProtocol, strlen( $prefix ) );
|
2019-05-09 01:36:18 +00:00
|
|
|
} else {
|
|
|
|
|
$this->protocol = '1.1';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->protocol;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function initHeaders() {
|
2020-03-18 08:49:15 +00:00
|
|
|
$this->setHeaders( getallheaders() );
|
2019-05-09 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getBody() {
|
|
|
|
|
return new LazyOpenStream( 'php://input', 'r' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ServerRequestInterface
|
|
|
|
|
|
|
|
|
|
public function getServerParams() {
|
|
|
|
|
return $_SERVER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCookieParams() {
|
|
|
|
|
return $_COOKIE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getQueryParams() {
|
|
|
|
|
return $_GET;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUploadedFiles() {
|
2022-12-16 23:48:27 +00:00
|
|
|
$this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES );
|
2019-05-09 01:36:18 +00:00
|
|
|
return $this->uploadedFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPostParams() {
|
|
|
|
|
return $_POST;
|
|
|
|
|
}
|
2024-02-27 11:42:55 +00:00
|
|
|
|
2019-05-09 01:36:18 +00:00
|
|
|
}
|