|
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
|
|
|
|
|
11
|
|
|
namespace App\Infrastructure; |
|
12
|
|
|
|
|
13
|
|
|
use App\Domain\Exceptions\ConfigException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Dirty. |
|
17
|
|
|
* Class CirrusSearch |
|
18
|
|
|
* |
|
19
|
|
|
* @package App\Infrastructure |
|
20
|
|
|
*/ |
|
21
|
|
|
class CirrusSearch implements PageListInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $url; |
|
27
|
|
|
private $options = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* CirrusSearch constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string|null $url |
|
33
|
|
|
* @param array|null $options |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(?string $url = null, ?array $options=[]) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->url = $url; |
|
38
|
|
|
$this->options = $options; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array|null |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getOptions(): ?array |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->options; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array|null $options |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setOptions(?array $options): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->options = $options; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function setUrl(string $url) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->url = $url; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* TODO: use Wiki API library or Guzzle |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
* @throws ConfigException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getPageTitles(): array |
|
69
|
|
|
{ |
|
70
|
|
|
if (!$this->url) { |
|
71
|
|
|
throw new ConfigException('CirrusSearch null URL'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$json = file_get_contents($this->url); |
|
75
|
|
|
if (false === $json) { |
|
76
|
|
|
return []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$myArray = json_decode($json, true); |
|
80
|
|
|
$result = $myArray['query']['search']; |
|
81
|
|
|
if (empty($result)) { |
|
82
|
|
|
return []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$titles = []; |
|
86
|
|
|
foreach ($result as $res) { |
|
87
|
|
|
$titles[] = trim($res['title']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if(isset($this->options['reverse']) && $this->options['reverse'] === true ) { |
|
91
|
|
|
krsort($titles); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $titles; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|