Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
MissingMediaInfoHandler | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
7 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getMediaInfoId | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
showVirtualMediaInfo | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikibase\MediaInfo\Content; |
4 | |
5 | use MediaWiki\Context\IContextSource; |
6 | use MediaWiki\Linker\LinkTarget; |
7 | use MediaWiki\Parser\ParserOptions; |
8 | use MediaWiki\Title\Title; |
9 | use Wikibase\Lib\Store\EntityRevision; |
10 | use Wikibase\MediaInfo\DataModel\MediaInfo; |
11 | use Wikibase\MediaInfo\DataModel\MediaInfoId; |
12 | use Wikibase\MediaInfo\Services\FilePageLookup; |
13 | use Wikibase\MediaInfo\Services\MediaInfoIdLookup; |
14 | use Wikibase\Repo\ParserOutput\EntityParserOutputGeneratorFactory; |
15 | |
16 | /** |
17 | * Handler for missing MediaInfo entities. |
18 | * |
19 | * @license GPL-2.0-or-later |
20 | * @author Daniel Kinzler |
21 | */ |
22 | class MissingMediaInfoHandler { |
23 | |
24 | /** |
25 | * @var MediaInfoIdLookup |
26 | */ |
27 | private $idLookup; |
28 | |
29 | /** |
30 | * @var FilePageLookup |
31 | */ |
32 | private $filePageLookup; |
33 | |
34 | /** |
35 | * @var EntityParserOutputGeneratorFactory |
36 | */ |
37 | private $outputGeneratorFactory; |
38 | |
39 | /** |
40 | * @param MediaInfoIdLookup $idLookup |
41 | * @param FilePageLookup $filePageLookup |
42 | * @param EntityParserOutputGeneratorFactory $outputGeneratorFactory |
43 | */ |
44 | public function __construct( |
45 | MediaInfoIdLookup $idLookup, |
46 | FilePageLookup $filePageLookup, |
47 | EntityParserOutputGeneratorFactory $outputGeneratorFactory |
48 | ) { |
49 | $this->idLookup = $idLookup; |
50 | $this->filePageLookup = $filePageLookup; |
51 | $this->outputGeneratorFactory = $outputGeneratorFactory; |
52 | } |
53 | |
54 | /** |
55 | * Returns the MediaInfoId that corresponds to the given $title, if the title is |
56 | * valid for a MediaInfo entity, and a corresponding File page exists. If the title |
57 | * does not correspond to a valid MediaInfoId or no corresponding File page exists, |
58 | * this methods returns null. In other words, if this methods returns a MediaInfoId, |
59 | * that ID can be used to show a "virtual" MediaInfo entity. |
60 | * |
61 | * @see EntityHandler::showMissingEntity |
62 | * |
63 | * @param LinkTarget $title The title of the page that potentially could, but does not, |
64 | * contain an entity. |
65 | * @param IContextSource $context Context to use for reporting. In particular, output |
66 | * will be written to $context->getOutput(). |
67 | * |
68 | * @return MediaInfoId|null |
69 | */ |
70 | public function getMediaInfoId( LinkTarget $title, IContextSource $context ) { |
71 | $mediaInfoId = $this->idLookup->getEntityIdForTitle( Title::newFromLinkTarget( $title ) ); |
72 | if ( $mediaInfoId === null || !( $mediaInfoId instanceof MediaInfoId ) ) { |
73 | return null; |
74 | } |
75 | |
76 | $filePageTitle = $this->filePageLookup->getFilePage( $mediaInfoId ); |
77 | if ( $filePageTitle === null || !$filePageTitle->exists() ) { |
78 | return null; |
79 | } |
80 | |
81 | return $mediaInfoId; |
82 | } |
83 | |
84 | /** |
85 | * Display a "virtual" (empty) MediaInfo entity with the given ID. |
86 | * |
87 | * @see EntityHandler::showMissingEntity |
88 | * |
89 | * @param MediaInfoId $mediaInfoId The ID of the virtual MediaInfo entity to show. |
90 | * @param IContextSource $context Context to use for display. In particular, output |
91 | * will be written to $context->getOutput(), and the output language is determined |
92 | * by $context->getLanguage(). |
93 | */ |
94 | public function showVirtualMediaInfo( MediaInfoId $mediaInfoId, IContextSource $context ) { |
95 | $userLanguage = $context->getLanguage(); |
96 | $outputPage = $context->getOutput(); |
97 | |
98 | // show an empty MediaInfo |
99 | $outputGenerator = $this->outputGeneratorFactory-> |
100 | getEntityParserOutputGenerator( $userLanguage ); |
101 | |
102 | $mediaInfo = new MediaInfo( $mediaInfoId ); |
103 | |
104 | $parserOutput = $outputGenerator->getParserOutput( new EntityRevision( $mediaInfo ), true ); |
105 | |
106 | $outputPage->addParserOutput( |
107 | $parserOutput, |
108 | ParserOptions::newFromContext( $context ) |
109 | ); |
110 | } |
111 | |
112 | } |