Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.00% |
7 / 10 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MediaInfoDataForSearchIndex | |
70.00% |
7 / 10 |
|
0.00% |
0 / 2 |
4.43 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onSearchDataForIndex2 | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikibase\MediaInfo; |
| 4 | |
| 5 | use MediaWiki\Content\ContentHandler; |
| 6 | use MediaWiki\Content\ContentHandlerFactory; |
| 7 | use MediaWiki\Content\Hook\SearchDataForIndex2Hook; |
| 8 | use MediaWiki\Page\WikiPage; |
| 9 | use MediaWiki\Parser\ParserOutput; |
| 10 | use MediaWiki\Revision\RevisionRecord; |
| 11 | use SearchEngine; |
| 12 | use Wikibase\MediaInfo\Content\MediaInfoHandler; |
| 13 | use Wikibase\MediaInfo\DataModel\MediaInfo; |
| 14 | use Wikimedia\Assert\Assert; |
| 15 | |
| 16 | /** |
| 17 | * Media info data is not stored in the main slot and thus the corresponding |
| 18 | * ContentHandler will never be called by the main slot ContentHandler. |
| 19 | * MW Core lacks support for such scenario, so we rely on a hook to inject the mediainfo |
| 20 | * slot content data into the search index. |
| 21 | */ |
| 22 | class MediaInfoDataForSearchIndex implements SearchDataForIndex2Hook { |
| 23 | public function __construct( |
| 24 | private readonly ContentHandlerFactory $contentHandlerFactory, |
| 25 | ) { |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @inheritDoc |
| 30 | */ |
| 31 | public function onSearchDataForIndex2( |
| 32 | array &$fields, |
| 33 | ContentHandler $handler, |
| 34 | WikiPage $page, |
| 35 | ParserOutput $output, |
| 36 | SearchEngine $engine, |
| 37 | RevisionRecord $revision |
| 38 | ) { |
| 39 | if ( !$revision->hasSlot( MediaInfo::ENTITY_TYPE ) ) { |
| 40 | return; |
| 41 | } |
| 42 | $content = $revision->getContent( MediaInfo::ENTITY_TYPE ); |
| 43 | if ( $content === null ) { |
| 44 | return; |
| 45 | } |
| 46 | $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() ); |
| 47 | Assert::invariant( $contentHandler instanceof MediaInfoHandler, |
| 48 | "Expected a MediaInfoHandler for the ContentHandler of the content of a MediaInfo slot" ); |
| 49 | /** @var $contentHandler MediaInfoHandler */ |
| 50 | $fields += $contentHandler->getContentDataForSearchIndex( $content ); |
| 51 | } |
| 52 | } |