Completed
Push — master ( 81538e...c2bf57 )
by Jeroen De
10:06
created

KmlPrinter::getKMLLink()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
cc 7
nc 16
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\SemanticMW;
6
7
use Maps\Presentation\KmlFormatter;
8
use Maps\SemanticMW\QueryHandler;
9
use ParamProcessor\ParamDefinition;
10
use SMW\Query\ResultPrinters\FileExportPrinter;
11
use SMWQueryResult;
12
13
/**
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class KmlPrinter extends FileExportPrinter {
18
19
	/**
20
	 * @param SMWQueryResult $res
21
	 * @param int $outputMode
22
	 *
23
	 * @return string
24
	 */
25
	public function getResultText( SMWQueryResult $res, $outputMode ) {
26
		if ( $outputMode == SMW_OUTPUT_FILE ) {
27
			return $this->getKML( $res, $outputMode );
28
		}
29
30
		return $this->getKMLLink( $res, $outputMode );
31
	}
32
33
	private function getKML( SMWQueryResult $res, int $outputMode ): string {
34
		$queryHandler = new QueryHandler( $res, $outputMode, $this->params['linkabsolute'] );
35
		$queryHandler->setText( $this->params['text'] );
36
		$queryHandler->setTitle( $this->params['title'] );
37
		$queryHandler->setSubjectSeparator( '' );
38
39
		$formatter = new KmlFormatter();
40
		return $formatter->formatLocationsAsKml( ...$queryHandler->getLocations() );
41
	}
42
43
	/**
44
	 * Returns a link (HTML) pointing to a query that returns the actual KML file.
45
	 */
46
	private function getKMLLink( SMWQueryResult $res, int $outputMode ): string {
47
		$searchLabel = $this->getSearchLabel( $outputMode );
48
		$link = $res->getQueryLink(
49
			$searchLabel ? $searchLabel : wfMessage( 'semanticmaps-kml-link' )->inContentLanguage()->text()
50
		);
51
		$link->setParameter( 'kml', 'format' );
52
		$link->setParameter( $this->params['linkabsolute'] ? 'yes' : 'no', 'linkabsolute' );
53
54
		if ( $this->params['title'] !== '' ) {
55
			$link->setParameter( $this->params['title'], 'title' );
56
		}
57
58
		if ( $this->params['text'] !== '' ) {
59
			$link->setParameter( $this->params['text'], 'text' );
60
		}
61
62
		// Fix for offset-error in getQueryLink()
63
		// (getQueryLink by default sets offset to point to the next
64
		// result set, fix by setting it to 0 if now explicitly set)
65
		if ( array_key_exists( 'offset', $this->params ) ) {
66
			$link->setParameter( $this->params['offset'], 'offset' );
67
		} else {
68
			$link->setParameter( 0, 'offset' );
69
		}
70
71
		if ( array_key_exists( 'limit', $this->params ) ) {
72
			$link->setParameter( $this->params['limit'], 'limit' );
73
		} else { // Use a reasonable default limit.
74
			$link->setParameter( 20, 'limit' );
75
		}
76
77
		$this->isHTML = ( $outputMode == SMW_OUTPUT_HTML );
78
79
		return $link->getText( $outputMode, $this->mLinker );
80
	}
81
82
	/**
83
	 * @see SMWResultPrinter::getParamDefinitions
84
	 *
85
	 * @param ParamDefinition[] $definitions
86
	 *
87
	 * @return array
88
	 */
89
	public function getParamDefinitions( array $definitions ) {
90
		global $egMapsDefaultLabel, $egMapsDefaultTitle;
91
92
		$definitions['text'] = [
93
			'message' => 'semanticmaps-kml-text',
94
			'default' => $egMapsDefaultLabel,
95
		];
96
97
		$definitions['title'] = [
98
			'message' => 'semanticmaps-kml-title',
99
			'default' => $egMapsDefaultTitle,
100
		];
101
102
		$definitions['linkabsolute'] = [
103
			'message' => 'semanticmaps-kml-linkabsolute',
104
			'type' => 'boolean',
105
			'default' => true,
106
		];
107
108
		return $definitions;
109
	}
110
111
	/**
112
	 * @see SMWIExportPrinter::getMimeType
113
	 *
114
	 * @param SMWQueryResult $queryResult
115
	 *
116
	 * @return string
117
	 */
118
	public function getMimeType( SMWQueryResult $queryResult ) {
119
		return 'application/vnd.google-earth.kml+xml';
120
	}
121
122
	/**
123
	 * @see SMWIExportPrinter::getFileName
124
	 *
125
	 * @param SMWQueryResult $queryResult
126
	 *
127
	 * @return string|boolean
128
	 */
129
	public function getFileName( SMWQueryResult $queryResult ) {
130
		return 'kml.kml';
131
	}
132
133
	/**
134
	 * @see SMWResultPrinter::getName()
135
	 */
136
	public final function getName() {
137
		return wfMessage( 'semanticmaps-kml' )->text();
138
	}
139
140
	/**
141
	 * @see SMWResultPrinter::handleParameters
142
	 *
143
	 * @param array $params
144
	 * @param $outputMode
145
	 */
146
	protected function handleParameters( array $params, $outputMode ) {
147
		$this->params = $params;
148
	}
149
}
150