KmlPrinter   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 0
loc 133
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

8 Methods

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