Completed
Pull Request — master (#131)
by None
07:13
created

Maps.hooks.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Static class for hooks handled by the Maps extension.
5
 *
6
 * @since 0.7
7
 *
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 */
11
final class MapsHooks {
12
13
 	public static function onExtensionCallback() {
0 ignored issues
show
onExtensionCallback uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
14
		if ( defined( 'Maps_COORDS_FLOAT' ) ) {
15
			// Do not initialize more than once.
16
			return 1;
17
		}
18
19
		// The different coordinate notations.
20
		define( 'Maps_COORDS_FLOAT' , 'float' );
21
		define( 'Maps_COORDS_DMS' , 'dms' );
22
		define( 'Maps_COORDS_DM' , 'dm' );
23
		define( 'Maps_COORDS_DD' , 'dd' );
24
25
		require_once __DIR__ . '/Maps_Settings.php';
26
27
		define( 'Maps_VERSION' , '4.2.1' );
28
		define( 'SM_VERSION', Maps_VERSION );
29
30
		// Only initialize the extension when all dependencies are present.
31
		if ( !defined( 'Validator_VERSION' ) ) {
32
			throw new Exception( 'You need to have Validator installed in order to use Maps' );
33
		}
34
35
		$GLOBALS['egMapsStyleVersion'] = $GLOBALS['wgStyleVersion'] . '-' . Maps_VERSION;
36
	}
37
38
	public static function onExtensionFunction() {
0 ignored issues
show
onExtensionFunction uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
39
		if ( $GLOBALS['egMapsDisableExtension'] ) {
40
			return true;
41
		}
42
		if ( defined( 'Maps_VERSION' ) ) {
43
			// Do not initialize more than once.
44
			return true;
45
		}
46
47
		// Only initialize the extension when all dependencies are present.
48
		if ( !defined( 'Validator_VERSION' ) ) {
49
			throw new Exception( 'You need to have Validator installed in order to use Maps' );
50
		}
51
52
		define( 'Maps_VERSION' , '4.2.1' );
53
		define( 'SM_VERSION', Maps_VERSION );
54
55
		if ( $GLOBALS['egMapsGMaps3Language'] === '' ) {
56
			$GLOBALS['egMapsGMaps3Language'] = $GLOBALS['wgLang'];
57
		}
58
59
		MapsMappingServices::registerService( 'googlemaps3', MapsGoogleMaps3::class );
60
61
		$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
62
		$googleMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
63
64
		MapsMappingServices::registerService(
65
			'openlayers',
66
			MapsOpenLayers::class,
67
			[ 'display_map' => MapsDisplayMapRenderer::class ]
68
		);
69
70
		MapsMappingServices::registerService( 'leaflet', MapsLeaflet::class );
71
		$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
72
		$leafletMaps->addFeature( 'display_map', MapsDisplayMapRenderer::class );
73
74
		if ( in_array( 'googlemaps3', $GLOBALS['egMapsAvailableServices'] ) ) {
75
			$GLOBALS['wgSpecialPages']['MapEditor'] = 'SpecialMapEditor';
76
			$GLOBALS['wgSpecialPageGroups']['MapEditor'] = 'maps';
77
		}
78
79
		$GLOBALS['egMapsStyleVersion'] = $GLOBALS['wgStyleVersion'] . '-' . Maps_VERSION;
80
81
		// Users that can geocode. By default the same as those that can edit.
82
		foreach ( $GLOBALS['wgGroupPermissions'] as $group => $rights ) {
83
			if ( array_key_exists( 'edit' , $rights ) ) {
84
				$GLOBALS['wgGroupPermissions'][$group]['geocode'] = $GLOBALS['wgGroupPermissions'][$group]['edit'];
85
			}
86
		}
87
88
		$GLOBALS['wgParamDefinitions']['coordinate'] = [
89
			'string-parser' => GeoCoordinateParser::class,
90
		];
91
92
		$GLOBALS['wgParamDefinitions']['mappingservice'] = [
93
			'definition'=> ServiceParam::class,
94
		];
95
96
		$GLOBALS['wgParamDefinitions']['mapslocation'] = [
97
			'string-parser' => LocationParser::class,
98
		];
99
100
		$GLOBALS['wgParamDefinitions']['mapsline'] = [
101
			'string-parser' => LineParser::class,
102
		];
103
104
		$GLOBALS['wgParamDefinitions']['mapscircle'] = [
105
			'string-parser' => CircleParser::class,
106
		];
107
108
		$GLOBALS['wgParamDefinitions']['mapsrectangle'] = [
109
			'string-parser' => RectangleParser::class,
110
		];
111
112
		$GLOBALS['wgParamDefinitions']['mapspolygon'] = [
113
			'string-parser' => PolygonParser::class,
114
		];
115
116
		$GLOBALS['wgParamDefinitions']['distance'] = [
117
			'string-parser' => DistanceParser::class,
118
		];
119
120
		$GLOBALS['wgParamDefinitions']['wmsoverlay'] = [
121
			'string-parser' => WmsOverlayParser::class,
122
		];
123
124
		$GLOBALS['wgParamDefinitions']['mapsimageoverlay'] = [
125
			'string-parser' => ImageOverlayParser::class,
126
		];
127
128
		if ( !$GLOBALS['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ) ) {
129
			SemanticMaps::newFromMediaWikiGlobals( $GLOBALS )->initExtension();
130
		}
131
132
		return true;
133
	}
134
135
	public static function onParserFirstCallInit1( Parser &$parser ) {
136
		$instance = new MapsCoordinates();
137
		return $instance->init( $parser );
138
	}
139
140
	public static function onParserFirstCallInit2( Parser &$parser ) {
141
		$instance = new MapsDisplayMap();
142
		return $instance->init( $parser );
143
	}
144
145
	public static function onParserFirstCallInit3( Parser &$parser ) {
146
		$instance = new MapsDistance();
147
		return $instance->init( $parser );
148
	}
149
150
	public static function onParserFirstCallInit4( Parser &$parser ) {
151
		$instance = new MapsFinddestination();
152
		return $instance->init( $parser );
153
	}
154
155
	public static function onParserFirstCallInit5( Parser &$parser ) {
156
		$instance = new MapsGeocode();
157
		return $instance->init( $parser );
158
	}
159
160
	public static function onParserFirstCallInit6( Parser &$parser ) {
161
		$instance = new MapsGeodistance();
162
		return $instance->init( $parser );
163
	}
164
165
	public static function onParserFirstCallInit7( Parser &$parser ) {
166
		$instance = new MapsMapsDoc();
167
		return $instance->init( $parser );
168
	}
169
170
	/**
171
	 * Adds a link to Admin Links page.
172
	 *
173
	 * @since 0.7
174
	 *
175
	 * @param ALTree $admin_links_tree
176
	 *
177
	 * @return boolean
178
	 */
179
	public static function addToAdminLinks( ALTree &$admin_links_tree ) {
180
		$displaying_data_section = $admin_links_tree->getSection( wfMessage( 'smw_adminlinks_displayingdata' )->text() );
181
182
		// Escape if SMW hasn't added links.
183
		if ( is_null( $displaying_data_section ) ) {
184
			return true;
185
		}
186
187
		$smw_docu_row = $displaying_data_section->getRow( 'smw' );
188
189
		$maps_docu_label = wfMessage( 'adminlinks_documentation', 'Maps' )->text();
190
		$smw_docu_row->addItem( AlItem::newFromExternalLink( 'https://semantic-mediawiki.org/wiki/Maps', $maps_docu_label ) );
191
192
		return true;
193
	}
194
195
	/**
196
	 * Adds global JavaScript variables.
197
	 *
198
	 * @since 1.0
199
	 * @see http://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
200
	 * @param array &$vars Variables to be added into the output
201
	 * @param OutputPage $outputPage OutputPage instance calling the hook
202
	 * @return boolean true in all cases
203
	 */
204
	public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $outputPage ) {
205
		global $egMapsGlobalJSVars;
206
207
		$vars['egMapsDebugJS'] = $GLOBALS['egMapsDebugJS'];
208
                $vars[ 'egMapsAvailableServices' ] = $GLOBALS['egMapsAvailableServices'];
209
210
		$vars += $egMapsGlobalJSVars;
211
212
		return true;
213
	}
214
215
}
216
217