Dispositif /
Wikibot
| 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\Domain\Publisher; |
||
| 11 | |||
| 12 | use App\Domain\InfrastructurePorts\WikidataAdapterInterface; |
||
| 13 | use App\Domain\Models\Wiki\OuvrageTemplate; |
||
| 14 | use App\Domain\Utils\TextUtil; |
||
| 15 | use App\Domain\Utils\WikiTextUtil; |
||
| 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 readonly OuvrageTemplate $ouvrage; |
||
| 30 | private readonly array $infos; |
||
| 31 | public $log = []; |
||
| 32 | /** |
||
| 33 | * @var array|null |
||
| 34 | */ |
||
| 35 | private $data; // article title |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Wikidata2Ouvrage constructor. |
||
| 39 | * |
||
| 40 | * @throws Exception |
||
| 41 | */ |
||
| 42 | public function __construct(private readonly WikidataAdapterInterface $adapter, OuvrageTemplate $ouvrage, private readonly ?string $title = null) |
||
| 43 | { |
||
| 44 | $clone = clone $ouvrage; |
||
| 45 | $this->infos = $clone->getInfos(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 46 | $clone->setInfos([]); // suppression Infos |
||
| 47 | $clone->setDataSource('WikiData'); |
||
| 48 | $this->ouvrage = $clone; |
||
|
0 ignored issues
–
show
|
|||
| 49 | $this->complete(); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function getOuvrage(): OuvrageTemplate |
||
| 53 | { |
||
| 54 | return $this->ouvrage; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * quick and dirty. |
||
| 59 | * |
||
| 60 | * @throws Exception |
||
| 61 | */ |
||
| 62 | private function complete(): void |
||
| 63 | { |
||
| 64 | $this->data = $this->adapter->getDataByInfos($this->infos); |
||
| 65 | |||
| 66 | if (empty($this->data)) { |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | $this->completeAuthorLink(); |
||
| 72 | $this->completeTitleLink(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @throws Exception |
||
| 77 | */ |
||
| 78 | private function completeAuthorLink(): void |
||
| 79 | { |
||
| 80 | // Note : auteur1 non wikifié puisque venant de BnF |
||
| 81 | if (!empty($this->data['articleAuthor']) && !empty($this->data['articleAuthor']['value']) |
||
| 82 | && !empty($this->ouvrage->getParam('lien auteur1')) |
||
| 83 | && !empty($this->title) |
||
| 84 | ) { |
||
| 85 | // ajout wikilien auteur1 |
||
| 86 | $lienTitre = $this->wikiURL2title($this->data['articleAuthor']['value']); |
||
| 87 | |||
| 88 | if (empty($lienTitre) || TextUtil::mb_ucfirst($lienTitre) === TextUtil::mb_ucfirst($this->title)) { |
||
| 89 | // skip wikilink if this is the article title |
||
| 90 | return; |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->ouvrage->setParam('lien auteur1', $lienTitre); |
||
| 94 | dump('Wikidata2Ouvrage: +lien auteur1='.$lienTitre); |
||
| 95 | $this->log[] = '+lien auteur1'; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * TODO : move to WikiTextUtil ? |
||
| 101 | * "https://fr.wikipedia.org/wiki/Michel_Houellebecq" => "Michel Houellebecq". |
||
| 102 | * |
||
| 103 | * @param $wikiURL |
||
| 104 | */ |
||
| 105 | private function wikiURL2title($wikiURL): ?string |
||
| 106 | { |
||
| 107 | $lienTitre = str_replace( |
||
| 108 | ['https://fr.wikipedia.org/wiki/', '_'], |
||
| 109 | ['', ' '], |
||
| 110 | (string) $wikiURL |
||
| 111 | ); |
||
| 112 | |||
| 113 | return trim(urldecode($lienTitre)); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @throws Exception |
||
| 118 | */ |
||
| 119 | private function completeTitleLink(): void |
||
| 120 | { |
||
| 121 | if (!empty($this->data['articleBook']) && !empty($this->data['articleBook']['value']) |
||
| 122 | && !empty($this->ouvrage->getParam('lien titre')) |
||
| 123 | && $this->ouvrage->getParam('titre') !== null |
||
| 124 | && !WikiTextUtil::isWikify($this->ouvrage->getParam('titre')) |
||
| 125 | && !empty($this->title) |
||
| 126 | ) { |
||
| 127 | // ajout wikilien titre |
||
| 128 | // "https://fr.wikipedia.org/wiki/La_Carte_et_le_Territoire" |
||
| 129 | $lienTitre = $this->wikiURL2title($this->data['articleBook']['value']); |
||
| 130 | if (TextUtil::mb_ucfirst($lienTitre) === TextUtil::mb_ucfirst($this->title)) { |
||
| 131 | // skip wikilink if this is the article title |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | $this->ouvrage->setParam('lien titre', $lienTitre); |
||
| 135 | dump('Wikidata2Ouvrage: +lien titre='.$lienTitre); |
||
| 136 | $this->log[] = '+lien titre'; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | } |
||
| 141 |