Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.29% covered (warning)
89.29%
25 / 28
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaInfoContent
89.29% covered (warning)
89.29%
25 / 28
66.67% covered (warning)
66.67%
4 / 6
11.15
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getIgnoreKeysForFilters
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getMediaInfo
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 getEntity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntityHolder
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTextForSearchIndex
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
3.21
1<?php
2
3namespace Wikibase\MediaInfo\Content;
4
5use InvalidArgumentException;
6use LogicException;
7use Wikibase\MediaInfo\DataModel\MediaInfo;
8use Wikibase\Repo\Content\EntityContent;
9use Wikibase\Repo\Content\EntityHolder;
10use Wikibase\Repo\FingerprintSearchTextGenerator;
11use Wikibase\Repo\Hooks\WikibaseTextForSearchIndexHook;
12
13/**
14 * @license GPL-2.0-or-later
15 * @author Bene* < benestar.wikimedia@gmail.com >
16 */
17class MediaInfoContent extends EntityContent {
18
19    public const CONTENT_MODEL_ID = 'wikibase-mediainfo';
20
21    /**
22     * @var EntityHolder|null
23     */
24    private $mediaInfoHolder;
25
26    /**
27     * @var WikibaseTextForSearchIndexHook
28     */
29    private $hookRunner;
30
31    /**
32     * Do not use to construct new stuff from outside of this class -
33     * prefer MediaInfoHandler::newEntityContent (since I61d9a89e3ef19e10c04dbfdea02d4096cd2b8cda)
34     *
35     * @param WikibaseTextForSearchIndexHook $hookRunner
36     * @param EntityHolder|null $mediaInfoHolder
37     * @throws InvalidArgumentException
38     */
39    public function __construct(
40        WikibaseTextForSearchIndexHook $hookRunner,
41        ?EntityHolder $mediaInfoHolder = null
42    ) {
43        parent::__construct( self::CONTENT_MODEL_ID );
44
45        if ( $mediaInfoHolder !== null
46            && $mediaInfoHolder->getEntityType() !== MediaInfo::ENTITY_TYPE
47        ) {
48            throw new InvalidArgumentException( '$mediaInfoHolder must contain a MediaInfo entity' );
49        }
50
51        $this->mediaInfoHolder = $mediaInfoHolder;
52        $this->hookRunner = $hookRunner;
53    }
54
55    /**
56     * @return array
57     */
58    protected function getIgnoreKeysForFilters() {
59        // We explicitly want to allow the 'labels' block's 'language' keys through, so that AbuseFilters
60        // can be written that check if e.g. Latin characters are written in a zh-hans label slot.
61        return [
62            // pageid, ns, title, lastrevid, & modified are injected in the API but not AF, so don't list
63            // MediaInfo entities don't have a "site" attribute, so this shouldn't show up, but for safety
64            'site',
65            // MediaInfo is not going to use descriptions but they currently appear in the
66            // serialization. These should not be checked for filter text.
67            'descriptions',
68            // Probably won't be used, could be added back if requested
69            'rank',
70            // Options: value, somevalue, novalue
71            'snaktype',
72            // Probably pointless in filter text
73            'hash',
74            // Statement guid
75            'id',
76            // Hits a few different things
77            'type',
78            // Hits a few different things
79            'datatype',
80        ];
81    }
82
83    /**
84     * @return MediaInfo
85     */
86    public function getMediaInfo() {
87        if ( !$this->mediaInfoHolder ) {
88            throw new LogicException( 'This content object is empty!' );
89        }
90
91        // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
92        return $this->mediaInfoHolder->getEntity( MediaInfo::class );
93    }
94
95    /**
96     * @see EntityContent::getEntity
97     *
98     * @return MediaInfo
99     */
100    public function getEntity() {
101        return $this->getMediaInfo();
102    }
103
104    /**
105     * @see EntityContent::getEntityHolder
106     *
107     * @return EntityHolder|null
108     */
109    public function getEntityHolder() {
110        return $this->mediaInfoHolder;
111    }
112
113    /**
114     * @see EntityContent::getTextForSearchIndex
115     *
116     * @return string
117     */
118    public function getTextForSearchIndex() {
119        if ( $this->isRedirect() ) {
120            return '';
121        }
122
123        $searchTextGenerator = new FingerprintSearchTextGenerator();
124        $text = $searchTextGenerator->generate( $this->getMediaInfo() );
125
126        if ( !$this->hookRunner->onWikibaseTextForSearchIndex( $this, $text ) ) {
127            return '';
128        }
129
130        return $text;
131    }
132
133}