1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\Console\Commands; |
5
|
|
|
|
6
|
|
|
use App\Src\UseCases\Infra\Sql\Model\PageModel; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
|
12
|
|
|
class ImportPagesWithIconAndTypeFromWiki extends Command |
13
|
|
|
{ |
14
|
|
|
protected $signature = 'pages:import-with-icons-type'; |
15
|
|
|
|
16
|
|
|
protected $description = 'Import pages with icons and types from the wiki'; |
17
|
|
|
|
18
|
|
|
protected $httpClient; |
19
|
|
|
protected $queryPicture = '?action=query&redirects=true&format=json&prop=imageinfo&iiprop=url&titles='; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
parent::__construct(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function handle() |
27
|
|
|
{ |
28
|
|
|
$this->httpClient = new Client(); |
29
|
|
|
Storage::makeDirectory('public/pages'); |
30
|
|
|
$queryPages = "?action=ask&format=json&api_version=3&query=[[A un type de page::%2B]]|?A un fichier d'icone de caractéristique|?A un type de page"; |
31
|
|
|
|
32
|
|
|
$pagesApiUri = config('wiki.api_uri').$queryPages; |
33
|
|
|
|
34
|
|
|
$response = $this->httpClient->get($pagesApiUri); |
35
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
36
|
|
|
|
37
|
|
|
$pages = $content['query']['results']; |
38
|
|
|
|
39
|
|
|
$this->handlePages($pages); |
40
|
|
|
$continue = $content['query-continue-offset'] ?? null; |
41
|
|
|
|
42
|
|
|
while($continue !== null && $continue !== ''){ |
43
|
|
|
$this->info($continue); |
44
|
|
|
|
45
|
|
|
$pagesApiUri = config('wiki.api_uri').$queryPages.'|offset='.$continue; |
46
|
|
|
$response = $this->httpClient->get($pagesApiUri); |
47
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
48
|
|
|
$pages = $content['query']['results']; |
49
|
|
|
$this->handlePages($pages); |
50
|
|
|
$continue = $content['query-continue-offset'] ?? null; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function handlePages($pages) |
55
|
|
|
{ |
56
|
|
|
foreach ($pages as $page) { |
57
|
|
|
$title = key($page); |
58
|
|
|
$page = last($page); |
59
|
|
|
$typePage = last($page['printouts']['A un type de page']); |
60
|
|
|
$icon = last($page['printouts']['A un fichier d\'icone de caractéristique']); |
61
|
|
|
|
62
|
|
|
$pageModel = PageModel::where('title', $title)->first(); |
63
|
|
|
if (!isset($pageModel)) { |
64
|
|
|
$this->info('Page not found : '.$title); |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if($icon !== false) { |
69
|
|
|
$picturesApiUri = config('wiki.api_uri').$this->queryPicture.$icon['fulltext']; |
70
|
|
|
|
71
|
|
|
$response = $this->httpClient->get($picturesApiUri); |
72
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
73
|
|
|
$picturesInfo = $content['query']['pages']; |
74
|
|
|
foreach($picturesInfo as $picture) { |
75
|
|
|
if (isset(last($picture['imageinfo'])['url'])) { |
76
|
|
|
try { |
77
|
|
|
$response = $this->httpClient->get(last($picture['imageinfo'])['url']); |
78
|
|
|
$content = $response->getBody()->getContents(); |
79
|
|
|
$path = 'public/pages/' . $pageModel->id . '.png'; |
80
|
|
|
Storage::put('public/pages/' . $pageModel->id . '.png', $content); |
81
|
|
|
}catch (ClientException $e){ |
82
|
|
|
$path = ''; |
83
|
|
|
} |
84
|
|
|
}else{ |
85
|
|
|
$path = ''; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$pageModel->icon = $path; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$pageModel->type = $typePage !== false ? $typePage : ''; |
92
|
|
|
$pageModel->save(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|