Completed
Push — master ( f04bbc...eab9d5 )
by Jeroen De
07:09
created

SMKMLPrinter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getResultText() 0 8 2
A handleParameters() 0 3 1
B getParamDefinitions() 0 28 1
A getKML() 0 13 1
C getKMLLink() 0 34 7
A getMimeType() 0 3 1
A getFileName() 0 3 1
A getName() 0 3 1
1
<?php
2
3
use ParamProcessor\ParamDefinition;
4
use SMW\FileExportPrinter;
5
6
/**
7
 * SMWResultPrinter class for printing a query result as KML.
8
 *
9
 * @since 0.7.3
10
 *
11
 * @file SM_KMLPrinter.php
12
 * @ingroup SemanticMaps
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class SMKMLPrinter extends FileExportPrinter {
18
19
	/**
20
	 * Handler of the print request.
21
	 *
22
	 * @since 0.7.3
23
	 *
24
	 * @param SMWQueryResult $res
25
	 * @param $outputmode
26
	 *
27
	 * @return array
28
	 */
29
	public function getResultText( SMWQueryResult $res, $outputmode ) {
30
		if ( $outputmode == SMW_OUTPUT_FILE ) {
31
			return $this->getKML( $res, $outputmode );
32
		}
33
		else {
34
			return $this->getKMLLink( $res, $outputmode );
35
		}
36
	}
37
38
	/**
39
	 * @see SMWResultPrinter::handleParameters
40
	 *
41
	 * @since 1.0
42
	 *
43
	 * @param array $params
44
	 * @param $outputmode
45
	 */
46
	protected function handleParameters( array $params, $outputmode ) {
47
		$this->params = $params;
48
	}
49
50
	/**
51
	 * @see SMWResultPrinter::getParamDefinitions
52
	 *
53
	 * @since 3.0
54
	 *
55
	 * @param ParamDefinition[] $definitions
56
	 *
57
	 * @return array of ParamDefinition|array
58
	 */
59
	public function getParamDefinitions( array $definitions ) {
60
		global $egMapsDefaultLabel, $egMapsDefaultTitle;
61
62
		$params = parent::getParamDefinitions( $definitions );
63
64
		$params['text'] = [
65
			'message' => 'semanticmaps-kml-text',
66
			'default' => $egMapsDefaultLabel,
67
		];
68
69
		$params['title'] = [
70
			'message' => 'semanticmaps-kml-title',
71
			'default' => $egMapsDefaultTitle,
72
		];
73
74
		$params['linkabsolute'] = [
75
			'message' => 'semanticmaps-kml-linkabsolute',
76
			'type' => 'boolean',
77
			'default' => true,
78
		];
79
80
		$params['pagelinktext'] = [
81
			'message' => 'semanticmaps-kml-pagelinktext',
82
			'default' => wfMessage( 'semanticmaps-default-kml-pagelink' )->text(),
83
		];
84
85
		return $params;
86
	}
87
88
	/**
89
	 * Returns the KML for the query result.
90
	 *
91
	 * @since 0.7.3
92
	 *
93
	 * @param SMWQueryResult $res
94
	 * @param integer $outputmode
95
	 *
96
	 * @return string
97
	 */
98
	protected function getKML( SMWQueryResult $res, $outputmode ) {
99
		$queryHandler = new SMQueryHandler( $res, $outputmode, $this->params['linkabsolute'], $this->params['pagelinktext'], false );
100
		$queryHandler->setText( $this->params['text'] );
101
		$queryHandler->setTitle( $this->params['title'] );
102
		$queryHandler->setSubjectSeparator( '' );
103
104
		$formatter = new MapsKMLFormatter( $this->params );
105
106
		$shapes = $queryHandler->getShapes();
107
		$formatter->addPlacemarks( $shapes['locations'] );
108
109
		return $formatter->getKML();
110
	}
111
112
	/**
113
	 * Returns a link (HTML) pointing to a query that returns the actual KML file.
114
	 *
115
	 * @since 0.7.3
116
	 *
117
	 * @param SMWQueryResult $res
118
	 * @param integer $outputmode
119
	 *
120
	 * @return string
121
	 */
122
	protected function getKMLLink( SMWQueryResult $res, $outputmode ) {
123
		$searchLabel = $this->getSearchLabel( $outputmode );
124
		$link = $res->getQueryLink( $searchLabel ? $searchLabel : wfMessage( 'semanticmaps-kml-link' )->inContentLanguage()->text() );
125
		$link->setParameter( 'kml', 'format' );
126
		$link->setParameter( $this->params['linkabsolute'] ? 'yes' : 'no', 'linkabsolute' );
127
		$link->setParameter( $this->params['pagelinktext'], 'pagelinktext' );
128
129
		if ( $this->params['title'] !== '' ) {
130
			$link->setParameter( $this->params['title'], 'title' );
131
		}
132
133
		if ( $this->params['text'] !== '' ) {
134
			$link->setParameter( $this->params['text'], 'text' );
135
		}
136
137
		// Fix for offset-error in getQueryLink()
138
		// (getQueryLink by default sets offset to point to the next 
139
		// result set, fix by setting it to 0 if now explicitly set)
140
		if ( array_key_exists( 'offset', $this->params ) ) {
141
			$link->setParameter( $this->params['offset'], 'offset' );
142
		} else {
143
			$link->setParameter( 0, 'offset' );
144
		}
145
146
		if ( array_key_exists( 'limit', $this->params ) ) {
147
			$link->setParameter( $this->params['limit'], 'limit' );
148
		} else { // Use a reasonable default limit.
149
			$link->setParameter( 20, 'limit' );
150
		}
151
152
		$this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
153
154
		return $link->getText( $outputmode, $this->mLinker );
155
	}
156
157
	/**
158
	 * @see SMWIExportPrinter::getMimeType
159
	 *
160
	 * @since 2.0
161
	 *
162
	 * @param SMWQueryResult $queryResult
163
	 *
164
	 * @return string
165
	 */
166
	public function getMimeType( SMWQueryResult $queryResult ) {
167
		return 'application/vnd.google-earth.kml+xml';
168
	}
169
170
	/**
171
	 * @see SMWIExportPrinter::getFileName
172
	 *
173
	 * @since 2.0
174
	 *
175
	 * @param SMWQueryResult $queryResult
176
	 *
177
	 * @return string|boolean
178
	 */
179
	public function getFileName( SMWQueryResult $queryResult ) {
180
		return 'kml.kml';
181
	}
182
183
	/**
184
	 * @see SMWResultPrinter::getName()
185
	 */
186
	public final function getName() {
187
		return wfMessage( 'semanticmaps-kml' )->text();
188
	}
189
}
190