Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CustomPageFields | |
0.00% |
0 / 11 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
onSearchIndexFields | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
buildSearchIndexFields | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Hooks; |
4 | |
5 | use CirrusSearch\CirrusSearch; |
6 | use MediaWiki\Config\Config; |
7 | use MediaWiki\MediaWikiServices; |
8 | use MediaWiki\Search\Hook\SearchIndexFieldsHook; |
9 | use NullIndexField; |
10 | use SearchEngine; |
11 | use SearchIndexField; |
12 | |
13 | /** |
14 | * Hooks to allow custom fields to be added to the search index for pages |
15 | */ |
16 | class CustomPageFields implements SearchIndexFieldsHook { |
17 | public const CONFIG_OPTION = 'CirrusSearchCustomPageFields'; |
18 | |
19 | /** |
20 | * Add configured fields to mapping |
21 | * @param array &$fields array of field definitions to update |
22 | * @param SearchEngine $engine the search engine requesting field definitions |
23 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/SearchIndexFields |
24 | */ |
25 | public function onSearchIndexFields( &$fields, $engine ) { |
26 | if ( !( $engine instanceof CirrusSearch ) ) { |
27 | return; |
28 | } |
29 | // += will not overwrite existing fields, only new fields may be added |
30 | $fields += self::buildSearchIndexFields( $engine, |
31 | MediaWikiServices::getInstance()->getMainConfig() ); |
32 | } |
33 | |
34 | /** |
35 | * Build configured fields |
36 | * @param SearchEngine $engine |
37 | * @param Config $config the wiki configuration |
38 | * @return SearchIndexField[] |
39 | */ |
40 | public static function buildSearchIndexFields( |
41 | SearchEngine $engine, |
42 | Config $config |
43 | ): array { |
44 | $fields = []; |
45 | foreach ( $config->get( self::CONFIG_OPTION ) as $name => $type ) { |
46 | $field = $engine->makeSearchFieldMapping( $name, $type ); |
47 | if ( $field instanceof NullIndexField ) { |
48 | throw new \RuntimeException( "Search field $name has invalid type of $type " ); |
49 | } |
50 | $fields[$name] = $field; |
51 | } |
52 | return $fields; |
53 | } |
54 | } |