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