Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ReleaseLookup | |
0.00% |
0 / 23 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
load | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
getLatestReleaseFor | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getLatestRelease | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Copyright (C) 2016, 2021 Kunal Mehta <legoktm@debian.org> |
4 | * |
5 | * This program is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | namespace MediaWiki\VersionInfo; |
19 | |
20 | /** |
21 | * A release is typed as ['version' => Version, 'date' => string] |
22 | */ |
23 | class ReleaseLookup { |
24 | |
25 | /** @var ?array */ |
26 | private $cfg; |
27 | |
28 | private function load() { |
29 | if ( $this->cfg ) { |
30 | // Already loaded |
31 | return; |
32 | } |
33 | |
34 | $msg = wfMessage( 'mwversioninfo.json' )->plain(); |
35 | $raw = json_decode( $msg, true ); |
36 | if ( !$raw ) { |
37 | // TODO: Add stricter validation here |
38 | throw new \RuntimeException( "Invalid mwversioninfo.json value" ); |
39 | } |
40 | $this->cfg = [ |
41 | 'releases' => [], |
42 | 'beta' => $raw['beta'], |
43 | ]; |
44 | // Make it easier to lookup by branch |
45 | foreach ( $raw['releases'] as $release ) { |
46 | $version = Version::newFromString( $release['version'] ); |
47 | $branch = (string)$version->getBranch(); |
48 | $this->cfg['releases'][$branch] = [ |
49 | 'version' => $version, |
50 | 'date' => $release['date'], |
51 | ]; |
52 | } |
53 | } |
54 | |
55 | /** |
56 | * Get the latest release that matches the given branch |
57 | * |
58 | * @param Version $version |
59 | * @return array|false if not found |
60 | */ |
61 | public function getLatestReleaseFor( Version $version ) { |
62 | $this->load(); |
63 | $branch = (string)$version->getBranch(); |
64 | |
65 | return $this->cfg['releases'][$branch] ?? false; |
66 | } |
67 | |
68 | /** |
69 | * Get the most recent overall release |
70 | * @return array |
71 | * @suppress PhanTypeArraySuspiciousNullable Okay after load() |
72 | */ |
73 | public function getLatestRelease() { |
74 | $this->load(); |
75 | $branch = max( array_keys( $this->cfg['releases'] ) ); |
76 | return $this->cfg['releases'][$branch]; |
77 | } |
78 | } |