MapsHooks::onShowMissingArticle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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