Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
onApiOpenSearchSuggest | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | namespace TextExtracts; |
4 | |
5 | use ApiBase; |
6 | use ApiMain; |
7 | use ApiResult; |
8 | use MediaWiki\Api\Hook\ApiOpenSearchSuggestHook; |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\Request\FauxRequest; |
11 | |
12 | /** |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class Hooks implements ApiOpenSearchSuggestHook { |
16 | |
17 | /** |
18 | * ApiOpenSearchSuggest hook handler |
19 | * @param array &$results Array of search results |
20 | */ |
21 | public function onApiOpenSearchSuggest( &$results ) { |
22 | $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'textextracts' ); |
23 | if ( !$config->get( 'ExtractsExtendOpenSearchXml' ) || $results === [] ) { |
24 | return; |
25 | } |
26 | |
27 | foreach ( array_chunk( array_keys( $results ), ApiBase::LIMIT_SML1 ) as $pageIds ) { |
28 | $api = new ApiMain( new FauxRequest( |
29 | [ |
30 | 'action' => 'query', |
31 | 'prop' => 'extracts', |
32 | 'explaintext' => true, |
33 | 'exintro' => true, |
34 | 'exlimit' => count( $pageIds ), |
35 | 'pageids' => implode( '|', $pageIds ), |
36 | ] ) |
37 | ); |
38 | $api->execute(); |
39 | $data = $api->getResult()->getResultData( [ 'query', 'pages' ] ); |
40 | foreach ( $pageIds as $id ) { |
41 | $contentKey = $data[$id]['extract'][ApiResult::META_CONTENT] ?? '*'; |
42 | if ( isset( $data[$id]['extract'][$contentKey] ) ) { |
43 | $results[$id]['extract'] = $data[$id]['extract'][$contentKey]; |
44 | $results[$id]['extract trimmed'] = false; |
45 | } |
46 | } |
47 | } |
48 | } |
49 | } |