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    private $uri;
19    private $protocol;
20    private $uploadedFiles;
21
22    /**
23     * @param array $params Associative array of parameters:
24     *   - cookiePrefix: The prefix for cookie names used by getCookie()
25     */
26    public function __construct( $params = [] ) {
27        parent::__construct( $params['cookiePrefix'] ?? '' );
28    }
29
30    // RequestInterface
31
32    public function getMethod() {
33        // Even though the spec says that method names should always be
34        // upper case, some clients may send lower case method names (T359306).
35        return strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' );
36    }
37
38    public function getUri() {
39        if ( $this->uri === null ) {
40            $requestUrl = WebRequest::getGlobalRequestURL();
41
42            try {
43                $uriInstance = new Uri( $requestUrl );
44            } catch ( InvalidArgumentException $e ) {
45                // Uri constructor will throw exception if the URL is
46                // relative and contains colon-number pattern that
47                // looks like a port.
48                //
49                // Since $requestUrl here is absolute-path references
50                // so all titles that contain colon followed by a
51                // number would be inaccessible if the exception occurs.
52                $uriInstance = (
53                    new Uri( '//HOST:80' . $requestUrl )
54                )->withScheme( '' )->withHost( '' )->withPort( null );
55            }
56            $this->uri = $uriInstance;
57        }
58        return $this->uri;
59    }
60
61    // MessageInterface
62
63    public function getProtocolVersion() {
64        if ( $this->protocol === null ) {
65            $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
66            $prefix = 'HTTP/';
67            if ( str_starts_with( $serverProtocol, $prefix ) ) {
68                $this->protocol = substr( $serverProtocol, strlen( $prefix ) );
69            } else {
70                $this->protocol = '1.1';
71            }
72        }
73        return $this->protocol;
74    }
75
76    protected function initHeaders() {
77        $this->setHeaders( getallheaders() );
78    }
79
80    public function getBody() {
81        return new LazyOpenStream( 'php://input', 'r' );
82    }
83
84    // ServerRequestInterface
85
86    public function getServerParams() {
87        return $_SERVER;
88    }
89
90    public function getCookieParams() {
91        return $_COOKIE;
92    }
93
94    public function getQueryParams() {
95        return $_GET;
96    }
97
98    public function getUploadedFiles() {
99        $this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES );
100        return $this->uploadedFiles;
101    }
102
103    public function getPostParams() {
104        return $_POST;
105    }
106
107}