summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/formats/valuerank/SRF_ValueRank.php
blob: f3df915ec0acb97c36aa189854c529344ee93571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

/**
 * Result printer that prints query results as a valuerank.
 * In other words, it prints a list of all occuring values, with duplicates removed,
 * together with their occurrence count.
 *
 * Build out of Tag Cloud Format by Jeroen De Dauw < jeroendedauw@gmail.com >
 *
 * For example, this result set: foo bar baz foo bar bar ohi
 * Will be turned into
 * * bar (3)
 * * foo (2)
 * * baz (1)
 * * ohi (1)
 *
 * @since 1.7
 *
 * @licence GNU GPL v2
 * @author DaSch < dasch@daschmedia.de >
 * @author mwjames
 */
class SRFValueRank extends SMWResultPrinter {

	/**
	 * @var array
	 */
	protected $tagsHtml = [];

	/**
	 * @see SMWResultPrinter::getName
	 *
	 * @return string
	 */
	public function getName() {
		return wfMessage( 'srf_printername_valuerank' )->text();
	}

	/**
	 * @see SMWResultPrinter::getResultText
	 *
	 * @since 1.7
	 *
	 * @param SMWQueryResult $results
	 * @param $outputMode
	 *
	 * @return string
	 */
	public function getResultText( SMWQueryResult $results, $outputMode ) {

		// Template support
		$this->hasTemplates = $this->params['template'] !== '';

		// Prioritize HTML setting
		$this->isHTML = $this->params['template'] === '';

		$outputMode = SMW_OUTPUT_HTML;

		return $this->getFormatOutput( $this->getValueRank( $this->getResultValues( $results, $outputMode ) ) );
	}

	/**
	 * Returns an array with the tags (keys) and the number of times they occur (values).
	 *
	 * @since 1.7
	 *
	 * @param SMWQueryResult $results
	 * @param $outputMode
	 *
	 * @return array
	 */
	protected function getResultValues( SMWQueryResult $results, $outputMode ) {
		$tags = [];

		/**
		 * @var $row SMWResultArray Objects (pages)
		 * @var $dataValue SMWDataValue
		 *
		 * @return array
		 */
		while ( $row = $results->getNext() ) {
			// SMWResultArray for a sinlge property
			for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) {
				while ( ( $dataValue = $row[$i]->getNextDataValue() ) !== false ) {

					$isSubject = $row[$i]->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS;

					// If the main object should not be included, skip it.
					if ( $i == 0 && !$this->params['includesubject'] && $isSubject ) {
						continue;
					}

					// Get the HTML for the tag content. Pages are linked, other stuff is just plaintext.
					if ( $dataValue->getTypeID() == '_wpg' ) {
						$value = $dataValue->getTitle()->getText();
						$html = $dataValue->getLongText( $outputMode, $this->getLinker( $isSubject ) );
					} else {
						$html = $dataValue->getShortText( $outputMode, $this->getLinker( false ) );
						$value = $html;
					}

					if ( !array_key_exists( $value, $tags ) ) {
						$tags[$value] = 0;
						$this->tagsHtml[$value] = $html; // Store the HTML separetely, so sorting can be done easily.
					}

					$tags[$value]++;
				}
			}
		}

		foreach ( $tags as $name => $count ) {
			if ( $count < $this->params['min'] ) {
				unset( $tags[$name] );
			}
		}
		return $tags;
	}

	/**
	 * Determine ranks
	 *
	 * @since 1.7
	 *
	 * @param array $tags
	 *
	 * @return array
	 */
	protected function getValueRank( array $tags ) {
		if ( count( $tags ) == 0 ) {
			return $tags;
		}

		arsort( $tags, SORT_NUMERIC );

		if ( count( $tags ) > $this->params['maxtags'] ) {
			$tags = array_slice( $tags, 0, $this->params['maxtags'], true );
		}

		return $tags;
	}

	/**
	 * Format the output representation
	 *
	 * @since 1.8
	 *
	 * @param array $tags
	 *
	 * @return string
	 */
	protected function getFormatOutput( array $tags ) {
		$htmlTags = [];

		if ( $this->params['introtemplate'] !== '' && $this->params['template'] !== '' ) {
			$htmlTags[] = "{{" . $this->params['introtemplate'] . "}}";
		}

		foreach ( $tags as $name => $size ) {
			if ( $this->params['template'] !== '' ) {
				$htmlTags[] = $this->addTemplateOutput( $name, $size, $rownum );
			} else {
				$htmlTags[] = Html::rawElement(
					( $this->params['liststyle'] === 'none' ? 'span' : 'li' ),
					[ 'style' => "font-size:$size" ],
					$this->tagsHtml[$name] . '&nbsp;(' . $size . ')'
				);
			}
		}

		if ( $this->params['outrotemplate'] !== '' && $this->params['template'] !== '' ) {
			$htmlTags[] = "{{" . $this->params['outrotemplate'] . "}}";
		}

		return Html::rawElement(
			( $this->params['liststyle'] === 'none' ? 'div' : $this->params['liststyle'] ),
			[ 'class' => $this->params['class'] ],
			implode( '', $htmlTags )
		);
	}

	/**
	 * Create a template output
	 *
	 * @since 1.8
	 *
	 * @param string $name
	 * @param integer $rank
	 * @param integer $rownum
	 *
	 * @return string
	 */
	protected function addTemplateOutput( $name, $rank, &$rownum ) {
		$rownum++;
		$wikitext = $this->params['userparam'] ? "|userparam=" . $this->params['userparam'] : '';
		$wikitext .= "|" . $name;
		$wikitext .= "|rank=" . $rank;
		$wikitext .= "|#=$rownum";
		return '{{' . trim( $this->params['template'] ) . $wikitext . '}}';
	}

	/**
	 * @see SMWResultPrinter::getParamDefinitions
	 *
	 * @since 1.8
	 *
	 * @param $definitions array of IParamDefinition
	 *
	 * @return array of IParamDefinition|array
	 */
	public function getParamDefinitions( array $definitions ) {
		$params = parent::getParamDefinitions( $definitions );

		$params['includesubject'] = [
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf_paramdesc_includesubject',
		];

		$params['min'] = [
			'type' => 'integer',
			'default' => 1,
			'message' => 'srf_paramdesc_mincount',
		];

		$params['maxtags'] = [
			'type' => 'integer',
			'default' => 1000,
			'message' => 'srf_paramdesc_maxtags',
		];

		$params['template'] = [
			'message' => 'srf-paramdesc-template',
			'default' => '',
		];

		$params['userparam'] = [
			'message' => 'srf-paramdesc-userparam',
			'default' => '',
		];

		$params['introtemplate'] = [
			'message' => 'smw-paramdesc-introtemplate',
			'default' => '',
		];

		$params['outrotemplate'] = [
			'message' => 'smw-paramdesc-outrotemplate',
			'default' => '',
		];

		$params['liststyle'] = [
			'message' => 'srf-paramdesc-liststyle',
			'default' => 'ul',
			'values' => [ 'ul', 'ol', 'none' ],
		];

		$params['class'] = [
			'message' => 'srf-paramdesc-class',
			'default' => '',
		];

		return $params;
	}
}