-
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 (#…
…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
1 parent
6348b10
commit 00946af
Showing
23 changed files
with
1,276 additions
and
15 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
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,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(); | ||
} | ||
} | ||
} |
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
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.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', | ||
], | ||
], | ||
]; |
Oops, something went wrong.