Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.43% covered (warning)
80.43%
37 / 46
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikiGlobalUserPage
80.43% covered (warning)
80.43%
37 / 46
42.86% covered (danger)
42.86%
3 / 7
10.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isLocal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWikiDisplayName
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSourceURL
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getRemoteURLFromAPI
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
2.01
 makeAPIRequest
76.47% covered (warning)
76.47%
13 / 17
0.00% covered (danger)
0.00%
0 / 1
2.05
1<?php
2/**
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17namespace MediaWiki\GlobalUserPage;
18
19use MediaWiki\Config\Config;
20use MediaWiki\Http\HttpRequestFactory;
21use MediaWiki\Json\FormatJson;
22use MediaWiki\Page\WikiPage;
23use MediaWiki\Status\Status;
24use MediaWiki\Title\Title;
25use MediaWiki\Utils\UrlUtils;
26use MediaWiki\WikiMap\WikiMap;
27use Wikimedia\ObjectCache\WANObjectCache;
28
29class WikiGlobalUserPage extends WikiPage {
30
31    public function __construct(
32        Title $title,
33        private readonly Config $config,
34        private readonly WANObjectCache $cache,
35        private readonly HttpRequestFactory $httpRequestFactory,
36        private readonly UrlUtils $urlUtils,
37    ) {
38        parent::__construct( $title );
39    }
40
41    /** @inheritDoc */
42    public function isLocal() {
43        return $this->getTitle()->exists();
44    }
45
46    /**
47     * @return string
48     */
49    public function getWikiDisplayName() {
50        $url = $this->getSourceURL();
51
52        return ( $this->urlUtils->parse( $url ) ?? [] )['host'];
53    }
54
55    /**
56     * Username for the given global user page
57     *
58     * @return string
59     */
60    public function getUsername() {
61        return $this->getTitle()->getText();
62    }
63
64    /**
65     * Returns a URL to the user page on the central wiki,
66     * attempts to use SiteConfiguration if possible, else
67     * falls back to using an API request
68     *
69     * @return string
70     */
71    public function getSourceURL() {
72        $wiki = WikiMap::getWiki( $this->config->get( 'GlobalUserPageDBname' ) );
73        if ( $wiki ) {
74            return $wiki->getCanonicalUrl(
75                'User:' . $this->getUsername()
76            );
77        }
78
79        // Fallback to the API
80        return $this->getRemoteURLFromAPI();
81    }
82
83    /**
84     * Returns a URL to the user page on the central wiki;
85     * if MW >= 1.24, this will be the cannonical url, otherwise
86     * it will be using whatever protocol was specified in
87     * $wgGlobalUserPageAPIUrl.
88     *
89     * @return string
90     */
91    protected function getRemoteURLFromAPI() {
92        $cache = $this->cache;
93
94        return $cache->getWithSetCallback(
95            $cache->makeGlobalKey( 'globaluserpage-url', sha1( $this->getUsername() ) ),
96            $cache::TTL_MONTH,
97            function ( $oldValue, &$ttl ) use ( $cache ) {
98                $resp = $this->makeAPIRequest( [
99                    'action' => 'query',
100                    'titles' => 'User:' . $this->getUsername(),
101                    'prop' => 'info',
102                    'inprop' => 'url',
103                    'formatversion' => '2',
104                ] );
105
106                if ( $resp === false ) {
107                    $ttl = $cache::TTL_UNCACHEABLE;
108
109                    return '';
110                }
111
112                return $resp['query']['pages'][0]['canonicalurl'];
113            }
114        );
115    }
116
117    /**
118     * Makes an API request to the central wiki
119     *
120     * @param array $params
121     * @return array|bool false if the request failed
122     */
123    public function makeAPIRequest( $params ) {
124        $params['format'] = 'json';
125        $url = wfAppendQuery( $this->config->get( 'GlobalUserPageAPIUrl' ), $params );
126        wfDebugLog( 'GlobalUserPage', "Making a request to $url" );
127        $req = $this->httpRequestFactory->create(
128            $url,
129            [ 'timeout' => $this->config->get( 'GlobalUserPageTimeout' ) ],
130            __METHOD__
131        );
132        $status = $req->execute();
133        if ( !$status->isOK() ) {
134            wfDebugLog(
135                'GlobalUserPage', __METHOD__ . ' Error: ' . Status::wrap( $status )->getWikitext()
136            );
137
138            return false;
139        }
140        $json = $req->getContent();
141        $decoded = FormatJson::decode( $json, true );
142
143        return $decoded;
144    }
145}