r65981 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65980‎ | r65981 | r65982 >
Date:10:13, 6 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.6 - partly implemented distance query
Modified paths:
  • /trunk/extensions/SemanticMaps/GeoCoords/SM_AreaValueDescription.php (modified) (history)
  • /trunk/extensions/SemanticMaps/GeoCoords/SM_GeoCoords.php (modified) (history)
  • /trunk/extensions/SemanticMaps/GeoCoords/SM_GeoCoordsValue.php (modified) (history)
  • /trunk/extensions/SemanticMaps/GeoCoords/SM_GeoCoordsValueDescription.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticMaps/GeoCoords/SM_GeoCoordsValue.php
@@ -54,14 +54,38 @@
5555 * @see SMWDataValue::parseUserValue
5656 */
5757 protected function parseUserValue( $value ) {
 58+ $this->parseUserValueOrQuery( $value );
 59+ }
 60+
 61+ /**
 62+ * Overwrite SMWDataValue::getQueryDescription() to be able to process
 63+ * comparators between all values.
 64+ *
 65+ * @param string $value
 66+ *
 67+ * @return SMWDescription
 68+ */
 69+ public function getQueryDescription( $value ) {
 70+ return $this->parseUserValueOrQuery( $value, true );
 71+ }
 72+
 73+ /**
 74+ * Parses the value into the coordinates and any meta data provided, such as distance.
 75+ */
 76+ protected function parseUserValueOrQuery( $value, $asQuery = false ) {
5877 if ( $value == '' ) {
5978 $this->addError( wfMsg( 'smw_novalues' ) );
6079 } else {
 80+ // TODO: parse the parts here
 81+ //$parts = preg_split( '', $value );
 82+ $distance = 5;
 83+ $hasDistance = false;
 84+
6185 $coordinates = MapsCoordinateParser::parseCoordinates( $value );
6286 if ( $coordinates ) {
6387 $this->mCoordinateSet = $coordinates;
6488
65 - if ( $this->m_caption === false ) {
 89+ if ( $this->m_caption === false && !$asQuery ) {
6690 global $smgQPCoodFormat, $smgQPCoodDirectional;
6791 $this->m_caption = MapsCoordinateParser::formatCoordinates( $coordinates, $smgQPCoodFormat, $smgQPCoodDirectional );
6892 }
@@ -69,16 +93,22 @@
7094 $this->addError( wfMsgExt( 'maps_unrecognized_coords', array( 'parsemag' ), $value, 1 ) );
7195 }
7296 }
73 - }
7497
75 - /**
76 - * Overwrite SMWDataValue::getQueryDescription() to be able to process
77 - * comparators between all values.
78 - *
79 - * @return SMWDescription
80 - */
81 - public function getQueryDescription( $value ) {
82 - return parent::getQueryDescription( $value );
 98+ if ( $asQuery ) {
 99+ $this->setUserValue( $value );
 100+
 101+ switch ( true ) {
 102+ case !$this->isValid() :
 103+ return new SMWThingDescription();
 104+ break;
 105+ case $hasDistance :
 106+ return new SMAreaValueDescription( $this, $distance );
 107+ break;
 108+ default :
 109+ return new SMGeoCoordsValueDescription( $this );
 110+ break;
 111+ }
 112+ }
83113 }
84114
85115 /**
@@ -95,6 +125,7 @@
96126 $smgQPCoodFormat,
97127 $smgQPCoodDirectional
98128 );
 129+
99130 $this->mWikivalue = $this->m_caption;
100131 }
101132
@@ -136,7 +167,6 @@
137168 * @see SMWDataValue::getShortHTMLText
138169 */
139170 public function getShortHTMLText( $linker = null ) {
140 - // TODO: parse to HTML?
141171 return $this->getShortWikiText( $linker );
142172 }
143173
@@ -156,7 +186,6 @@
157187 * @see SMWDataValue::getLongHTMLText
158188 */
159189 public function getLongHTMLText( $linker = null ) {
160 - // TODO: parse to HTML?
161190 return $this->getLongWikiText( $linker );
162191 }
163192
@@ -194,5 +223,12 @@
195224 protected function getServiceLinkParams() {
196225 return array( ); // TODO
197226 }
 227+
 228+ /**
 229+ * @return array
 230+ */
 231+ public function getCoordinateSet() {
 232+ return $this->mCoordinateSet;
 233+ }
198234
199235 }
Index: trunk/extensions/SemanticMaps/GeoCoords/SM_GeoCoords.php
@@ -8,7 +8,6 @@
99 * @ingroup SemanticMaps
1010 *
1111 * @author Jeroen De Dauw
12 - * @author Markus Krötzsch
1312 */
1413
1514 // Registration of the GoeCoords class.
@@ -18,56 +17,21 @@
1918 $wgAutoloadClasses['SMGeoCoordsValue'] = dirname( __FILE__ ) . '/SM_GeoCoordsValue.php';
2019
2120 // Registration of the Geographical Coordinate value description class.
 21+$wgAutoloadClasses['SMGeoCoordsValueDescription'] = dirname( __FILE__ ) . '/SM_GeoCoordsValueDescription.php';
 22+
 23+// Registration of the Geographical Coordinate are description class.
2224 $wgAutoloadClasses['SMAreaValueDescription'] = dirname( __FILE__ ) . '/SM_AreaValueDescription.php';
2325
2426 // Hook for initializing the Geographical Coordinate type.
2527 $wgHooks['smwInitDatatypes'][] = 'SMGeoCoordsValue::initGeoCoordsType';
2628
 29+// Hook for defining a table to store geographical coordinates in.
2730 $wgHooks['SMWPropertyTables'][] = 'SMGeoCoordsValue::initGeoCoordsTable';
2831
2932 // Hook for initializing the Geographical Proximity query support.
30 -$wgHooks['smwGetSQLConditionForValue'][] = 'SMGeoCoords::getGeoProximitySQLCondition';
 33+$wgHooks['smwGetSQLConditionForValue'][] = 'SMAreaValueDescription::getSQLCondition';
3134
32 -define( 'SM_CMP_NEAR', 101 ); // Define the near comparator for proximity queries.
 35+// Hook for initializing the Geographical Proximity query support.
 36+$wgHooks['smwGetSQLConditionForValue'][] = 'SMGeoCoordsValueDescription::getSQLCondition';
3337
34 -final class SMGeoCoords {
35 -
36 - /**
37 - * Custom SQL query extension for matching geographic coordinates.
38 - *
39 - * @param string $whereSQL The SQL where condition to expand.
40 - * @param SMAreaValueDescription $description The description of center coordinate.
41 - * @param string $tablename
42 - * @param string $fieldname
43 - * @param DatabaseBase $dbs
44 - *
45 - * @return true
46 - */
47 - public static function getGeoProximitySQLCondition( &$whereSQL, SMAreaValueDescription $description, $tablename, $fieldname, DatabaseBase $dbs ) {
48 - $dataValue = $description->getDatavalue();
49 -
50 - // Only execute the query when the description's type is geographical coordinates,
51 - // the description is valid, and the near comparator is used.
52 - if ( ( $dataValue->getTypeID() != '_geo' )
53 - || ( !$dataValue->isValid() )
54 - || ( $description->getComparator() != SM_CMP_NEAR )
55 - ) return true;
56 -
57 - $boundingBox = $description->getBounds();
58 -
59 - $north = $dbs->addQuotes( $boundingBox['north'] );
60 - $east = $dbs->addQuotes( $boundingBox['east'] );
61 - $south = $dbs->addQuotes( $boundingBox['south'] );
62 - $west = $dbs->addQuotes( $boundingBox['west'] );
63 -
64 - // TODO: The field names are hardcoded in, since SMW offers no support for selection based on multiple fields.
65 - // Ideally SMW's setup should be changed to allow for this. Now the query can break when other extensions
66 - // add their own semantic tables with similar signatures.
67 - $whereSQL .= "{$tablename}.lat < $north && {$tablename}.lat > $south && {$tablename}.lon < $east && {$tablename}.lon > $west";
68 -
69 - return true;
70 - }
71 -
72 -
73 -}
74 -
 38+// define( 'SM_CMP_NEAR', 101 ); // Define the near comparator for proximity queries.
\ No newline at end of file
Index: trunk/extensions/SemanticMaps/GeoCoords/SM_GeoCoordsValueDescription.php
@@ -0,0 +1,79 @@
 2+<?php
 3+
 4+/**
 5+ * File holding the SMGeoCoordsValueDescription class.
 6+ *
 7+ * @file SM_GeoCoordsValueDescription.php
 8+ * @ingroup SemanticMaps
 9+ *
 10+ * @author Jeroen De Dauw
 11+ */
 12+
 13+if ( !defined( 'MEDIAWIKI' ) ) {
 14+ die( 'Not an entry point.' );
 15+}
 16+
 17+/**
 18+ * Description of one data value of type Goegraphical Coordinates.
 19+ *
 20+ * @author Jeroen De Dauw
 21+ *
 22+ * @ingroup SemanticMaps
 23+ */
 24+class SMGeoCoordsValueDescription extends SMWValueDescription {
 25+
 26+ /**
 27+ * TODO: re-add the coparator parameter once support for this is implemented in the query hook.
 28+ *
 29+ * @param SMGeoCoordsValue $dataValue
 30+ */
 31+ public function __construct( SMGeoCoordsValue $dataValue ) {
 32+ parent::__construct( $dataValue );
 33+ }
 34+
 35+ /**
 36+ * @see SMWDescription:getQueryString
 37+ *
 38+ * @param Boolean $asvalue
 39+ */
 40+ public function getQueryString( $asValue = false ) {
 41+ if ( $this->m_datavalue !== null ) {
 42+ $queryString = $this->m_datavalue->getWikiValue();
 43+ return $asValue ? $queryString : "[[$queryString]]";
 44+ } else {
 45+ return $asValue ? '+' : '';
 46+ }
 47+ }
 48+
 49+ /**
 50+ * Custom SQL query extension for matching geographic coordinates.
 51+ *
 52+ * @param string $whereSQL The SQL where condition to expand.
 53+ * @param SMWDescription $description
 54+ * @param string $tablename
 55+ * @param string $fieldname
 56+ * @param DatabaseBase $dbs
 57+ *
 58+ * @return true
 59+ */
 60+ public static function getSQLCondition( &$whereSQL, SMWDescription $description, $tablename, $fieldname, DatabaseBase $dbs ) {
 61+ $dataValue = $description->getDatavalue();
 62+
 63+ // Only execute the query when the description's type is geographical coordinates,
 64+ // the description is valid, and the near comparator is used.
 65+ if ( $dataValue->getTypeID() != '_geo'
 66+ || !$dataValue->isValid()
 67+ || !$description instanceof SMGeoCoordsValueDescription
 68+ ) return true;
 69+
 70+ $coordinates = $dataValue->getCoordinateSet();
 71+
 72+ // TODO: The field names are hardcoded in, since SMW offers no support for selection based on multiple fields.
 73+ // Ideally SMW's setup should be changed to allow for this. Now the query can break when other extensions
 74+ // add their own semantic tables with similar signatures.
 75+ $whereSQL .= "{$tablename}.lat = {$coordinates['lat']} && {$tablename}.lon = {$coordinates['lon']}";
 76+
 77+ return true;
 78+ }
 79+
 80+}
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMaps/GeoCoords/SM_GeoCoordsValueDescription.php
___________________________________________________________________
Name: svn:eol-style
181 + native
Index: trunk/extensions/SemanticMaps/GeoCoords/SM_AreaValueDescription.php
@@ -14,7 +14,9 @@
1515 }
1616
1717 /**
18 - * Description of one data value of type Goegraphical Areas.
 18+ * Description of a geographical area defined by a coordinates set and a distance to the bounds.
 19+ * The bounds are a 'rectangle' (but bend due to the earhs curvature), as the resulting query
 20+ * would otherwise be to resource intensive.
1921 *
2022 * @author Jeroen De Dauw
2123 *
@@ -23,8 +25,8 @@
2426 class SMAreaValueDescription extends SMWValueDescription {
2527 protected $mBounds = false;
2628
27 - public function __construct( SMGeoCoordsValue $dataValue, $radius, $comparator = SMW_CMP_EQ ) {
28 - parent::__construct( $dataValue, $comparator );
 29+ public function __construct( SMGeoCoordsValue $dataValue, $radius ) {
 30+ parent::__construct( $dataValue );
2931
3032 // If the MapsGeoFunctions class is not loaded, we can not create the bounding box, so don't add any conditions.
3133 if ( self::geoFunctionsAreAvailable() ) {
@@ -39,7 +41,7 @@
4042 );
4143 }
4244 }
43 -
 45+
4446 /**
4547 * @see SMWDescription:getQueryString
4648 *
@@ -75,7 +77,7 @@
7678 */
7779 public function getBounds() {
7880 return $this->mBounds;
79 - }
 81+ }
8082
8183 /**
8284 * Returns the lat and lon limits of a bounding box around a circle defined by the provided parameters.
@@ -106,4 +108,40 @@
107109 global $wgAutoloadClasses;
108110 return array_key_exists( 'MapsGeoFunctions', $wgAutoloadClasses );
109111 }
 112+
 113+ /**
 114+ * Custom SQL query extension for matching geographic coordinates in an area.
 115+ *
 116+ * @param string $whereSQL The SQL where condition to expand.
 117+ * @param SMWDescription $description
 118+ * @param string $tablename
 119+ * @param string $fieldname
 120+ * @param DatabaseBase $dbs
 121+ *
 122+ * @return true
 123+ */
 124+ public static function getSQLCondition( &$whereSQL, SMWDescription $description, $tablename, $fieldname, DatabaseBase $dbs ) {
 125+ $dataValue = $description->getDatavalue();
 126+
 127+ // Only execute the query when the description's type is geographical coordinates,
 128+ // the description is valid, and the near comparator is used.
 129+ if ( $dataValue->getTypeID() != '_geo'
 130+ || !$dataValue->isValid()
 131+ || !$description instanceof SMAreaValueDescription
 132+ ) return true;
 133+
 134+ $boundingBox = $description->getBounds();
 135+
 136+ $north = $dbs->addQuotes( $boundingBox['north'] );
 137+ $east = $dbs->addQuotes( $boundingBox['east'] );
 138+ $south = $dbs->addQuotes( $boundingBox['south'] );
 139+ $west = $dbs->addQuotes( $boundingBox['west'] );
 140+
 141+ // TODO: The field names are hardcoded in, since SMW offers no support for selection based on multiple fields.
 142+ // Ideally SMW's setup should be changed to allow for this. Now the query can break when other extensions
 143+ // add their own semantic tables with similar signatures.
 144+ $whereSQL .= "{$tablename}.lat < $north && {$tablename}.lat > $south && {$tablename}.lon < $east && {$tablename}.lon > $west";
 145+
 146+ return true;
 147+ }
110148 }
\ No newline at end of file

Status & tagging log