|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\Repo\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Wikibase\Lib\Interactors\ConfigurableTermSearchInteractor; |
|
6
|
|
|
use Wikibase\Lib\Interactors\TermSearchOptions; |
|
7
|
|
|
use Wikibase\Lib\Interactors\TermSearchResult; |
|
8
|
|
|
use Wikibase\Lib\TermIndexEntry; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Helper class to search for entities. |
|
12
|
|
|
* |
|
13
|
|
|
* @license GPL-2.0-or-later |
|
14
|
|
|
* @author Bene* < [email protected] > |
|
15
|
|
|
*/ |
|
16
|
|
|
class EntityTermSearchHelper implements EntitySearchHelper { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ConfigurableTermSearchInteractor |
|
20
|
|
|
*/ |
|
21
|
|
|
private $termSearchInteractor; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param ConfigurableTermSearchInteractor $termSearchInteractor |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct( |
|
27
|
|
|
ConfigurableTermSearchInteractor $termSearchInteractor |
|
28
|
|
|
) { |
|
29
|
|
|
$this->termSearchInteractor = $termSearchInteractor; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Gets exact matches. If there are not enough exact matches, it gets prefixed matches. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $text |
|
36
|
|
|
* @param string $languageCode |
|
37
|
|
|
* @param string $entityType |
|
38
|
|
|
* @param int $limit |
|
39
|
|
|
* @param bool $strictLanguage |
|
40
|
|
|
* |
|
41
|
|
|
* @return TermSearchResult[] Key: string Serialized EntityId |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getRankedSearchResults( $text, $languageCode, $entityType, $limit, $strictLanguage ) { |
|
44
|
|
|
$allSearchResults = []; |
|
45
|
|
|
|
|
46
|
|
|
$exactSearchResults = $this->searchEntities( |
|
47
|
|
|
$text, |
|
48
|
|
|
$languageCode, |
|
49
|
|
|
$entityType, |
|
50
|
|
|
$limit, |
|
51
|
|
|
false, |
|
52
|
|
|
$strictLanguage |
|
53
|
|
|
); |
|
54
|
|
|
$allSearchResults = $this->mergeSearchResults( $allSearchResults, $exactSearchResults, $limit ); |
|
55
|
|
|
|
|
56
|
|
|
// If still not enough matched then search for prefix matches |
|
57
|
|
|
$missing = $limit - count( $allSearchResults ); |
|
58
|
|
|
if ( $missing > 0 ) { |
|
59
|
|
|
$prefixSearchResults = $this->searchEntities( |
|
60
|
|
|
$text, |
|
61
|
|
|
$languageCode, |
|
62
|
|
|
$entityType, |
|
63
|
|
|
$limit, // needs to be the full limit as exact matches are also contained in the prefix search |
|
64
|
|
|
true, |
|
65
|
|
|
$strictLanguage |
|
66
|
|
|
); |
|
67
|
|
|
$allSearchResults = $this->mergeSearchResults( $allSearchResults, $prefixSearchResults, $limit ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $allSearchResults; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param TermSearchResult[] $searchResults |
|
75
|
|
|
* @param TermSearchResult[] $newSearchResults |
|
76
|
|
|
* @param int $limit |
|
77
|
|
|
* |
|
78
|
|
|
* @return TermSearchResult[] |
|
79
|
|
|
*/ |
|
80
|
|
|
private function mergeSearchResults( array $searchResults, array $newSearchResults, $limit ) { |
|
81
|
|
|
$searchResultEntityIdSerializations = array_keys( $searchResults ); |
|
82
|
|
|
|
|
83
|
|
|
foreach ( $newSearchResults as $searchResultToAdd ) { |
|
84
|
|
|
$entityIdString = $searchResultToAdd->getEntityId()->getSerialization(); |
|
85
|
|
|
|
|
86
|
|
|
if ( !in_array( $entityIdString, $searchResultEntityIdSerializations ) ) { |
|
87
|
|
|
$searchResults[$entityIdString] = $searchResultToAdd; |
|
88
|
|
|
$searchResultEntityIdSerializations[] = $entityIdString; |
|
89
|
|
|
$missing = $limit - count( $searchResults ); |
|
90
|
|
|
|
|
91
|
|
|
if ( $missing <= 0 ) { |
|
92
|
|
|
return $searchResults; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $searchResults; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Wrapper around TermSearchInteractor::searchForEntities |
|
102
|
|
|
* |
|
103
|
|
|
* @see TermSearchInteractor::searchForEntities |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $text |
|
106
|
|
|
* @param string $languageCode |
|
107
|
|
|
* @param string $entityType |
|
108
|
|
|
* @param int $limit |
|
109
|
|
|
* @param bool $prefixSearch |
|
110
|
|
|
* @param bool $strictLanguage |
|
111
|
|
|
* |
|
112
|
|
|
* @return TermSearchResult[] |
|
113
|
|
|
*/ |
|
114
|
|
|
private function searchEntities( $text, $languageCode, $entityType, $limit, $prefixSearch, $strictLanguage ) { |
|
115
|
|
|
$searchOptions = new TermSearchOptions(); |
|
116
|
|
|
$searchOptions->setLimit( $limit ); |
|
117
|
|
|
$searchOptions->setIsPrefixSearch( $prefixSearch ); |
|
118
|
|
|
$searchOptions->setIsCaseSensitive( false ); |
|
119
|
|
|
$searchOptions->setUseLanguageFallback( !$strictLanguage ); |
|
120
|
|
|
|
|
121
|
|
|
$this->termSearchInteractor->setTermSearchOptions( $searchOptions ); |
|
122
|
|
|
|
|
123
|
|
|
return $this->termSearchInteractor->searchForEntities( |
|
124
|
|
|
$text, |
|
125
|
|
|
$languageCode, |
|
126
|
|
|
$entityType, |
|
127
|
|
|
[ TermIndexEntry::TYPE_LABEL, TermIndexEntry::TYPE_ALIAS ] |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.