Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
DatabaseLookup | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
getDatabases | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
getDBName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\MassMessage\Lookup; |
4 | |
5 | use MediaWiki\MassMessage\UrlHelper; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\WikiMap\WikiMap; |
8 | |
9 | class DatabaseLookup { |
10 | |
11 | /** |
12 | * Get a mapping from site domains to database names. |
13 | * Requires $wgConf to be set up properly. |
14 | * Tries to read from cache if possible. |
15 | * |
16 | * @return array |
17 | */ |
18 | public static function getDatabases() { |
19 | global $wgConf; |
20 | $dbs = $wgConf->getLocalDatabases(); |
21 | $configHash = md5( implode( '|', $dbs ) ); |
22 | $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
23 | |
24 | return $cache->getWithSetCallback( |
25 | $cache->makeGlobalKey( 'massmessage', 'urltodb', $configHash ), |
26 | $cache::TTL_HOUR, |
27 | static function () use ( $dbs ) { |
28 | $mapping = []; |
29 | foreach ( $dbs as $dbname ) { |
30 | $url = WikiMap::getWiki( $dbname )->getCanonicalServer(); |
31 | $site = UrlHelper::getBaseUrl( $url ); |
32 | $mapping[$site] = $dbname; |
33 | } |
34 | return $mapping; |
35 | } |
36 | ); |
37 | } |
38 | |
39 | /** |
40 | * Get database name from URL hostname or null if nothing is found. |
41 | * |
42 | * @param string $host |
43 | * @return string|null |
44 | */ |
45 | public static function getDBName( $host ) { |
46 | $configuredAliases = MediaWikiServices::getInstance()->getMainConfig()->get( 'MassMessageWikiAliases' ); |
47 | $mapping = self::getDatabases(); |
48 | return $mapping[$host] ?? $configuredAliases[$host] ?? null; |
49 | } |
50 | } |