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\Models\Wiki\OuvrageTemplate; |
13
|
|
|
use App\Domain\Utils\TextUtil; |
14
|
|
|
use App\Domain\Utils\WikiTextUtil; |
15
|
|
|
use App\Infrastructure\WikidataAdapter; |
16
|
|
|
use Exception; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Complete un OuvrageTemplate à partir des données Infos[] en faisant |
20
|
|
|
* des requêtes vers Wikidata (ISBN => article du titre livre et ISNI => article |
21
|
|
|
* de l'auteur). |
22
|
|
|
* Class Wikidata2Ouvrage |
23
|
|
|
* |
24
|
|
|
* @package App\Domain\Publisher |
25
|
|
|
*/ |
26
|
|
|
class Wikidata2Ouvrage |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
private $ouvrage; |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $infos; |
34
|
|
|
private $adapter; |
35
|
|
|
public $log = []; |
36
|
|
|
/** |
37
|
|
|
* @var array|null |
38
|
|
|
*/ |
39
|
|
|
private $data; |
40
|
|
|
/** |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
private $title; // article title |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Wikidata2Ouvrage constructor. |
47
|
|
|
* |
48
|
|
|
* @param WikidataAdapter $wdAdapter |
49
|
|
|
* @param OuvrageTemplate $ouvrage |
50
|
|
|
* @param string|null $title |
51
|
|
|
* |
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
|
|
public function __construct(WikidataAdapter $wdAdapter, OuvrageTemplate $ouvrage, ?string $title = null) |
55
|
|
|
{ |
56
|
|
|
$this->adapter = $wdAdapter; |
57
|
|
|
|
58
|
|
|
$clone = clone $ouvrage; |
59
|
|
|
$this->title = $title; |
60
|
|
|
$this->infos = $clone->getInfos(); |
61
|
|
|
$clone->setInfos([]); // suppression Infos |
62
|
|
|
$clone->setDataSource('WikiData'); |
63
|
|
|
$this->ouvrage = $clone; |
64
|
|
|
$this->complete(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getOuvrage(): OuvrageTemplate |
68
|
|
|
{ |
69
|
|
|
return $this->ouvrage; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* quick and dirty. |
74
|
|
|
* |
75
|
|
|
* @throws Exception |
76
|
|
|
*/ |
77
|
|
|
private function complete(): void |
78
|
|
|
{ |
79
|
|
|
$this->data = $this->adapter->getDataByInfos($this->infos); |
80
|
|
|
|
81
|
|
|
if (empty($this->data)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
$this->completeAuthorLink(); |
87
|
|
|
$this->completeTitleLink(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return void |
92
|
|
|
* @throws Exception |
93
|
|
|
*/ |
94
|
|
|
private function completeAuthorLink(): void |
95
|
|
|
{ |
96
|
|
|
// Note : auteur1 non wikifié puisque venant de BnF |
97
|
|
|
if (!empty($this->data['articleAuthor']) && !empty($this->data['articleAuthor']['value']) |
98
|
|
|
&& !empty( |
99
|
|
|
$this->ouvrage->getParam('lien auteur1') |
100
|
|
|
&& !empty($this->title) |
101
|
|
|
) |
102
|
|
|
) { |
103
|
|
|
// ajout wikilien auteur1 |
104
|
|
|
$lienTitre = $this->wikiURL2title($this->data['articleAuthor']['value']); |
105
|
|
|
|
106
|
|
|
if (empty($lienTitre) || TextUtil::mb_ucfirst($lienTitre) === TextUtil::mb_ucfirst($this->title)) { |
|
|
|
|
107
|
|
|
// skip wikilink if this is the article title |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->ouvrage->setParam('lien auteur1', $lienTitre); |
112
|
|
|
dump('Wikidata2Ouvrage: +lien auteur1='.$lienTitre); |
113
|
|
|
$this->log[] = '+lien auteur1'; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* TODO : move to WikiTextUtil ? |
119
|
|
|
* "https://fr.wikipedia.org/wiki/Michel_Houellebecq" => "Michel Houellebecq". |
120
|
|
|
* |
121
|
|
|
* @param $wikiURL |
122
|
|
|
* |
123
|
|
|
* @return string|null |
124
|
|
|
*/ |
125
|
|
|
private function wikiURL2title($wikiURL): ?string |
126
|
|
|
{ |
127
|
|
|
$lienTitre = str_replace( |
128
|
|
|
['https://fr.wikipedia.org/wiki/', '_'], |
129
|
|
|
['', ' '], |
130
|
|
|
$wikiURL |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
return trim(urldecode($lienTitre)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @throws Exception |
138
|
|
|
*/ |
139
|
|
|
private function completeTitleLink(): void |
140
|
|
|
{ |
141
|
|
|
if (!empty($this->data['articleBook']) && !empty($this->data['articleBook']['value']) |
142
|
|
|
&& !empty($this->ouvrage->getParam('lien titre')) |
143
|
|
|
&& false === WikiTextUtil::isWikify($this->ouvrage->getParam('titre')) |
|
|
|
|
144
|
|
|
&& !empty($this->title) |
145
|
|
|
) { |
146
|
|
|
// ajout wikilien titre |
147
|
|
|
// "https://fr.wikipedia.org/wiki/La_Carte_et_le_Territoire" |
148
|
|
|
$lienTitre = $this->wikiURL2title($this->data['articleBook']['value']); |
149
|
|
|
if (TextUtil::mb_ucfirst($lienTitre) === TextUtil::mb_ucfirst($this->title)) { |
150
|
|
|
// skip wikilink if this is the article title |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
$this->ouvrage->setParam('lien titre', $lienTitre); |
154
|
|
|
dump('Wikidata2Ouvrage: +lien titre='.$lienTitre); |
155
|
|
|
$this->log[] = '+lien titre'; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|