Debian Codegraph @

extensions/Math/src/MathWikibaseInfo.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math;
4
5 use DataValues\StringValue;
6 use MediaWiki\Html\Html;
7 use MediaWiki\MediaWikiServices;
8 use Wikibase\DataModel\Entity\EntityId;
9 use Wikibase\Lib\Formatters\SnakFormatter;
10
11 /**
12 * This class stores information about mathematical Wikibase items.
13 */
14 class MathWikibaseInfo {
15 /**
16 * @var EntityId
17 */
18 private $id;
19
20 /**
21 * @var string|null the label of the item
22 */
23 private $label;
24
25 /**
26 * @var string|null description of the item
27 */
28 private $description;
29
30 /**
31 * @var StringValue|null 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|null
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|null label
110 */
111 public function getLabel() {
112 return $this->label;
113 }
114
115 /**
116 * @return string|null description
117 */
118 public function getDescription() {
119 return $this->description;
120 }
121
122 /**
123 * @return StringValue|null 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|null
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 = '';
179
180 foreach ( $this->hasParts as $part ) {
181 $output .= Html::openElement( "tr" );
182
183 $output .= Html::openElement(
184 "td",
185 [ "style" => "font-weight: bold; text-align:$labelAlign;" ]
186 );
187
188 if ( $part->url ) {
189 $output .= Html::element(
190 "a",
191 [ "href" => $part->url ],
192 $part->getLabel()
193 );
194 } else {
195 $output .= htmlspecialchars( $part->getLabel() );
196 }
197
198 $output .= Html::closeElement( "td" );
199 $output .= Html::openElement(
200 "td",
201 [ "style" => "text-align:center; padding: 2px; padding-left: 10px; padding-right: 10px;" ]
202 );
203
204 if ( $part->url ) {
205 $output .= Html::rawElement(
206 "a",
207 [ "href" => $part->url ],
208 $part->getFormattedSymbol()
209 );
210 } else {
211 $output .= $part->getFormattedSymbol();
212 }
213
214 $output .= Html::closeElement( "td" );
215 $output .= Html::element(
216 "td",
217 [ "style" => "font-style: italic; text-align:$labelAlignOpposite;" ],
218 $part->getDescription()
219 );
220 $output .= Html::closeElement( "tr" );
221 }
222
223 return Html::rawElement( 'table', [ 'style' => 'padding: 5px' ],
224 Html::rawElement( 'tbody', [], $output )
225 );
226 }
227
228 /**
229 * Generates a minimalized Html representation of the has-parts elements.
230 * @return string
231 */
232 public function generateSmallTableOfParts() {
233 $output = '';
234
235 foreach ( $this->hasParts as $part ) {
236 $output .= Html::rawElement( 'tr', [],
237 Html::rawElement( 'td',
238 [ 'style' => 'text-align: center; padding-right: 5px;' ],
239 $part->getFormattedSymbol()
240 ) .
241 Html::element( 'td', [ 'style' => 'text-align:left;' ], $part->getLabel() )
242 );
243 }
244
245 return Html::rawElement( 'table', [],
246 Html::rawElement( 'tbody', [], $output )
247 );
248 }
249 }