summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/ResultPrinters/ListResultPrinter.php
blob: 6c24bc686c10ac0f21aa2d4e837823fd9eca477f (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
<?php

namespace SMW\Query\ResultPrinters;

use ParamProcessor\ParamDefinition;
use SMW\Message;
use SMW\Query\ResultPrinters\ListResultPrinter\ListResultBuilder;
use SMWQueryResult;

/**
 * Print query results in lists.
 *
 * @license GNU GPL v2+
 *
 * @author Markus Krötzsch
 */

/**
 * SMW's printer for results in lists.
 * The implementation covers comma-separated lists, ordered and unordered lists.
 * List items may be formatted using templates.
 *
 * In the code below, one list item (with all extra information displayed for
 * it) is called a "row", while one entry in this row is called a "field".
 * Every field may in turn contain many "values".
 */
class ListResultPrinter extends ResultPrinter {

	/**
	 * Get a human readable label for this printer.
	 *
	 * @return string
	 */
	public function getName() {
		// Give grep a chance to find the usages:
		// smw_printername_list, smw_printername_ol,smw_printername_ul, smw_printername_plainlist, smw_printername_template
		return Message::get( 'smw_printername_' . $this->mFormat, Message::TEXT, Message::USER_LANGUAGE );
	}

	/**
	 * @see ResultPrinter::isDeferrable
	 *
	 * {@inheritDoc}
	 */
	public function isDeferrable() {
		return true;
	}

	/**
	 * @see ResultPrinter::getResultText
	 *
	 * @param SMWQueryResult $queryResult
	 * @param $outputMode
	 *
	 * @return string
	 */
	protected function getResultText( SMWQueryResult $queryResult, $outputMode ) {

		$builder = $this->getBuilder( $queryResult );

		$this->hasTemplates = $this->hasTemplates();

		return $builder->getResultText() . $this->getFurtherResultsText( $queryResult, $outputMode );
	}

	/**
	 * @param SMWQueryResult $queryResult
	 *
	 * @return ListResultBuilder
	 */
	private function getBuilder( SMWQueryResult $queryResult ) {

		$builder = new ListResultBuilder( $queryResult, $this->mLinker );

		$builder->set( $this->params );

		$builder->set( [
			'link-first' => $this->mLinkFirst,
			'link-others' => $this->mLinkOthers,
			'show-headers' => $this->mShowHeaders,
		] );

		if ( $this->params[ 'template' ] !== '' && isset( $this->fullParams[ 'sep' ] ) && $this->fullParams[ 'sep' ]->wasSetToDefault() === true ) {
			$builder->set( 'sep', '' );
		}

		return $builder;
	}

	/**
	 * @return bool
	 */
	private function hasTemplates() {
		return $this->params[ 'template' ] !== '' || $this->params[ 'introtemplate' ] !== '' || $this->params[ 'outrotemplate' ] !== '';
	}


	/**
	 * Get text for further results link. Used only during getResultText().
	 *
	 * @since 1.9
	 * @param SMWQueryResult $res
	 * @param integer $outputMode
	 * @return string
	 */
	private function getFurtherResultsText( SMWQueryResult $res, $outputMode ) {

		if ( $this->linkFurtherResults( $res) ) {

			$link = $this->getFurtherResultsLink( $res, $outputMode );
			return $link->getText( SMW_OUTPUT_WIKI, $this->mLinker );

		}

		return '';
	}

	/**
	 * @since 3.0
	 *
	 * @return boolean
	 */
	public function supportsRecursiveAnnotation() {
		return true;
	}

	/**
	 * @see SMWIResultPrinter::getParamDefinitions
	 *
	 * @since 3.0
	 *
	 * @param ParamDefinition[] $definitions
	 *
	 * @return ParamDefinition[]
	 * @throws \Exception
	 */
	public function getParamDefinitions( array $definitions ) {

		$listFormatDefinitions = [

			'propsep' => [
				'message' => 'smw-paramdesc-propsep',
				'default' => Message::get( 'smw-format-list-property-separator' ),
			],

			'valuesep' => [
				'message' => 'smw-paramdesc-valuesep',
				'default' => Message::get( 'smw-format-list-value-separator' ),
			],

			'template' => [
				'message' => 'smw-paramdesc-template',
				'default' => '',
				'trim' => true,
			],

			'named args' => [
				'type' => 'boolean',
				'message' => 'smw-paramdesc-named_args',
				'default' => false,
			],

			'userparam' => [
				'message' => 'smw-paramdesc-userparam',
				'default' => '',
			],

			'class' => [
				'message' => 'smw-paramdesc-class',
				'default' => '',
			],

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

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

		];

		if ( $this->mFormat !== 'ul' && $this->mFormat !== 'ol' ) {

			$listFormatDefinitions[ 'sep' ] =
				[
					'message' => 'smw-paramdesc-sep',
					'default' => ', ',
				];
		}

		return array_merge( $definitions, ParamDefinition::getCleanDefinitions( $listFormatDefinitions ) );
	}
}