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