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