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