Completed
Push — master ( d1fdab...eafdea )
by Jeroen De
04:02
created

MapsHooks   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 0
loc 96
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addToAdminLinks() 0 19 2
A onMakeGlobalVariablesScript() 0 10 1
A onSkinTemplateNavigation() 0 15 5
A onBeforeDisplayNoArticleText() 0 3 1
A onShowMissingArticle() 0 8 2
A shouldShowGeoJsonCreatePageUi() 0 4 2
A onRegisterTags() 0 4 1
A onChangeTagsAllowedAdd() 0 3 1
1
<?php
2
3
namespace Maps\MediaWiki;
4
5
use AlItem;
6
use ALTree;
7
use Maps\MediaWiki\Content\GeoJsonContent;
8
use Maps\Presentation\GeoJsonMapPageUi;
9
use Maps\Presentation\GeoJsonNewPageUi;
10
use Maps\Presentation\OutputFacade;
11
use SkinTemplate;
12
13
/**
14
 * Static class for hooks handled by the Maps extension.
15
 *
16
 * @since 0.7
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
final class MapsHooks {
22
23
	/**
24
	 * Adds a link to Admin Links page.
25
	 *
26
	 * @since 0.7
27
	 *
28
	 * @param ALTree $admin_links_tree
29
	 *
30
	 * @return boolean
31
	 */
32
	public static function addToAdminLinks( ALTree &$admin_links_tree ) {
33
		$displaying_data_section = $admin_links_tree->getSection(
34
			wfMessage( 'smw_adminlinks_displayingdata' )->text()
35
		);
36
37
		// Escape if SMW hasn't added links.
38
		if ( is_null( $displaying_data_section ) ) {
39
			return true;
40
		}
41
42
		$smw_docu_row = $displaying_data_section->getRow( 'smw' );
43
44
		$maps_docu_label = wfMessage( 'adminlinks_documentation', 'Maps' )->text();
45
		$smw_docu_row->addItem(
46
			AlItem::newFromExternalLink( 'https://www.semantic-mediawiki.org/wiki/Extension:Maps', $maps_docu_label )
47
		);
48
49
		return true;
50
	}
51
52
	/**
53
	 * Adds global JavaScript variables.
54
	 *
55
	 * @since 1.0
56
	 * @see http://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
57
	 *
58
	 * @param array &$vars Variables to be added into the output
59
	 *
60
	 * @return boolean true in all cases
61
	 */
62
	public static function onMakeGlobalVariablesScript( array &$vars ) {
63
		$vars['egMapsScriptPath'] = $GLOBALS['wgScriptPath'] . '/extensions/Maps/'; // TODO: wgExtensionDirectory?
64
		$vars['egMapsDebugJS'] = $GLOBALS['egMapsDebugJS'];
65
		$vars['egMapsAvailableServices'] = $GLOBALS['egMapsAvailableServices'];
66
		$vars['egMapsLeafletLayersApiKeys'] = $GLOBALS['egMapsLeafletLayersApiKeys'];
67
68
		$vars += $GLOBALS['egMapsGlobalJSVars'];
69
70
		return true;
71
	}
72
73
	public static function onSkinTemplateNavigation( SkinTemplate $skinTemplate, array &$links ) {
74
		if ( $skinTemplate->getTitle() === null ) {
75
			return true;
76
		}
77
78
		if ( $skinTemplate->getTitle()->getNamespace() === NS_GEO_JSON ) {
79
			if ( array_key_exists( 'edit', $links['views'] ) ) {
80
				$links['views']['edit']['text'] = wfMessage(
81
					$skinTemplate->getTitle()->exists() ? 'maps-geo-json-edit-source': 'maps-geo-json-create-source'
82
				);
83
			}
84
		}
85
86
		return true;
87
	}
88
89
	public static function onBeforeDisplayNoArticleText( \Article $article ) {
90
		return !self::shouldShowGeoJsonCreatePageUi( $article );
91
	}
92
93
	public static function onShowMissingArticle( \Article $article ) {
94
		if ( self::shouldShowGeoJsonCreatePageUi( $article ) ) {
95
			$ui = new GeoJsonNewPageUi( OutputFacade::newFromOutputPage( $article->getContext()->getOutput() ) );
96
			$ui->addToOutput();
97
		}
98
99
		return true;
100
	}
101
102
	private static function shouldShowGeoJsonCreatePageUi( \Article $article ): bool {
103
		return $article->getTitle()->getNamespace() === NS_GEO_JSON
104
			&& $article->getContext()->getUser()->isAllowed( 'createpage' );
105
	}
106
107
	public static function onRegisterTags( array &$tags ) {
108
		$tags[] = 'maps-visual-edit';
109
		return true;
110
	}
111
112
	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...
113
		$allowedTags[] = 'maps-visual-edit';
114
	}
115
116
}
117