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