Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
MathWikibaseInfo
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 18
702
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 setLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSymbol
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addHasPartElement
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addHasPartElements
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSymbol
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFormattedSymbol
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getParts
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFormatter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasParts
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 generateTableOfParts
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
42
 generateSmallTableOfParts
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\Math;
4
5use DataValues\StringValue;
6use MediaWiki\Html\Html;
7use MediaWiki\MediaWikiServices;
8use Wikibase\DataModel\Entity\EntityId;
9use Wikibase\Lib\Formatters\SnakFormatter;
10
11/**
12 * This class stores information about mathematical Wikibase items.
13 */
14class MathWikibaseInfo {
15
16    /**
17     * @var string|null the label of the item
18     */
19    private $label;
20
21    /**
22     * @var string|null description of the item
23     */
24    private $description;
25
26    /**
27     * @var StringValue|null a symbol representing the item
28     */
29    private $symbol;
30
31    /**
32     * @var self[]
33     */
34    private $hasParts = [];
35
36    private readonly MathFormatter $mathFormatter;
37
38    /**
39     * @var string|null
40     */
41    private $url;
42
43    /**
44     * @param EntityId $entityId
45     * @param MathFormatter|null $mathFormatter to format math equations. Default format is HTML.
46     */
47    public function __construct(
48        private readonly EntityId $entityId,
49        ?MathFormatter $mathFormatter = null,
50    ) {
51        $this->mathFormatter = $mathFormatter ?: new MathFormatter( SnakFormatter::FORMAT_HTML );
52    }
53
54    /**
55     * @param string $label
56     */
57    public function setLabel( $label ) {
58        $this->label = $label;
59    }
60
61    /**
62     * @param string $description
63     */
64    public function setDescription( $description ) {
65        $this->description = $description;
66    }
67
68    /**
69     * @param StringValue $symbol
70     */
71    public function setSymbol( $symbol ) {
72        $this->symbol = $symbol;
73    }
74
75    public function addHasPartElement( self $info ) {
76        $this->hasParts[] = $info;
77    }
78
79    /**
80     * @param string $link
81     */
82    public function setUrl( $link ) {
83        $this->url = $link;
84    }
85
86    /**
87     * @param self[] $infos
88     */
89    public function addHasPartElements( $infos ) {
90        array_push( $this->hasParts, ...$infos );
91    }
92
93    /**
94     * @return EntityId id
95     */
96    public function getId() {
97        return $this->entityId;
98    }
99
100    /**
101     * @return string|null label
102     */
103    public function getLabel() {
104        return $this->label;
105    }
106
107    /**
108     * @return string|null description
109     */
110    public function getDescription() {
111        return $this->description;
112    }
113
114    /**
115     * @return StringValue|null symbol
116     */
117    public function getSymbol() {
118        return $this->symbol;
119    }
120
121    /**
122     * @return string|null html formatted version of the symbol
123     */
124    public function getFormattedSymbol() {
125        if ( $this->symbol ) {
126            return $this->mathFormatter->format( $this->getSymbol() );
127        } else {
128            return null;
129        }
130    }
131
132    /**
133     * @return self[] hasparts
134     */
135    public function getParts() {
136        return $this->hasParts;
137    }
138
139    /**
140     * @return string|null
141     */
142    public function getUrl() {
143        return $this->url;
144    }
145
146    /**
147     * @return MathFormatter
148     */
149    public function getFormatter() {
150        return $this->mathFormatter;
151    }
152
153    /**
154     * Does this info object has elements?
155     * @return bool true if there are elements otherwise false
156     */
157    public function hasParts() {
158        return $this->hasParts !== [];
159    }
160
161    /**
162     * Generates an HTML table representation of the has-parts elements
163     * @return string
164     */
165    public function generateTableOfParts() {
166        $lang = MediaWikiServices::getInstance()->getContentLanguage();
167        $labelAlign = $lang->isRTL() ? 'left' : 'right';
168        $labelAlignOpposite = !$lang->isRTL() ? 'left' : 'right';
169
170        $output = '';
171
172        foreach ( $this->hasParts as $part ) {
173            $output .= Html::openElement( "tr" );
174
175            $output .= Html::openElement(
176                "td",
177                [ "style" => "font-weight: bold; text-align:$labelAlign;" ]
178            );
179
180            if ( $part->url ) {
181                $output .= Html::element(
182                    "a",
183                    [ "href" => $part->url ],
184                    $part->getLabel()
185                );
186            } else {
187                $output .= htmlspecialchars( $part->getLabel() );
188            }
189
190            $output .= Html::closeElement( "td" );
191            $output .= Html::openElement(
192                "td",
193                [ "style" => "text-align:center; padding: 2px; padding-left: 10px; padding-right: 10px;" ]
194            );
195
196            if ( $part->url ) {
197                $output .= Html::rawElement(
198                    "a",
199                    [ "href" => $part->url ],
200                    $part->getFormattedSymbol()
201                );
202            } else {
203                $output .= $part->getFormattedSymbol();
204            }
205
206            $output .= Html::closeElement( "td" );
207            $output .= Html::element(
208                "td",
209                [ "style" => "font-style: italic; text-align:$labelAlignOpposite;" ],
210                $part->getDescription()
211            );
212            $output .= Html::closeElement( "tr" );
213        }
214
215        return Html::rawElement( 'table', [ 'style' => 'padding: 5px' ],
216            Html::rawElement( 'tbody', [], $output )
217        );
218    }
219
220    /**
221     * Generates a minimalized Html representation of the has-parts elements.
222     * @return string
223     */
224    public function generateSmallTableOfParts() {
225        $output = '';
226
227        foreach ( $this->hasParts as $part ) {
228            $output .= Html::rawElement( 'tr', [],
229                Html::rawElement( 'td',
230                    [ 'style' => 'text-align: center; padding-right: 5px;' ],
231                    $part->getFormattedSymbol()
232                ) .
233                Html::element( 'td', [ 'style' => 'text-align:left;' ], $part->getLabel() )
234            );
235        }
236
237        return Html::rawElement( 'table', [],
238            Html::rawElement( 'tbody', [], $output )
239        );
240    }
241}