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