|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of dispositif/wikibot application |
|
4
|
|
|
* 2019 © 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\Application\PublisherAction; |
|
13
|
|
|
use App\Domain\Models\Wiki\ArticleOrLienBriseInterface; |
|
14
|
|
|
use App\Domain\WikiTemplateFactory; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use Throwable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* news URL to {{Article}}. |
|
20
|
|
|
* Class ArticleFromURL |
|
21
|
|
|
* |
|
22
|
|
|
* @package App\Application\Examples |
|
23
|
|
|
*/ |
|
24
|
|
|
class ArticleFromURL |
|
25
|
|
|
{ |
|
26
|
|
|
private $publisherAction; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ArticleOrLienBriseInterface|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $article; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $url; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(PublisherAction $publisher) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->publisherAction = $publisher; |
|
39
|
|
|
$this->url = $publisher->getUrl(); |
|
40
|
|
|
try { |
|
41
|
|
|
$this->article = $this->process(); |
|
42
|
|
|
} catch (Exception $e) { |
|
43
|
|
|
dump($e); |
|
44
|
|
|
die; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @throws Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
private function process(): ?ArticleOrLienBriseInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$mapper = PublisherMapperFactory::fromURL($this->url); |
|
54
|
|
|
if (!$mapper) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
sleep(10); |
|
58
|
|
|
$arrayLD = []; |
|
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
$html = $this->publisherAction->getHTMLSource(); |
|
61
|
|
|
$htmlData = $this->publisherAction->extractWebData($html); |
|
62
|
|
|
} catch (Throwable $e) { |
|
63
|
|
|
if (strpos($e->getMessage(), '404') !== false) { |
|
64
|
|
|
dump('****** lien brisé !!!!'); |
|
65
|
|
|
$lienBrise = WikiTemplateFactory::create('lien brisé'); |
|
66
|
|
|
$lienBrise->hydrate(['url' => $this->url, 'titre' => 'Article de presse', 'brisé le' => date('d-m-Y')]); |
|
67
|
|
|
|
|
68
|
|
|
return $lienBrise; // ok |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
echo "*** Erreur ".$e->getMessage()."\n"; |
|
71
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (empty($htmlData)) { |
|
76
|
|
|
echo "*** Pas de data Json-LD ou meta\n"; |
|
77
|
|
|
|
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
$htmlData['url'] = $this->url; |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
$articleData = $mapper->process($htmlData); |
|
84
|
|
|
} catch (Throwable $e) { |
|
85
|
|
|
echo sprintf( |
|
86
|
|
|
"SKIP : %s %s:%s \n", |
|
87
|
|
|
$e->getMessage(), |
|
88
|
|
|
$e->getFile(), |
|
89
|
|
|
$e->getLine() |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (!empty($articleData)) { |
|
96
|
|
|
$article = WikiTemplateFactory::create('article'); |
|
97
|
|
|
$article->hydrate($articleData); |
|
98
|
|
|
|
|
99
|
|
|
return $article; // ok |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getResult(): ?ArticleOrLienBriseInterface |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->article; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.