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