Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.55% covered (success)
98.55%
68 / 69
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SensesView
98.55% covered (success)
98.55%
68 / 69
75.00% covered (warning)
75.00%
3 / 4
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getHtml
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getSenseHtml
97.50% covered (success)
97.50%
39 / 40
0.00% covered (danger)
0.00%
0 / 1
1
 getStatementSectionHtml
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Presentation\View;
4
5use Wikibase\DataModel\Term\Term;
6use Wikibase\Lexeme\Domain\Model\Sense;
7use Wikibase\Lexeme\Domain\Model\SenseSet;
8use Wikibase\Lexeme\MediaWiki\Content\LexemeLanguageNameLookup;
9use Wikibase\Lexeme\Presentation\View\Template\LexemeTemplateFactory;
10use Wikibase\Lexeme\Presentation\View\Template\VueTemplates;
11use Wikibase\View\LanguageDirectionalityLookup;
12use Wikibase\View\LocalizedTextProvider;
13use Wikibase\View\StatementGroupListView;
14use WMDE\VueJsTemplating\Templating;
15
16/**
17 * @license GPL-2.0-or-later
18 */
19class SensesView {
20
21    /**
22     * @var LocalizedTextProvider
23     */
24    private $textProvider;
25
26    /**
27     * @var LanguageDirectionalityLookup
28     */
29    private $languageDirectionalityLookup;
30
31    /**
32     * @var LexemeTemplateFactory
33     */
34    private $templateFactory;
35
36    /**
37     * @var StatementGroupListView
38     */
39    private $statementGroupListView;
40
41    /**
42     * @var LexemeLanguageNameLookup
43     */
44    private $languageNameLookup;
45
46    /**
47     * @param LocalizedTextProvider $textProvider
48     * @param LanguageDirectionalityLookup $languageDirectionalityLookup
49     * @param LexemeTemplateFactory $templateFactory
50     * @param StatementGroupListView $statementGroupListView
51     * @param LexemeLanguageNameLookup $languageNameLookup
52     */
53    public function __construct(
54        LocalizedTextProvider $textProvider,
55        LanguageDirectionalityLookup $languageDirectionalityLookup,
56        LexemeTemplateFactory $templateFactory,
57        StatementGroupListView $statementGroupListView,
58        LexemeLanguageNameLookup $languageNameLookup
59    ) {
60        $this->textProvider = $textProvider;
61        $this->languageDirectionalityLookup = $languageDirectionalityLookup;
62        $this->templateFactory = $templateFactory;
63        $this->statementGroupListView = $statementGroupListView;
64        $this->languageNameLookup = $languageNameLookup;
65    }
66
67    /**
68     * @param SenseSet $senses
69     *
70     * @return string HTML
71     */
72    public function getHtml( SenseSet $senses ) {
73        $html = '<div class="wikibase-lexeme-senses-section">';
74        $html .= '<h2 class="wb-section-heading section-heading" id="senses">'
75            . htmlspecialchars( $this->textProvider->get( 'wikibaselexeme-header-senses' ) )
76            . '</h2>';
77
78        $html .= '<div class="wikibase-lexeme-senses">';
79        foreach ( $senses->toArray() as $sense ) {
80            $html .= $this->getSenseHtml( $sense );
81        }
82        $html .= '</div>'; // wikibase-lexeme-senses
83        // @phan-suppress-next-line PhanPluginDuplicateAdjacentStatement
84        $html .= '</div>'; // wikibase-lexeme-senses-section
85
86        return $html;
87    }
88
89    /**
90     * @param Sense $sense
91     *
92     * @return string HTML
93     */
94    private function getSenseHtml( Sense $sense ) {
95        $templating = new Templating();
96        $template = file_get_contents( __DIR__ . VueTemplates::GLOSS_WIDGET );
97
98        $glosses = array_map(
99            static function ( Term $gloss ) {
100                return [ 'value' => $gloss->getText(), 'language' => $gloss->getLanguageCode() ];
101            },
102            iterator_to_array( $sense->getGlosses() )
103        );
104        ksort( $glosses );
105
106        $glossWidget = $templating->render(
107            $template,
108            [
109                'senseId' => $sense->getId()->getSerialization(),
110                'inEditMode' => false,
111                'isSaving' => false,
112                'glosses' => $glosses,
113                'isUnsaveable' => true,
114            ],
115            [
116                'message' => function ( $key ) {
117                    return $this->textProvider->get( $key );
118                },
119                'directionality' => function ( $languageCode ) {
120                    return $this->languageDirectionalityLookup->getDirectionality( $languageCode );
121                },
122                'languageName' => function ( $languageCode ) {
123                    return $this->languageNameLookup->getName( $languageCode );
124                },
125
126            ]
127        );
128
129        return $this->templateFactory->render(
130            'wikibase-lexeme-sense',
131            [
132                htmlspecialchars( $sense->getId()->getSerialization() ),
133                $glossWidget,
134                $this->getStatementSectionHtml( $sense ),
135                htmlspecialchars( $sense->getId()->getIdSuffix() ),
136                htmlspecialchars( $sense->getId()->getSerialization() ),
137            ]
138        );
139    }
140
141    /**
142     * @param Sense $sense
143     *
144     * @return string HTML
145     */
146    private function getStatementSectionHtml( Sense $sense ) {
147        $headerText = htmlspecialchars(
148            $this->textProvider->get(
149                'wikibaselexeme-statementsection-statements-about-sense',
150                [ $sense->getId()->getSerialization() ]
151            )
152        );
153
154        $statementHeader = <<<HTML
155<h2 class="wb-section-heading section-heading wikibase-statements" dir="auto">
156    $headerText
157</h2>
158HTML;
159
160        $statementSection = $this->statementGroupListView->getHtml(
161            $sense->getStatements()->toArray(), $sense->getId()->getIdSuffix()
162        );
163        return $statementHeader . $statementSection;
164    }
165}