Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaInfoDataForSearchIndex
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 2
4.43
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onSearchDataForIndex2
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
3.10
1<?php
2
3namespace Wikibase\MediaInfo;
4
5use ContentHandler;
6use MediaWiki\Content\ContentHandlerFactory;
7use MediaWiki\Content\Hook\SearchDataForIndex2Hook;
8use MediaWiki\Parser\ParserOutput;
9use MediaWiki\Revision\RevisionRecord;
10use SearchEngine;
11use Wikibase\MediaInfo\Content\MediaInfoHandler;
12use Wikibase\MediaInfo\DataModel\MediaInfo;
13use Wikimedia\Assert\Assert;
14use 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 */
22class 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}