Skip to content

Commit

Permalink
Fixed AreaDescription::getQueryString
Browse files Browse the repository at this point in the history
Fixes #314
  • Loading branch information
JeroenDeDauw committed Apr 13, 2017
1 parent 0c179aa commit 70f9cf2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
3 changes: 2 additions & 1 deletion RELEASE-NOTES.md
Expand Up @@ -3,8 +3,9 @@ These are the release notes for the [Maps extension](README.md). For an overview

## Maps 4.1.0

Released on April 15th, 2017.
Released on April 14th, 2017.

* Fixed rendering of area query values (they now work properly in SMW "further result" links)
* Fixed type warning in `SMMapPrinter::getMapHTML`
* Added missing geographical polygon type i18n messages

Expand Down
29 changes: 18 additions & 11 deletions src/Semantic/ValueDescriptions/AreaDescription.php
Expand Up @@ -53,21 +53,24 @@ public function __construct( SMWDataItem $areaCenter, $comparator, $radius, DIPr
$this->radius = $radius;
}

private function getPropertyCompat() {
return method_exists( $this, 'getProperty' ) ? $this->getProperty() : $this->m_property;
}

/**
* @see \SMW\Query\Language\Description::getQueryString
*
* @param boolean $asValue
* @return string
*/
public function getQueryString( $asValue = false ) {
$queryString = DataValueFactory::newDataItemValue( $this->getDataItem(), $this->getPropertyCompat() )->getWikiValue();
$centerString = DataValueFactory::newDataItemValue( $this->center, $this->getPropertyCompat() )->getWikiValue();

$queryString = "$centerString ({$this->radius})";

return $asValue ? $queryString : "[[$queryString]]";
}

private function getPropertyCompat() {
return method_exists( $this, 'getProperty' ) ? $this->getProperty() : $this->m_property;
}

/**
* @see \SMW\Query\Language\Description::prune
*/
Expand Down Expand Up @@ -96,14 +99,14 @@ public function prune( &$maxsize, &$maxdepth, &$log ) {
* @param array $fieldNames
* @param DatabaseBase $dbs
*
* @return string or false
* @return string|false
*/
public function getSQLCondition( $tableName, array $fieldNames, DatabaseBase $dbs ) {
// Only execute the query when the description's type is geographical coordinates,
// the description is valid, and the near comparator is used.
if ( $this->getDataItem()->getDIType() != SMWDataItem::TYPE_GEO
|| ( $this->getComparator() != SMW_CMP_EQ && $this->getComparator() != SMW_CMP_NEQ )
) {
if ( $this->center->getDIType() != SMWDataItem::TYPE_GEO ) {
throw new \LogicException( 'Constructor should have prevented this' );
}

if ( !$this->comparatorIsSupported() ) {
return false;
}

Expand All @@ -130,6 +133,10 @@ public function getSQLCondition( $tableName, array $fieldNames, DatabaseBase $db
return implode( " $joinCond ", $conditions );
}

private function comparatorIsSupported() {
return $this->getComparator() === SMW_CMP_EQ || $this->getComparator() === SMW_CMP_NEQ;
}

/**
* @return float[] An associative array containing the limits with keys north, east, south and west.
*/
Expand Down
Expand Up @@ -25,34 +25,59 @@ public function setUp() {

public function testGetBoundingBox() {
$area = new AreaDescription(
new SMWDIGeoCoord( 0, 0 ),
new SMWDIGeoCoord( 0, 5 ),
SMW_CMP_EQ,
'10 km'
);

$this->assertEquals(
[
'north' => 0.089932160591873,
'east' => 0.089932160591873,
'east' => 5.089932160591873,
'south' => -0.089932160591873,
'west' => -0.089932160591873
'west' => 4.9100678394081
],
$area->getBoundingBox()
);
}

public function testGetSQLCondition() {
$area = new AreaDescription(
new SMWDIGeoCoord( 0, 0 ),
new SMWDIGeoCoord( 0, 5 ),
SMW_CMP_EQ,
'10 km'
);

$this->assertSame(
'geo_table.lat_field < \'0.089932160591873\' AND geo_table.lat_field > \'-0.089932160591873\' '
. 'AND geo_table.long_field < \'0.089932160591873\' AND geo_table.long_field > \'-0.089932160591873\'',
. 'AND geo_table.long_field < \'5.0899321605919\' AND geo_table.long_field > \'4.9100678394081\'',
$area->getSQLCondition( 'geo_table', ['id_field', 'lat_field', 'long_field'], wfGetDB( DB_MASTER ) )
);
}

public function testWhenComparatorIsNotSupported_getSQLConditionReturnsFalse() {
$area = new AreaDescription(
new SMWDIGeoCoord( 0, 5 ),
SMW_CMP_LIKE,
'10 km'
);

$this->assertFalse(
$area->getSQLCondition( 'geo_table', ['id_field', 'lat_field', 'long_field'], wfGetDB( DB_MASTER ) )
);
}

public function testGetQueryString() {
$area = new AreaDescription(
new SMWDIGeoCoord( 1, 5 ),
SMW_CMP_EQ,
'10 km'
);

$this->assertSame(
'[[1掳 0\' 0", 5掳 0\' 0" (10 km)]]',
$area->getQueryString()
);
}

}

0 comments on commit 70f9cf2

Please sign in to comment.