Skip to content

Commit

Permalink
Merge pull request #614 from JeroenDeDauw/cargo
Browse files Browse the repository at this point in the history
Add cargo integration
  • Loading branch information
JeroenDeDauw committed Apr 12, 2020
2 parents 4d0076b + 8058d29 commit 81538ee
Show file tree
Hide file tree
Showing 26 changed files with 648 additions and 110 deletions.
8 changes: 4 additions & 4 deletions INSTALL.md
Expand Up @@ -25,21 +25,21 @@ minimum requirements are indicated in bold. For a detailed list of changes, see
<th>Release status<br>&nbsp;</th>
</tr>
<tr>
<th>7.18.x</th>
<th>7.19.x</th>
<td>7.1 - 7.4+</td>
<td>1.31 - 1.34+</td>
<td>3.1.x</td>
<td>Planned Q2 2020</td>
</tr>
<tr>
<th>7.17.x</th>
<th>7.18.x</th>
<td>7.1 - 7.4</td>
<td>1.31 - 1.34</td>
<td>3.0 - 3.1</td>
<td><strong>Stable release</strong></td>
</tr>
<tr>
<th>7.16.x</th>
<th>7.17.x<br>-<br>7.16.x</th>
<td>7.1 - 7.4</td>
<td>1.31 - 1.34</td>
<td>3.0 - 3.1</td>
Expand Down Expand Up @@ -106,7 +106,7 @@ minimum requirements are indicated in bold. For a detailed list of changes, see
<td>7.0 - 7.2</td>
<td>1.27 - 1.31</td>
<td>2.1 - 3.0</td>
<td>Security fixes only</td>
<td>Obsolete release, no support</td>
</tr>
<tr>
<th>5.5.x</th>
Expand Down
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,7 @@ Features:
* Distance formatting and format conversion via the [`#distance`](https://www.semantic-mediawiki.org/wiki/Maps/Distance) parser function.
* Visual map editor (Special:MapEditor) to edit [`#display_map`](https://www.semantic-mediawiki.org/wiki/Extension:Maps/Displaying_maps) wikitext (requires Google Maps).
* Image base layer support for custom maps (ie floor plans, game worlds, etc)
* Integration with [Cargo]
* Translations in over 100 languages

Missing a feature? [Professional.Wiki](https://professional.wiki/) does custom development at a discount if it is open sourced.
Expand Down Expand Up @@ -109,3 +110,5 @@ Beware that due to technical debt, some tests access the network.
* [Maps on WikiApiary](https://wikiapiary.com/wiki/Extension:Maps)
* [Maps on MediaWiki.org](https://www.mediawiki.org/wiki/Extension:Maps)
* [TravisCI build status](https://travis-ci.org/JeroenDeDauw/Maps)

[Cargo]: https://www.mediawiki.org/wiki/Extension:Cargo
3 changes: 0 additions & 3 deletions composer.json
Expand Up @@ -43,9 +43,6 @@
"jeroen/simple-geocoder": "~1.2",
"jmikola/geojson": "^1.0.2"
},
"conflict": {
"mediawiki/mediawiki": "< 1.31"
},
"require-dev": {
"data-values/common": "~0.4.1"
},
Expand Down
2 changes: 1 addition & 1 deletion extension.json
@@ -1,6 +1,6 @@
{
"name": "Maps",
"version": "7.17.2",
"version": "7.18.0",

"author": [
"[https://www.entropywins.wtf/mediawiki Jeroen De Dauw]",
Expand Down
9 changes: 5 additions & 4 deletions src/GoogleMapsService.php
Expand Up @@ -5,6 +5,7 @@
namespace Maps;

use Html;
use Maps\Map\MapData;
use ParamProcessor\ProcessedParam;
use ParamProcessor\ProcessingResult;

Expand Down Expand Up @@ -333,7 +334,7 @@ private function getDependencies(): array {
];
}

public function processingResultToMapParams( ProcessingResult $processingResult ): array {
public function newMapDataFromProcessingResult( ProcessingResult $processingResult ): MapData {
$parameters = $processingResult->getParameters();

if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
Expand All @@ -348,7 +349,7 @@ public function processingResultToMapParams( ProcessingResult $processingResult
$mapParams[$parameter->getName()] = $parameter->getValue();
}

return $this->processedParamsToMapParams( $mapParams );
return $this->newMapDataFromParameters( $mapParams );
}


Expand All @@ -362,8 +363,8 @@ private function getParameterWithValue( ProcessedParam $param, $value ) {
);
}

public function processedParamsToMapParams( array $params ): array {
return $params;
public function newMapDataFromParameters( array $params ): MapData {
return new MapData( $params );
}

}
9 changes: 5 additions & 4 deletions src/LeafletService.php
Expand Up @@ -6,6 +6,7 @@

use Html;
use Maps\DataAccess\ImageRepository;
use Maps\Map\MapData;
use ParamProcessor\ParameterTypes;
use ParamProcessor\ProcessingResult;

Expand Down Expand Up @@ -226,11 +227,11 @@ private function getLayerDependencies( array $params ) {
return array_unique( $layerDependencies );
}

public function processingResultToMapParams( ProcessingResult $processingResult ): array {
return $this->processedParamsToMapParams( $processingResult->getParameterArray() );
public function newMapDataFromProcessingResult( ProcessingResult $processingResult ): MapData {
return $this->newMapDataFromParameters( $processingResult->getParameterArray() );
}

public function processedParamsToMapParams( array $params ): array {
public function newMapDataFromParameters( array $params ): MapData {
if ( $params['geojson'] !== '' ) {
$fetcher = MapsFactory::globalInstance()->newGeoJsonFetcher();

Expand All @@ -243,7 +244,7 @@ public function processedParamsToMapParams( array $params ): array {

$params['imageLayers'] = $this->getJsImageLayers( $params['image layers'] );

return $params;
return new MapData( $params );
}

private function getJsImageLayers( array $imageLayers ) {
Expand Down
47 changes: 47 additions & 0 deletions src/Map/CargoFormat/CargoFormat.php
@@ -0,0 +1,47 @@
<?php

declare( strict_types = 1 );

namespace Maps\Map\CargoFormat;

use CargoDisplayFormat;
use Maps\Map\CargoFormat\CargoOutputBuilder;
use Maps\MapsFactory;

class CargoFormat extends CargoDisplayFormat {

private $parserOutput;

public function __construct( \ParserOutput $parserOutput ) {
parent::__construct( $parserOutput );
$this->parserOutput = $parserOutput;
}

public static function allowedParameters() {
return [
'height' => [ 'type' => 'int', 'label' => wfMessage( 'cargo-viewdata-heightparam' )->parse() ],
'width' => [ 'type' => 'int', 'label' => wfMessage( 'cargo-viewdata-widthparam' )->parse() ],
'icon' => [ 'type' => 'string' ],
'zoom' => [ 'type' => 'int' ]
];
}

public function display( array $valuesTable, array $formattedValuesTable, array $fieldDescriptions, array $displayParams ) {
$mapOutput = $this->getResultBuilder()->buildOutputFromCargoData(
$valuesTable,
$formattedValuesTable,
$fieldDescriptions,
$displayParams
);

$this->parserOutput->addHeadItem( $mapOutput->getHeadItems() );
$this->parserOutput->addModules( $mapOutput->getResourceModules() );

return $mapOutput->getHtml();
}

private function getResultBuilder(): CargoOutputBuilder {
return MapsFactory::globalInstance()->newCargoOutputBuilder();
}

}
154 changes: 154 additions & 0 deletions src/Map/CargoFormat/CargoOutputBuilder.php
@@ -0,0 +1,154 @@
<?php

declare( strict_types = 1 );

namespace Maps\Map\CargoFormat;

use CargoFieldDescription;
use DataValues\Geo\Values\LatLongValue;
use Maps\DataAccess\ImageRepository;
use Maps\Map\MapData;
use Maps\Map\MapOutput;
use Maps\Map\MapOutputBuilder;
use Maps\Map\Marker;
use Maps\Map\StructuredPopup;
use Maps\MappingService;
use Maps\MappingServices;
use ParamProcessor\ParamDefinitionFactory;
use ParamProcessor\Processor;

class CargoOutputBuilder {

private $outputBuilder;
private $services;
private $paramDefinitionFactory;
private $imageRepository;

public function __construct( MapOutputBuilder $outputBuilder, MappingServices $services,
ParamDefinitionFactory $paramDefinitionFactory, ImageRepository $imageRepository ) {

$this->outputBuilder = $outputBuilder;
$this->services = $services;
$this->paramDefinitionFactory = $paramDefinitionFactory;
$this->imageRepository = $imageRepository;
}

public function buildOutputFromCargoData( array $values, array $formattedValues, array $fieldDescriptions, array $displayParams ): MapOutput {
$service = $this->services->getDefaultService();

$mapData = $service->newMapDataFromProcessingResult( $this->processParameters( $service, $displayParams ) );
$mapData->setMarkers( $this->createMarkers( $formattedValues, $fieldDescriptions, $mapData ) );

return $this->outputBuilder->buildOutput(
$service,
$mapData
);
}

private function createMarkers( array $formattedValues, array $fieldDescriptions, MapData $mapData ): array {
$markers = $this->cargoValuesToMarkers(
$formattedValues,
$fieldDescriptions
);

$this->setIconUrl( $markers, $mapData->getParameters()['icon'] );

return $markers;
}

private function setIconUrl( array $markers, string $iconParameter ): void {
if ( $iconParameter !== '' ) {
$iconUrl = $this->imageRepository->getByName( $iconParameter )->getUrl();

foreach ( $markers as $marker ) {
$marker->setIconUrl( $iconUrl );
}
}
}

private function processParameters( MappingService $service, array $displayParams ) {
$processor = Processor::newDefault();

$processor->setParameters( $displayParams );
$processor->setParameterDefinitions( $this->getParameterDefinitions( $service ) );

return $processor->processParameters();
}

private function getParameterDefinitions( MappingService $service ): array {
return $this->paramDefinitionFactory->newDefinitionsFromArrays( $service->getParameterInfo() ) ;
}

/**
* @return Marker[]
*/
private function cargoValuesToMarkers( array $formattedValues, array $fieldDescriptions ): array {
$coordinateFields = $this->getCoordinateFieldNames( $fieldDescriptions );
$markers = [];

foreach ( $formattedValues as $valuesRow ) {
foreach ( $coordinateFields as $coordinateField ) {
$markers[] = $this->newMarker( $valuesRow, $coordinateField, $coordinateFields );
}
}

return $markers;
}

/**
* @param CargoFieldDescription[] $fieldDescriptions
* @return string[]
*/
private function getCoordinateFieldNames( array $fieldDescriptions ): array {
$names = [];

foreach ( $fieldDescriptions as $fieldName => $field ) {
if ( $field->mType === 'Coordinates' ) {
$names[] = str_replace( ' ', '_', $fieldName );
}
}

return $names;
}

private function newMarker( array $valuesRow, string $coordinateField, array $coordinateFields ): Marker {
$marker = new Marker( new LatLongValue(
(float)$valuesRow[$coordinateField . ' lat'],
(float)$valuesRow[$coordinateField . ' lon']
) );

$marker->setText(
( new StructuredPopup(
array_shift( $valuesRow ) ?? '',
$this->getPropertyValuesToDisplay( $valuesRow, $coordinateFields )
) )->getHtml()
);

return $marker;
}

private function getPropertyValuesToDisplay( array $valuesRow, array $coordinateFields ): array {
$propertyValues = [];

foreach ( $valuesRow as $name => $value ) {
if ( $this->shouldDisplayValue( $name, $coordinateFields ) ) {
$propertyValues[$name] = $value;
}
}

return $propertyValues;
}

private function shouldDisplayValue( string $name, array $coordinateFields ): bool {
$hiddenFields = [];

foreach ( $coordinateFields as $field ) {
$hiddenFields[] = $field;
$hiddenFields[] = $field . ' lat';
$hiddenFields[] = $field . ' lon';
}

return !in_array( $name, $hiddenFields );
}

}
Expand Up @@ -2,7 +2,7 @@

declare( strict_types = 1 );

namespace Maps\MediaWiki\ParserHooks;
namespace Maps\Map\DisplayMap;

use Maps;
use Maps\MappingService;
Expand Down Expand Up @@ -62,10 +62,14 @@ public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters

$this->trackMap( $parser );

return $this->renderer->renderMap(
$service->processingResultToMapParams( $processor->processParameters() ),
$mapOutput = $this->renderer->renderMap(
$service->newMapDataFromProcessingResult( $processor->processParameters() ),
$parser
);

$mapOutput->addResourcesToParserOutput( $parser->getOutput() );

return $mapOutput->getHtml();
}

/**
Expand All @@ -76,7 +80,7 @@ public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters
* @return string
* @throws MWException
*/
public function getMapHtmlForParameterList( Parser $parser, array $parameters ) {
public function getMapHtmlForParameterList( Parser $parser, array $parameters ): string {
$processor = new Processor( new \ParamProcessor\Options() );

$service = $this->services->getServiceOrDefault( $this->extractServiceName( $parameters ) );
Expand All @@ -90,10 +94,14 @@ public function getMapHtmlForParameterList( Parser $parser, array $parameters )

$this->trackMap( $parser );

return $this->renderer->renderMap(
$service->processingResultToMapParams( $processor->processParameters() ),
$mapOutput = $this->renderer->renderMap(
$service->newMapDataFromProcessingResult( $processor->processParameters() ),
$parser
);

$mapOutput->addResourcesToParserOutput( $parser->getOutput() );

return $mapOutput->getHtml();
}

private function getAllParameterDefinitions( MappingService $service, string $locationDelimiter ) {
Expand Down

0 comments on commit 81538ee

Please sign in to comment.