Completed
Push — master ( 81538e...c2bf57 )
by Jeroen De
10:06
created

MapsHooks::addGeoCoordsDefaultFormat()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.0995
c 0
b 0
f 0
cc 8
nc 6
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use AlItem;
8
use ALTree;
9
use Maps\GeoJsonPages\GeoJsonNewPageUi;
10
use Maps\Presentation\OutputFacade;
11
use SkinTemplate;
12
use SMWPrintRequest;
13
14
/**
15
 * Static class for hooks handled by the Maps extension.
16
 *
17
 * @since 0.7
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
final class MapsHooks {
23
24
	/**
25
	 * Adds a link to Admin Links page.
26
	 *
27
	 * @since 0.7
28
	 *
29
	 * @param ALTree $admin_links_tree
30
	 *
31
	 * @return boolean
32
	 */
33
	public static function addToAdminLinks( ALTree &$admin_links_tree ) {
34
		$displaying_data_section = $admin_links_tree->getSection(
35
			wfMessage( 'smw_adminlinks_displayingdata' )->text()
36
		);
37
38
		// Escape if SMW hasn't added links.
39
		if ( is_null( $displaying_data_section ) ) {
40
			return true;
41
		}
42
43
		$smw_docu_row = $displaying_data_section->getRow( 'smw' );
44
45
		$maps_docu_label = wfMessage( 'adminlinks_documentation', 'Maps' )->text();
46
		$smw_docu_row->addItem(
47
			AlItem::newFromExternalLink( 'https://www.semantic-mediawiki.org/wiki/Extension:Maps', $maps_docu_label )
48
		);
49
50
		return true;
51
	}
52
53
	/**
54
	 * Adds global JavaScript variables.
55
	 *
56
	 * @since 1.0
57
	 * @see http://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
58
	 *
59
	 * @param array &$vars Variables to be added into the output
60
	 *
61
	 * @return boolean true in all cases
62
	 */
63
	public static function onMakeGlobalVariablesScript( array &$vars ) {
64
		$vars['egMapsScriptPath'] = $GLOBALS['wgScriptPath'] . '/extensions/Maps/'; // TODO: wgExtensionDirectory?
65
		$vars['egMapsDebugJS'] = $GLOBALS['egMapsDebugJS'];
66
		$vars['egMapsAvailableServices'] = $GLOBALS['egMapsAvailableServices'];
67
		$vars['egMapsLeafletLayersApiKeys'] = $GLOBALS['egMapsLeafletLayersApiKeys'];
68
69
		$vars += $GLOBALS['egMapsGlobalJSVars'];
70
71
		return true;
72
	}
73
74
	public static function onSkinTemplateNavigation( SkinTemplate $skinTemplate, array &$links ) {
75
		if ( $skinTemplate->getTitle() === null ) {
76
			return true;
77
		}
78
79
		if ( $skinTemplate->getTitle()->getNamespace() === NS_GEO_JSON ) {
80
			if ( array_key_exists( 'edit', $links['views'] ) ) {
81
				$links['views']['edit']['text'] = wfMessage(
82
					$skinTemplate->getTitle()->exists() ? 'maps-geo-json-edit-source': 'maps-geo-json-create-source'
83
				);
84
			}
85
		}
86
87
		return true;
88
	}
89
90
	public static function onBeforeDisplayNoArticleText( \Article $article ) {
91
		return !self::shouldShowGeoJsonCreatePageUi( $article );
92
	}
93
94
	public static function onShowMissingArticle( \Article $article ) {
95
		if ( self::shouldShowGeoJsonCreatePageUi( $article ) ) {
96
			$ui = new GeoJsonNewPageUi( OutputFacade::newFromOutputPage( $article->getContext()->getOutput() ) );
97
			$ui->addToOutput();
98
		}
99
100
		return true;
101
	}
102
103
	private static function shouldShowGeoJsonCreatePageUi( \Article $article ): bool {
104
		return $article->getTitle()->getNamespace() === NS_GEO_JSON
105
			&& $article->getContext()->getUser()->isAllowed( 'createpage' );
106
	}
107
108
	public static function onRegisterTags( array &$tags ) {
109
		$tags[] = 'maps-visual-edit';
110
		return true;
111
	}
112
113
	public static function onChangeTagsAllowedAdd( array &$allowedTags, array $tags, \User $user = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $tags 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...
Unused Code introduced by
The parameter $user 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...
114
		$allowedTags[] = 'maps-visual-edit';
115
	}
116
117
	public static function onResourceLoaderTestModules( array &$modules, $resourceLoader ) {
0 ignored issues
show
Unused Code introduced by
The parameter $resourceLoader 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...
118
		$modules['qunit']['ext.maps.test'] = [
119
			'scripts' => [
120
				'tests/js/leaflet/GeoJsonTest.js',
121
			],
122
			'dependencies' => [
123
				'ext.maps.leaflet.geojson',
124
			],
125
			'localBasePath' => __DIR__ . '/../../',
126
			'remoteExtPath' => 'Maps'
127
		];
128
	}
129
130
	/**
131
	 * Set the default format to 'map' when the requested properties are
132
	 * of type geographic coordinates.
133
	 *
134
	 * TODO: have a setting to turn this off and have it off by default for #show
135
	 *
136
	 * @since 1.0
137
	 *
138
	 * @param $format Mixed: The format (string), or false when not set yet
139
	 * @param SMWPrintRequest[] $printRequests The print requests made
140
	 *
141
	 * @return boolean
142
	 */
143
	public static function addGeoCoordsDefaultFormat( &$format, array $printRequests ) {
144
		// Only set the format when not set yet. This allows other extensions to override the Maps behavior.
145
		if ( $format === false ) {
146
			// Only apply when there is more then one print request.
147
			// This way requests comming from #show are ignored.
148
			if ( count( $printRequests ) > 1 ) {
149
				$allValid = true;
150
				$hasCoords = false;
151
152
				// Loop through the print requests to determine their types.
153
				foreach ( $printRequests as $printRequest ) {
154
					// Skip the first request, as it's the object.
155
					if ( $printRequest->getMode() == SMWPrintRequest::PRINT_THIS ) {
156
						continue;
157
					}
158
159
					$typeId = $printRequest->getTypeID();
160
161
					if ( $typeId == '_geo' ) {
162
						$hasCoords = true;
163
					} else {
164
						$allValid = false;
165
						break;
166
					}
167
				}
168
169
				// If they are all coordinates, set the result format to 'map'.
170
				if ( $allValid && $hasCoords ) {
171
					$format = 'map';
172
				}
173
			}
174
175
		}
176
177
		return true;
178
	}
179
180
	public static function addSmwSettings( array &$settings ) {
0 ignored issues
show
Unused Code introduced by
The parameter $settings 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...
181
		// TODO: uncomment when it is safe for the semantic integration to be enabled by default
182
		// $settings['smwgNamespacesWithSemanticLinks'][NS_GEO_JSON] = true;
183
		return true;
184
	}
185
186
}
187