summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/queryprinters/DsvResultPrinter.php
blob: f472132e5ff529fc8a4bfce1338f0add69a47cf2 (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
<?php

namespace SMW;

use Sanitizer;
use SMWQueryResult;

/**
 * Result printer to print results in UNIX-style DSV (deliminter separated value) format.
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class DsvResultPrinter extends FileExportPrinter {

	protected $separator = ':';
	protected $fileName = 'result.dsv';

	/**
	 * @see SMWResultPrinter::handleParameters
	 *
	 * @since 1.6
	 *
	 * @param array $params
	 * @param $outputmode
	 */
	protected function handleParameters( array $params, $outputmode ) {
		parent::handleParameters( $params, $outputmode );

		// Do not allow backspaces as delimiter, as they'll break stuff.
		if ( trim( $params['separator'] ) != '\\' ) {
			$this->separator = trim( $params['separator'] );
		}

		$this->fileName = str_replace( ' ', '_', $params['filename'] );
	}

	/**
	 * @see SMWIExportPrinter::getMimeType
	 *
	 * @since 1.8
	 *
	 * @param SMWQueryResult $queryResult
	 *
	 * @return string
	 */
	public function getMimeType( SMWQueryResult $queryResult ) {
		return 'text/dsv';
	}

	/**
	 * @see SMWIExportPrinter::getFileName
	 *
	 * @since 1.8
	 *
	 * @param SMWQueryResult $queryResult
	 *
	 * @return string|boolean
	 */
	public function getFileName( SMWQueryResult $queryResult ) {
		return $this->fileName;
	}

	public function getName() {
		return wfMessage( 'smw_printername_dsv' )->text();
	}

	protected function getResultText( SMWQueryResult $res, $outputMode ) {
		if ( $outputMode == SMW_OUTPUT_FILE ) { // Make the DSV file.
			return $this->getResultFileContents( $res );
		}
		else { // Create a link pointing to the DSV file.
			return $this->getLinkToFile( $res, $outputMode );
		}
	}

	/**
	 * Returns the query result in DSV.
	 *
	 * @since 1.6
	 *
	 * @param SMWQueryResult $res
	 *
	 * @return string
	 */
	protected function getResultFileContents( SMWQueryResult $queryResult ) {
		$lines = [];

		if ( $this->mShowHeaders ) {
			$headerItems = [];

			foreach ( $queryResult->getPrintRequests() as $printRequest ) {
				$headerItems[] = $printRequest->getLabel();
			}

			$lines[] = $this->getDSVLine( $headerItems );
		}

		// Loop over the result objects (pages).
		while ( $row = $queryResult->getNext() ) {
			$rowItems = [];

			/**
			 * Loop over their fields (properties).
			 * @var SMWResultArray $field
			 */
			foreach ( $row as $field ) {
				$itemSegments = [];

				// Loop over all values for the property.
				while ( ( $object = $field->getNextDataValue() ) !== false ) {
					$itemSegments[] = Sanitizer::decodeCharReferences( $object->getWikiValue() );
				}

				// Join all values into a single string, separating them with comma's.
				$rowItems[] = implode( ',', $itemSegments );
			}

			$lines[] = $this->getDSVLine( $rowItems );
		}

		return implode( "\n", $lines );
	}

	/**
	 * Returns a single DSV line.
	 *
	 * @since 1.6
	 *
	 * @param array $fields
	 *
	 * @return string
	 */
	protected function getDSVLine( array $fields ) {
		return implode( $this->separator, array_map( [ $this, 'encodeDSV' ], $fields ) );
	}

	/**
	 * Encodes a single DSV.
	 *
	 * @since 1.6
	 *
	 * @param string $value
	 *
	 * @return string
	 */
	protected function encodeDSV( $value ) {
		// TODO
		// \nnn or \onnn or \0nnn for the character with octal value nnn
		// \xnn for the character with hexadecimal value nn
		// \dnnn for the character with decimal value nnn
		// \unnnn for a hexadecimal Unicode literal.
		return str_replace(
			[ '\n', '\r', '\t', '\b', '\f', '\\', $this->separator ],
			[ "\n", "\r", "\t", "\b", "\f", '\\\\', "\\$this->separator" ],
			$value
		);
	}

	/**
	 * Returns html for a link to a query that returns the DSV file.
	 *
	 * @since 1.6
	 *
	 * @param SMWQueryResult $res
	 * @param $outputMode
	 *
	 * @return string
	 */
	protected function getLinkToFile( SMWQueryResult $res, $outputMode ) {
		// yes, our code can be viewed as HTML if requested, no more parsing needed
		$this->isHTML = ( $outputMode == SMW_OUTPUT_HTML );
		return $this->getLink( $res, $outputMode )->getText( $outputMode, $this->mLinker );
	}

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

		$params['searchlabel']->setDefault( wfMessage( 'smw_dsv_link' )->text() );

		$params['limit']->setDefault( 100 );

		$params[] = [
			'name' => 'separator',
			'message' => 'smw-paramdesc-dsv-separator',
			'default' => $this->separator,
			'aliases' => 'sep',
		];

		$params[] = [
			'name' => 'filename',
			'message' => 'smw-paramdesc-dsv-filename',
			'default' => $this->fileName,
		];

		return $params;
	}

}