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

namespace SMW\Query\ResultPrinters;

use Sanitizer;
use SMW\Utils\Csv;
use SMWQueryResult as QueryResult;

/**
 * CSV export support
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author Nathan R. Yergler
 * @author Markus Krötzsch
 * @author mwjames
 */
class CsvFileExportPrinter extends FileExportPrinter {

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

	/**
	 * @see FileExportPrinter::getMimeType
	 *
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getMimeType( QueryResult $queryResult ) {
		return 'text/csv';
	}

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

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

		$definitions['searchlabel']->setDefault(
			$this->msg( 'smw_csv_link' )->inContentLanguage()->text()
		);

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

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

		$params['showsep'] = [
			'type' => 'boolean',
			'default' => false,
			'message' => 'smw-paramdesc-showsep',
		];

		$params[] = [
			'name' => 'filename',
			'message' => 'smw-paramdesc-filename',
			'default' => 'result.csv',
		];

		$params['merge'] = [
			'type' => 'boolean',
			'default' => false,
			'message' => 'smw-paramdesc-csv-merge',
		];

		$params['bom'] = [
			'type' => 'boolean',
			'default' => false,
			'message' => 'smw-paramdesc-csv-bom',
		];

		return $params;
	}

	/**
	 * @see ResultPrinter::getResultText
	 *
	 * {@inheritDoc}
	 */
	protected function getResultText( QueryResult $res, $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->getCsvLink( $res, $outputMode );
		}

		$csv = new Csv(
			$this->params['showsep'],
			$this->params['bom']
		);

		return $this->getCsv( $csv, $res );
	}

	private function getCsvLink( QueryResult $res, $outputMode ) {

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

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

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

	private function getCsv( Csv $csv, $res ) {

		$sep = str_replace( '_', ' ', $this->params['sep'] );
		$vsep = str_replace( '_', ' ', $this->params['valuesep'] );

		$header = [];
		$rows = [];

		if ( $this->mShowHeaders ) {
			foreach ( $res->getPrintRequests() as $pr ) {
				$header[] = $pr->getLabel();
			}
		}

		while ( $row = $res->getNext() ) {
			$row_items = [];

			foreach ( $row as /* SMWResultArray */ $field ) {
				$growing = [];

				while ( ( $object = $field->getNextDataValue() ) !== false ) {
					$growing[] = Sanitizer::decodeCharReferences( $object->getWikiValue() );
				}

				$row_items[] = implode( $vsep, $growing );
			}

			$rows[] = $row_items;
		}

		if ( $this->params['merge'] === true ) {
			$rows = $csv->merge( $rows, $vsep );
		}

		return $csv->toString(
			$header,
			$rows,
			$sep
		);
	}

}