Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.33% |
28 / 30 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
TermSearchApiInteractor | |
93.33% |
28 / 30 |
|
33.33% |
1 / 3 |
11.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
searchForEntities | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
4.00 | |||
parseDatum | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
6.03 |
1 | <?php |
2 | |
3 | namespace ArticlePlaceholder; |
4 | |
5 | use Wikibase\DataModel\Entity\EntityIdParser; |
6 | use Wikibase\DataModel\Term\Term; |
7 | use Wikibase\Lib\Interactors\TermSearchInteractor; |
8 | use Wikibase\Lib\Interactors\TermSearchResult; |
9 | |
10 | /** |
11 | * Repository's API-based term search interactor |
12 | * |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class TermSearchApiInteractor implements TermSearchInteractor { |
16 | |
17 | /** |
18 | * @var RepoApiInteractor |
19 | */ |
20 | private $repoApiInteractor; |
21 | |
22 | /** |
23 | * @var EntityIdParser |
24 | */ |
25 | private $entityIdParser; |
26 | |
27 | /** |
28 | * @param RepoApiInteractor $repoApiInteractor |
29 | * @param EntityIdParser $entityIdParser |
30 | */ |
31 | public function __construct( |
32 | RepoApiInteractor $repoApiInteractor, |
33 | EntityIdParser $entityIdParser |
34 | ) { |
35 | $this->repoApiInteractor = $repoApiInteractor; |
36 | $this->entityIdParser = $entityIdParser; |
37 | } |
38 | |
39 | /** |
40 | * @param string $text Term text to search for |
41 | * @param string $languageCode Language code to search in |
42 | * @param string $entityType Type of Entity to return |
43 | * @param string[] $termTypes Types of Term to return, array of Wikibase\Lib\TermIndexEntry::TYPE_* |
44 | * |
45 | * @return TermSearchResult[] |
46 | */ |
47 | public function searchForEntities( $text, $languageCode, $entityType, array $termTypes ) { |
48 | $params = [ |
49 | 'action' => 'wbsearchentities', |
50 | 'language' => $languageCode, |
51 | 'strictlanguage' => 1, |
52 | 'search' => $text, |
53 | 'format' => 'json', |
54 | 'type' => $entityType |
55 | ]; |
56 | |
57 | $data = $this->repoApiInteractor->request( $params ); |
58 | if ( !isset( $data['search'] ) ) { |
59 | return []; |
60 | } |
61 | |
62 | $results = []; |
63 | foreach ( $data['search'] as $datum ) { |
64 | $result = $this->parseDatum( $datum, $languageCode ); |
65 | if ( $result ) { |
66 | $results[] = $result; |
67 | } |
68 | } |
69 | return $results; |
70 | } |
71 | |
72 | /** |
73 | * @param array $datum |
74 | * @param string $languageCode |
75 | * @return null|TermSearchResult |
76 | */ |
77 | private function parseDatum( array $datum, $languageCode ) { |
78 | if ( !isset( $datum['title'] ) || |
79 | !isset( $datum['match']['text'] ) || |
80 | !isset( $datum['match']['type'] ) |
81 | ) { |
82 | return null; |
83 | } |
84 | |
85 | return new TermSearchResult( |
86 | new Term( $languageCode, $datum['match']['text'] ), |
87 | $datum['match']['type'], |
88 | $this->entityIdParser->parse( $datum['title'] ), |
89 | isset( $datum['label'] ) ? new Term( $languageCode, $datum['label'] ) : null, |
90 | isset( $datum['description'] ) ? new Term( $languageCode, $datum['description'] ) : null |
91 | ); |
92 | } |
93 | |
94 | } |