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