1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Repository; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
8
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A WikiDomainRepository is responsible for fetching a list of all wikis from the Sitematrix API. |
12
|
|
|
*/ |
13
|
|
|
class WikiDomainRepository |
14
|
|
|
{ |
15
|
|
|
/** @var GuzzleClient */ |
16
|
|
|
private $guzzle; |
17
|
|
|
|
18
|
|
|
/** @var CacheItemPoolInterface */ |
19
|
|
|
private $cache; |
20
|
|
|
|
21
|
|
|
private const CACHE_KEY = 'global-search-wikidomainlookup'; |
22
|
|
|
|
23
|
|
|
/** @var string Duration of cache for lookup table, as accepted by DateInterval::createFromDateString() */ |
24
|
|
|
private const CACHE_TIME = '10 hours'; |
25
|
|
|
|
26
|
|
|
public function __construct(GuzzleClient $guzzle, CacheItemPoolInterface $cache) |
27
|
|
|
{ |
28
|
|
|
$this->guzzle = $guzzle; |
29
|
|
|
$this->cache = $cache; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Load the site matrix, either from cache or using the sitematrix API. |
34
|
|
|
* @return string[] |
35
|
|
|
*/ |
36
|
|
|
public function load(): array |
37
|
|
|
{ |
38
|
|
|
$cacheItem = $this->cache->getItem(self::CACHE_KEY); |
39
|
|
|
$lookup = $cacheItem->get(); |
40
|
|
|
if (null === $lookup) { |
41
|
|
|
$lookup = $this->loadUncached(); |
42
|
|
|
$cacheItem->set($lookup) |
43
|
|
|
->expiresAfter(\DateInterval::createFromDateString(self::CACHE_TIME)); |
44
|
|
|
$this->cache->save($cacheItem); |
45
|
|
|
} |
46
|
|
|
return $lookup; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Fetch the site matrix from the API. |
51
|
|
|
* @return string[] |
52
|
|
|
*/ |
53
|
|
|
public function loadUncached(): array |
54
|
|
|
{ |
55
|
|
|
$res = $this->guzzle->request('GET', 'https://meta.wikimedia.org/w/api.php', [ |
56
|
|
|
'query' => [ |
57
|
|
|
'format' => 'json', |
58
|
|
|
'formatversion' => 2, |
59
|
|
|
'action' => 'sitematrix', |
60
|
|
|
'smlangprop' => 'site', |
61
|
|
|
'smsiteprop' => 'url|dbname', |
62
|
|
|
], |
63
|
|
|
]); |
64
|
|
|
$decoded = json_decode($res->getBody()->getContents(), true)['sitematrix']; |
65
|
|
|
$lookup = []; |
66
|
|
|
foreach ($decoded as $k => $v) { |
67
|
|
|
if ('count' === $k) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
$sites = 'specials' === $k ? $v : $v['site']; |
71
|
|
|
foreach ($sites as $site) { |
72
|
|
|
$lookup[$site['dbname']] = parse_url($site['url'], PHP_URL_HOST); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $lookup; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|