Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.86% |
78 / 84 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
SearchHookHandler | |
92.86% |
78 / 84 |
|
87.50% |
7 / 8 |
17.11 | |
0.00% |
0 / 1 |
newFromGlobalState | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
1 | |||
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
onSpecialSearchResultsAppend | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
addToSearch | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
3 | |||
getTermSearchResults | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
renderTermSearchResults | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
renderTermSearchResult | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
searchEntities | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace ArticlePlaceholder; |
4 | |
5 | use MediaWiki\Config\Config; |
6 | use MediaWiki\Hook\SpecialSearchResultsAppendHook; |
7 | use MediaWiki\MainConfigNames; |
8 | use MediaWiki\MediaWikiServices; |
9 | use MediaWiki\Output\OutputPage; |
10 | use MediaWiki\Specials\SpecialSearch; |
11 | use Wikibase\Client\WikibaseClient; |
12 | use Wikibase\Lib\Interactors\TermSearchInteractor; |
13 | use Wikibase\Lib\Interactors\TermSearchResult; |
14 | use Wikibase\Lib\TermIndexEntry; |
15 | use Wikimedia\Rdbms\SessionConsistentConnectionManager; |
16 | use Wikimedia\Stats\Metrics\CounterMetric; |
17 | |
18 | /** |
19 | * Adding results from ArticlePlaceholder to search |
20 | * |
21 | * @license GPL-2.0-or-later |
22 | * @author Lucie-Aimée Kaffee |
23 | */ |
24 | class SearchHookHandler implements SpecialSearchResultsAppendHook { |
25 | |
26 | /** |
27 | * @var TermSearchInteractor |
28 | */ |
29 | private $termSearchInteractor; |
30 | |
31 | /** |
32 | * @var string |
33 | */ |
34 | private $languageCode; |
35 | |
36 | /** |
37 | * @var ItemNotabilityFilter |
38 | */ |
39 | private $itemNotabilityFilter; |
40 | |
41 | /** |
42 | * @var CounterMetric |
43 | */ |
44 | private $searchesMetric; |
45 | |
46 | /** |
47 | * @param Config $config |
48 | * |
49 | * @return self |
50 | */ |
51 | public static function newFromGlobalState( Config $config ) { |
52 | // TODO inject services into hook handler instance |
53 | $mwServices = MediaWikiServices::getInstance(); |
54 | $repoDB = WikibaseClient::getItemAndPropertySource()->getDatabaseName(); |
55 | $lbFactory = $mwServices->getDBLoadBalancerFactory(); |
56 | $clientSettings = WikibaseClient::getSettings( $mwServices ); |
57 | |
58 | $itemNotabilityFilter = new ItemNotabilityFilter( |
59 | new SessionConsistentConnectionManager( $lbFactory->getMainLB( $repoDB ), $repoDB ), |
60 | WikibaseClient::getEntityNamespaceLookup( $mwServices ), |
61 | WikibaseClient::getStore()->getSiteLinkLookup(), |
62 | $clientSettings->getSetting( 'siteGlobalID' ) |
63 | ); |
64 | |
65 | $statsFactory = $mwServices->getStatsFactory(); |
66 | |
67 | $termSearchInteractor = new TermSearchApiInteractor( |
68 | new RepoApiInteractor( |
69 | $config->get( 'ArticlePlaceholderRepoApiUrl' ), |
70 | $statsFactory, |
71 | $mwServices->getHttpRequestFactory() |
72 | ), |
73 | WikibaseClient::getEntityIdParser() |
74 | ); |
75 | |
76 | return new self( |
77 | $termSearchInteractor, |
78 | $config->get( MainConfigNames::LanguageCode ), |
79 | $itemNotabilityFilter, |
80 | $statsFactory->getCounter( 'ArticlePlaceholder_search_total' ) |
81 | ); |
82 | } |
83 | |
84 | /** |
85 | * @param TermSearchInteractor $termSearchInteractor |
86 | * @param string $languageCode content language |
87 | * @param ItemNotabilityFilter $itemNotabilityFilter |
88 | * @param CounterMetric $searchesMetric |
89 | */ |
90 | public function __construct( |
91 | TermSearchInteractor $termSearchInteractor, |
92 | $languageCode, |
93 | ItemNotabilityFilter $itemNotabilityFilter, |
94 | CounterMetric $searchesMetric |
95 | ) { |
96 | $this->termSearchInteractor = $termSearchInteractor; |
97 | $this->languageCode = $languageCode; |
98 | $this->itemNotabilityFilter = $itemNotabilityFilter; |
99 | $this->searchesMetric = $searchesMetric; |
100 | } |
101 | |
102 | /** |
103 | * @param SpecialSearch $specialSearch |
104 | * @param OutputPage $output |
105 | * @param string $term |
106 | */ |
107 | public function onSpecialSearchResultsAppend( |
108 | $specialSearch, |
109 | $output, |
110 | $term |
111 | ) { |
112 | if ( trim( $term ) === '' ) { |
113 | return; |
114 | } |
115 | |
116 | $config = $specialSearch->getConfig(); |
117 | if ( !$config->get( 'ArticlePlaceholderSearchIntegrationEnabled' ) ) { |
118 | return; |
119 | } |
120 | |
121 | $this->addToSearch( $output, $term ); |
122 | } |
123 | |
124 | /** |
125 | * @param OutputPage $output |
126 | * @param string $term |
127 | */ |
128 | private function addToSearch( OutputPage $output, $term ): void { |
129 | $termSearchResults = $this->getTermSearchResults( $term ); |
130 | |
131 | if ( $termSearchResults ) { |
132 | $renderedTermSearchResults = $this->renderTermSearchResults( $termSearchResults ); |
133 | |
134 | if ( $renderedTermSearchResults !== '' ) { |
135 | $output->addWikiTextAsInterface( |
136 | '==' . |
137 | $output->msg( 'articleplaceholder-search-header' )->plain() . |
138 | '==' |
139 | ); |
140 | |
141 | $output->addWikiTextAsInterface( $renderedTermSearchResults ); |
142 | |
143 | $this->searchesMetric |
144 | ->setLabel( 'results', 'yes' ) |
145 | ->copyToStatsdAt( 'wikibase.articleplaceholder.search.has_results' ) |
146 | ->increment(); |
147 | |
148 | return; |
149 | } |
150 | } |
151 | $this->searchesMetric |
152 | ->setLabel( 'results', 'no' ) |
153 | ->copyToStatsdAt( 'wikibase.articleplaceholder.search.no_results' ) |
154 | ->increment(); |
155 | } |
156 | |
157 | /** |
158 | * @param string $term |
159 | * |
160 | * @return TermSearchResult[] |
161 | */ |
162 | private function getTermSearchResults( $term ) { |
163 | $termSearchResults = []; |
164 | |
165 | foreach ( $this->searchEntities( $term ) as $searchResult ) { |
166 | $termSearchResults[ $searchResult->getEntityIdSerialization() ] = $searchResult; |
167 | } |
168 | |
169 | return $termSearchResults; |
170 | } |
171 | |
172 | /** |
173 | * Render search results, filtered for notability. |
174 | * |
175 | * @param TermSearchResult[] $termSearchResults |
176 | * |
177 | * @return string Wikitext |
178 | */ |
179 | private function renderTermSearchResults( array $termSearchResults ) { |
180 | $wikitext = ''; |
181 | |
182 | $itemIds = []; |
183 | foreach ( $termSearchResults as $termSearchResult ) { |
184 | $itemIds[] = $termSearchResult->getEntityId(); |
185 | } |
186 | |
187 | $notableEntityIds = $this->itemNotabilityFilter->getNotableEntityIds( $itemIds ); |
188 | |
189 | foreach ( $notableEntityIds as $entityId ) { |
190 | $result = $this->renderTermSearchResult( $termSearchResults[ $entityId->getSerialization() ] ); |
191 | |
192 | $wikitext .= '<div class="article-placeholder-searchResult">' |
193 | . $result |
194 | . '</div>'; |
195 | } |
196 | |
197 | return $wikitext; |
198 | } |
199 | |
200 | /** |
201 | * @param TermSearchResult $searchResult |
202 | * |
203 | * @return string Wikitext |
204 | */ |
205 | private function renderTermSearchResult( TermSearchResult $searchResult ) { |
206 | $entityIdString = $searchResult->getEntityIdSerialization(); |
207 | |
208 | $displayLabel = $searchResult->getDisplayLabel(); |
209 | $displayDescription = $searchResult->getDisplayDescription(); |
210 | |
211 | $label = $displayLabel ? $displayLabel->getText() : $entityIdString; |
212 | |
213 | // TODO: Properly construct the page name of the special page. |
214 | $wikitext = '[[Special:AboutTopic/' . wfEscapeWikiText( $entityIdString ) . '|' |
215 | . wfEscapeWikiText( $label ) . ']]'; |
216 | |
217 | if ( $displayDescription ) { |
218 | $wikitext .= ': ' . wfEscapeWikiText( $displayDescription->getText() ); |
219 | } |
220 | |
221 | return $wikitext; |
222 | } |
223 | |
224 | /** |
225 | * @param string $term |
226 | * |
227 | * @return TermSearchResult[] |
228 | */ |
229 | private function searchEntities( $term ) { |
230 | return $this->termSearchInteractor->searchForEntities( |
231 | $term, |
232 | $this->languageCode, |
233 | 'item', |
234 | [ TermIndexEntry::TYPE_LABEL, TermIndexEntry::TYPE_ALIAS ] |
235 | ); |
236 | } |
237 | |
238 | } |