Skip to content

Commit

Permalink
Further result printer simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Nov 23, 2018
1 parent b949e28 commit e8a759f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 70 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Expand Up @@ -10,6 +10,7 @@ Under development
* Fixed `link=all` in Ask queries: values are now linked where appropriate
* Fixed `link=none` and `link=subject` in Ask queries: properties are no longer linked
* Fixed `headers=hide` in Ask queries: the values now show
* Fixed KML result format (6.1.0 regression)

## Maps 6.1.0

Expand Down
2 changes: 1 addition & 1 deletion src/SemanticMW/ResultPrinters/KmlPrinter.php
Expand Up @@ -35,7 +35,7 @@ private function getKML( SMWQueryResult $res, int $outputMode ): string {
$queryHandler->setPageLinkText( $this->params['pagelinktext'] );

$formatter = new KmlFormatter();
return $formatter->formatLocationsAsKml( $queryHandler->getShapes()['locations'] );
return $formatter->formatLocationsAsKml( ...$queryHandler->getLocations() );
}

/**
Expand Down
25 changes: 11 additions & 14 deletions src/SemanticMW/ResultPrinters/MapPrinter.php
Expand Up @@ -171,7 +171,15 @@ private function handleMarkerData( array &$params, QueryHandler $queryHandler )

unset( $params['staticlocations'] );

$this->addShapeData( $queryHandler->getShapes(), $params, $iconUrl, $visitedIconUrl );
$params['locations'] = array_merge(
$params['locations'],
$this->getJsonForLocations(
$queryHandler->getLocations(),
$params,
$iconUrl,
$visitedIconUrl
)
);

if ( $params['format'] === 'openlayers' ) {
$params['layers'] = DisplayMapRenderer::evilOpenLayersHack( $params['layers'] );
Expand Down Expand Up @@ -230,19 +238,8 @@ private function getJsonForStaticLocation( Location $location, array $params, $i
return $jsonObj;
}

private function addShapeData( array $queryShapes, array &$params, string $iconUrl, string $visitedIconUrl ) {
$params['locations'] = array_merge(
$params['locations'],
$this->getJsonForLocations(
$queryShapes['locations'],
$params,
$iconUrl,
$visitedIconUrl
)
);
private function addShapeData( array $locations, array &$params, string $iconUrl, string $visitedIconUrl ) {

$params['lines'] = $this->getElementJsonArray( $queryShapes['lines'], $params );
$params['polygons'] = $this->getElementJsonArray( $queryShapes['polygons'], $params );
}

/**
Expand All @@ -253,7 +250,7 @@ private function addShapeData( array $queryShapes, array &$params, string $iconU
*
* @return array
*/
private function getJsonForLocations( array $locations, array $params, string $iconUrl, string $visitedIconUrl ): array {
private function getJsonForLocations( iterable $locations, array $params, string $iconUrl, string $visitedIconUrl ): array {
$locationsJson = [];

foreach ( $locations as $location ) {
Expand Down
98 changes: 43 additions & 55 deletions src/SemanticMW/ResultPrinters/QueryHandler.php
Expand Up @@ -51,15 +51,6 @@ class QueryHandler {

private $outputMode;

/**
* @var array
*/
private $geoShapes = [
'lines' => [],
'locations' => [],
'polygons' => []
];

/**
* The template to use for the text, or false if there is none.
* @var string|boolean false
Expand Down Expand Up @@ -178,53 +169,45 @@ public function setHeaderStyle( string $headers ) {
}

/**
* @return array
*/
public function getShapes() {
$this->findShapes();
return $this->geoShapes;
}

/**
* @since 2.0
* @return Location[]
*/
private function findShapes() {
public function getLocations(): iterable {
while ( ( $row = $this->queryResult->getNext() ) !== false ) {
$this->handleResultRow( $row );
yield from $this->handlePageResult( $row );
}
}

private function getTitleAndText( SMWResultArray $resultArray ): array {
while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) {
if ( $dataValue instanceof SMWWikiPageValue ) {
return [
$dataValue->getLongText( $this->outputMode, null ),
$this->getResultSubjectText( $dataValue )
];
}
/**
* @param SMWResultArray[] $row
* @return Location[]
*/
private function handlePageResult( array $row ): array {
[ $title, $text ] = $this->getTitleAndText( $row[0] );
[ $locations, $properties ] = $this->getLocationsAndProperties( $row );

if ( $dataValue->getTypeID() == '_str' ) {
return [
$dataValue->getLongText( $this->outputMode, null ),
$dataValue->getLongText( $this->outputMode, smwfGetLinker() )
];
}
if ( $properties !== [] && $text !== '' ) {
$text .= $this->subjectSeparator;
}

return [ '', '' ];
$icon = $this->getLocationIcon( $row );

return $this->buildLocationsList(
$locations,
$text,
$icon,
$properties,
Title::newFromText( $title )
);
}

/**
* Returns the locations found in the provided result row.
*
* @param SMWResultArray[] $row
* @return array
*/
private function handleResultRow( array $row ) {
private function getLocationsAndProperties( array $row ): array {
$locations = [];
$properties = [];

[ $title, $text ] = $this->getTitleAndText( $row[0] );

// Loop through all fields of the record.
foreach ( $row as $i => $resultArray ) {
if ( $i === 0 ) {
Expand All @@ -251,22 +234,27 @@ private function handleResultRow( array $row ) {
}
}

if ( $properties !== [] && $text !== '' ) {
$text .= $this->subjectSeparator;
}
return [ $locations, $properties ];
}

$icon = $this->getLocationIcon( $row );
private function getTitleAndText( SMWResultArray $resultArray ): array {
while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) {
if ( $dataValue instanceof SMWWikiPageValue ) {
return [
$dataValue->getLongText( $this->outputMode, null ),
$this->getResultSubjectText( $dataValue )
];
}

$this->geoShapes['locations'] = array_merge(
$this->geoShapes['locations'],
$this->buildLocationsList(
$locations,
$text,
$icon,
$properties,
Title::newFromText( $title )
)
);
if ( $dataValue->getTypeID() == '_str' ) {
return [
$dataValue->getLongText( $this->outputMode, null ),
$dataValue->getLongText( $this->outputMode, smwfGetLinker() )
];
}
}

return [ '', '' ];
}

private function locationFromDataItem( SMWDIGeoCoord $dataItem ): Location {
Expand Down Expand Up @@ -483,7 +471,7 @@ private function shouldGetActiveIconUrlFor( Title $title ) {
*
* @return Location[]
*/
private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) {
private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ): array {
if ( !$this->hasTemplate() ) {
$text .= implode( '<br />', $properties );
}
Expand Down

0 comments on commit e8a759f

Please sign in to comment.