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

namespace SMW\Query\ResultPrinters;

use Sanitizer;
use SMW\ApplicationFactory;
use SMWQueryResult as QueryResult;

/**
 * Exports data as file in a format that is defined by its invoked templates.
 * Custom specifications and requirements can be specified freely by relying on
 * the available template system.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class TemplateFileExportPrinter extends FileExportPrinter {

	/**
	 * @var integer
	 */
	private $numRows = 0;

	/**
	 * @var TemplateRenderer
	 */
	private $templateRenderer;

	/**
	 * @see ResultPrinter::getName
	 *
	 * {@inheritDoc}
	 */
	public function getName() {
		return $this->msg( 'smw_printername_templatefile' )->text();
	}

	/**
	 * @see FileExportPrinter::getMimeType
	 *
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function getMimeType( QueryResult $queryResult ) {

		if ( $this->params['mimetype'] !== '' ) {
			return $this->params['mimetype'];
		}

		return 'text/plain';
	}

	/**
	 * @see FileExportPrinter::getFileName
	 *
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function getFileName( QueryResult $queryResult ) {
		return $this->params['filename'];
	}

	/**
	 * @see ResultPrinter::getParamDefinitions
	 *
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function getParamDefinitions( array $definitions ) {
		$params = parent::getParamDefinitions( $definitions );

		$params['searchlabel']->setDefault( 'templateFile' );

		$params['template arguments'] = [
			'message' => 'smw-paramdesc-template-arguments',
			'default' => 'legacy',
			'values' => [ 'numbered', 'named', 'legacy' ],
		];

		$params['template'] = [
			'type' => 'string',
			'default' => '',
			'message' => 'smw-paramdesc-template',
		];

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

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

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

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

		$params['filename'] = [
			'message' => 'smw-paramdesc-filename',
			'default' => 'file.txt',
		];

		$params['mimetype'] = [
			'type' => 'string',
			'message' => 'smw-paramdesc-mimetype',
			'default' => 'text/plain',
		];

		return $params;
	}

	/**
	 * @see ResultPrinter::getResultText
	 *
	 * {@inheritDoc}
	 */
	protected function getResultText( QueryResult $queryResult, $outputMode ) {

		// Always return a link for when the output mode is not a file request,
		// a file request is normally only initiated when resolving the query
		// via Special:Ask
		if ( $outputMode !== SMW_OUTPUT_FILE ) {
			return $this->getFileLink( $queryResult, $outputMode );
		}

		$text = $this->expandTemplates(
			$this->getText( $queryResult )
		);

		return trim( $text, "\n" );
	}

	private function getFileLink( QueryResult $queryResult, $outputMode ) {

		// Can be viewed as HTML if requested, no more parsing needed
		$this->isHTML = $outputMode == SMW_OUTPUT_HTML;

		$link = $this->getLink(
			$queryResult,
			$outputMode
		);

		return $link->getText( $outputMode, $this->mLinker );
	}

	private function getText( $queryResult ) {

		$this->templateRenderer = ApplicationFactory::getInstance()->newMwCollaboratorFactory()->newWikitextTemplateRenderer();
		$result = '';

		$link = $this->getLink(
			$queryResult,
			SMW_OUTPUT_RAW
		);

		$link = $link->getText( SMW_OUTPUT_RAW, $this->mLinker );

		// Extra fields include:
		// - {{{userparam}}}
		// - {{{querylink}}}

		if ( $this->params['introtemplate'] !== '' ) {
			$this->templateRenderer->addField( 'userparam', $this->params['userparam'] );
			$this->templateRenderer->addField( 'querylink', $link );

			$this->templateRenderer->packFieldsForTemplate(
				$this->params['introtemplate']
			);

			$result .= $this->templateRenderer->render();
		}

		while ( $row = $queryResult->getNext() ) {
			$result .= $this->row( $queryResult, $row );
		}

		// Extra fields include:
		// - {{{userparam}}}
		// - {{{querylink}}}

		if ( $this->params['outrotemplate'] !== '' ) {
			$this->templateRenderer->addField( 'userparam', $this->params['userparam'] );
			$this->templateRenderer->addField( 'querylink', $link );

			$this->templateRenderer->packFieldsForTemplate(
				$this->params['outrotemplate']
			);

			$result .= $this->templateRenderer->render();
		}

		return $result;
	}

	private function row( QueryResult $queryResult, array $row ) {

		$this->numRows + 1;
		$this->addFields( $row );

		$this->templateRenderer->packFieldsForTemplate(
			$this->params['template']
		);

		return $this->templateRenderer->render();
	}

	private function addFields( $row ) {

		foreach ( $row as $i => $field ) {

			$value = '';
			$fieldName = '';

			// {{{?Foo}}}
			if ( $this->params['template arguments'] === 'legacy'  ) {
				$fieldName = '?' . $field->getPrintRequest()->getLabel();
			}

			// {{{Foo}}}
			if ( $this->params['template arguments'] === 'named' ) {
				$fieldName = $field->getPrintRequest()->getLabel();
			}

			// {{{1}}}
			if ( $fieldName === '' || $fieldName === '?' || $this->params['template arguments'] === 'numbered' ) {
				$fieldName = intval( $i + 1 );
			}

			while ( ( $text = $field->getNextText( SMW_OUTPUT_WIKI, $this->getLinker( $i == 0 ) ) ) !== false ) {
				$value .= $value === '' ? $text : $this->params['valuesep'] . ' ' . $text;
			}

			$this->templateRenderer->addField( $fieldName, $value );
		}

		$this->templateRenderer->addField( '#', $this->numRows );
	}

}