Skip to content

Commit

Permalink
Properly handle latitude and longitude during Destination One import (#…
Browse files Browse the repository at this point in the history
…32)

They sometimes use a different separator.
The code is adjusted to always use same separator and precision.

That will prevent the same location from showing up multiple times due
to different latitude and longitude values.
  • Loading branch information
DanielSiepmann authored Jul 6, 2023
1 parent 6348b10 commit 00946af
Show file tree
Hide file tree
Showing 23 changed files with 1,276 additions and 15 deletions.
29 changes: 25 additions & 4 deletions Classes/Domain/Model/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function __construct(
$this->district = $district;
$this->country = $country;
$this->phone = $phone;
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->latitude = $this->normalizeGeocoordinate($latitude);
$this->longitude = $this->normalizeGeocoordinate($longitude);
$this->_languageUid = $languageUid;

$this->globalId = $this->generateGlobalId();
Expand Down Expand Up @@ -132,6 +132,14 @@ public function getGlobalId(): string
return $this->globalId;
}

public function updateFromLocation(self $location): void
{
// Only updates values not being part of global id.
$this->phone = $location->getPhone();
$this->longitude = $location->getLongitude();
$this->latitude = $location->getLatitude();
}

/**
* Validates the location.
*
Expand All @@ -158,8 +166,21 @@ private function generateGlobalId(): string
$this->city,
$this->district,
$this->country,
$this->latitude,
$this->longitude,
]));
}

private function normalizeGeocoordinate(string $coordinate): string
{
$numberOfCommas = substr_count($coordinate, ',');
$numberOfPoints = substr_count($coordinate, '.');

if (
$numberOfCommas === 1
&& $numberOfPoints === 0
) {
$coordinate = str_replace(',', '.', $coordinate);
}

return number_format((float)$coordinate, 6, '.', '');
}
}
12 changes: 12 additions & 0 deletions Classes/Domain/Repository/LocationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
namespace Wrm\Events\Domain\Repository;

use TYPO3\CMS\Extbase\Persistence\Repository;
use Wrm\Events\Domain\Model\Location;

class LocationRepository extends Repository
{
public function findOneByGlobalId(string $globalId): ?Location
{
$query = $this->createQuery();

return $query
->matching($query->equals('globalId', $globalId))
->setLimit(1)
->execute()
->getFirst()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function getLocation(array $event): ?Location

$existingLocation = $this->repository->findOneByGlobalId($newLocation->getGlobalId());

return $existingLocation ?? $newLocation;
if ($existingLocation === null) {
return $newLocation;
}

$existingLocation->updateFromLocation($newLocation);

return $existingLocation;
}
}
204 changes: 204 additions & 0 deletions Classes/Updates/MigrateDuplicateLocations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

namespace Wrm\Events\Updates;

use Generator;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
use Wrm\Events\Domain\Model\Location;

final class MigrateDuplicateLocations implements UpgradeWizardInterface
{
/**
* @var ConnectionPool
*/
private $connectionPool;

public function __construct(
ConnectionPool $connectionPool
) {
$this->connectionPool = $connectionPool;
}

public function getIdentifier(): string
{
return self::class;
}

public function getTitle(): string
{
return 'Remove duplicate locations of EXT:event';
}

public function getDescription(): string
{
return 'Checks for duplicates and reduces them to one entry, fixing relations to events.';
}

public function updateNecessary(): bool
{
return true;
}

public function executeUpdate(): bool
{
$duplicates = [];

foreach ($this->getLocations() as $location) {
$locationObject = $this->buildObject($location);
if ($locationObject->getGlobalId() === $location['global_id']) {
continue;
}

$uid = (int)$location['uid'];
$matchingLocation = $this->getMatchingLocation(
$locationObject->getGlobalId(),
$uid
);

// Already have entries for the new id, this one is duplicate
if ($matchingLocation > 0) {
$duplicates[$uid] = $matchingLocation;
continue;
}

// No duplicates, update this one
$this->updateLocation($locationObject, $uid);
}

$this->removeDuplicates(array_keys($duplicates));
$this->updateRelations($duplicates);
return true;
}

public function getPrerequisites(): array
{
return [];
}

public static function register(): void
{
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][self::class] = self::class;
}

/**
* @return Generator<array>
*/
private function getLocations(): Generator
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_location');
$queryBuilder->select(
'name',
'street',
'zip',
'city',
'district',
'country',
'phone',
'latitude',
'longitude',
'global_id',
'uid',
'sys_language_uid'
);
$queryBuilder->from('tx_events_domain_model_location');
$queryBuilder->orderBy('uid', 'asc');
$result = $queryBuilder->execute();

foreach ($result->fetchAllAssociative() as $location) {
yield $location;
}
}

private function getMatchingLocation(
string $globalId,
int $uid
): int {
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_location');
$queryBuilder->select('uid');
$queryBuilder->from('tx_events_domain_model_location');
$queryBuilder->where($queryBuilder->expr()->eq('global_id', $queryBuilder->createNamedParameter($globalId)));
$queryBuilder->andWhere($queryBuilder->expr()->neq('uid', $queryBuilder->createNamedParameter($uid)));
$queryBuilder->setMaxResults(1);

$uid = $queryBuilder->execute()->fetchOne();
if (is_numeric($uid) === false) {
return 0;
}

return (int) $uid;
}

private function buildObject(array $location): Location
{
return new Location(
$location['name'],
$location['street'],
$location['zip'],
$location['city'],
$location['district'],
$location['country'],
$location['phone'],
$location['latitude'],
$location['longitude'],
(int)$location['sys_language_uid']
);
}

private function updateLocation(Location $location, int $uid): void
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_location');
$queryBuilder->update('tx_events_domain_model_location');
$queryBuilder->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid)));
$queryBuilder->set('global_id', $location->getGlobalId());
$queryBuilder->set('latitude', $location->getLatitude());
$queryBuilder->set('longitude', $location->getLongitude());
$queryBuilder->execute();
}

/**
* @param int[] $uids
*/
private function removeDuplicates(array $uids): void
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_location');
$queryBuilder->delete('tx_events_domain_model_location');
$queryBuilder->where($queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($uids, Connection::PARAM_INT_ARRAY)));
$queryBuilder->execute();
}

private function updateRelations(array $migration): void
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_event');
$queryBuilder->update('tx_events_domain_model_event');

foreach ($migration as $legacyLocationUid => $newLocationUid) {
$finalBuilder = clone $queryBuilder;
$finalBuilder->where($finalBuilder->expr()->eq('location', $finalBuilder->createNamedParameter($legacyLocationUid)));
$finalBuilder->set('location', $newLocationUid);
$finalBuilder->execute();
}
}
}
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ services:
identifier: 'WrmEventsAddSpecialPropertiesToDate'
event: TYPO3\CMS\Extbase\Event\Persistence\AfterObjectThawedEvent

Wrm\Events\Updates\MigrateDuplicateLocations:
public: true

Wrm\Events\Updates\MigrateOldLocations:
public: true

Expand Down
11 changes: 11 additions & 0 deletions Documentation/Changelog/3.4.0.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
3.4.0
=====

Manual steps
------------

* Determining global_id of locations has changed.
An Update Wizard is provided in order to migrate existing data.
This is only necessary when using the import of locations from Destination One.

Breaking
--------

Expand Down Expand Up @@ -74,6 +81,10 @@ Fixes
Those might not exist in newer systems where migration is not necessary.
The wizard now properly checks for existence before querying the data.

* Prevent duplicate location entries from Destination One import.
They seem to differ in writing of latitude and longitude.
An update wizard is provided to clean up existing duplicates.

Tasks
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Es gilt die 2G-PLUS-Regel.",,,,,,,"1","2",,"8","3",,"1",,3,"adventliche-orgelmus
,"13","2","0","0","0","0",-1,0,"0","0","0","3","1671732000","1671735600","no","0",,"adventliche-orgelmusik-orgel-kmd-frank-bettenhausen-2022-12-22t18-00-00",,,,,,,,,,,,,,,,,,
"tx_events_domain_model_location",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,"uid","pid","cruser_id","hidden","starttime","endtime","sys_language_uid","l10n_parent","t3ver_oid","t3ver_wsid","t3ver_state","name","street","district","city","zip","country","latitude","longitude","phone",,,,,,,,,,,,,,,,
,"1","2","0","0","0","0",-1,0,"0","0","0","Schillerhaus Rudolstadt","Schillerstraße 25",,"Rudolstadt","07407","Deutschland","50.720971023259","11.335229873657","+ 49 3672 / 486470",,,,,,,,,,,,,,,,
,"2","2","0","0","0","0",-1,0,"0","0","0","Stadtbibliothek Rudolstadt","Schulplatz 13",,"Rudolstadt","07407","Deutschland","50.720835175056","11.342568397522","0 36 72 - 48 64 20",,,,,,,,,,,,,,,,
,"3","2","0","0","0","0",-1,0,"0","0","0","Lutherkirche","Caspar-Schulte-Straße",,"Rudolstadt","07407","Deutschland","50.718688721183","11.327333450317","03672 - 48 96 13",,,,,,,,,,,,,,,,
,"1","2","0","0","0","0",-1,0,"0","0","0","Schillerhaus Rudolstadt","Schillerstraße 25",,"Rudolstadt","07407","Deutschland","50.720971","11.335230","+ 49 3672 / 486470",,,,,,,,,,,,,,,,
,"2","2","0","0","0","0",-1,0,"0","0","0","Stadtbibliothek Rudolstadt","Schulplatz 13",,"Rudolstadt","07407","Deutschland","50.720835","11.342568","0 36 72 - 48 64 20",,,,,,,,,,,,,,,,
,"3","2","0","0","0","0",-1,0,"0","0","0","Lutherkirche","Caspar-Schulte-Straße",,"Rudolstadt","07407","Deutschland","50.718689","11.327333","03672 - 48 96 13",,,,,,,,,,,,,,,,
"sys_category",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,"uid","pid","cruser_id","hidden","starttime","endtime","sys_language_uid","l10n_parent","title","items","parent",,,,,,,,,,,,,,,,,,,,,,,,,
,1,2,0,0,0,0,0,0,"Top Category",0,0,,,,,,,,,,,,,,,,,,,,,,,,,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

return [
'tx_events_domain_model_location' => [
[
'uid' => 1,
'pid' => 2,
'cruser_id' => 0,
'hidden' => 0,
'starttime' => 0,
'endtime' => 0,
'sys_language_uid' => -1,
'l10n_parent' => 0,
't3ver_oid' => 0,
't3ver_wsid' => 0,
't3ver_state' => 0,
'name' => 'Schillerhaus Rudolstadt',
'street' => 'Schillerstraße 25',
'district' => '',
'city' => 'Rudolstadt',
'zip' => '07407',
'country' => 'Deutschland',
'latitude' => '50.720971',
'longitude' => '11.335230',
'phone' => '+ 49 3672 / 486470',
'global_id' => '2c898a5e8f751906124a6eb6384a3b111754db37d3330b169ec022b98a0ddcbe',
],
[
'uid' => 2,
'pid' => 2,
'cruser_id' => 0,
'hidden' => 0,
'starttime' => 0,
'endtime' => 0,
'sys_language_uid' => -1,
'l10n_parent' => 0,
't3ver_oid' => 0,
't3ver_wsid' => 0,
't3ver_state' => 0,
'name' => 'Schillerhaus Rudolstadt',
'street' => 'Other address',
'district' => '',
'city' => 'Rudolstadt',
'zip' => '07407',
'country' => 'Deutschland',
'latitude' => '50.720971',
'longitude' => '11.335230',
'phone' => '+ 49 3672 / 486470',
'global_id' => 'c0df4e7901836412bf6e45289cf749f60b86cf8405aa23767b5e18890fa4cf30',
],
],
];
Loading

0 comments on commit 00946af

Please sign in to comment.