Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.25% |
39 / 48 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
WikiGlobalUserPage | |
81.25% |
39 / 48 |
|
42.86% |
3 / 7 |
10.66 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
isLocal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getWikiDisplayName | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getUsername | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSourceURL | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
getRemoteURLFromAPI | |
88.89% |
16 / 18 |
|
0.00% |
0 / 1 |
2.01 | |||
makeAPIRequest | |
76.47% |
13 / 17 |
|
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 | |
17 | namespace MediaWiki\GlobalUserPage; |
18 | |
19 | use MediaWiki\Config\Config; |
20 | use MediaWiki\Json\FormatJson; |
21 | use MediaWiki\MediaWikiServices; |
22 | use MediaWiki\Page\WikiPage; |
23 | use MediaWiki\Status\Status; |
24 | use MediaWiki\Title\Title; |
25 | use MediaWiki\WikiMap\WikiMap; |
26 | use Wikimedia\ObjectCache\WANObjectCache; |
27 | |
28 | class WikiGlobalUserPage extends WikiPage { |
29 | |
30 | /** |
31 | * @var Config |
32 | */ |
33 | private $config; |
34 | |
35 | /** |
36 | * @var WANObjectCache |
37 | */ |
38 | private $cache; |
39 | |
40 | public function __construct( Title $title, Config $config ) { |
41 | parent::__construct( $title ); |
42 | $this->config = $config; |
43 | $this->cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
44 | } |
45 | |
46 | /** @inheritDoc */ |
47 | public function isLocal() { |
48 | return $this->getTitle()->exists(); |
49 | } |
50 | |
51 | /** |
52 | * @return string |
53 | */ |
54 | public function getWikiDisplayName() { |
55 | $url = $this->getSourceURL(); |
56 | |
57 | return wfParseUrl( $url )['host']; |
58 | } |
59 | |
60 | /** |
61 | * Username for the given global user page |
62 | * |
63 | * @return string |
64 | */ |
65 | public function getUsername() { |
66 | return $this->getTitle()->getText(); |
67 | } |
68 | |
69 | /** |
70 | * Returns a URL to the user page on the central wiki, |
71 | * attempts to use SiteConfiguration if possible, else |
72 | * falls back to using an API request |
73 | * |
74 | * @return string |
75 | */ |
76 | public function getSourceURL() { |
77 | $wiki = WikiMap::getWiki( $this->config->get( 'GlobalUserPageDBname' ) ); |
78 | if ( $wiki ) { |
79 | return $wiki->getCanonicalUrl( |
80 | 'User:' . $this->getUsername() |
81 | ); |
82 | } |
83 | |
84 | // Fallback to the API |
85 | return $this->getRemoteURLFromAPI(); |
86 | } |
87 | |
88 | /** |
89 | * Returns a URL to the user page on the central wiki; |
90 | * if MW >= 1.24, this will be the cannonical url, otherwise |
91 | * it will be using whatever protocol was specified in |
92 | * $wgGlobalUserPageAPIUrl. |
93 | * |
94 | * @return string |
95 | */ |
96 | protected function getRemoteURLFromAPI() { |
97 | $cache = $this->cache; |
98 | |
99 | return $cache->getWithSetCallback( |
100 | $cache->makeGlobalKey( 'globaluserpage-url', sha1( $this->getUsername() ) ), |
101 | $cache::TTL_MONTH, |
102 | function ( $oldValue, &$ttl ) use ( $cache ) { |
103 | $resp = $this->makeAPIRequest( [ |
104 | 'action' => 'query', |
105 | 'titles' => 'User:' . $this->getUsername(), |
106 | 'prop' => 'info', |
107 | 'inprop' => 'url', |
108 | 'formatversion' => '2', |
109 | ] ); |
110 | |
111 | if ( $resp === false ) { |
112 | $ttl = $cache::TTL_UNCACHEABLE; |
113 | |
114 | return ''; |
115 | } |
116 | |
117 | return $resp['query']['pages'][0]['canonicalurl']; |
118 | } |
119 | ); |
120 | } |
121 | |
122 | /** |
123 | * Makes an API request to the central wiki |
124 | * |
125 | * @param array $params |
126 | * @return array|bool false if the request failed |
127 | */ |
128 | public function makeAPIRequest( $params ) { |
129 | $params['format'] = 'json'; |
130 | $url = wfAppendQuery( $this->config->get( 'GlobalUserPageAPIUrl' ), $params ); |
131 | wfDebugLog( 'GlobalUserPage', "Making a request to $url" ); |
132 | $req = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( |
133 | $url, |
134 | [ 'timeout' => $this->config->get( 'GlobalUserPageTimeout' ) ], |
135 | __METHOD__ |
136 | ); |
137 | $status = $req->execute(); |
138 | if ( !$status->isOK() ) { |
139 | wfDebugLog( |
140 | 'GlobalUserPage', __METHOD__ . ' Error: ' . Status::wrap( $status )->getWikitext() |
141 | ); |
142 | |
143 | return false; |
144 | } |
145 | $json = $req->getContent(); |
146 | $decoded = FormatJson::decode( $json, true ); |
147 | |
148 | return $decoded; |
149 | } |
150 | } |