Completed
Push — newparam ( 72433c...c5fad3 )
by Jeroen De
01:21
created

SemanticMapsHooks   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 18.75%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 99
ccs 6
cts 32
cp 0.1875
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addToAdminLinks() 0 19 2
A initGeoDataTypes() 0 9 1
B addGeoCoordsDefaultFormat() 0 36 8
1
<?php
2
3
namespace Maps\MediaWiki;
4
5
use AlItem;
6
use ALTree;
7
use Maps\SemanticMW\DataValues\CoordinateValue;
8
use Maps\SemanticMW\DataValues\GeoPolygonValue;
9
use SMW\DataTypeRegistry;
10
use SMWDataItem;
11
use SMWPrintRequest;
12
13
/**
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
final class SemanticMapsHooks {
18
19
	/**
20
	 * Adds a link to Admin Links page.
21
	 *
22
	 * @since 0.7
23
	 *
24
	 * @param ALTree $admin_links_tree
25
	 *
26
	 * @return boolean
27
	 */
28
	public static function addToAdminLinks( ALTree &$admin_links_tree ) {
29
		$displaying_data_section = $admin_links_tree->getSection(
30
			wfMessage( 'smw_adminlinks_displayingdata' )->text()
31
		);
32
33
		// Escape if SMW hasn't added links.
34
		if ( is_null( $displaying_data_section ) ) {
35
			return true;
36
		}
37
38
		$smw_docu_row = $displaying_data_section->getRow( 'smw' );
39
40
		$sm_docu_label = wfMessage( 'adminlinks_documentation', 'Semantic Maps' )->text();
41
		$smw_docu_row->addItem(
42
			AlItem::newFromExternalLink( 'https://www.semantic-mediawiki.org/wiki/Semantic_Maps', $sm_docu_label )
43
		);
44
45
		return true;
46
	}
47
48
	/**
49
	 * Adds support for the geographical coordinates and shapes data type to Semantic MediaWiki.
50
	 *
51
	 * @since 2.0
52
	 *
53
	 * @return boolean
54
	 */
55 1
	public static function initGeoDataTypes() {
56 1
		DataTypeRegistry::getInstance()->registerDatatype(
57 1
			'_geo',
58 1
			CoordinateValue::class,
59 1
			SMWDataItem::TYPE_GEO
60
		);
61
62 1
		return true;
63
	}
64
65
	/**
66
	 * Set the default format to 'map' when the requested properties are
67
	 * of type geographic coordinates.
68
	 *
69
	 * TODO: have a setting to turn this off and have it off by default for #show
70
	 *
71
	 * @since 1.0
72
	 *
73
	 * @param $format Mixed: The format (string), or false when not set yet
74
	 * @param SMWPrintRequest[] $printRequests The print requests made
75
	 *
76
	 * @return boolean
77
	 */
78
	public static function addGeoCoordsDefaultFormat( &$format, array $printRequests ) {
79
		// Only set the format when not set yet. This allows other extensions to override the Maps behavior.
80
		if ( $format === false ) {
81
			// Only apply when there is more then one print request.
82
			// This way requests comming from #show are ignored.
83
			if ( count( $printRequests ) > 1 ) {
84
				$allValid = true;
85
				$hasCoords = false;
86
87
				// Loop through the print requests to determine their types.
88
				foreach ( $printRequests as $printRequest ) {
89
					// Skip the first request, as it's the object.
90
					if ( $printRequest->getMode() == SMWPrintRequest::PRINT_THIS ) {
91
						continue;
92
					}
93
94
					$typeId = $printRequest->getTypeID();
95
96
					if ( $typeId == '_geo' ) {
97
						$hasCoords = true;
98
					} else {
99
						$allValid = false;
100
						break;
101
					}
102
				}
103
104
				// If they are all coordinates, set the result format to 'map'.
105
				if ( $allValid && $hasCoords ) {
106
					$format = 'map';
107
				}
108
			}
109
110
		}
111
112
		return true;
113
	}
114
115
}
116