r86075 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86074‎ | r86075 | r86076 >
Date:19:54, 14 April 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
split geocoords hooks into separate class, as the autoloader is apparently parsing all files with hooks on every run
Modified paths:
  • /trunk/extensions/SemanticMaps/SemanticMaps.php (modified) (history)
  • /trunk/extensions/SemanticMaps/includes/SM_GeoCoordsHooks.php (added) (history)
  • /trunk/extensions/SemanticMaps/includes/SM_GeoCoordsValue.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMaps/includes/SM_GeoCoordsValue.php
@@ -9,7 +9,8 @@
1010 * @ingroup SemanticMaps
1111 * @ingroup SMWDataValues
1212 *
13 - * @author Jeroen De Dauw
 13+ * @licence GNU GPL v3
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1415 * @author Markus Krötzsch
1516 */
1617 class SMGeoCoordsValue extends SMWDataValue {
@@ -18,113 +19,6 @@
1920 protected $wikiValue;
2021
2122 /**
22 - * Set the default format to 'map' when the requested properties are
23 - * of type geographic coordinates.
24 - *
25 - * TODO: have a setting to turn this off and have it off by default for #show
26 - *
27 - * @since 0.6.5
28 - *
29 - * @param $format Mixed: The format (string), or false when not set yet
30 - * @param $printRequests Array: The print requests made
31 - * @param $params Array: The parameters for the query printer
32 - *
33 - * @return true
34 - */
35 - public static function addGeoCoordsDefaultFormat( &$format, array $printRequests, array $params ) {
36 - // Only set the format when not set yet. This allows other extensions to override the Semantic Maps behaviour.
37 - if ( $format === false ) {
38 - // Only apply when there is more then one print request.
39 - // This way requests comming from #show are ignored.
40 - if ( count( $printRequests ) > 1 ) {
41 - $allCoords = true;
42 - $first = true;
43 -
44 - // Loop through the print requests to determine their types.
45 - foreach( $printRequests as $printRequest ) {
46 - // Skip the first request, as it's the object.
47 - if ( $first ) {
48 - $first = false;
49 - continue;
50 - }
51 -
52 - $typeId = $printRequest->getTypeID();
53 -
54 - if ( $typeId != '_geo' ) {
55 - $allCoords = false;
56 - break;
57 - }
58 - }
59 -
60 - // If they are all coordinates, set the result format to 'map'.
61 - if ( $allCoords ) {
62 - $format = 'map';
63 - }
64 - }
65 -
66 - }
67 -
68 - return true;
69 - }
70 -
71 - /**
72 - * Adds support for the geographical coordinate data type to Semantic MediaWiki.
73 - *
74 - * @since 0.6
75 - *
76 - * TODO: i18n keys still need to be moved
77 - *
78 - * @return true
79 - */
80 - public static function initGeoCoordsType() {
81 - SMWDataValueFactory::registerDatatype( '_geo', __CLASS__, 'Geographic coordinate' );
82 - return true;
83 - }
84 -
85 - /**
86 - * Defines the signature for geographical fields needed for the smw_coords table.
87 - *
88 - * @since 0.6
89 - *
90 - * @param array $fieldTypes The field types defined by SMW, passed by reference.
91 - *
92 - * @return true
93 - */
94 - public static function initGeoCoordsFieldTypes( array $fieldTypes ) {
95 - global $smgUseSpatialExtensions;
96 -
97 - // Only add the table when the SQL store is not a postgres database, and it has not been added by SMW itself.
98 - if ( $smgUseSpatialExtensions && !array_key_exists( 'c', $fieldTypes ) ) {
99 - $fieldTypes['c'] = 'Point NOT NULL';
100 - }
101 -
102 - return true;
103 - }
104 -
105 - /**
106 - * Defines the layout for the smw_coords table which is used to store value of the GeoCoords type.
107 - *
108 - * @since 0.6
109 - *
110 - * @param array $propertyTables The property tables defined by SMW, passed by reference.
111 - */
112 - public static function initGeoCoordsTable( array $propertyTables ) {
113 - global $smgUseSpatialExtensions;
114 -
115 - // No spatial extensions support for postgres yet, so just store as 2 float fields.
116 - $signature = $smgUseSpatialExtensions ? array( 'point' => 'c' ) : array( 'lat' => 'f', 'lon' => 'f' );
117 - $indexes = $smgUseSpatialExtensions ? array( array( 'point', 'SPATIAL INDEX' ) ) : array_keys( $signature );
118 -
119 - $propertyTables['smw_coords'] = new SMWSQLStore2Table(
120 - 'sm_coords',
121 - $signature,
122 - $indexes // These are the fields that should be indexed.
123 - );
124 -
125 - return true;
126 - }
127 -
128 - /**
12923 * @see SMWDataValue::parseUserValue
13024 *
13125 * @since 0.6
@@ -388,4 +282,4 @@
389283 return 0;
390284 }
391285
392 -}
\ No newline at end of file
 286+}
Index: trunk/extensions/SemanticMaps/includes/SM_GeoCoordsHooks.php
@@ -0,0 +1,123 @@
 2+<?php
 3+
 4+/**
 5+ * Implementation of datavalues that are geographic coordinates.
 6+ *
 7+ * @since 0.8
 8+ *
 9+ * @file SM_GeoCoordsHooks.php
 10+ * @ingroup SemanticMaps
 11+ *
 12+ * @licence GNU GPL v3
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+final class SMGeoCoordsHooks {
 16+
 17+ /**
 18+ * Set the default format to 'map' when the requested properties are
 19+ * of type geographic coordinates.
 20+ *
 21+ * TODO: have a setting to turn this off and have it off by default for #show
 22+ *
 23+ * @since 0.8
 24+ *
 25+ * @param $format Mixed: The format (string), or false when not set yet
 26+ * @param $printRequests Array: The print requests made
 27+ * @param $params Array: The parameters for the query printer
 28+ *
 29+ * @return true
 30+ */
 31+ public static function addGeoCoordsDefaultFormat( &$format, array $printRequests, array $params ) {
 32+ // Only set the format when not set yet. This allows other extensions to override the Semantic Maps behaviour.
 33+ if ( $format === false ) {
 34+ // Only apply when there is more then one print request.
 35+ // This way requests comming from #show are ignored.
 36+ if ( count( $printRequests ) > 1 ) {
 37+ $allCoords = true;
 38+ $first = true;
 39+
 40+ // Loop through the print requests to determine their types.
 41+ foreach( $printRequests as $printRequest ) {
 42+ // Skip the first request, as it's the object.
 43+ if ( $first ) {
 44+ $first = false;
 45+ continue;
 46+ }
 47+
 48+ $typeId = $printRequest->getTypeID();
 49+
 50+ if ( $typeId != '_geo' ) {
 51+ $allCoords = false;
 52+ break;
 53+ }
 54+ }
 55+
 56+ // If they are all coordinates, set the result format to 'map'.
 57+ if ( $allCoords ) {
 58+ $format = 'map';
 59+ }
 60+ }
 61+
 62+ }
 63+
 64+ return true;
 65+ }
 66+
 67+ /**
 68+ * Adds support for the geographical coordinate data type to Semantic MediaWiki.
 69+ *
 70+ * @since 0.8
 71+ *
 72+ * TODO: i18n keys still need to be moved
 73+ *
 74+ * @return true
 75+ */
 76+ public static function initGeoCoordsType() {
 77+ SMWDataValueFactory::registerDatatype( '_geo', __CLASS__, 'Geographic coordinate' );
 78+ return true;
 79+ }
 80+
 81+ /**
 82+ * Defines the signature for geographical fields needed for the smw_coords table.
 83+ *
 84+ * @since 0.8
 85+ *
 86+ * @param array $fieldTypes The field types defined by SMW, passed by reference.
 87+ *
 88+ * @return true
 89+ */
 90+ public static function initGeoCoordsFieldTypes( array $fieldTypes ) {
 91+ global $smgUseSpatialExtensions;
 92+
 93+ // Only add the table when the SQL store is not a postgres database, and it has not been added by SMW itself.
 94+ if ( $smgUseSpatialExtensions && !array_key_exists( 'c', $fieldTypes ) ) {
 95+ $fieldTypes['c'] = 'Point NOT NULL';
 96+ }
 97+
 98+ return true;
 99+ }
 100+
 101+ /**
 102+ * Defines the layout for the smw_coords table which is used to store value of the GeoCoords type.
 103+ *
 104+ * @since 0.8
 105+ *
 106+ * @param array $propertyTables The property tables defined by SMW, passed by reference.
 107+ */
 108+ public static function initGeoCoordsTable( array $propertyTables ) {
 109+ global $smgUseSpatialExtensions;
 110+
 111+ // No spatial extensions support for postgres yet, so just store as 2 float fields.
 112+ $signature = $smgUseSpatialExtensions ? array( 'point' => 'c' ) : array( 'lat' => 'f', 'lon' => 'f' );
 113+ $indexes = $smgUseSpatialExtensions ? array( array( 'point', 'SPATIAL INDEX' ) ) : array_keys( $signature );
 114+
 115+ $propertyTables['smw_coords'] = new SMWSQLStore2Table(
 116+ 'sm_coords',
 117+ $signature,
 118+ $indexes // These are the fields that should be indexed.
 119+ );
 120+
 121+ return true;
 122+ }
 123+
 124+}
Property changes on: trunk/extensions/SemanticMaps/includes/SM_GeoCoordsHooks.php
___________________________________________________________________
Added: svn:eol-style
1125 + native
Index: trunk/extensions/SemanticMaps/SemanticMaps.php
@@ -86,6 +86,8 @@
8787
8888 $incDir = dirname( __FILE__ ) . '/includes/';
8989
 90+$wgAutoloadClasses['SMGeoCoordsHooks'] = $incDir . 'SM_GeoCoordsHooks.php';
 91+
9092 // Data values
9193 $wgAutoloadClasses['SMGeoCoordsValue'] = $incDir . 'SM_GeoCoordsValue.php';
9294
@@ -96,16 +98,16 @@
9799 $wgAutoloadClasses['SemanticMapsHooks'] = dirname( __FILE__ ) . '/SemanticMaps.hooks.php';
98100
99101 // Hook for initializing the Geographical Coordinate type.
100 -$wgHooks['smwInitDatatypes'][] = 'SMGeoCoordsValue::initGeoCoordsType';
 102+$wgHooks['smwInitDatatypes'][] = 'SMGeoCoordsHooks::initGeoCoordsType';
101103
102104 // Hook for initializing the field types needed by Geographical Coordinates.
103 -$wgHooks['SMWCustomSQLStoreFieldType'][] = 'SMGeoCoordsValue::initGeoCoordsFieldTypes';
 105+$wgHooks['SMWCustomSQLStoreFieldType'][] = 'SMGeoCoordsHooks::initGeoCoordsFieldTypes';
104106
105107 // Hook for defining a table to store geographical coordinates in.
106 -$wgHooks['SMWPropertyTables'][] = 'SMGeoCoordsValue::initGeoCoordsTable';
 108+$wgHooks['SMWPropertyTables'][] = 'SMGeoCoordsHooks::initGeoCoordsTable';
107109
108110 // Hook for defining the default query printer for queries that ask for geographical coordinates.
109 -$wgHooks['SMWResultFormat'][] = 'SMGeoCoordsValue::addGeoCoordsDefaultFormat';
 111+$wgHooks['SMWResultFormat'][] = 'SMGeoCoordsHooks::addGeoCoordsDefaultFormat';
110112
111113 // Hook for adding a Semantic Maps links to the Admin Links extension.
112114 $wgHooks['AdminLinks'][] = 'SemanticMapsHooks::addToAdminLinks';

Status & tagging log