-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly handle latitude and longitude during Destination One import
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
1 parent
6348b10
commit 2c6ddda
Showing
17 changed files
with
1,131 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de> | ||
* | ||
* 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']; | ||
$matchingLocations = $this->getMatchingLocations( | ||
$locationObject->getGlobalId(), | ||
$uid | ||
); | ||
|
||
// Already have entries for the new id, this one is duplicate | ||
if ($matchingLocations !== []) { | ||
$duplicates[$uid] = $matchingLocations[0]; | ||
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 getMatchingLocations( | ||
string $globalId, | ||
int $uid | ||
): array { | ||
$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))); | ||
|
||
return $queryBuilder->execute()->fetchFirstColumn(); | ||
} | ||
|
||
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'], | ||
$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(); | ||
} | ||
|
||
/** | ||
* @param int[] $uids | ||
*/ | ||
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
Tests/Functional/Import/DestinationDataTest/Assertions/ImportsWithLocations.csv
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
Tests/Functional/Import/DestinationDataTest/Assertions/ImportsWithLocations.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.720971023258805', | ||
'longitude' => '11.335229873657227', | ||
'phone' => '+ 49 3672 / 486470', | ||
'global_id' => '2e49097eda43aa83a6d98e25416078f7552525088ee403a6de272cb10301baf6', | ||
], | ||
[ | ||
'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.720971023258805', | ||
'longitude' => '11.335229873657227', | ||
'phone' => '+ 49 3672 / 486470', | ||
'global_id' => '8b3bfd7f6cd0a9899dbfa6c2e80614b1fa6100710bb28c281f3e3dd31d9d1ef1', | ||
], | ||
], | ||
]; |
20 changes: 20 additions & 0 deletions
20
...unctional/Import/DestinationDataTest/Assertions/ImportsWithSingleLocationOnDuplicates.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
return [ | ||
'tx_events_domain_model_location' => [ | ||
[ | ||
'uid' => 1, | ||
'pid' => 2, | ||
'name' => 'Schillerhaus Rudolstadt', | ||
'street' => 'Schillerstraße 25', | ||
'district' => '', | ||
'city' => 'Rudolstadt', | ||
'zip' => '07407', | ||
'country' => 'Deutschland', | ||
'latitude' => '50.720971023258805', | ||
'longitude' => '11.335229873657227', | ||
'phone' => '+ 49 3672 / 486470', | ||
'global_id' => '2e49097eda43aa83a6d98e25416078f7552525088ee403a6de272cb10301baf6', | ||
], | ||
], | ||
]; |
Oops, something went wrong.