mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Improved how existing imported short URLs are checked by tracking its original short code
This commit is contained in:
@@ -36,6 +36,7 @@ class ShortUrl extends AbstractEntity
|
||||
private bool $customSlugWasProvided;
|
||||
private int $shortCodeLength;
|
||||
private ?string $importSource = null;
|
||||
private ?string $importOriginalShortCode = null;
|
||||
|
||||
public function __construct(
|
||||
string $longUrl,
|
||||
@@ -72,6 +73,7 @@ class ShortUrl extends AbstractEntity
|
||||
|
||||
$instance = new self($url->longUrl(), ShortUrlMeta::fromRawData($meta), $domainResolver);
|
||||
$instance->importSource = $url->source();
|
||||
$instance->importOriginalShortCode = $url->shortCode();
|
||||
$instance->dateCreated = Chronos::instance($url->createdAt());
|
||||
|
||||
return $instance;
|
||||
|
||||
@@ -50,7 +50,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||
$longUrl = $url->longUrl();
|
||||
|
||||
// Skip already imported URLs
|
||||
if ($shortUrlRepo->importedUrlExists($url, $importShortCodes)) {
|
||||
if ($shortUrlRepo->importedUrlExists($url)) {
|
||||
$io->text(sprintf('%s: <comment>Skipped</comment>', $longUrl));
|
||||
continue;
|
||||
}
|
||||
@@ -58,7 +58,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||
$shortUrl = ShortUrl::fromImport($url, $importShortCodes, $this->domainResolver);
|
||||
$shortUrl->setTags($this->tagNamesToEntities($this->em, $url->tags()));
|
||||
|
||||
if (! $this->handleShortcodeUniqueness($url, $shortUrl, $io, $importShortCodes)) {
|
||||
if (! $this->handleShortCodeUniqueness($url, $shortUrl, $io, $importShortCodes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function handleShortcodeUniqueness(
|
||||
private function handleShortCodeUniqueness(
|
||||
ImportedShlinkUrl $url,
|
||||
ShortUrl $shortUrl,
|
||||
StyleInterface $io,
|
||||
@@ -90,6 +90,6 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->handleShortcodeUniqueness($url, $shortUrl, $io, false);
|
||||
return $this->handleShortCodeUniqueness($url, $shortUrl, $io, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,13 +190,7 @@ DQL;
|
||||
->setParameter('slug', $slug)
|
||||
->setMaxResults(1);
|
||||
|
||||
if ($domain !== null) {
|
||||
$qb->join('s.domain', 'd')
|
||||
->andWhere($qb->expr()->eq('d.authority', ':authority'))
|
||||
->setParameter('authority', $domain);
|
||||
} else {
|
||||
$qb->andWhere($qb->expr()->isNull('s.domain'));
|
||||
}
|
||||
$this->whereDomainIs($qb, $domain);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
@@ -256,15 +250,31 @@ DQL;
|
||||
return $qb->getQuery()->getOneOrNullResult();
|
||||
}
|
||||
|
||||
public function importedUrlExists(ImportedShlinkUrl $url, bool $importShortCodes): bool
|
||||
public function importedUrlExists(ImportedShlinkUrl $url): bool
|
||||
{
|
||||
$findConditions = ['importSource' => $url->source()];
|
||||
if ($importShortCodes) {
|
||||
$findConditions['shortCode'] = $url->shortCode();
|
||||
} else {
|
||||
$findConditions['longUrl'] = $url->longUrl();
|
||||
}
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->select('COUNT(DISTINCT s.id)')
|
||||
->from(ShortUrl::class, 's')
|
||||
->andWhere($qb->expr()->eq('s.importOriginalShortCode', ':shortCode'))
|
||||
->setParameter('shortCode', $url->shortCode())
|
||||
->andWhere($qb->expr()->eq('s.importSource', ':importSource'))
|
||||
->setParameter('importSource', $url->source())
|
||||
->setMaxResults(1);
|
||||
|
||||
return $this->count($findConditions) > 0;
|
||||
$this->whereDomainIs($qb, $url->domain());
|
||||
|
||||
$result = (int) $qb->getQuery()->getSingleScalarResult();
|
||||
return $result > 0;
|
||||
}
|
||||
|
||||
private function whereDomainIs(QueryBuilder $qb, ?string $domain): void
|
||||
{
|
||||
if ($domain !== null) {
|
||||
$qb->join('s.domain', 'd')
|
||||
->andWhere($qb->expr()->eq('d.authority', ':authority'))
|
||||
->setParameter('authority', $domain);
|
||||
} else {
|
||||
$qb->andWhere($qb->expr()->isNull('s.domain'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,5 +32,5 @@ interface ShortUrlRepositoryInterface extends ObjectRepository
|
||||
|
||||
public function findOneMatching(string $url, array $tags, ShortUrlMeta $meta): ?ShortUrl;
|
||||
|
||||
public function importedUrlExists(ImportedShlinkUrl $url, bool $importShortCodes): bool;
|
||||
public function importedUrlExists(ImportedShlinkUrl $url): bool;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user