1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This program is free software; you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU General Public License as published by |
6
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* This program is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU General Public License along |
15
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
16
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
18
|
|
|
* |
19
|
|
|
* @since 1.25 |
20
|
|
|
* |
21
|
|
|
* @file |
22
|
|
|
* |
23
|
|
|
* @license GNU GPL v2+ |
24
|
|
|
*/ |
25
|
|
|
class SitesCacheFileBuilder { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var SiteLookup |
29
|
|
|
*/ |
30
|
|
|
private $siteLookup; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $cacheFile; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param SiteLookup $siteLookup |
39
|
|
|
* @param string $cacheFile |
40
|
|
|
*/ |
41
|
|
|
public function __construct( SiteLookup $siteLookup, $cacheFile ) { |
42
|
|
|
$this->siteLookup = $siteLookup; |
43
|
|
|
$this->cacheFile = $cacheFile; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function build() { |
47
|
|
|
$this->sites = $this->siteLookup->getSites(); |
|
|
|
|
48
|
|
|
$this->cacheSites( $this->sites->getArrayCopy() ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Site[] $sites |
53
|
|
|
* |
54
|
|
|
* @throws MWException if in manualRecache mode |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
private function cacheSites( array $sites ) { |
58
|
|
|
$sitesArray = []; |
59
|
|
|
|
60
|
|
|
foreach ( $sites as $site ) { |
61
|
|
|
$globalId = $site->getGlobalId(); |
62
|
|
|
$sitesArray[$globalId] = $this->getSiteAsArray( $site ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$json = json_encode( [ |
66
|
|
|
'sites' => $sitesArray |
67
|
|
|
] ); |
68
|
|
|
|
69
|
|
|
$result = file_put_contents( $this->cacheFile, $json ); |
70
|
|
|
|
71
|
|
|
return $result !== false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Site $site |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
private function getSiteAsArray( Site $site ) { |
80
|
|
|
$siteEntry = unserialize( $site->serialize() ); |
81
|
|
|
$siteIdentifiers = $this->buildLocalIdentifiers( $site ); |
82
|
|
|
$identifiersArray = []; |
83
|
|
|
|
84
|
|
|
foreach ( $siteIdentifiers as $identifier ) { |
85
|
|
|
$identifiersArray[] = $identifier; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$siteEntry['identifiers'] = $identifiersArray; |
89
|
|
|
|
90
|
|
|
return $siteEntry; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Site $site |
95
|
|
|
* |
96
|
|
|
* @return array Site local identifiers |
97
|
|
|
*/ |
98
|
|
|
private function buildLocalIdentifiers( Site $site ) { |
99
|
|
|
$localIds = []; |
100
|
|
|
|
101
|
|
View Code Duplication |
foreach ( $site->getLocalIds() as $idType => $ids ) { |
102
|
|
|
foreach ( $ids as $id ) { |
103
|
|
|
$localIds[] = [ |
104
|
|
|
'type' => $idType, |
105
|
|
|
'key' => $id |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $localIds; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: