|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Result printer that prints query results as a valuerank. |
|
5
|
|
|
* In other words, it prints a list of all occuring values, with duplicates removed, |
|
6
|
|
|
* together with their occurrence count. |
|
7
|
|
|
* |
|
8
|
|
|
* Build out of Tag Cloud Format by Jeroen De Dauw < [email protected] > |
|
9
|
|
|
* |
|
10
|
|
|
* For example, this result set: foo bar baz foo bar bar ohi |
|
11
|
|
|
* Will be turned into |
|
12
|
|
|
* * bar (3) |
|
13
|
|
|
* * foo (2) |
|
14
|
|
|
* * baz (1) |
|
15
|
|
|
* * ohi (1) |
|
16
|
|
|
* |
|
17
|
|
|
* @since 1.7 |
|
18
|
|
|
* |
|
19
|
|
|
* @licence GNU GPL v2 |
|
20
|
|
|
* @author DaSch < [email protected] > |
|
21
|
|
|
* @author mwjames |
|
22
|
|
|
*/ |
|
23
|
|
|
class SRFValueRank extends SMWResultPrinter { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $tagsHtml = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @see SMWResultPrinter::getName |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getName() { |
|
36
|
|
|
return wfMessage( 'srf_printername_valuerank' )->text(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @see SMWResultPrinter::getResultText |
|
41
|
|
|
* |
|
42
|
|
|
* @since 1.7 |
|
43
|
|
|
* |
|
44
|
|
|
* @param SMWQueryResult $results |
|
45
|
|
|
* @param $outputMode |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getResultText( SMWQueryResult $results, $outputMode ) { |
|
50
|
|
|
|
|
51
|
|
|
// Template support |
|
52
|
|
|
$this->hasTemplates = $this->params['template'] !== ''; |
|
53
|
|
|
|
|
54
|
|
|
// Prioritize HTML setting |
|
55
|
|
|
$this->isHTML = $this->params['template'] === ''; |
|
56
|
|
|
|
|
57
|
|
|
$outputMode = SMW_OUTPUT_HTML; |
|
58
|
|
|
|
|
59
|
|
|
return $this->getFormatOutput( $this->getValueRank( $this->getResultValues( $results, $outputMode ) ) ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns an array with the tags (keys) and the number of times they occur (values). |
|
64
|
|
|
* |
|
65
|
|
|
* @since 1.7 |
|
66
|
|
|
* |
|
67
|
|
|
* @param SMWQueryResult $results |
|
68
|
|
|
* @param $outputMode |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getResultValues( SMWQueryResult $results, $outputMode ) { |
|
73
|
|
|
$tags = []; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var $row SMWResultArray Objects (pages) |
|
77
|
|
|
* @var $dataValue SMWDataValue |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
while ( $row = $results->getNext() ) { |
|
82
|
|
|
// SMWResultArray for a sinlge property |
|
83
|
|
|
for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) { |
|
84
|
|
|
while ( ( $dataValue = $row[$i]->getNextDataValue() ) !== false ) { |
|
85
|
|
|
|
|
86
|
|
|
$isSubject = $row[$i]->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS; |
|
87
|
|
|
|
|
88
|
|
|
// If the main object should not be included, skip it. |
|
89
|
|
|
if ( $i == 0 && !$this->params['includesubject'] && $isSubject ) { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Get the HTML for the tag content. Pages are linked, other stuff is just plaintext. |
|
94
|
|
|
if ( $dataValue->getTypeID() == '_wpg' && $dataValue->getDataItem()->getTitle() !== null ) { |
|
95
|
|
|
$value = $dataValue->getDataItem()->getTitle()->getText(); |
|
96
|
|
|
$html = $dataValue->getLongText( $outputMode, $this->getLinker( $isSubject ) ); |
|
97
|
|
|
} else { |
|
98
|
|
|
$html = $dataValue->getShortText( $outputMode, $this->getLinker( false ) ); |
|
99
|
|
|
$value = $html; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ( !array_key_exists( $value, $tags ) ) { |
|
103
|
|
|
$tags[$value] = 0; |
|
104
|
|
|
$this->tagsHtml[$value] = $html; // Store the HTML separetely, so sorting can be done easily. |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$tags[$value]++; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
foreach ( $tags as $name => $count ) { |
|
113
|
|
|
if ( $count < $this->params['min'] ) { |
|
114
|
|
|
unset( $tags[$name] ); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
return $tags; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Determine ranks |
|
122
|
|
|
* |
|
123
|
|
|
* @since 1.7 |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $tags |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getValueRank( array $tags ) { |
|
130
|
|
|
if ( count( $tags ) == 0 ) { |
|
131
|
|
|
return $tags; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
arsort( $tags, SORT_NUMERIC ); |
|
135
|
|
|
|
|
136
|
|
|
if ( count( $tags ) > $this->params['maxtags'] ) { |
|
137
|
|
|
$tags = array_slice( $tags, 0, $this->params['maxtags'], true ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $tags; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Format the output representation |
|
145
|
|
|
* |
|
146
|
|
|
* @since 1.8 |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $tags |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function getFormatOutput( array $tags ) { |
|
153
|
|
|
$htmlTags = []; |
|
154
|
|
|
|
|
155
|
|
|
if ( $this->params['introtemplate'] !== '' && $this->params['template'] !== '' ) { |
|
156
|
|
|
$htmlTags[] = "{{" . $this->params['introtemplate'] . "}}"; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
foreach ( $tags as $name => $size ) { |
|
160
|
|
|
if ( $this->params['template'] !== '' ) { |
|
161
|
|
|
$htmlTags[] = $this->addTemplateOutput( $name, $size, $rownum ); |
|
162
|
|
|
} else { |
|
163
|
|
|
$htmlTags[] = Html::rawElement( |
|
164
|
|
|
( $this->params['liststyle'] === 'none' ? 'span' : 'li' ), |
|
165
|
|
|
[ 'style' => "font-size:$size" ], |
|
166
|
|
|
$this->tagsHtml[$name] . ' (' . $size . ')' |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ( $this->params['outrotemplate'] !== '' && $this->params['template'] !== '' ) { |
|
172
|
|
|
$htmlTags[] = "{{" . $this->params['outrotemplate'] . "}}"; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return Html::rawElement( |
|
176
|
|
|
( $this->params['liststyle'] === 'none' ? 'div' : $this->params['liststyle'] ), |
|
177
|
|
|
[ 'class' => $this->params['class'] ], |
|
178
|
|
|
implode( '', $htmlTags ) |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Create a template output |
|
184
|
|
|
* |
|
185
|
|
|
* @since 1.8 |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $name |
|
188
|
|
|
* @param integer $rank |
|
189
|
|
|
* @param integer $rownum |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function addTemplateOutput( $name, $rank, &$rownum ) { |
|
194
|
|
|
$rownum++; |
|
195
|
|
|
$wikitext = $this->params['userparam'] ? "|userparam=" . $this->params['userparam'] : ''; |
|
196
|
|
|
$wikitext .= "|" . $name; |
|
197
|
|
|
$wikitext .= "|rank=" . $rank; |
|
198
|
|
|
$wikitext .= "|#=$rownum"; |
|
199
|
|
|
return '{{' . trim( $this->params['template'] ) . $wikitext . '}}'; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @see SMWResultPrinter::getParamDefinitions |
|
204
|
|
|
* |
|
205
|
|
|
* @since 1.8 |
|
206
|
|
|
* |
|
207
|
|
|
* @param $definitions array of IParamDefinition |
|
208
|
|
|
* |
|
209
|
|
|
* @return array of IParamDefinition|array |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getParamDefinitions( array $definitions ) { |
|
212
|
|
|
$params = parent::getParamDefinitions( $definitions ); |
|
213
|
|
|
|
|
214
|
|
|
$params['includesubject'] = [ |
|
215
|
|
|
'type' => 'boolean', |
|
216
|
|
|
'default' => false, |
|
217
|
|
|
'message' => 'srf_paramdesc_includesubject', |
|
218
|
|
|
]; |
|
219
|
|
|
|
|
220
|
|
|
$params['min'] = [ |
|
221
|
|
|
'type' => 'integer', |
|
222
|
|
|
'default' => 1, |
|
223
|
|
|
'message' => 'srf_paramdesc_mincount', |
|
224
|
|
|
]; |
|
225
|
|
|
|
|
226
|
|
|
$params['maxtags'] = [ |
|
227
|
|
|
'type' => 'integer', |
|
228
|
|
|
'default' => 1000, |
|
229
|
|
|
'message' => 'srf_paramdesc_maxtags', |
|
230
|
|
|
]; |
|
231
|
|
|
|
|
232
|
|
|
$params['template'] = [ |
|
233
|
|
|
'message' => 'smw-paramdesc-template', |
|
234
|
|
|
'default' => '', |
|
235
|
|
|
]; |
|
236
|
|
|
|
|
237
|
|
|
$params['userparam'] = [ |
|
238
|
|
|
'message' => 'smw-paramdesc-userparam', |
|
239
|
|
|
'default' => '', |
|
240
|
|
|
]; |
|
241
|
|
|
|
|
242
|
|
|
$params['introtemplate'] = [ |
|
243
|
|
|
'message' => 'smw-paramdesc-introtemplate', |
|
244
|
|
|
'default' => '', |
|
245
|
|
|
]; |
|
246
|
|
|
|
|
247
|
|
|
$params['outrotemplate'] = [ |
|
248
|
|
|
'message' => 'smw-paramdesc-outrotemplate', |
|
249
|
|
|
'default' => '', |
|
250
|
|
|
]; |
|
251
|
|
|
|
|
252
|
|
|
$params['liststyle'] = [ |
|
253
|
|
|
'message' => 'srf-paramdesc-liststyle', |
|
254
|
|
|
'default' => 'ul', |
|
255
|
|
|
'values' => [ 'ul', 'ol', 'none' ], |
|
256
|
|
|
]; |
|
257
|
|
|
|
|
258
|
|
|
$params['class'] = [ |
|
259
|
|
|
'message' => 'smw-paramdesc-class', |
|
260
|
|
|
'default' => '', |
|
261
|
|
|
]; |
|
262
|
|
|
|
|
263
|
|
|
return $params; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|