Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaInfoView
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 getTitleHtml
n/a
0 / 0
n/a
0 / 0
1
 getContent
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getCaptionsHtml
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getStatementsHtml
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\MediaInfo\View;
4
5use InvalidArgumentException;
6use MediaWiki\Html\Html;
7use Wikibase\DataModel\Entity\EntityDocument;
8use Wikibase\MediaInfo\DataModel\MediaInfo;
9use Wikibase\View\EntityDocumentView;
10use Wikibase\View\ViewContent;
11
12/**
13 * Class for creating HTML views for MediaInfo instances.
14 *
15 * @license GPL-2.0-or-later
16 */
17class MediaInfoView implements EntityDocumentView {
18
19    /**
20     * @var MediaInfoEntityTermsView
21     */
22    private $captionsView;
23
24    /**
25     * @var string
26     */
27    protected $languageCode;
28
29    /**
30     * @var MediaInfoEntityStatementsView
31     */
32    private $statementsView;
33
34    public const MEDIAINFOVIEW_CUSTOM_TAG = 'mediaInfoView';
35
36    /**
37     * @param MediaInfoEntityTermsView $captionsView
38     * @param string $languageCode
39     * @param MediaInfoEntityStatementsView $statementsView
40     * @codeCoverageIgnore
41     */
42    public function __construct(
43        MediaInfoEntityTermsView $captionsView,
44        $languageCode,
45        MediaInfoEntityStatementsView $statementsView
46    ) {
47        $this->captionsView = $captionsView;
48        $this->languageCode = $languageCode;
49        $this->statementsView = $statementsView;
50    }
51
52    /**
53     * @param EntityDocument $entity
54     * @return string
55     * @codeCoverageIgnore
56     */
57    public function getTitleHtml( EntityDocument $entity ) {
58        return '';
59    }
60
61    public function getContent( EntityDocument $entity, $revision = null ): ViewContent {
62        if ( !( $entity instanceof MediaInfo ) ) {
63            throw new InvalidArgumentException( '$entity must be a MediaInfo entity.' );
64        }
65
66        return new ViewContent(
67            // Wrap the whole thing in a custom tag so we can manipulate its position on the page
68            // later on
69            Html::rawElement(
70                self::MEDIAINFOVIEW_CUSTOM_TAG,
71                // Hide this custom tag by default: it's only really used to signal the boundaries
72                // of MediaInfo content, and we'll extract the relevant parts out of this if
73                // MediaInfo is installed.
74                // If MediaInfo is not installed, however (e.g. pulling in content from another
75                // wiki, or MediaInfo is uninstalled), this routine won't happen, styles and
76                // scripts to ensure this view works properly aren't loaded, etc, so we'd
77                // rather not have this content show up at all in that case...
78                [ 'style' => 'display: none' ],
79                $this->getCaptionsHtml( $entity ) . $this->getStatementsHtml( $entity )
80            )
81        );
82    }
83
84    public function getCaptionsHtml( MediaInfo $entity ) {
85        return $this->captionsView->getHtml(
86            $entity
87        );
88    }
89
90    public function getStatementsHtml( MediaInfo $entity ) {
91        return $this->statementsView->getHtml(
92            $entity
93        );
94    }
95
96}