Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
62.50% |
10 / 16 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
MediaInfoIdLookup | |
62.50% |
10 / 16 |
|
33.33% |
1 / 3 |
7.90 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getEntityIds | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getEntityIdForTitle | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
3.14 |
1 | <?php |
2 | |
3 | namespace Wikibase\MediaInfo\Services; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\Title\Title; |
7 | use UnexpectedValueException; |
8 | use Wikibase\DataModel\Services\EntityId\EntityIdComposer; |
9 | use Wikibase\Lib\Store\EntityIdLookup; |
10 | use Wikibase\MediaInfo\DataModel\MediaInfo; |
11 | use Wikimedia\Assert\Assert; |
12 | |
13 | /** |
14 | * Lookup service for getting the MediaInfoId that corresponds to a Title. |
15 | * |
16 | * @license GPL-2.0-or-later |
17 | * @author Daniel Kinzler |
18 | */ |
19 | class MediaInfoIdLookup implements EntityIdLookup { |
20 | |
21 | /** |
22 | * @var EntityIdComposer |
23 | */ |
24 | private $entityIdComposer; |
25 | |
26 | /** |
27 | * @var int |
28 | */ |
29 | private $mediaInfoNamespace; |
30 | |
31 | /** |
32 | * @param EntityIdComposer $entityIdComposer |
33 | * @param int $mediaInfoNamespace numeric namespace ID of the namespace in which MediaInfo |
34 | * entities reside. |
35 | */ |
36 | public function __construct( EntityIdComposer $entityIdComposer, $mediaInfoNamespace ) { |
37 | Assert::parameterType( 'integer', $mediaInfoNamespace, '$mediaInfoNamespace' ); |
38 | Assert::parameter( $mediaInfoNamespace >= 0, '$mediaInfoNamespace', 'Must not be negative' ); |
39 | |
40 | $this->entityIdComposer = $entityIdComposer; |
41 | $this->mediaInfoNamespace = $mediaInfoNamespace; |
42 | } |
43 | |
44 | public function getEntityIds( array $titles ) { |
45 | $results = []; |
46 | foreach ( $titles as $title ) { |
47 | $results[$title->getArticleID()] = $this->getEntityIdForTitle( $title ); |
48 | } |
49 | return array_filter( $results ); |
50 | } |
51 | |
52 | public function getEntityIdForTitle( Title $title ) { |
53 | if ( !$title->inNamespace( $this->mediaInfoNamespace ) ) { |
54 | return null; |
55 | } |
56 | |
57 | // The ID for a MediaInfo item is the same as the ID of its associated File page, with |
58 | // an 'M' prepended - this is encapsulated by EntityIdComposer::composeEntityId() |
59 | try { |
60 | return $this->entityIdComposer->composeEntityId( |
61 | MediaInfo::ENTITY_TYPE, |
62 | $title->getArticleID() |
63 | ); |
64 | } catch ( InvalidArgumentException | UnexpectedValueException $e ) { |
65 | return null; |
66 | } |
67 | } |
68 | |
69 | } |