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
|
|
|
* |
23
|
|
|
* @see Xml::encodeJsVar |
24
|
|
|
* which fixes incorrect behaviour with floats. |
25
|
|
|
* |
26
|
|
|
* @since 0.7.1 |
27
|
|
|
* |
28
|
|
|
* @param mixed $value |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public static function encodeJsVar( $value ) { |
33
|
|
|
if ( is_bool( $value ) ) { |
34
|
|
|
$s = $value ? 'true' : 'false'; |
35
|
|
|
} elseif ( is_null( $value ) ) { |
36
|
|
|
$s = 'null'; |
37
|
|
|
} elseif ( is_int( $value ) || is_float( $value ) ) { |
38
|
|
|
$s = $value; |
39
|
|
|
} elseif ( is_array( $value ) && // Make sure it's not associative. |
40
|
|
|
array_keys( $value ) === range( 0, count( $value ) - 1 ) || |
41
|
|
|
count( $value ) == 0 |
42
|
|
|
) { |
43
|
|
|
$s = '['; |
44
|
|
|
foreach ( $value as $elt ) { |
|
|
|
|
45
|
|
|
if ( $s != '[' ) { |
46
|
|
|
$s .= ', '; |
47
|
|
|
} |
48
|
|
|
$s .= self::encodeJsVar( $elt ); |
49
|
|
|
} |
50
|
|
|
$s .= ']'; |
51
|
|
|
} elseif ( is_object( $value ) || is_array( $value ) ) { |
52
|
|
|
// Objects and associative arrays |
53
|
|
|
$s = '{'; |
54
|
|
|
foreach ( (array)$value as $name => $elt ) { |
55
|
|
|
if ( $s != '{' ) { |
56
|
|
|
$s .= ', '; |
57
|
|
|
} |
58
|
|
|
$s .= '"' . Xml::encodeJsVar( $name ) . '": ' . |
59
|
|
|
self::encodeJsVar( $elt ); |
60
|
|
|
} |
61
|
|
|
$s .= '}'; |
62
|
|
|
} else { |
63
|
|
|
$s = '"' . Xml::encodeJsVar( $value ) . '"'; |
64
|
|
|
} |
65
|
|
|
return $s; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* This function returns the definitions for the parameters used by every map feature. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public static function getCommonParameters() { |
74
|
|
|
$params = []; |
75
|
|
|
|
76
|
|
|
$params['mappingservice'] = [ |
77
|
|
|
'type' => 'string', |
78
|
|
|
'aliases' => 'service', |
79
|
|
|
'default' => $GLOBALS['egMapsDefaultService'], |
80
|
|
|
'values' => self::getAllPossibleServiceValues(), |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
$params['width'] = [ |
84
|
|
|
'type' => 'dimension', |
85
|
|
|
'allowauto' => true, |
86
|
|
|
'units' => [ 'px', 'ex', 'em', '%', '' ], |
87
|
|
|
'default' => $GLOBALS['egMapsMapWidth'], |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
$params['height'] = [ |
91
|
|
|
'type' => 'dimension', |
92
|
|
|
'units' => [ 'px', 'ex', 'em', '' ], |
93
|
|
|
'default' => $GLOBALS['egMapsMapHeight'], |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$params['centre'] = [ |
97
|
|
|
'type' => 'string', |
98
|
|
|
'aliases' => [ 'center' ], |
99
|
|
|
'default' => false, |
100
|
|
|
'manipulatedefault' => false, |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
// Give grep a chance to find the usages: |
104
|
|
|
// maps-par-mappingservice, maps-par-geoservice, maps-par-width, |
105
|
|
|
// maps-par-height, maps-par-centre |
106
|
|
|
foreach ( $params as $name => &$data ) { |
107
|
|
|
$data['name'] = $name; |
108
|
|
|
$data['message'] = 'maps-par-' . $name; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return array_merge( $params, self::getEvenMawrCommonParameters() ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private static function getAllPossibleServiceValues(): array { |
115
|
|
|
$allServiceValues = []; |
116
|
|
|
|
117
|
|
|
foreach ( $GLOBALS['egMapsAvailableServices'] as $availableService ) { |
118
|
|
|
$allServiceValues = array_merge( |
119
|
|
|
$allServiceValues, |
120
|
|
|
[ $availableService ], |
121
|
|
|
MapsMappingServices::getServiceInstance( $availableService )->getAliases() |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $allServiceValues; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private static function getEvenMawrCommonParameters() { |
129
|
|
|
global $egMapsDefaultTitle, $egMapsDefaultLabel; |
130
|
|
|
|
131
|
|
|
$params = []; |
132
|
|
|
|
133
|
|
|
$params['title'] = [ |
134
|
|
|
'name' => 'title', |
135
|
|
|
'default' => $egMapsDefaultTitle, |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
$params['label'] = [ |
139
|
|
|
'default' => $egMapsDefaultLabel, |
140
|
|
|
'aliases' => 'text', |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
$params['icon'] = [ |
144
|
|
|
'default' => '', // TODO: image param |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
$params['visitedicon'] = [ |
148
|
|
|
'default' => '', //TODO: image param |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
$params['lines'] = [ |
152
|
|
|
'type' => 'mapsline', |
153
|
|
|
'default' => [], |
154
|
|
|
'delimiter' => ';', |
155
|
|
|
'islist' => true, |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$params['polygons'] = [ |
159
|
|
|
'type' => 'mapspolygon', |
160
|
|
|
'default' => [], |
161
|
|
|
'delimiter' => ';', |
162
|
|
|
'islist' => true, |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
$params['circles'] = [ |
166
|
|
|
'type' => 'mapscircle', |
167
|
|
|
'default' => [], |
168
|
|
|
'delimiter' => ';', |
169
|
|
|
'islist' => true, |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
$params['rectangles'] = [ |
173
|
|
|
'type' => 'mapsrectangle', |
174
|
|
|
'default' => [], |
175
|
|
|
'delimiter' => ';', |
176
|
|
|
'islist' => true, |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
$params['wmsoverlay'] = [ |
180
|
|
|
'type' => 'wmsoverlay', |
181
|
|
|
'default' => false, |
182
|
|
|
'delimiter' => ' ', |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
$params['maxzoom'] = [ |
186
|
|
|
'type' => 'integer', |
187
|
|
|
'default' => false, |
188
|
|
|
'manipulatedefault' => false, |
189
|
|
|
'dependencies' => 'minzoom', |
190
|
|
|
]; |
191
|
|
|
|
192
|
|
|
$params['minzoom'] = [ |
193
|
|
|
'type' => 'integer', |
194
|
|
|
'default' => false, |
195
|
|
|
'manipulatedefault' => false, |
196
|
|
|
'lowerbound' => 0, |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
$params['copycoords'] = [ |
200
|
|
|
'type' => 'boolean', |
201
|
|
|
'default' => false, |
202
|
|
|
]; |
203
|
|
|
|
204
|
|
|
$params['static'] = [ |
205
|
|
|
'type' => 'boolean', |
206
|
|
|
'default' => false, |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
// Give grep a chance to find the usages: |
210
|
|
|
// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon, |
211
|
|
|
// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons, |
212
|
|
|
// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay, |
213
|
|
|
// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords, |
214
|
|
|
// maps-displaymap-par-static |
215
|
|
|
foreach ( $params as $name => &$param ) { |
216
|
|
|
if ( !array_key_exists( 'message', $param ) ) { |
217
|
|
|
$param['message'] = 'maps-displaymap-par-' . $name; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $params; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Resolves the url of images provided as wiki page; leaves others alone. |
226
|
|
|
* |
227
|
|
|
* @since 1.0 |
228
|
|
|
* @deprecated |
229
|
|
|
* |
230
|
|
|
* @param string $file |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public static function getFileUrl( $file ) { |
235
|
|
|
$title = Title::makeTitle( NS_FILE, $file ); |
236
|
|
|
|
237
|
|
|
if ( $title !== null && $title->exists() ) { |
238
|
|
|
$imagePage = new ImagePage( $title ); |
239
|
|
|
return $imagePage->getDisplayedFile()->getURL(); |
240
|
|
|
} |
241
|
|
|
return $file; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns JS to init the vars to hold the map data when they are not there already. |
246
|
|
|
* |
247
|
|
|
* @since 1.0 |
248
|
|
|
* @deprecated |
249
|
|
|
* |
250
|
|
|
* @param string $serviceName |
251
|
|
|
* |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
|
|
public static function getBaseMapJSON( $serviceName ) { |
255
|
|
|
static $baseInit = false; |
256
|
|
|
static $serviceInit = []; |
257
|
|
|
|
258
|
|
|
$json = ''; |
259
|
|
|
|
260
|
|
|
if ( !$baseInit ) { |
261
|
|
|
$baseInit = true; |
262
|
|
|
$json .= 'var mwmaps={};'; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if ( !in_array( $serviceName, $serviceInit ) ) { |
266
|
|
|
$serviceInit[] = $serviceName; |
267
|
|
|
$json .= "mwmaps.$serviceName={};"; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $json; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
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.