mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-09 08:43:13 +08:00
Added dependency on shlinkio/shlink-importer
This commit is contained in:
@@ -10,6 +10,7 @@ use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Resolver;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
|
||||
|
||||
return [
|
||||
|
||||
@@ -42,6 +43,12 @@ return [
|
||||
Resolver\PersistenceDomainResolver::class => ConfigAbstractFactory::class,
|
||||
|
||||
Mercure\MercureUpdatesGenerator::class => ConfigAbstractFactory::class,
|
||||
|
||||
Importer\ImportedLinksProcessor::class => ConfigAbstractFactory::class,
|
||||
],
|
||||
|
||||
'aliases' => [
|
||||
ImportedLinksProcessorInterface::class => Importer\ImportedLinksProcessor::class,
|
||||
],
|
||||
],
|
||||
|
||||
@@ -96,6 +103,8 @@ return [
|
||||
Resolver\PersistenceDomainResolver::class => ['em'],
|
||||
|
||||
Mercure\MercureUpdatesGenerator::class => ['config.url_shortener.domain'],
|
||||
|
||||
Importer\ImportedLinksProcessor::class => ['em', Resolver\PersistenceDomainResolver::class],
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
@@ -14,6 +14,8 @@ use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
|
||||
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Importer\Model\ShlinkUrl;
|
||||
|
||||
use function count;
|
||||
use function Shlinkio\Shlink\Core\generateRandomShortCode;
|
||||
@@ -33,6 +35,7 @@ class ShortUrl extends AbstractEntity
|
||||
private ?Domain $domain = null;
|
||||
private bool $customSlugWasProvided;
|
||||
private int $shortCodeLength;
|
||||
private ?string $source = null;
|
||||
|
||||
public function __construct(
|
||||
string $longUrl,
|
||||
@@ -54,6 +57,27 @@ class ShortUrl extends AbstractEntity
|
||||
$this->domain = ($domainResolver ?? new SimpleDomainResolver())->resolveDomain($meta->getDomain());
|
||||
}
|
||||
|
||||
public static function fromImport(
|
||||
ShlinkUrl $url,
|
||||
string $source,
|
||||
bool $importShortCode,
|
||||
?DomainResolverInterface $domainResolver = null
|
||||
): self {
|
||||
$meta = [
|
||||
ShortUrlMetaInputFilter::DOMAIN => $url->domain(),
|
||||
ShortUrlMetaInputFilter::VALIDATE_URL => false,
|
||||
];
|
||||
if ($importShortCode) {
|
||||
$meta[ShortUrlMetaInputFilter::CUSTOM_SLUG] = $url->shortCode();
|
||||
}
|
||||
|
||||
$instance = new self($url->longUrl(), ShortUrlMeta::fromRawData($meta), $domainResolver);
|
||||
$instance->source = $source;
|
||||
$instance->dateCreated = Chronos::instance($url->createdAt());
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function getLongUrl(): string
|
||||
{
|
||||
return $this->longUrl;
|
||||
|
||||
55
module/Core/src/Importer/ImportedLinksProcessor.php
Normal file
55
module/Core/src/Importer/ImportedLinksProcessor.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Importer;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
||||
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
|
||||
use Shlinkio\Shlink\Importer\Model\ShlinkUrl;
|
||||
|
||||
class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||
{
|
||||
use TagManagerTrait;
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
private DomainResolverInterface $domainResolver;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, DomainResolverInterface $domainResolver)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->domainResolver = $domainResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ShlinkUrl[] $shlinkUrls
|
||||
*/
|
||||
public function process(iterable $shlinkUrls, string $source, array $params): void
|
||||
{
|
||||
$importShortCodes = $params['import_short_codes'];
|
||||
$count = 0;
|
||||
$persistBlock = 100;
|
||||
|
||||
foreach ($shlinkUrls as $url) {
|
||||
$count++;
|
||||
|
||||
$shortUrl = ShortUrl::fromImport($url, $source, $importShortCodes, $this->domainResolver);
|
||||
$shortUrl->setTags($this->tagNamesToEntities($this->em, $url->tags()));
|
||||
|
||||
// TODO Handle errors while creating short URLs, to avoid making the whole process fail
|
||||
$this->em->persist($shortUrl);
|
||||
|
||||
// Flush and clear after X iterations
|
||||
if ($count % $persistBlock === 0) {
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user