From 1ed6458b39dad70b41a76fdc46f4bec195c0caea Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 2 Oct 2021 09:32:04 +0200 Subject: [PATCH] Added forwardQuery property in short URLs, that determines if the query should be forwarded to the long URL --- data/migrations/Version20211002072605.php | 26 +++++++++++++++++++ .../Shlinkio.Shlink.Core.Entity.ShortUrl.php | 5 ++++ module/Core/src/Entity/ShortUrl.php | 6 +++++ .../Helper/ShortUrlRedirectionBuilder.php | 3 ++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 data/migrations/Version20211002072605.php diff --git a/data/migrations/Version20211002072605.php b/data/migrations/Version20211002072605.php new file mode 100644 index 00000000..5f8db987 --- /dev/null +++ b/data/migrations/Version20211002072605.php @@ -0,0 +1,26 @@ +getTable('short_urls'); + $this->skipIf($shortUrls->hasColumn('forward_query')); + $shortUrls->addColumn('forward_query', Types::BOOLEAN, ['default' => true]); + } + + public function down(Schema $schema): void + { + $shortUrls = $schema->getTable('short_urls'); + $this->skipIf(! $shortUrls->hasColumn('forward_query')); + $shortUrls->dropColumn('forward_query'); + } +} diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php index a9269d36..83fd7e79 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php @@ -100,4 +100,9 @@ return static function (ClassMetadata $metadata, array $emConfig): void { ->columnName('crawlable') ->option('default', false) ->build(); + + $builder->createField('forwardQuery', Types::BOOLEAN) + ->columnName('forward_query') + ->option('default', true) + ->build(); }; diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 78527115..210310f5 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -43,6 +43,7 @@ class ShortUrl extends AbstractEntity private ?string $title = null; private bool $titleWasAutoResolved = false; private bool $crawlable = false; + private bool $forwardQuery = true; private function __construct() { @@ -207,6 +208,11 @@ class ShortUrl extends AbstractEntity return $this->crawlable; } + public function forwardQuery(): bool + { + return $this->forwardQuery; + } + public function update( ShortUrlEdit $shortUrlEdit, ?ShortUrlRelationResolverInterface $relationResolver = null, diff --git a/module/Core/src/ShortUrl/Helper/ShortUrlRedirectionBuilder.php b/module/Core/src/ShortUrl/Helper/ShortUrlRedirectionBuilder.php index f83a7eb9..3251922d 100644 --- a/module/Core/src/ShortUrl/Helper/ShortUrlRedirectionBuilder.php +++ b/module/Core/src/ShortUrl/Helper/ShortUrlRedirectionBuilder.php @@ -21,9 +21,10 @@ class ShortUrlRedirectionBuilder implements ShortUrlRedirectionBuilderInterface public function buildShortUrlRedirect(ShortUrl $shortUrl, array $currentQuery, ?string $extraPath = null): string { $uri = Uri::createFromString($shortUrl->getLongUrl()); + $shouldForwardQuery = $shortUrl->forwardQuery(); return $uri - ->withQuery($this->resolveQuery($uri, $currentQuery)) + ->withQuery($shouldForwardQuery ? $this->resolveQuery($uri, $currentQuery) : $uri->getQuery()) ->withPath($this->resolvePath($uri, $extraPath)) ->__toString(); }