Completed
Push — move ( 875a6f )
by Jeroen De
04:17
created

SemanticMapsHooks   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 105
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addToAdminLinks() 0 19 2
A initGeoDataTypes() 0 15 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
	public static function initGeoDataTypes() {
56
		DataTypeRegistry::getInstance()->registerDatatype(
57
			'_geo',
58
			CoordinateValue::class,
59
			SMWDataItem::TYPE_GEO
60
		);
61
62
		DataTypeRegistry::getInstance()->registerDatatype(
63
			'_gpo',
64
			GeoPolygonValue::class,
65
			SMWDataItem::TYPE_BLOB
66
		);
67
68
		return true;
69
	}
70
71
	/**
72
	 * Set the default format to 'map' when the requested properties are
73
	 * of type geographic coordinates.
74
	 *
75
	 * TODO: have a setting to turn this off and have it off by default for #show
76
	 *
77
	 * @since 1.0
78
	 *
79
	 * @param $format Mixed: The format (string), or false when not set yet
80
	 * @param SMWPrintRequest[] $printRequests The print requests made
81
	 *
82
	 * @return boolean
83
	 */
84
	public static function addGeoCoordsDefaultFormat( &$format, array $printRequests ) {
85
		// Only set the format when not set yet. This allows other extensions to override the Maps behavior.
86
		if ( $format === false ) {
87
			// Only apply when there is more then one print request.
88
			// This way requests comming from #show are ignored.
89
			if ( count( $printRequests ) > 1 ) {
90
				$allValid = true;
91
				$hasCoords = false;
92
93
				// Loop through the print requests to determine their types.
94
				foreach ( $printRequests as $printRequest ) {
95
					// Skip the first request, as it's the object.
96
					if ( $printRequest->getMode() == SMWPrintRequest::PRINT_THIS ) {
97
						continue;
98
					}
99
100
					$typeId = $printRequest->getTypeID();
101
102
					if ( $typeId == '_geo' ) {
103
						$hasCoords = true;
104
					} else {
105
						$allValid = false;
106
						break;
107
					}
108
				}
109
110
				// If they are all coordinates, set the result format to 'map'.
111
				if ( $allValid && $hasCoords ) {
112
					$format = 'map';
113
				}
114
			}
115
116
		}
117
118
		return true;
119
	}
120
121
}
122