Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
RequestFromGlobals
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
11 / 11
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUri
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getProtocolVersion
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 initHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getServerParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCookieParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQueryParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUploadedFiles
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPostParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Rest;
4
5use GuzzleHttp\Psr7\LazyOpenStream;
6use GuzzleHttp\Psr7\ServerRequest;
7use GuzzleHttp\Psr7\Uri;
8use InvalidArgumentException;
9use MediaWiki\Request\WebRequest;
10
11// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
12
13/**
14 * This is a request class that gets data directly from the superglobals and
15 * other global PHP state, notably php://input.
16 */
17class RequestFromGlobals extends RequestBase {
18    /** @var Uri|null */
19    private $uri;
20    /** @var string|null */
21    private $protocol;
22    /** @var array|null */
23    private $uploadedFiles;
24
25    /**
26     * @param array $params Associative array of parameters:
27     *   - cookiePrefix: The prefix for cookie names used by getCookie()
28     */
29    public function __construct( $params = [] ) {
30        parent::__construct( $params['cookiePrefix'] ?? '' );
31    }
32
33    // RequestInterface
34
35    /** @inheritDoc */
36    public function getMethod() {
37        // Even though the spec says that method names should always be
38        // upper case, some clients may send lower case method names (T359306).
39        return strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' );
40    }
41
42    /** @inheritDoc */
43    public function getUri() {
44        if ( $this->uri === null ) {
45            $requestUrl = WebRequest::getGlobalRequestURL();
46
47            try {
48                $uriInstance = new Uri( $requestUrl );
49            } catch ( InvalidArgumentException ) {
50                // Uri constructor will throw exception if the URL is
51                // relative and contains colon-number pattern that
52                // looks like a port.
53                //
54                // Since $requestUrl here is absolute-path references
55                // so all titles that contain colon followed by a
56                // number would be inaccessible if the exception occurs.
57                $uriInstance = (
58                    new Uri( '//HOST:80' . $requestUrl )
59                )->withScheme( '' )->withHost( '' )->withPort( null );
60            }
61            $this->uri = $uriInstance;
62        }
63        return $this->uri;
64    }
65
66    // MessageInterface
67
68    /** @inheritDoc */
69    public function getProtocolVersion() {
70        if ( $this->protocol === null ) {
71            $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
72            $prefix = 'HTTP/';
73            if ( str_starts_with( $serverProtocol, $prefix ) ) {
74                $this->protocol = substr( $serverProtocol, strlen( $prefix ) );
75            } else {
76                $this->protocol = '1.1';
77            }
78        }
79        return $this->protocol;
80    }
81
82    protected function initHeaders() {
83        $this->setHeaders( getallheaders() );
84    }
85
86    /** @inheritDoc */
87    public function getBody() {
88        return new LazyOpenStream( 'php://input', 'r' );
89    }
90
91    // ServerRequestInterface
92
93    /** @inheritDoc */
94    public function getServerParams() {
95        return $_SERVER;
96    }
97
98    /** @inheritDoc */
99    public function getCookieParams() {
100        return $_COOKIE;
101    }
102
103    /** @inheritDoc */
104    public function getQueryParams() {
105        return $_GET;
106    }
107
108    /** @inheritDoc */
109    public function getUploadedFiles() {
110        $this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES );
111        return $this->uploadedFiles;
112    }
113
114    /** @inheritDoc */
115    public function getPostParams() {
116        return $_POST;
117    }
118
119}