1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
4
|
|
|
* 2019/2020 © Philippe M. <[email protected]> |
5
|
|
|
* For the full copyright and MIT license information, please view the license file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Domain\Publisher; |
11
|
|
|
|
12
|
|
|
use App\Domain\Utils\ArrayProcessTrait; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Generic mapper for press/revue article on web. |
17
|
|
|
* Using JSON-LD and meta tags to obtain {article} data. |
18
|
|
|
* Generic mapper for web pages URL to wiki-template references. |
19
|
|
|
* Converting to {article}, {lien web} or {lien brisé} |
20
|
|
|
* Using JSON-LD, Open Graph and Dublin Core meta extracted from HTML. |
21
|
|
|
* Class ExternMapper |
22
|
|
|
* |
23
|
|
|
* @package App\Domain\Publisher |
24
|
|
|
*/ |
25
|
|
|
class ExternMapper implements MapperInterface |
26
|
|
|
{ |
27
|
|
|
use ArrayProcessTrait, ExternConverterTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
private $log; |
33
|
|
|
|
34
|
5 |
|
public function __construct(LoggerInterface $log) |
35
|
|
|
{ |
36
|
5 |
|
$this->log = $log; |
37
|
5 |
|
} |
38
|
|
|
|
39
|
5 |
|
public function process($data): array |
40
|
|
|
{ |
41
|
5 |
|
$dat = $this->processMapping($data); |
42
|
|
|
|
43
|
5 |
|
return (!empty($dat)) ? $this->postProcess($dat) : []; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
protected function processMapping($data): array |
47
|
|
|
{ |
48
|
5 |
|
$mapJson = []; |
49
|
5 |
|
$mapMeta = []; |
50
|
5 |
|
if (!empty($data['JSON-LD'])) { |
51
|
3 |
|
$mapJson = $this->processJsonLDMapping($data['JSON-LD']); |
52
|
|
|
} |
53
|
5 |
|
if (!empty($data['meta'])) { |
54
|
5 |
|
$mapMeta = (new OpenGraphMapper())->process($data['meta']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// langue absente JSON-LD mais array_merge risqué (doublon) |
58
|
5 |
|
if (!empty($mapJson)) { |
59
|
3 |
|
if (!isset($mapJson['langue']) && isset($mapMeta['langue'])) { |
60
|
2 |
|
$mapJson['langue'] = $mapMeta['langue']; |
61
|
2 |
|
$mapJson['DATA-TYPE'] = 'JSON-LD+META'; |
62
|
|
|
} |
63
|
|
|
// récupère "accès url" de OpenGraph (prévaut sur JSON:'isAccessibleForFree' |
64
|
3 |
|
if(isset($mapMeta['accès url']) ){ |
65
|
|
|
$mapJson['accès url'] = $mapMeta['accès url']; |
66
|
|
|
$mapJson['DATA-TYPE'] = 'JSON-LD+META'; |
67
|
2 |
|
} |
68
|
|
|
|
69
|
|
|
return $mapJson; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $mapMeta; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* todo move to mapper ? |
77
|
3 |
|
* |
78
|
|
|
* @param array $LDdata |
79
|
3 |
|
* |
80
|
2 |
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private function processJsonLDMapping(array $LDdata): array |
83
|
1 |
|
{ |
84
|
1 |
|
if ($this->checkJSONLD($LDdata)) { |
85
|
1 |
|
return (new JsonLDMapper())->process($LDdata); |
86
|
|
|
} |
87
|
|
|
// gestion des multiples objets comme Figaro |
88
|
|
|
foreach ($LDdata as $dat) { |
89
|
|
|
if (is_array($dat) && $this->checkJSONLD($dat)) { |
90
|
|
|
return (new JsonLDMapper())->process($dat); |
91
|
|
|
} |
92
|
3 |
|
} |
93
|
|
|
|
94
|
3 |
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function checkJSONLD(array $jsonLD): bool |
98
|
|
|
{ |
99
|
|
|
return isset($jsonLD['headline']) && isset($jsonLD['@type']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* todo Refac/move domain special mapping |
104
|
|
|
* todo Config parameter for post-process |
105
|
5 |
|
* |
106
|
|
|
* @param array $dat |
107
|
5 |
|
* |
108
|
5 |
|
* @return array |
109
|
3 |
|
*/ |
110
|
|
|
protected function postProcess(array $dat): array |
111
|
|
|
{ |
112
|
|
|
$dat = $this->deleteEmptyValueArray($dat); |
113
|
5 |
|
if (isset($dat['langue']) && 'fr' === $dat['langue']) { |
114
|
|
|
unset($dat['langue']); |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
// Ça m'énerve ! Gallica met "vidéo" pour livre numérisé |
118
|
|
|
if (isset($dat['site']) && $dat['site'] === 'Gallica') { |
119
|
|
|
unset($dat['format']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $dat; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|