1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\MediaWiki\Renderer; |
4
|
|
|
|
5
|
|
|
use Html; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Simple list formatter to transform an indexed array (e.g. array( 'F' => array( 'Foo', 'Bar' ) ) |
9
|
|
|
* into a column divided list. |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 2.1 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
* @author Markus Krötzsch |
16
|
|
|
*/ |
17
|
|
|
class HtmlColumnListRenderer { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var integer |
21
|
|
|
*/ |
22
|
|
|
private $numberOfColumns = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $contentsByIndex = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var integer |
31
|
|
|
*/ |
32
|
|
|
private $numRows = 0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var integer |
36
|
|
|
*/ |
37
|
|
|
private $numberOfResults = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var integer |
41
|
|
|
*/ |
42
|
|
|
private $rowsPerColumn = 0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var integer |
46
|
|
|
*/ |
47
|
|
|
private $columnWidth = 0; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $listType = 'ul'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $columnListClass = 'smw-columnlist-container'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $columnClass = 'smw-column'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var boolean |
66
|
|
|
*/ |
67
|
|
|
private $isRTL = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @since 2.2 |
71
|
|
|
* |
72
|
|
|
* @param string $columnListClass |
73
|
|
|
* |
74
|
|
|
* @return HtmlColumnListRenderer |
75
|
|
|
*/ |
76
|
2 |
|
public function setColumnListClass( $columnListClass ) { |
77
|
2 |
|
$this->columnListClass = htmlspecialchars( $columnListClass ); |
78
|
2 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @since 2.2 |
83
|
|
|
* |
84
|
|
|
* @param string $columnListClass |
|
|
|
|
85
|
|
|
* |
86
|
|
|
* @return HtmlColumnListRenderer |
87
|
|
|
*/ |
88
|
2 |
|
public function setColumnClass( $columnClass ) { |
89
|
2 |
|
$this->columnClass = htmlspecialchars( $columnClass ); |
90
|
2 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @since 2.3 |
95
|
|
|
* |
96
|
|
|
* @param boolean $isRTL |
97
|
|
|
*/ |
98
|
1 |
|
public function setColumnRTLDirectionalityState( $isRTL ) { |
99
|
1 |
|
$this->isRTL = (bool)$isRTL; |
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @since 2.1 |
105
|
|
|
* |
106
|
|
|
* @param integer $numberOfColumns |
107
|
|
|
* |
108
|
|
|
* @return HtmlColumnListRenderer |
109
|
|
|
*/ |
110
|
8 |
|
public function setNumberOfColumns( $numberOfColumns ) { |
111
|
8 |
|
$this->numberOfColumns = $numberOfColumns; |
112
|
8 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @since 2.1 |
117
|
|
|
* |
118
|
|
|
* @param string $listType |
119
|
|
|
* |
120
|
|
|
* @return HtmlColumnListRenderer |
121
|
|
|
*/ |
122
|
3 |
|
public function setListType( $listType ) { |
123
|
|
|
|
124
|
3 |
|
if ( in_array( $listType, array( 'ul', 'ol' ) ) ) { |
125
|
3 |
|
$this->listType = $listType; |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @since 2.1 |
133
|
|
|
* |
134
|
|
|
* @param string[] $contentsByNoIndex |
135
|
|
|
* |
136
|
|
|
* @return HtmlColumnListRenderer |
137
|
|
|
*/ |
138
|
2 |
|
public function addContentsByNoIndex( array $contentsByNoIndex ) { |
139
|
|
|
|
140
|
2 |
|
$contentsByEmptyIndex[''] = array(); |
|
|
|
|
141
|
|
|
|
142
|
2 |
|
foreach ( $contentsByNoIndex as $value ) { |
143
|
2 |
|
$contentsByEmptyIndex[''][] = $value; |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
return $this->addContentsByIndex( $contentsByEmptyIndex ); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @since 2.1 |
151
|
|
|
* |
152
|
|
|
* @param string[] $contentsByIndex |
153
|
|
|
* |
154
|
|
|
* @return HtmlColumnListRenderer |
155
|
|
|
*/ |
156
|
9 |
|
public function addContentsByIndex( array $contentsByIndex ) { |
157
|
9 |
|
$this->contentsByIndex = $contentsByIndex; |
158
|
9 |
|
$this->numberOfResults = count( $this->contentsByIndex, COUNT_RECURSIVE ) - count( $this->contentsByIndex ); |
159
|
9 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @since 2.1 |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
9 |
|
public function getHtml() { |
168
|
|
|
|
169
|
9 |
|
$result = ''; |
170
|
9 |
|
$usedColumnCloser = false; |
171
|
9 |
|
$this->numRows = 0; |
172
|
|
|
|
173
|
|
|
// Class to determine whether we want responsive columns width |
174
|
9 |
|
if ( strpos( $this->columnClass, 'responsive' ) !== false ) { |
175
|
1 |
|
$this->columnWidth = 100; |
176
|
1 |
|
$this->numberOfColumns = 1; |
177
|
|
|
} else { |
178
|
8 |
|
$this->columnWidth = floor( 100 / $this->numberOfColumns ); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
9 |
|
$this->rowsPerColumn = ceil( $this->numberOfResults / $this->numberOfColumns ); |
|
|
|
|
182
|
9 |
|
$listContinuesAbbrev = wfMessage( 'listingcontinuesabbrev' )->text(); |
183
|
|
|
|
184
|
9 |
|
foreach ( $this->contentsByIndex as $key => $resultItems ) { |
185
|
|
|
|
186
|
9 |
|
if ( $resultItems === array() ) { |
187
|
|
|
continue; |
188
|
|
|
} |
189
|
|
|
|
190
|
9 |
|
$result .= $this->doFormat( |
191
|
|
|
$key, |
192
|
|
|
$listContinuesAbbrev, |
193
|
|
|
$resultItems, |
194
|
|
|
$usedColumnCloser |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
9 |
|
if ( !$usedColumnCloser ) { |
199
|
9 |
|
$result .= "</{$this->listType}></div> <!-- end column -->"; |
200
|
|
|
} |
201
|
|
|
|
202
|
9 |
|
return Html::rawElement( |
203
|
9 |
|
'div', |
204
|
|
|
array( |
205
|
9 |
|
'class' => $this->columnListClass, |
206
|
9 |
|
'dir' => $this->isRTL ? 'rtl' : 'ltr' |
207
|
|
|
), |
208
|
9 |
|
$result . "\n" . '<br style="clear: both;"/>' |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
9 |
|
private function doFormat( $key, $listContinuesAbbrev, $resultItems, &$usedColumnCloser ) { |
213
|
|
|
|
214
|
9 |
|
$result = ''; |
215
|
9 |
|
$previousKey = ""; |
216
|
9 |
|
$dir = $this->isRTL ? 'rtl' : 'ltr'; |
217
|
|
|
|
218
|
9 |
|
foreach ( $resultItems as $resultItem ) { |
219
|
|
|
|
220
|
9 |
|
if ( $this->numRows % $this->rowsPerColumn == 0 ) { |
221
|
9 |
|
$result .= "<div class=\"$this->columnClass\" style=\"width:$this->columnWidth%;\" dir=\"$dir\">"; |
222
|
|
|
|
223
|
9 |
|
$numRowsInColumn = $this->numRows + 1; |
224
|
|
|
|
225
|
9 |
|
if ( $key == $previousKey ) { |
226
|
|
|
// @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength.MaxExceeded |
227
|
5 |
|
$result .= ( $key !== '' ? Html::element( 'div', array( 'class' => 'smw-column-header' ), "$key $listContinuesAbbrev" ) : '' ) . "<{$this->listType} start={$numRowsInColumn}>"; |
228
|
|
|
// @codingStandardsIgnoreEnd |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// if we're at a new first letter, end |
233
|
|
|
// the last list and start a new one |
234
|
9 |
|
if ( $key != $previousKey ) { |
235
|
7 |
|
$result .= $this->numRows % $this->rowsPerColumn > 0 ? "</{$this->listType}>" : ''; |
236
|
7 |
|
$result .= ( $key !== '' ? Html::element( 'div', array( 'class' => 'smw-column-header' ), $key ) : '' ) . "<{$this->listType}>"; |
237
|
|
|
} |
238
|
|
|
|
239
|
9 |
|
$previousKey = $key; |
240
|
9 |
|
$result .= Html::rawElement( 'li', array(), $resultItem ); |
241
|
9 |
|
$usedColumnCloser = false; |
242
|
|
|
|
243
|
9 |
|
if ( ( $this->numRows + 1 ) % $this->rowsPerColumn == 0 && ( $this->numRows + 1 ) < $this->numberOfResults ) { |
244
|
6 |
|
$result .= "</{$this->listType}></div> <!-- end column -->"; |
245
|
6 |
|
$usedColumnCloser = true; |
246
|
|
|
} |
247
|
|
|
|
248
|
9 |
|
$this->numRows++; |
249
|
|
|
} |
250
|
|
|
|
251
|
9 |
|
return $result; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.