1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
4
|
|
|
* 2019-2023 © Philippe M./Irønie <[email protected]> |
5
|
|
|
* For the full copyright and MIT license information, view the license file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Infrastructure; |
11
|
|
|
|
12
|
|
|
use App\Domain\InfrastructurePorts\DeadlinkArchiverInterface; |
13
|
|
|
use App\Domain\InfrastructurePorts\ExternHttpClientInterface; |
14
|
|
|
use App\Domain\Models\WebarchiveDTO; |
15
|
|
|
use DateTimeImmutable; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Psr\Log\NullLogger; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get archive url from Wikiwix webarchiver API. |
22
|
|
|
* |
23
|
|
|
* JSON response example: |
24
|
|
|
* "status": 200, |
25
|
|
|
* "contenttype": "text\/html; charset=utf-8", |
26
|
|
|
* "timestamp": 912380400, |
27
|
|
|
* "datetime": "19981130000000", |
28
|
|
|
* "longformurl": "https:\/\/archive.wikiwix.com\/cache\/19981130000000\/http:\/\/casamaures.org\/keskispas.php?lng=fr&pg=1064" |
29
|
|
|
*/ |
30
|
|
|
class WikiwixAdapter implements DeadlinkArchiverInterface |
31
|
|
|
{ |
32
|
|
|
public const ARCHIVER_NAME = '[[Wikiwix]]'; |
33
|
|
|
private const API_URL = 'https://archive.wikiwix.com/cache/index2.php?apiresponse=1&url='; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
protected readonly ExternHttpClientInterface $externHttpClient, |
37
|
|
|
protected readonly LoggerInterface $log = new NullLogger() |
38
|
|
|
) |
39
|
|
|
{ |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function searchWebarchive(string $url): ?WebarchiveDTO |
43
|
|
|
{ |
44
|
|
|
$archiveData = $this->requestWikiwixApi($url); |
45
|
|
|
if (empty($archiveData['longformurl'])) { |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return new WebarchiveDTO( |
50
|
|
|
self::ARCHIVER_NAME, |
51
|
|
|
$url, |
52
|
|
|
(string) $archiveData['longformurl'], |
53
|
|
|
$archiveData['timestamp'] |
54
|
|
|
? DateTimeImmutable::createFromFormat('U', (string)$archiveData['timestamp']) |
55
|
|
|
: null |
56
|
|
|
); // todo factory ? |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function requestWikiwixApi(string $url): array |
60
|
|
|
{ |
61
|
|
|
$response = $this->externHttpClient->getClient()->request( |
|
|
|
|
62
|
|
|
'GET', |
63
|
|
|
self::API_URL . urlencode($url) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if (!$response instanceof ResponseInterface || $response->getStatusCode() !== 200) { |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
$jsonString = $response->getBody()->getContents(); |
70
|
|
|
$data = json_decode($jsonString, true) ?? []; |
71
|
|
|
|
72
|
|
|
// check wikiwix archive status |
73
|
|
|
if (empty($data['status']) || (int) $data['status'] !== 200) { |
74
|
|
|
$this->log->debug('WikiwixAdapter response: ' . $jsonString); |
75
|
|
|
|
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $data; |
80
|
|
|
} |
81
|
|
|
} |