Completed
Push — master ( 4c0a47...4e7d8d )
by
unknown
17:45
created

SRFValueRank::getResultValues()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 32
cp 0
rs 7.3166
c 0
b 0
f 0
cc 11
nc 24
nop 2
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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' ) {
95
						$value = $dataValue->getTitle()->getText();
96
						$html = $dataValue->getLongText( $outputMode, $this->getLinker( $isSubject ) );
97
					}
98
					else {
99
						$html = $dataValue->getShortText( $outputMode, $this->getLinker( false ) );
100
						$value = $html;
101
					}
102
103
					if ( !array_key_exists( $value, $tags ) ) {
104
						$tags[$value] = 0;
105
						$this->tagsHtml[$value] = $html; // Store the HTML separetely, so sorting can be done easily.
106
					}
107
108
					$tags[$value]++;
109
				}
110
			}
111
		}
112
113
		foreach ( $tags as $name => $count ) {
114
			if ( $count < $this->params['min'] ) {
115
				unset( $tags[$name] );
116
			}
117
		}
118
		return $tags;
119
	}
120
121
	/**
122
	 * Determine ranks
123
	 *
124
	 * @since 1.7
125
	 *
126
	 * @param array $tags
127
	 *
128
	 * @return array
129
	 */
130
	protected function getValueRank( array $tags ) {
131
		if ( count( $tags ) == 0 ) {
132
			return $tags;
133
		}
134
135
		arsort( $tags, SORT_NUMERIC );
136
137
		if ( count( $tags ) > $this->params['maxtags'] ) {
138
			$tags = array_slice( $tags, 0, $this->params['maxtags'], true );
139
		}
140
141
		return $tags;
142
	}
143
144
	/**
145
	 * Format the output representation
146
	 *
147
	 * @since 1.8
148
	 *
149
	 * @param array $tags
150
	 *
151
	 * @return string
152
	 */
153
	protected function getFormatOutput( array $tags ) {
154
		$htmlTags = [];
155
156
		if ( $this->params['introtemplate'] !== '' && $this->params['template'] !== '' ){
157
			$htmlTags[] = "{{" . $this->params['introtemplate'] . "}}";
158
		}
159
160
		foreach ( $tags as $name => $size ) {
161
			if ( $this->params['template'] !== '' ){
162
				$htmlTags[] = $this->addTemplateOutput( $name, $size, $rownum );
163
			} else {
164
				$htmlTags[] = Html::rawElement(
165
					( $this->params['liststyle'] === 'none' ? 'span' : 'li' ),
166
					[ 'style' => "font-size:$size" ],
167
					$this->tagsHtml[$name] . '&nbsp;(' . $size . ')'
168
				);
169
			}
170
		}
171
172
		if ( $this->params['outrotemplate'] !== '' && $this->params['template'] !== '' ){
173
			$htmlTags[] = "{{" . $this->params['outrotemplate'] . "}}";
174
		}
175
176
		return Html::rawElement(
177
			( $this->params['liststyle'] === 'none' ? 'div' : $this->params['liststyle'] ),
178
			[ 'class' => $this->params['class']  ],
179
			implode( '' , $htmlTags )
180
		);
181
	}
182
183
	/**
184
	 * Create a template output
185
	 *
186
	 * @since 1.8
187
	 *
188
	 * @param string $name
189
	 * @param integer $rank
190
	 * @param integer $rownum
191
	 *
192
	 * @return string
193
	 */
194
	protected function addTemplateOutput( $name, $rank, &$rownum ) {
195
		$rownum++;
196
		$wikitext  = $this->params['userparam'] ? "|userparam=" . $this->params['userparam'] : '';
197
		$wikitext .= "|" . $name;
198
		$wikitext .= "|rank=" . $rank;
199
		$wikitext .= "|#=$rownum";
200
		return '{{' . trim ( $this->params['template'] ) . $wikitext . '}}';
201
	}
202
203
	/**
204
	 * @see SMWResultPrinter::getParamDefinitions
205
	 *
206
	 * @since 1.8
207
	 *
208
	 * @param $definitions array of IParamDefinition
209
	 *
210
	 * @return array of IParamDefinition|array
211
	 */
212
	public function getParamDefinitions( array $definitions ) {
213
		$params = parent::getParamDefinitions( $definitions );
214
215
		$params['includesubject'] = [
216
			'type' => 'boolean',
217
			'default' => false,
218
			'message' => 'srf_paramdesc_includesubject',
219
		];
220
221
		$params['min'] = [
222
			'type' => 'integer',
223
			'default' => 1,
224
			'message' => 'srf_paramdesc_mincount',
225
		];
226
227
		$params['maxtags'] = [
228
			'type' => 'integer',
229
			'default' => 1000,
230
			'message' => 'srf_paramdesc_maxtags',
231
		];
232
233
		$params['template'] = [
234
			'message' => 'srf-paramdesc-template',
235
			'default' => '',
236
		];
237
238
		$params['userparam'] = [
239
			'message' => 'srf-paramdesc-userparam',
240
			'default' => '',
241
		];
242
243
		$params['introtemplate'] = [
244
			'message' => 'smw-paramdesc-introtemplate',
245
			'default' => '',
246
		];
247
248
		$params['outrotemplate'] = [
249
			'message' => 'smw-paramdesc-outrotemplate',
250
			'default' => '',
251
		];
252
253
		$params['liststyle'] = [
254
			'message' => 'srf-paramdesc-liststyle',
255
			'default' => 'ul',
256
			'values' => [ 'ul', 'ol', 'none' ],
257
		];
258
259
		$params['class'] = [
260
			'message' => 'srf-paramdesc-class',
261
			'default' => '',
262
		];
263
264
		return $params;
265
	}
266
}