|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\Lib\Sites; |
|
4
|
|
|
|
|
5
|
|
|
use Site; |
|
6
|
|
|
use SiteStore; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Builds the site identifiers table |
|
10
|
|
|
* |
|
11
|
|
|
* @note: this should move out of Wikibase |
|
12
|
|
|
* |
|
13
|
|
|
* @license GPL-2.0-or-later |
|
14
|
|
|
* @author Katie Filbert < [email protected] > |
|
15
|
|
|
*/ |
|
16
|
|
|
class SitesBuilder { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var SiteStore |
|
20
|
|
|
*/ |
|
21
|
|
|
private $store; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $validGroups; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param SiteStore $store |
|
30
|
|
|
* @param string[] $validGroups |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( SiteStore $store, array $validGroups ) { |
|
33
|
|
|
$this->store = $store; |
|
34
|
|
|
$this->validGroups = $validGroups; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Site[] $sites |
|
39
|
|
|
* @param string|null $siteGroup |
|
40
|
|
|
* @param string|null $wikiId |
|
41
|
|
|
*/ |
|
42
|
|
|
public function buildStore( array $sites, $siteGroup = null, $wikiId = null ) { |
|
43
|
|
|
if ( $siteGroup === null && is_string( $wikiId ) ) { |
|
44
|
|
|
$siteGroup = $this->getInterwikiGroup( $sites, $wikiId ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ( $siteGroup && in_array( $siteGroup, $this->validGroups ) ) { |
|
|
|
|
|
|
48
|
|
|
$sites = $this->addInterwikiIdsToGroup( $sites, $siteGroup ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$existingSites = $this->store->getSites(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ( $sites as $site ) { |
|
54
|
|
|
$siteId = $site->getGlobalId(); |
|
55
|
|
|
|
|
56
|
|
|
if ( $existingSites->hasSite( $siteId ) ) { |
|
57
|
|
|
$existingSite = $existingSites->getSite( $siteId ); |
|
58
|
|
|
$site->setInternalId( $existingSite->getInternalId() ); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->store->saveSites( $sites ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Site[] $sites |
|
67
|
|
|
* @param string $siteGroup |
|
68
|
|
|
* |
|
69
|
|
|
* @return Site[] |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function addInterwikiIdsToGroup( array $sites, $siteGroup ) { |
|
72
|
|
|
foreach ( $sites as $site ) { |
|
73
|
|
|
if ( $site->getGroup() === $siteGroup ) { |
|
74
|
|
|
$localId = $site->getLanguageCode(); |
|
75
|
|
|
|
|
76
|
|
|
if ( $localId ) { |
|
77
|
|
|
$site->addNavigationId( $localId ); |
|
78
|
|
|
$site->addInterwikiId( $localId ); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $sites; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Site[] $sites |
|
88
|
|
|
* @param string $wikiId |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
private function getInterwikiGroup( array $sites, $wikiId ) { |
|
93
|
|
|
if ( !array_key_exists( $wikiId, $sites ) ) { |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$site = $sites[$wikiId]; |
|
98
|
|
|
|
|
99
|
|
|
// @fixme: handle interwiki prefixes in a better way! |
|
100
|
|
|
if ( preg_match( '/^([\w-]*)wiki$/', $site->getGlobalId() ) ) { |
|
101
|
|
|
return 'wikipedia'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $site->getGroup(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: