Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 104 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
0.00% |
0 / 104 |
|
0.00% |
0 / 4 |
1260 | |
0.00% |
0 / 1 |
onParserFirstCallInit | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
wfdeg2dd | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
56 | |||
wfdd2dms | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
90 | |||
wfgeoLink | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
342 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MapSources; |
4 | |
5 | use MediaWiki\Hook\ParserFirstCallInitHook; |
6 | use MediaWiki\Parser\Parser; |
7 | |
8 | class Hooks implements ParserFirstCallInitHook { |
9 | /** @inheritDoc */ |
10 | public function onParserFirstCallInit( $parser ) { |
11 | $parser->setFunctionHook( 'dd2dms', [ self::class, 'wfdd2dms' ] ); |
12 | $parser->setFunctionHook( 'deg2dd', [ self::class, 'wfdeg2dd' ] ); |
13 | $parser->setFunctionHook( 'geoLink', [ self::class, 'wfgeoLink' ] ); |
14 | } |
15 | |
16 | /** |
17 | * @param Parser $parser |
18 | * @param string ...$args |
19 | * @return string |
20 | */ |
21 | public static function wfdeg2dd( Parser $parser, ...$args ) { |
22 | $precision = -1; |
23 | if ( count( $args ) < 1 ) { |
24 | return wfMessage( 'mapsources-math-missing-operand' )->inContentLanguage()->text(); |
25 | } |
26 | |
27 | $coord = array_shift( $args ); |
28 | foreach ( $args as $arg ) { |
29 | $parts = array_map( 'trim', explode( '=', $arg, 2 ) ); |
30 | if ( count( $parts ) == 2 ) { |
31 | switch ( $parts[0] ) { |
32 | case 'precision': |
33 | $precision = $parts[1]; |
34 | |
35 | if ( !is_numeric( $precision ) ) { |
36 | $precision = -1; |
37 | } |
38 | break; |
39 | } |
40 | } |
41 | } |
42 | |
43 | $geo = new MapSourcesMath( $coord, $precision ); |
44 | if ( $geo->error < 0 ) { |
45 | return wfMessage( 'mapsources-math-incorrect-input' )->inContentLanguage()->text(); |
46 | } |
47 | return $geo->coord['dec']; |
48 | } |
49 | |
50 | /** |
51 | * @param Parser $parser |
52 | * @param string ...$args |
53 | * @return string |
54 | */ |
55 | public static function wfdd2dms( Parser $parser, ...$args ) { |
56 | $plus = ''; |
57 | $minus = ''; |
58 | $precision = 4; |
59 | if ( count( $args ) < 1 ) { |
60 | return wfMessage( 'mapsources-math-missing-operand' )->inContentLanguage()->text(); |
61 | } |
62 | |
63 | $coord = array_shift( $args ); |
64 | foreach ( $args as $arg ) { |
65 | $parts = array_map( 'trim', explode( '=', $arg, 2 ) ); |
66 | if ( count( $parts ) == 2 ) { |
67 | switch ( $parts[0] ) { |
68 | case 'plus': |
69 | $plus = $parts[1]; |
70 | break; |
71 | case 'minus': |
72 | $minus = $parts[1]; |
73 | break; |
74 | case 'precision': |
75 | $precision = $parts[1]; |
76 | if ( !is_numeric( $precision ) ) { |
77 | $precision = 4; |
78 | } |
79 | break; |
80 | } |
81 | } |
82 | } |
83 | |
84 | $geo = new MapSourcesMath( $coord, $precision ); |
85 | if ( $geo->error < 0 ) { |
86 | return wfMessage( 'mapsources-math-incorrect-input' )->inContentLanguage()->text(); |
87 | } |
88 | return $geo->getDMSString( $plus, $minus ); |
89 | } |
90 | |
91 | /** |
92 | * @param Parser $parser |
93 | * @param string ...$args |
94 | * @return string |
95 | */ |
96 | public static function wfgeoLink( Parser $parser, ...$args ) { |
97 | $plusLat = ''; |
98 | $minusLat = ''; |
99 | $plusLong = ''; |
100 | $minusLong = ''; |
101 | $lat = 0; |
102 | $long = 0; |
103 | $precision = 4; |
104 | if ( count( $args ) < 1 ) { |
105 | return wfMessage( 'mapsources-math-missing-operand' )->inContentLanguage()->text(); |
106 | } |
107 | |
108 | $pattern = array_shift( $args ); |
109 | foreach ( $args as $arg ) { |
110 | $parts = array_map( 'trim', explode( '=', $arg, 2 ) ); |
111 | if ( count( $parts ) == 2 ) { |
112 | switch ( $parts[0] ) { |
113 | case 'lat': |
114 | $lat = $parts[1]; |
115 | break; |
116 | case 'long': |
117 | $long = $parts[1]; |
118 | break; |
119 | case 'plusLat': |
120 | $plusLat = $parts[1]; |
121 | break; |
122 | case 'minusLat': |
123 | $minusLat = $parts[1]; |
124 | break; |
125 | case 'plusLong': |
126 | $plusLong = $parts[1]; |
127 | break; |
128 | case 'minusLong': |
129 | $minusLong = $parts[1]; |
130 | break; |
131 | case 'precision': |
132 | $precision = $parts[1]; |
133 | |
134 | if ( !is_numeric( $precision ) ) { |
135 | $precision = 4; |
136 | } |
137 | break; |
138 | } |
139 | } |
140 | } |
141 | |
142 | $args = []; |
143 | $lat = new MapSourcesMath( $lat, $precision, 'lat' ); |
144 | $long = new MapSourcesMath( $long, $precision, 'long' ); |
145 | |
146 | if ( $lat->error == 0 ) { |
147 | $args[] = abs( $lat->dec ) . '_' . $lat->coord['NS']; |
148 | } else { |
149 | $args[] = '0_N'; |
150 | } |
151 | |
152 | if ( $long->error == 0 ) { |
153 | $args[] = abs( $long->dec ) . '_' . $long->coord['EW']; |
154 | } else { |
155 | $args[] = '0_E'; |
156 | } |
157 | |
158 | if ( $lat->error == 0 ) { |
159 | $args[] = $lat->getDMSString( $plusLat, $minusLat ); |
160 | } else { |
161 | $args[] = wfMessage( 'mapsources-math-incorrect-input' )->inContentLanguage()->text(); |
162 | } |
163 | |
164 | if ( $long->error == 0 ) { |
165 | $args[] = $long->getDMSString( $plusLong, $minusLong ); |
166 | } else { |
167 | $args[] = wfMessage( 'mapsources-math-incorrect-input' )->inContentLanguage()->text(); |
168 | } |
169 | |
170 | if ( $lat->error == 0 ) { |
171 | $args[] = $lat->dec; |
172 | } else { |
173 | $args[] = '0'; |
174 | } |
175 | |
176 | if ( $long->error == 0 ) { |
177 | $args[] = $long->dec; |
178 | } else { |
179 | $args[] = '0'; |
180 | } |
181 | |
182 | return wfMsgReplaceArgs( $pattern, $args ); |
183 | } |
184 | } |