1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A class that holds static helper functions for generic mapping-related functions. |
5
|
|
|
* |
6
|
|
|
* @since 0.1 |
7
|
|
|
* |
8
|
|
|
* @deprecated |
9
|
|
|
* |
10
|
|
|
* @licence GNU GPL v2+ |
11
|
|
|
* @author Jeroen De Dauw < [email protected] > |
12
|
|
|
*/ |
13
|
|
|
final class MapsMapper { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Encode a variable of unknown type to JavaScript. |
17
|
|
|
* Arrays are converted to JS arrays, objects are converted to JS associative |
18
|
|
|
* arrays (objects). So cast your PHP associative arrays to objects before |
19
|
|
|
* passing them to here. |
20
|
|
|
* |
21
|
|
|
* This is a copy of |
22
|
|
|
* @see Xml::encodeJsVar |
23
|
|
|
* which fixes incorrect behaviour with floats. |
24
|
|
|
* |
25
|
|
|
* @since 0.7.1 |
26
|
|
|
* |
27
|
|
|
* @param mixed $value |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function encodeJsVar( $value ) { |
32
|
|
|
if ( is_bool( $value ) ) { |
33
|
|
|
$s = $value ? 'true' : 'false'; |
34
|
|
|
} elseif ( is_null( $value ) ) { |
35
|
|
|
$s = 'null'; |
36
|
|
|
} elseif ( is_int( $value ) || is_float( $value ) ) { |
37
|
|
|
$s = $value; |
38
|
|
|
} elseif ( is_array( $value ) && // Make sure it's not associative. |
39
|
|
|
array_keys($value) === range( 0, count($value) - 1 ) || |
40
|
|
|
count($value) == 0 |
41
|
|
|
) { |
42
|
|
|
$s = '['; |
43
|
|
|
foreach ( $value as $elt ) { |
|
|
|
|
44
|
|
|
if ( $s != '[' ) { |
45
|
|
|
$s .= ', '; |
46
|
|
|
} |
47
|
|
|
$s .= self::encodeJsVar( $elt ); |
48
|
|
|
} |
49
|
|
|
$s .= ']'; |
50
|
|
|
} elseif ( is_object( $value ) || is_array( $value ) ) { |
51
|
|
|
// Objects and associative arrays |
52
|
|
|
$s = '{'; |
53
|
|
|
foreach ( (array)$value as $name => $elt ) { |
54
|
|
|
if ( $s != '{' ) { |
55
|
|
|
$s .= ', '; |
56
|
|
|
} |
57
|
|
|
$s .= '"' . Xml::encodeJsVar( $name ) . '": ' . |
58
|
|
|
self::encodeJsVar( $elt ); |
59
|
|
|
} |
60
|
|
|
$s .= '}'; |
61
|
|
|
} else { |
62
|
|
|
$s = '"' . Xml::encodeJsVar( $value ) . '"'; |
63
|
|
|
} |
64
|
|
|
return $s; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* This function returns the definitions for the parameters used by every map feature. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public static function getCommonParameters() { |
73
|
|
|
global $egMapsAvailableGeoServices, $egMapsDefaultGeoService, $egMapsMapWidth, $egMapsMapHeight, $egMapsDefaultService; |
74
|
|
|
|
75
|
|
|
$params = []; |
76
|
|
|
|
77
|
|
|
$params['mappingservice'] = [ |
78
|
|
|
'type' => 'mappingservice', |
79
|
|
|
'aliases' => 'service', |
80
|
|
|
'default' => $egMapsDefaultService, |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
$params['geoservice'] = [ |
84
|
|
|
'default' => $egMapsDefaultGeoService, |
85
|
|
|
'values' => $egMapsAvailableGeoServices, |
86
|
|
|
'dependencies' => 'mappingservice', |
87
|
|
|
// TODO 'manipulations' => new MapsParamGeoService( 'mappingservice' ), |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
$params['width'] = [ |
91
|
|
|
'type' => 'dimension', |
92
|
|
|
'allowauto' => true, |
93
|
|
|
'units' => [ 'px', 'ex', 'em', '%', '' ], |
94
|
|
|
'default' => $egMapsMapWidth, |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$params['height'] = [ |
98
|
|
|
'type' => 'dimension', |
99
|
|
|
'units' => [ 'px', 'ex', 'em', '' ], |
100
|
|
|
'default' => $egMapsMapHeight, |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
// TODO$manipulation = new MapsParamLocation(); |
104
|
|
|
// TODO$manipulation->toJSONObj = true; |
105
|
|
|
|
106
|
|
|
$params['centre'] = [ |
107
|
|
|
'type' => 'mapslocation', |
108
|
|
|
'aliases' => [ 'center' ], |
109
|
|
|
'default' => false, |
110
|
|
|
'manipulatedefault' => false, |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
// Give grep a chance to find the usages: |
114
|
|
|
// maps-par-mappingservice, maps-par-geoservice, maps-par-width, |
115
|
|
|
// maps-par-height, maps-par-centre |
116
|
|
|
foreach ( $params as $name => &$data ) { |
117
|
|
|
$data['name'] = $name; |
118
|
|
|
$data['message'] = 'maps-par-' . $name; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return array_merge( $params, self::getEvenMawrCommonParameters() ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private static function getEvenMawrCommonParameters() { |
125
|
|
|
global $egMapsDefaultTitle, $egMapsDefaultLabel; |
126
|
|
|
|
127
|
|
|
$params = []; |
128
|
|
|
|
129
|
|
|
$params['title'] = [ |
130
|
|
|
'name' => 'title', |
131
|
|
|
'default' => $egMapsDefaultTitle, |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
$params['label'] = [ |
135
|
|
|
'default' => $egMapsDefaultLabel, |
136
|
|
|
'aliases' => 'text', |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$params['icon'] = [ |
140
|
|
|
'default' => '', // TODO: image param |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
$params['visitedicon'] = [ |
144
|
|
|
'default' => '', //TODO: image param |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
$params['lines'] = [ |
148
|
|
|
'type' => 'mapsline', |
149
|
|
|
'default' => [], |
150
|
|
|
'delimiter' => ';', |
151
|
|
|
'islist' => true, |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
$params['polygons'] = [ |
155
|
|
|
'type' => 'mapspolygon', |
156
|
|
|
'default' => [], |
157
|
|
|
'delimiter' => ';', |
158
|
|
|
'islist' => true, |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$params['circles'] = [ |
162
|
|
|
'type' => 'mapscircle', |
163
|
|
|
'default' => [], |
164
|
|
|
'delimiter' => ';', |
165
|
|
|
'islist' => true, |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
$params['rectangles'] = [ |
169
|
|
|
'type' => 'mapsrectangle', |
170
|
|
|
'default' => [], |
171
|
|
|
'delimiter' => ';', |
172
|
|
|
'islist' => true, |
173
|
|
|
]; |
174
|
|
|
|
175
|
|
|
$params['wmsoverlay'] = [ |
176
|
|
|
'type' => 'wmsoverlay', |
177
|
|
|
'default' => false, |
178
|
|
|
'delimiter' => ' ', |
179
|
|
|
]; |
180
|
|
|
|
181
|
|
|
$params['maxzoom'] = [ |
182
|
|
|
'type' => 'integer', |
183
|
|
|
'default' => false, |
184
|
|
|
'manipulatedefault' => false, |
185
|
|
|
'dependencies' => 'minzoom', |
186
|
|
|
]; |
187
|
|
|
|
188
|
|
|
$params['minzoom'] = [ |
189
|
|
|
'type' => 'integer', |
190
|
|
|
'default' => false, |
191
|
|
|
'manipulatedefault' => false, |
192
|
|
|
'lowerbound' => 0, |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
$params['copycoords'] = [ |
196
|
|
|
'type' => 'boolean', |
197
|
|
|
'default' => false, |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
$params['static'] = [ |
201
|
|
|
'type' => 'boolean', |
202
|
|
|
'default' => false, |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
// Give grep a chance to find the usages: |
206
|
|
|
// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon, |
207
|
|
|
// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons, |
208
|
|
|
// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay, |
209
|
|
|
// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords, |
210
|
|
|
// maps-displaymap-par-static |
211
|
|
|
foreach ( $params as $name => &$param ) { |
212
|
|
|
if ( !array_key_exists( 'message', $param ) ) { |
213
|
|
|
$param['message'] = 'maps-displaymap-par-' . $name; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $params; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Resolves the url of images provided as wiki page; leaves others alone. |
222
|
|
|
* |
223
|
|
|
* @since 1.0 |
224
|
|
|
* @deprecated |
225
|
|
|
* |
226
|
|
|
* @param string $file |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
5 |
|
public static function getFileUrl( $file ) { |
231
|
5 |
|
$title = Title::makeTitle( NS_FILE, $file ); |
232
|
|
|
|
233
|
5 |
|
if( $title !== null && $title->exists() ) { |
234
|
|
|
$imagePage = new ImagePage( $title ); |
235
|
|
|
return $imagePage->getDisplayedFile()->getURL(); |
236
|
|
|
} |
237
|
5 |
|
return $file; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns JS to init the vars to hold the map data when they are not there already. |
242
|
|
|
* |
243
|
|
|
* @since 1.0 |
244
|
|
|
* @deprecated |
245
|
|
|
* |
246
|
|
|
* @param string $serviceName |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public static function getBaseMapJSON( $serviceName ) { |
251
|
|
|
static $baseInit = false; |
252
|
|
|
static $serviceInit = []; |
253
|
|
|
|
254
|
|
|
$json = ''; |
255
|
|
|
|
256
|
|
|
if ( !$baseInit ) { |
257
|
|
|
$baseInit = true; |
258
|
|
|
$json .= 'var mwmaps={};'; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if ( !in_array( $serviceName, $serviceInit ) ) { |
262
|
|
|
$serviceInit[] = $serviceName; |
263
|
|
|
$json .= "mwmaps.$serviceName={};"; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $json; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.