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 ContentHandler; |
6 | use MediaWiki\Content\ContentHandlerFactory; |
7 | use MediaWiki\Content\Hook\SearchDataForIndex2Hook; |
8 | use MediaWiki\Parser\ParserOutput; |
9 | use MediaWiki\Revision\RevisionRecord; |
10 | use SearchEngine; |
11 | use Wikibase\MediaInfo\Content\MediaInfoHandler; |
12 | use Wikibase\MediaInfo\DataModel\MediaInfo; |
13 | use Wikimedia\Assert\Assert; |
14 | use WikiPage; |
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 | /** |
24 | * @var ContentHandlerFactory |
25 | */ |
26 | private $contentHandlerFactory; |
27 | |
28 | public function __construct( ContentHandlerFactory $contentHandlerFactory ) { |
29 | $this->contentHandlerFactory = $contentHandlerFactory; |
30 | } |
31 | |
32 | /** |
33 | * @inheritDoc |
34 | */ |
35 | public function onSearchDataForIndex2( |
36 | array &$fields, |
37 | ContentHandler $handler, |
38 | WikiPage $page, |
39 | ParserOutput $output, |
40 | SearchEngine $engine, |
41 | RevisionRecord $revision |
42 | ) { |
43 | if ( !$revision->hasSlot( MediaInfo::ENTITY_TYPE ) ) { |
44 | return; |
45 | } |
46 | $content = $revision->getContent( MediaInfo::ENTITY_TYPE ); |
47 | if ( $content === null ) { |
48 | return; |
49 | } |
50 | $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() ); |
51 | Assert::invariant( $contentHandler instanceof MediaInfoHandler, |
52 | "Expected a MediaInfoHandler for the ContentHandler of the content of a MediaInfo slot" ); |
53 | /** @var $contentHandler MediaInfoHandler */ |
54 | $fields += $contentHandler->getContentDataForSearchIndex( $content ); |
55 | } |
56 | } |