Completed
Pull Request — master (#131)
by None
09:08 queued 02:21
created

MapsHooks::onParserFirstCallInit5()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
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
 * @author Daniel Werner
11
 */
12
final class MapsHooks {
13
	/**
14
	 * Helper flag indicating whether the page has been purged.
15
	 * @var bool
16
	 *
17
	 * TODO: Figure out a better way to do this, not requiring this flag and make sure it works with
18
	 *       later MW versions (purging mechanism got changed somewhat around 1.18).
19
	 */
20
	static $purgedBeforeStore = false;
21
22
	public static function onExtensionCallback() {
23
		global $egMapsNamespaceIndex, $egMapsStyleVersion, $wgStyleVersion;
24
25
		if ( defined( 'Maps_VERSION' ) ) {
26
			// Do not initialize more than once.
27
			return 1;
28
		}
29
30
		define( 'Maps_VERSION' , '3.6.0-alpha' );
31
32
		// Only initialize the extension when all dependencies are present.
33
		if ( !defined( 'Validator_VERSION' ) ) {
34
			throw new Exception( 'You need to have Validator installed in order to use Maps' );
35
		}
36
37
		$egMapsStyleVersion = $wgStyleVersion . '-' . Maps_VERSION;
38
39
		// The different coordinate notations.
40
		define( 'Maps_COORDS_FLOAT' , 'float' );
41
		define( 'Maps_COORDS_DMS' , 'dms' );
42
		define( 'Maps_COORDS_DM' , 'dm' );
43
		define( 'Maps_COORDS_DD' , 'dd' );
44
45
		require_once __DIR__ . '/Maps_Settings.php';
46
47
		define( 'Maps_NS_LAYER' , $egMapsNamespaceIndex + 0 );
48
		define( 'Maps_NS_LAYER_TALK' , $egMapsNamespaceIndex + 1 );
49
	}
50
51
	public static function onExtensionFunction() {
0 ignored issues
show
Coding Style introduced by
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...
52
		if ( $GLOBALS['egMapsGMaps3Language'] === '' ) {
53
			$GLOBALS['egMapsGMaps3Language'] = $GLOBALS['wgLang'];
54
		}
55
56
		Hooks::run( 'MappingServiceLoad' );
57
		Hooks::run( 'MappingFeatureLoad' );
58
59
		if ( in_array( 'googlemaps3', $GLOBALS['egMapsAvailableServices'] ) ) {
60
			$GLOBALS['wgSpecialPages']['MapEditor'] = 'SpecialMapEditor';
61
			$GLOBALS['wgSpecialPageGroups']['MapEditor'] = 'maps';
62
		}
63
64
		return true;
65
	}
66
67
	public static function onParserFirstCallInit1( Parser &$parser ) {
68
		$instance = new MapsCoordinates();
69
		return $instance->init( $parser );
70
	}
71
72
	public static function onParserFirstCallInit2( Parser &$parser ) {
73
		$instance = new MapsDisplayMap();
74
		return $instance->init( $parser );
75
	}
76
77
	public static function onParserFirstCallInit3( Parser &$parser ) {
78
		$instance = new MapsDistance();
79
		return $instance->init( $parser );
80
	}
81
82
	public static function onParserFirstCallInit4( Parser &$parser ) {
83
		$instance = new MapsFinddestination();
84
		return $instance->init( $parser );
85
	}
86
87
	public static function onParserFirstCallInit5( Parser &$parser ) {
88
		$instance = new MapsGeocode();
89
		return $instance->init( $parser );
90
	}
91
92
	public static function onParserFirstCallInit6( Parser &$parser ) {
93
		$instance = new MapsGeodistance();
94
		return $instance->init( $parser );
95
	}
96
97
	public static function onParserFirstCallInit7( Parser &$parser ) {
98
		$instance = new MapsMapsDoc();
99
		return $instance->init( $parser );
100
	}
101
102
	public static function onParserFirstCallInit8( Parser &$parser ) {
103
		$instance = new MapsLayerDefinition();
104
		return $instance->init( $parser );
105
	}
106
107
	/**
108
	 * Initialization function for the Google Maps v3 service. 
109
	 * 
110
	 * @since 0.6.3
111
	 * @ingroup MapsGoogleMaps3
112
	 * 
113
	 * @return boolean true
114
	 */
115
	public static function efMapsInitGoogleMaps3() {
116
		global $wgAutoloadClasses;
117
118
		$wgAutoloadClasses['MapsGoogleMaps3'] = __DIR__ . '/includes/services/GoogleMaps3/Maps_GoogleMaps3.php';
119
120
		MapsMappingServices::registerService( 'googlemaps3', 'MapsGoogleMaps3' );
121
122
		// TODO: kill below code
123
		$googleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
124
		$googleMaps->addFeature( 'display_map', 'MapsDisplayMapRenderer' );
125
126
		return true;
127
	}
128
129
	public static function efMapsInitOpenLayers() {
130
		MapsMappingServices::registerService( 
131
			'openlayers',
132
			'MapsOpenLayers',
133
			array( 'display_map' => 'MapsDisplayMapRenderer' )
134
		);
135
		
136
		return true;
137
	}
138
139
	/**
140
	 * Initialization function for the Leaflet service.
141
	 *
142
	 * @ingroup Leaflet
143
	 *
144
	 * @return boolean true
145
	 */
146
	public static function efMapsInitLeaflet() {
147
		global $wgAutoloadClasses;
148
149
		$wgAutoloadClasses['MapsLeaflet'] = __DIR__ . '/Maps_Leaflet.php';
150
151
		MapsMappingServices::registerService( 'leaflet', 'MapsLeaflet' );
152
		$leafletMaps = MapsMappingServices::getServiceInstance( 'leaflet' );
153
		$leafletMaps->addFeature( 'display_map', 'MapsDisplayMapRenderer' );
154
155
		return true;
156
	}
157
158
	/**
159
	 * Adds a link to Admin Links page.
160
	 *
161
	 * @since 0.7
162
	 *
163
	 * @param ALTree $admin_links_tree
164
	 *
165
	 * @return boolean
166
	 */
167
	public static function addToAdminLinks( ALTree &$admin_links_tree ) {
168
		$displaying_data_section = $admin_links_tree->getSection( wfMessage( 'smw_adminlinks_displayingdata' )->text() );
169
170
		// Escape if SMW hasn't added links.
171
		if ( is_null( $displaying_data_section ) ) {
172
			return true;
173
		}
174
175
		$smw_docu_row = $displaying_data_section->getRow( 'smw' );
176
177
		$maps_docu_label = wfMessage( 'adminlinks_documentation', 'Maps' )->text();
178
		$smw_docu_row->addItem( AlItem::newFromExternalLink( 'https://semantic-mediawiki.org/wiki/Maps', $maps_docu_label ) );
179
180
		return true;
181
	}
182
183
	/**
184
	 * Intercept pages in the Layer namespace to handle them correctly.
185
	 *
186
	 * @param $title: Title
187
	 * @param $article: Article or null
188
	 *
189
	 * @return boolean
190
	 */
191
	public static function onArticleFromTitle( Title &$title, /* Article */ &$article ) {
192
		if ( $title->getNamespace() == Maps_NS_LAYER ) {
193
			$article = new MapsLayerPage( $title );
194
		}
195
196
		return true;
197
	}
198
199
	/**
200
	 * Adds global JavaScript variables.
201
	 *
202
	 * @since 1.0
203
         * @see http://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
204
         * @param array &$vars Variables to be added into the output
205
         * @param OutputPage $outputPage OutputPage instance calling the hook
206
         * @return boolean true in all cases
207
	 */
208
	public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $outputPage ) {
0 ignored issues
show
Unused Code introduced by
The parameter $outputPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
onMakeGlobalVariablesScript 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...
209
		global $egMapsGlobalJSVars;
210
211
		$vars['egMapsDebugJS'] = $GLOBALS['egMapsDebugJS'];
212
                $vars[ 'egMapsAvailableServices' ] = $GLOBALS['egMapsAvailableServices'];
213
214
		$vars += $egMapsGlobalJSVars;
215
216
		return true;
217
	}
218
219
	/**
220
	 * @since 0.7
221
	 *
222
	 * @param array $list
223
	 *
224
	 * @return boolean
225
	 */
226
	public static function onCanonicalNamespaces( array &$list ) {
227
		$list[Maps_NS_LAYER] = 'Layer';
228
		$list[Maps_NS_LAYER_TALK] = 'Layer_talk';
229
		return true;
230
	}
231
232
	/**
233
	 * This will setup database tables for layer functionality.
234
	 *
235
	 * @since 3.0
236
	 *
237
	 * @param DatabaseUpdater $updater
238
	 *
239
	 * @return true
240
	 */
241
	public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
0 ignored issues
show
Coding Style introduced by
onLoadExtensionSchemaUpdates 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...
242
		switch( $GLOBALS['wgDBtype'] ) {
243
			case 'mysql':
244
			case 'sqlite':
245
				$updater->addExtensionTable( 'maps_layers', __DIR__ . '/schema/MapsLayers.sql' );
246
				break;
247
			case 'postgres':
248
				$updater->addExtensionTable( 'maps_layers', __DIR__ . '/schema/MapsLayers-postgres.sql' );
249
				break;
250
		}
251
252
		return true;
253
	}
254
255
	/**
256
	 * Make sure layer data will be stored into database when purging the page
257
	 *
258
	 * @since 3.0
259
	 *
260
	 * @param $article WikiPage|Article (depending on MW version, WikiPage in 1.18+)
261
	 * @return type
262
	 */
263
	public static function onArticlePurge( &$article ) {
0 ignored issues
show
Unused Code introduced by
The parameter $article is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
264
		self::$purgedBeforeStore = true;
265
		return true;
266
	}
267
268
	/**
269
	 * At the end of article parsing, in case of layer page, save layers to database
270
	 *
271
	 * @since 3.0
272
	 *
273
	 * @param Parser &$parser
274
	 * @param string &$text
275
	 *
276
	 * @return true
277
	 */
278
	public static function onParserAfterTidy( Parser &$parser, &$text ) {
0 ignored issues
show
Unused Code introduced by
The parameter $text is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
279
280
		$title = $parser->getTitle();
281
282
		if( $title === null
283
			|| self::$purgedBeforeStore !== true
284
		) {
285
			// just preprocessing some stuff or no purge
286
			return true;
287
		}
288
289
		self::processLayersStoreCandidate( $parser->getOutput(), $title );
290
291
		// Set helper to false immediately so we won't run into job-processing weirdness:
292
		self::$purgedBeforeStore = false;
293
294
		return true;
295
	}
296
297
	/**
298
	 * After article was edited and parsed, in case of layer page, save layers to database
299
	 *
300
	 * @since 3.0
301
	 *
302
	 * @param LinksUpdate &$linksUpdate
303
	 *
304
	 * @return true
305
	 */
306
	public static function onLinksUpdateConstructed( LinksUpdate &$linksUpdate ) {
307
		$title = $linksUpdate->getTitle();
308
309
		self::processLayersStoreCandidate( $linksUpdate->mParserOutput, $title );
310
311
		return true;
312
	}
313
314
	/**
315
	 * Checks whether the parser output has some layer data which should be stored of the
316
	 * given title and performs the task.
317
	 *
318
	 * @since 3.0
319
	 *
320
	 * @param ParserOutput $parserOutput
321
	 * @param Title $title
322
	 */
323
	protected static function processLayersStoreCandidate( ParserOutput $parserOutput, Title $title ) {
324
325
		// if site which is being parsed is in maps namespace:
326
		if( $title->getNamespace() === Maps_NS_LAYER ) {
327
328
			if( ! isset( $parserOutput->mExtMapsLayers ) ) {
329
				$parserOutput->mExtMapsLayers = new MapsLayerGroup();
330
			}
331
332
			// get MapsLayerGroup object with layers to be stored:
333
			$mapsForStore = $parserOutput->mExtMapsLayers;
334
335
			// store layers in database (also deletes previous definitions still in db):
336
			MapsLayers::storeLayers( $mapsForStore, $title );
337
		}
338
	}
339
340
	/**
341
	 * If a new parser process is getting started, clear collected layer data of the
342
	 * previous one.
343
	 *
344
	 * @since 3.0
345
	 *
346
	 * @param Parser $parser
347
	 *
348
	 * @return true
349
	 */
350
	public static function onParserClearState( Parser &$parser ) {
351
		$parser->getOutput()->mExtMapsLayers = null;
352
		return true;
353
	}
354
}
355
356