summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/queryprinters/AggregatablePrinter.php
blob: 7439d2f8124aef2622d6a2d478cfd8777f10774e (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php

namespace SMW;

use SMWDataItem;
use SMWQueryResult;

/**
 * Abstract class that supports the aggregation and distributive calculation
 * of numerical data.
 *
 * @since 1.9
 *
 *
 * @license GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */

/**
 * Abstract class that supports the aggregation and distributive calculation
 * of numerical data. Supports the distribution parameter, and related
 * parameters that allows the user to choose between regular behavior or
 * generating a distribution of values.
 *
 * For example, this result set: foo bar baz foo bar bar ohi
 * Will be turned into
 * * bar (3)
 * * foo (2)
 * * baz (1)
 * * ohi (1)
 *
 * @ingroup QueryPrinter
 */
abstract class AggregatablePrinter extends ResultPrinter {

	/**
	 * Create the formats output given the result data and return it.
	 *
	 * @since 1.7
	 *
	 * @param array $data label => value
	 */
	protected abstract function getFormatOutput( array $data );

	/**
	 * Method gets called right before the result is returned
	 * in case there are values to display. It is meant for
	 * adding resources such as JS and CSS only needed for this
	 * format when it has actual values.
	 *
	 * @since 1.7
	 */
	protected function addResources() {
	}

	/**
	 * (non-PHPdoc)
	 * @see SMWResultPrinter::getResultText()
	 */
	protected function getResultText( SMWQueryResult $queryResult, $outputMode ) {
		$data = $this->getResults( $queryResult, $outputMode );

		if ( $data === [] ) {
			$queryResult->addErrors( [
				$this->msg( 'smw-qp-empty-data' )->inContentLanguage()->text()
			] );
			return '';
		} else {
			$this->applyDistributionParams( $data );
			$this->addResources();
			return $this->getFormatOutput( $data );
		}
	}

	/**
	 * Apply the distribution specific parameters.
	 *
	 * @since 1.7
	 *
	 * @param array $data
	 */
	protected function applyDistributionParams( array &$data ) {
		if ( $this->params['distributionsort'] == 'asc' ) {
			asort( $data, SORT_NUMERIC );
		}
		elseif ( $this->params['distributionsort'] == 'desc' ) {
			arsort( $data, SORT_NUMERIC );
		}

		if ( $this->params['distributionlimit'] !== false ) {
			$data = array_slice( $data, 0, $this->params['distributionlimit'], true );
		}
	}

	/**
	 * Gets and processes the results so they can be fed directly to the
	 * getFormatOutput method. They are returned as an array with the keys
	 * being the labels and the values being their corresponding (numeric) values.
	 *
	 * @since 1.7
	 *
	 * @param SMWQueryResult $result
	 * @param $outputMode
	 *
	 * @return array label => value
	 */
	protected function getResults( SMWQueryResult $result, $outputMode ) {
		if ( $this->params['distribution'] ) {
			return $this->getDistributionResults( $result, $outputMode );
		}
		else {
			return $this->getNumericResults( $result, $outputMode );
		}
	}

	/**
	 * Counts all the occurrences of all values in the query result,
	 * and returns an array with as key the value and as value the count.
	 *
	 * @since 1.7
	 *
	 * @param SMWQueryResult $result
	 * @param $outputMode
	 *
	 * @return array label => value
	 */
	protected function getDistributionResults( SMWQueryResult $result, $outputMode ) {
		$values = [];

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

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

					if ( !array_key_exists( $value, $values ) ) {
						$values[$value] = 0;
					}

					$values[$value]++;
				}
			}
		}

		return $values;
	}

	/**
	 * Returns an array with the numerical data in the query result.
	 *
	 * @since 1.7
	 *
	 * @param SMWQueryResult $res
	 * @param $outputMode
	 *
	 * @return array label => value
	 */
	protected function getNumericResults( SMWQueryResult $res, $outputMode ) {
		$values = [];

		// print all result rows
		while ( $subject = $res->getNext() ) {
			$dataValue = $subject[0]->getNextDataValue();

			if ( $dataValue !== false ) {
				$name = $dataValue->getShortWikiText();

				foreach ( $subject as $field ) {

					// Use the aggregation parameter to determine the source of
					// the number composition
					if ( $this->params['aggregation'] === 'property' ) {
						$name = $field->getPrintRequest()->getLabel();
					}

					// Aggregated array key depends on the mainlabel which is
					// either the subject or a printout property
					if ( $this->params['mainlabel'] === '-' ) {

						// In case of '-', getNextDataValue() already shifted the
						// array forward which means the first column
						// ( $subject[0] == $field ) contains a possible value
						// and has to be collected as well
						if ( ( $subject[0] == $field ) && $dataValue->getDataItem()->getDIType() === SMWDataItem::TYPE_NUMBER ) {
							$value = $dataValue->getDataItem( )->getNumber();
							$values[$name] = isset( $values[$name] ) ?  $values[$name] + $value : $value;
						}
					}

					while ( ( /* SMWDataItem */ $dataItem = $field->getNextDataItem() ) !== false ) {
						$this->addNumbersForDataItem( $dataItem, $values, $name );
					}
				}
			}
		}

		return $values;
	}

	/**
	 * Adds all numbers contained in a dataitem to the list.
	 *
	 * @since 1.7
	 *
	 * @param SMWDataItem $dataItem
	 * @param array $values
	 * @param string $name
	 */
	protected function addNumbersForDataItem( SMWDataItem $dataItem, array &$values, $name ) {
		switch ( $dataItem->getDIType() ) {
			case SMWDataItem::TYPE_NUMBER:
				// Collect and aggregate values for the same array key
				$value = $dataItem->getNumber();
				if ( !isset( $values[$name] ) ) {
					$values[$name] = 0;
				}
				$values[$name] += $value;
				break;
			case SMWDataItem::TYPE_CONTAINER:
				foreach ( $dataItem->getDataItems() as $di ) {
					$this->addNumbersForDataItem( $di, $values, $name );
				}
				break;
			default:
		}
	}

	/**
	 * @codeCoverageIgnore
	 * @see SMWResultPrinter::getParamDefinitions
	 *
	 * @since 1.8
	 *
	 * @param ParamDefinition[] $definitions
	 *
	 * @return array
	 */
	public function getParamDefinitions( array $definitions ) {
		$definitions = parent::getParamDefinitions( $definitions );

		$definitions['distribution'] = [
			'name' => 'distribution',
			'type' => 'boolean',
			'default' => false,
			'message' => 'smw-paramdesc-distribution',
		];

		$definitions['distributionsort'] = [
			'name' => 'distributionsort',
			'type' => 'string',
			'default' => 'none',
			'message' => 'smw-paramdesc-distributionsort',
			'values' => [ 'asc', 'desc', 'none' ],
		];

		$definitions['distributionlimit'] = [
			'name' => 'distributionlimit',
			'type' => 'integer',
			'default' => false,
			'manipulatedefault' => false,
			'message' => 'smw-paramdesc-distributionlimit',
			'lowerbound' => 1,
		];

		$definitions['aggregation'] = [
			'message' => 'smw-paramdesc-aggregation',
			'default' => 'subject',
			'values' => [ 'property', 'subject' ],
		];

		return $definitions;
	}

}