Merge pull request #2099 from shlinkio/develop

Release 4.1.0
This commit is contained in:
Alejandro Celaya 2024-04-14 09:12:56 +02:00 committed by GitHub
commit 35508e253d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
134 changed files with 4998 additions and 3079 deletions

View file

@ -23,7 +23,7 @@ jobs:
run: sudo ./data/infra/ci/install-ms-odbc.sh
- name: Start database server
if: ${{ inputs.platform != 'sqlite:ci' }}
run: docker-compose -f docker-compose.yml -f docker-compose.ci.yml up -d shlink_db_${{ inputs.platform }}
run: docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d shlink_db_${{ inputs.platform }}
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}
@ -31,7 +31,7 @@ jobs:
extensions-cache-key: db-tests-extensions-${{ matrix.php-version }}-${{ inputs.platform }}
- name: Create test database
if: ${{ inputs.platform == 'ms' }}
run: docker-compose exec -T shlink_db_ms /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'Passw0rd!' -Q "CREATE DATABASE shlink_test;"
run: docker compose exec -T shlink_db_ms /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'Passw0rd!' -Q "CREATE DATABASE shlink_test;"
- name: Run tests
run: composer test:db:${{ inputs.platform }}
- name: Upload code coverage

View file

@ -20,10 +20,10 @@ jobs:
- uses: actions/checkout@v4
- name: Start postgres database server
if: ${{ inputs.test-group == 'api' }}
run: docker-compose -f docker-compose.yml -f docker-compose.ci.yml up -d shlink_db_postgres
run: docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d shlink_db_postgres
- name: Start maria database server
if: ${{ inputs.test-group == 'cli' }}
run: docker-compose -f docker-compose.yml -f docker-compose.ci.yml up -d shlink_db_maria
run: docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d shlink_db_maria
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
use Mezzio\Application;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Middleware\RequestIdMiddleware;
use Shlinkio\Shlink\EventDispatcher\RoadRunner\RoadRunnerTaskConsumerToListener;
use Spiral\RoadRunner\Http\PSR7Worker;
@ -27,6 +28,9 @@ use function Shlinkio\Shlink\Config\env;
}
}
} else {
$container->get(RoadRunnerTaskConsumerToListener::class)->listenForTasks();
$requestIdMiddleware = $container->get(RequestIdMiddleware::class);
$container->get(RoadRunnerTaskConsumerToListener::class)->listenForTasks(
fn (string $requestId) => $requestIdMiddleware->setCurrentRequestId($requestId),
);
}
})();

View file

@ -43,11 +43,11 @@
"pugx/shortid-php": "^1.1",
"ramsey/uuid": "^4.7",
"shlinkio/doctrine-specification": "^2.1.1",
"shlinkio/shlink-common": "^6.0",
"shlinkio/shlink-common": "^6.1",
"shlinkio/shlink-config": "^3.0",
"shlinkio/shlink-event-dispatcher": "^4.0",
"shlinkio/shlink-importer": "^5.3",
"shlinkio/shlink-installer": "^9.0",
"shlinkio/shlink-event-dispatcher": "^4.1",
"shlinkio/shlink-importer": "^5.3.2",
"shlinkio/shlink-installer": "^9.1",
"shlinkio/shlink-ip-geolocation": "^4.0",
"shlinkio/shlink-json": "^1.1",
"spiral/roadrunner": "^2023.3",
@ -118,17 +118,21 @@
"@parallel test:unit test:db",
"@parallel test:api test:cli"
],
"test:unit": "@php vendor/bin/phpunit --order-by=random --colors=always --testdox",
"test:unit": "COLUMNS=120 vendor/bin/phpunit --order-by=random --colors=always --testdox",
"test:unit:ci": "@test:unit --coverage-php=build/coverage-unit.cov",
"test:unit:pretty": "@test:unit --coverage-html build/coverage-unit/coverage-html",
"test:db": "@parallel test:db:sqlite:ci test:db:mysql test:db:maria test:db:postgres test:db:ms",
"test:db:sqlite": "APP_ENV=test php vendor/bin/phpunit --order-by=random --colors=always --testdox -c phpunit-db.xml",
"test:db:sqlite:ci": "@test:db:sqlite --coverage-php build/coverage-db.cov",
"test:db:mysql": "DB_DRIVER=mysql composer test:db:sqlite",
"test:db:maria": "DB_DRIVER=maria composer test:db:sqlite",
"test:db:postgres": "DB_DRIVER=postgres composer test:db:sqlite",
"test:db:ms": "DB_DRIVER=mssql composer test:db:sqlite",
"test:db:mysql": "DB_DRIVER=mysql composer test:db:sqlite -- $*",
"test:db:maria": "DB_DRIVER=maria composer test:db:sqlite -- $*",
"test:db:postgres": "DB_DRIVER=postgres composer test:db:sqlite -- $*",
"test:db:ms": "DB_DRIVER=mssql composer test:db:sqlite -- $*",
"test:api": "bin/test/run-api-tests.sh",
"test:api:sqlite": "DB_DRIVER=sqlite composer test:api -- $*",
"test:api:mysql": "DB_DRIVER=mysql composer test:api -- $*",
"test:api:maria": "DB_DRIVER=maria composer test:api -- $*",
"test:api:mssql": "DB_DRIVER=mssql composer test:api -- $*",
"test:api:ci": "GENERATE_COVERAGE=yes composer test:api && vendor/bin/phpcov merge build/coverage-api --php build/coverage-api.cov && rm build/coverage-api/*.cov",
"test:api:pretty": "GENERATE_COVERAGE=yes composer test:api && vendor/bin/phpcov merge build/coverage-api --html build/coverage-api/coverage-html && rm build/coverage-api/*.cov",
"test:cli": "APP_ENV=test DB_DRIVER=maria TEST_ENV=cli php vendor/bin/phpunit --order-by=random --colors=always --testdox -c phpunit-cli.xml",

View file

@ -2,8 +2,11 @@
declare(strict_types=1);
use Doctrine\ORM\Events;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Core\Config\EnvVars;
use Shlinkio\Shlink\Core\Visit\Listener\OrphanVisitsCountTracker;
use Shlinkio\Shlink\Core\Visit\Listener\ShortUrlVisitsCountTracker;
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
@ -60,6 +63,10 @@ return (static function (): array {
'proxies_dir' => 'data/proxies',
'load_mappings_using_functional_style' => true,
'default_repository_classname' => EntitySpecificationRepository::class,
'listeners' => [
Events::onFlush => [ShortUrlVisitsCountTracker::class, OrphanVisitsCountTracker::class],
Events::postFlush => [ShortUrlVisitsCountTracker::class, OrphanVisitsCountTracker::class],
],
],
'connection' => $resolveConnection(),
],

View file

@ -12,6 +12,7 @@ return [
'installer' => [
'enabled_options' => [
Option\Server\RuntimeConfigOption::class,
Option\Server\MemoryLimitConfigOption::class,
Option\Database\DatabaseDriverConfigOption::class,
Option\Database\DatabaseNameConfigOption::class,
Option\Database\DatabaseHostConfigOption::class,

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink;
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Monolog\Level;
use Monolog\Logger;
@ -13,6 +14,8 @@ use Shlinkio\Shlink\Common\Logger\LoggerFactory;
use Shlinkio\Shlink\Common\Logger\LoggerType;
use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware;
use Shlinkio\Shlink\Common\Middleware\RequestIdMiddleware;
use Shlinkio\Shlink\Core\EventDispatcher\Helper\RequestIdProvider;
use Shlinkio\Shlink\EventDispatcher\Util\RequestIdProviderInterface;
use function Shlinkio\Shlink\Config\runningInRoadRunner;
@ -44,14 +47,20 @@ return (static function (): array {
'Logger_Shlink' => [LoggerFactory::class, 'Shlink'],
'Logger_Access' => [LoggerFactory::class, 'Access'],
NullLogger::class => InvokableFactory::class,
RequestIdProvider::class => ConfigAbstractFactory::class,
],
'aliases' => [
'logger' => 'Logger_Shlink',
Logger::class => 'Logger_Shlink',
LoggerInterface::class => 'Logger_Shlink',
AccessLogMiddleware::LOGGER_SERVICE_NAME => 'Logger_Access',
RequestIdProviderInterface::class => RequestIdProvider::class,
],
],
ConfigAbstractFactory::class => [
RequestIdProvider::class => [RequestIdMiddleware::class],
],
];
})();

View file

@ -7,7 +7,7 @@ return [
'rabbitmq' => [
'enabled' => true,
'host' => 'shlink_rabbitmq',
'port' => '5673',
'port' => '5672',
'user' => 'rabbit',
'password' => 'rabbit',
],

View file

@ -12,6 +12,8 @@ chdir(dirname(__DIR__));
require 'vendor/autoload.php';
// Set a default memory limit, but allow custom values
ini_set('memory_limit', EnvVars::MEMORY_LIMIT->loadFromEnv('512M'));
// This is one of the first files loaded. Configure the timezone here
date_default_timezone_set(EnvVars::TIMEZONE->loadFromEnv(date_default_timezone_get()));

View file

@ -1,6 +1,5 @@
display_errors=On
error_reporting=-1
memory_limit=-1
log_errors_max_len=0
zend.assertions=1
assert.exception=1

View file

@ -169,7 +169,7 @@ services:
shlink_matomo:
container_name: shlink_matomo
image: matomo:4.15-apache
image: matomo:5.0-apache
ports:
- "8003:80"
volumes:

View file

@ -1,4 +1,3 @@
log_errors_max_len=0
zend.assertions=1
assert.exception=1
memory_limit=512M

View file

@ -232,6 +232,11 @@
"potentialBot": {
"type": "boolean",
"description": "Tells if Shlink thinks this visit comes potentially from a bot or crawler"
},
"visitedUrl": {
"type": "string",
"nullable": true,
"description": "The originally visited URL that triggered the tracking of this visit"
}
},
"example": {
@ -247,7 +252,8 @@
"regionName": "California",
"timezone": "America/Los_Angeles"
},
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
}
},
"OrphanVisit": {
@ -256,11 +262,6 @@
{
"type": "object",
"properties": {
"visitedUrl": {
"type": "string",
"nullable": true,
"description": "The originally visited URL that triggered the tracking of this visit"
},
"type": {
"type": "string",
"enum": [

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,912 @@
# CHANGELOG 2.x
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
## [2.10.3] - 2022-01-23
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1349](https://github.com/shlinkio/shlink/issues/1349) Fixed memory leak in cache implementation.
## [2.10.2] - 2022-01-07
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1293](https://github.com/shlinkio/shlink/issues/1293) Fixed error when trying to create/import short URLs with a too long title.
* [#1306](https://github.com/shlinkio/shlink/issues/1306) Ensured remote IP address is not logged when using swoole/openswoole.
* [#1308](https://github.com/shlinkio/shlink/issues/1308) Fixed memory leak when using redis due to the amount of non-expiring keys created by doctrine. Now they have a 24h expiration by default.
## [2.10.1] - 2021-12-21
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1285](https://github.com/shlinkio/shlink/issues/1285) Fixed error caused by database connections expiring after some hours of inactivity.
* [#1286](https://github.com/shlinkio/shlink/issues/1286) Fixed `x-request-id` header not being generated during non-rest requests.
## [2.10.0] - 2021-12-12
### Added
* [#1163](https://github.com/shlinkio/shlink/issues/1163) Allowed setting not-found redirects for default domain in the same way it's done for any other domain.
This implies a few non-breaking changes:
* The domains list no longer has the values of `INVALID_SHORT_URL_REDIRECT_TO`, `REGULAR_404_REDIRECT_TO` and `BASE_URL_REDIRECT_TO` on the default domain redirects.
* The `GET /domains` endpoint includes a new `defaultRedirects` property in the response, with the default redirects set via config or env vars.
* The `INVALID_SHORT_URL_REDIRECT_TO`, `REGULAR_404_REDIRECT_TO` and `BASE_URL_REDIRECT_TO` env vars are now deprecated, and should be replaced by `DEFAULT_INVALID_SHORT_URL_REDIRECT`, `DEFAULT_REGULAR_404_REDIRECT` and `DEFAULT_BASE_URL_REDIRECT` respectively. Deprecated ones will continue to work until v3.0.0, where they will be removed.
* [#868](https://github.com/shlinkio/shlink/issues/868) Added support to publish real-time updates in a RabbitMQ server.
Shlink will create new exchanges and queues for every topic documented in the [Async API spec](https://api-spec.shlink.io/async-api/), meaning, you will have one queue for orphan visits, one for regular visits, and one queue for every short URL with its visits.
The RabbitMQ server config can be provided via installer config options, or via environment variables.
* [#1204](https://github.com/shlinkio/shlink/issues/1204) Added support for `openswoole` and migrated official docker image to `openswoole`.
* [#1242](https://github.com/shlinkio/shlink/issues/1242) Added support to import urls and visits from YOURLS.
In order to do it, you need to first install this [dedicated plugin](https://slnk.to/yourls-import) in YOURLS, and then run the `short-url:import yourls` command, as with any other source.
* [#1235](https://github.com/shlinkio/shlink/issues/1235) Added support to disable rounding QR codes block sizing via config option, env var or query param.
* [#1188](https://github.com/shlinkio/shlink/issues/1188) Added support for PHP 8.1.
The official docker image has also been updated to use PHP 8.1 by default.
### Changed
* [#844](https://github.com/shlinkio/shlink/issues/844) Added mutation checks to API tests.
* [#1218](https://github.com/shlinkio/shlink/issues/1218) Updated to symfony/mercure 0.6.
* [#1223](https://github.com/shlinkio/shlink/issues/1223) Updated to phpstan 1.0.
* [#1258](https://github.com/shlinkio/shlink/issues/1258) Updated to Symfony 6 components, except symfony/console.
* Added `domain` field to `DeleteShortUrlException` exception.
### Deprecated
* [#1260](https://github.com/shlinkio/shlink/issues/1260) Deprecated `USE_HTTPS` env var that was added in previous release, in favor of the new `IS_HTTPS_ENABLED`.
The old one proved to be confusing and misleading, making people think it was used to actually enable HTTPS transparently, instead of its actual purpose, which is just telling Shlink it is being served with HTTPS.
### Removed
* *Nothing*
### Fixed
* [#1206](https://github.com/shlinkio/shlink/issues/1206) Fixed debugging of the docker image, so that it does not run the commands with `-q` when the `SHELL_VERBOSITY` env var has been provided.
* [#1254](https://github.com/shlinkio/shlink/issues/1254) Fixed examples in swagger docs.
## [2.9.3] - 2021-11-15
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1232](https://github.com/shlinkio/shlink/issues/1232) Solved potential SQL injection by enforcing `doctrine/dbal` 3.1.4.
## [2.9.2] - 2021-10-23
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1210](https://github.com/shlinkio/shlink/issues/1210) Fixed real time updates not being notified due to an incorrect handling of db transactions on multi-process tasks.
* [#1211](https://github.com/shlinkio/shlink/issues/1211) Fixed `There is no active transaction` error when running migrations in MySQL/Mariadb after updating to doctrine-migrations 3.3.
* [#1197](https://github.com/shlinkio/shlink/issues/1197) Fixed amount of task workers provided via config option or env var not being validated to ensure enough workers to process all parallel tasks.
## [2.9.1] - 2021-10-11
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1201](https://github.com/shlinkio/shlink/issues/1201) Fixed crash when using the new `USE_HTTPS`, as it's boolean raw value was being used instead of resolving "https" or "http".
## [2.9.0] - 2021-10-10
### Added
* [#1015](https://github.com/shlinkio/shlink/issues/1015) Shlink now accepts configuration via env vars even when not using docker.
The config generated with the installing tool still has precedence over the env vars, so it cannot be combined. Either you use the tool, or use env vars.
* [#1149](https://github.com/shlinkio/shlink/issues/1149) Allowed to set custom defaults for the QR codes.
* [#1112](https://github.com/shlinkio/shlink/issues/1112) Added new option to define if the query string should be forwarded on a per-short URL basis.
The new `forwardQuery=true|false` param can be provided during short URL creation or edition, via REST API or CLI command, allowing to override the default behavior which makes the query string to always be forwarded.
* [#1105](https://github.com/shlinkio/shlink/issues/1105) Added support to define placeholders on not-found redirects, so that the redirected URL receives the originally visited path and/or domain.
Currently, `{DOMAIN}` and `{ORIGINAL_PATH}` placeholders are supported, and they can be used both in the redirected URL's path or query.
When they are used in the query, the values are URL encoded.
* [#1119](https://github.com/shlinkio/shlink/issues/1119) Added support to provide redis sentinel when using redis cache.
* [#1016](https://github.com/shlinkio/shlink/issues/1016) Added new option to send orphan visits to webhooks, via `NOTIFY_ORPHAN_VISITS_TO_WEBHOOKS` env var or installer tool.
The option is disabled by default, as the payload is backwards incompatible. You will need to adapt your webhooks to treat the `shortUrl` property as optional before enabling this option.
* [#1104](https://github.com/shlinkio/shlink/issues/1104) Added ability to disable tracking based on IP addresses.
IP addresses can be provided in the form of fixed addresses, CIDR blocks, or wildcard patterns (192.168.*.*).
### Changed
* [#1142](https://github.com/shlinkio/shlink/issues/1142) Replaced `doctrine/cache` package with `symfony/cache`.
* [#1157](https://github.com/shlinkio/shlink/issues/1157) All routes now support CORS, not only rest ones.
* [#1144](https://github.com/shlinkio/shlink/issues/1144) Added experimental builds under PHP 8.1.
### Deprecated
* [#1164](https://github.com/shlinkio/shlink/issues/1164) Deprecated `SHORT_DOMAIN_HOST` and `SHORT_DOMAIN_SCHEMA` env vars. Use `DEFAULT_DOMAIN` and `USE_HTTPS=true|false` instead.
### Removed
* *Nothing*
### Fixed
* [#1165](https://github.com/shlinkio/shlink/issues/1165) Fixed warning displayed when trying to locate visits and there are none pending.
* [#1172](https://github.com/shlinkio/shlink/pull/1172) Removed unneeded explicitly defined volumes in docker image.
## [2.8.1] - 2021-08-15
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1155](https://github.com/shlinkio/shlink/issues/1155) Fixed numeric query params in long URLs being replaced by `0`.
## [2.8.0] - 2021-08-04
### Added
* [#1089](https://github.com/shlinkio/shlink/issues/1089) Added new `ENABLE_PERIODIC_VISIT_LOCATE` env var to docker image which schedules the `visit:locate` command every hour when provided with value `true`.
* [#1082](https://github.com/shlinkio/shlink/issues/1082) Added support for error correction level on QR codes.
Now, when calling the `GET /{shorCode}/qr-code` URL, you can pass the `errorCorrection` query param with values `L` for Low, `M` for Medium, `Q` for Quartile or `H` for High.
* [#1080](https://github.com/shlinkio/shlink/issues/1080) Added support to redirect to URLs as soon as the path starts with a valid short code, appending the rest of the path to the redirected long URL.
With this, if you have the `https://example.com/abc123` short URL redirecting to `https://www.twitter.com`, a visit to `https://example.com/abc123/shlinkio` will take you to `https://www.twitter.com/shlinkio`.
This behavior needs to be actively opted in, via installer config options or env vars.
* [#943](https://github.com/shlinkio/shlink/issues/943) Added support to define different "not-found" redirects for every domain handled by Shlink.
Shlink will continue to allow defining the default values via env vars or config, but afterwards, you can use the `domain:redirects` command or the `PATCH /domains/redirects` REST endpoint to define specific values for every single domain.
### Changed
* [#1118](https://github.com/shlinkio/shlink/issues/1118) Increased phpstan required level to 8.
* [#1127](https://github.com/shlinkio/shlink/issues/1127) Updated to infection 0.24.
* [#1139](https://github.com/shlinkio/shlink/issues/1139) Updated project dependencies, including base docker image to use PHP 8.0.9 and Alpine 3.14.
### Deprecated
* *Nothing*
### Removed
* [#1046](https://github.com/shlinkio/shlink/issues/1046) Dropped support for PHP 7.4.
### Fixed
* [#1098](https://github.com/shlinkio/shlink/issues/1098) Fixed errors when using Redis for caching, caused by some third party lib bug that was fixed on dependencies update.
## [2.7.3] - 2021-08-02
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1135](https://github.com/shlinkio/shlink/issues/1135) Fixed error when importing short URLs with no visits from another Shlink instance.
* [#1136](https://github.com/shlinkio/shlink/issues/1136) Fixed error when fetching tag/short-url/orphan visits for a page lower than 1.
## [2.7.2] - 2021-07-30
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1128](https://github.com/shlinkio/shlink/issues/1128) Increased memory limit reserved for the docker image, preventing it from crashing on GeoLite db download.
## [2.7.1] - 2021-05-30
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1100](https://github.com/shlinkio/shlink/issues/1100) Fixed Shlink trying to download GeoLite2 db files even when tracking has been disabled.
## [2.7.0] - 2021-05-23
### Added
* [#1044](https://github.com/shlinkio/shlink/issues/1044) Added ability to set names on API keys, which helps to identify them when the list grows.
* [#819](https://github.com/shlinkio/shlink/issues/819) Visits are now always located in real time, even when not using swoole.
The only side effect is that a GeoLite2 db file is now installed when the docker image starts or during shlink installation or update.
Also, when using swoole, the file is now updated **after** tracking a visit, which means it will not apply until the next one.
* [#1059](https://github.com/shlinkio/shlink/issues/1059) Added ability to optionally display author API key and its name when listing short URLs from the command line.
* [#1066](https://github.com/shlinkio/shlink/issues/1066) Added support to import short URLs and their visits from another Shlink instance using its API.
* [#898](https://github.com/shlinkio/shlink/issues/898) Improved tracking granularity, allowing to disable visits tracking completely, or just parts of it.
In order to achieve it, Shlink now supports 4 new tracking-related options, that can be customized via env vars for docker, or via installer:
* `disable_tracking`: If true, visits will not be tracked at all.
* `disable_ip_tracking`: If true, visits will be tracked, but neither the IP address, nor the location will be resolved.
* `disable_referrer_tracking`: If true, the referrer will not be tracked.
* `disable_ua_tracking`: If true, the user agent will not be tracked.
* [#955](https://github.com/shlinkio/shlink/issues/955) Added new option to set short URLs as crawlable, making them be listed in the robots.txt as Allowed.
* [#900](https://github.com/shlinkio/shlink/issues/900) Shlink now tries to detect if the visit is coming from a potential bot or crawler, and allows to exclude those visits from visits lists if desired.
### Changed
* [#1036](https://github.com/shlinkio/shlink/issues/1036) Updated to `happyr/doctrine-specification` 2.0.
* [#1039](https://github.com/shlinkio/shlink/issues/1039) Updated to `endroid/qr-code` 4.0.
* [#1008](https://github.com/shlinkio/shlink/issues/1008) Ensured all logs are sent to the filesystem while running API tests, which helps debugging the reason for tests to fail.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1041](https://github.com/shlinkio/shlink/issues/1041) Ensured the default value for the version while building the docker image is `latest`.
* [#1067](https://github.com/shlinkio/shlink/issues/1067) Fixed exception when persisting multiple short URLs in one batch which include the same new tags/domains. This can potentially happen when importing URLs.
## [2.6.2] - 2021-03-12
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1047](https://github.com/shlinkio/shlink/issues/1047) Fixed error in migrations when doing a fresh installation using PHP8 and MySQL/Mariadb databases.
## [2.6.1] - 2021-02-22
### Added
* *Nothing*
### Changed
* [#1026](https://github.com/shlinkio/shlink/issues/1026) Removed non-inclusive terms from source code.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1024](https://github.com/shlinkio/shlink/issues/1024) Fixed migration that is incorrectly skipped due to the wrong condition being used to check it.
* [#1031](https://github.com/shlinkio/shlink/issues/1031) Fixed shortening of twitter URLs with URL validation enabled.
* [#1034](https://github.com/shlinkio/shlink/issues/1034) Fixed warning displayed when shlink is stopped while running it with swoole.
## [2.6.0] - 2021-02-13
### Added
* [#856](https://github.com/shlinkio/shlink/issues/856) Added PHP 8.0 support.
* [#941](https://github.com/shlinkio/shlink/issues/941) Added support to provide a title for every short URL.
The title can also be automatically resolved from the long URL, when no title was explicitly provided, but this option needs to be opted in.
* [#913](https://github.com/shlinkio/shlink/issues/913) Added support to import short URLs from a standard CSV file.
The file requires the `Long URL` and `Short code` columns, and it also accepts the optional `title`, `domain` and `tags` columns.
* [#1000](https://github.com/shlinkio/shlink/issues/1000) Added support to provide a `margin` query param when generating some URL's QR code.
* [#675](https://github.com/shlinkio/shlink/issues/675) Added ability to track visits to the base URL, invalid short URLs or any other "not found" URL, as known as orphan visits.
This behavior is enabled by default, but you can opt out via env vars or config options.
This new orphan visits can be consumed in these ways:
* The `https://shlink.io/new-orphan-visit` mercure topic, which gets notified when an orphan visit occurs.
* The `GET /visits/orphan` REST endpoint, which behaves like the short URL visits and tags visits endpoints, but returns only orphan visits.
### Changed
* [#977](https://github.com/shlinkio/shlink/issues/977) Migrated from `laminas/laminas-paginator` to `pagerfanta/core` to handle pagination.
* [#986](https://github.com/shlinkio/shlink/issues/986) Updated official docker image to use PHP 8.
* [#1010](https://github.com/shlinkio/shlink/issues/1010) Increased timeout for database commands to 10 minutes.
* [#874](https://github.com/shlinkio/shlink/issues/874) Changed how dist files are generated. Now there will be two for every supported PHP version, with and without support for swoole.
The dist files will have been built under the same PHP version they are meant to be run under, ensuring resolved dependencies are the proper ones.
### Deprecated
* [#959](https://github.com/shlinkio/shlink/issues/959) Deprecated all command flags using camelCase format (like `--expirationDate`), adding kebab-case replacements for all of them (like `--expiration-date`).
All the existing camelCase flags will continue working for now, but will be removed in Shlink 3.0.0
* [#862](https://github.com/shlinkio/shlink/issues/862) Deprecated the endpoint to edit tags for a short URL (`PUT /short-urls/{shortCode}/tags`).
The short URL edition endpoint (`PATCH /short-urls/{shortCode}`) now supports setting the tags too. Use it instead.
### Removed
* *Nothing*
### Fixed
* [#988](https://github.com/shlinkio/shlink/issues/988) Fixed serving zero-byte static files in apache and apache-compatible web servers.
* [#990](https://github.com/shlinkio/shlink/issues/990) Fixed short URLs not properly composed in REST API endpoints when both custom domain and custom base path are used.
* [#1002](https://github.com/shlinkio/shlink/issues/1002) Fixed weird behavior in which GeoLite2 metadata's `buildEpoch` is parsed as string instead of int.
* [#851](https://github.com/shlinkio/shlink/issues/851) Fixed error when trying to schedule swoole tasks in ARM architectures (like raspberry).
## [2.5.2] - 2021-01-24
### Added
* [#965](https://github.com/shlinkio/shlink/issues/965) Added docs section for Architectural Decision Records, including the one for API key roles.
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#979](https://github.com/shlinkio/shlink/issues/979) Added missing `itemsPerPage` query param to swagger docs for short URLs list.
* [#980](https://github.com/shlinkio/shlink/issues/980) Fixed value used for `Access-Control-Allow-Origin`, that could not work as expected when including an IP address.
* [#947](https://github.com/shlinkio/shlink/issues/947) Fixed incorrect value returned in `Access-Control-Allow-Methods` header, which always contained all methods.
## [2.5.1] - 2021-01-21
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#968](https://github.com/shlinkio/shlink/issues/968) Fixed index error in MariaDB while updating to v2.5.0.
* [#972](https://github.com/shlinkio/shlink/issues/972) Fixed 500 error when calling single-step short URL creation endpoint.
## [2.5.0] - 2021-01-17
### Added
* [#795](https://github.com/shlinkio/shlink/issues/795) and [#882](https://github.com/shlinkio/shlink/issues/882) Added new roles system to API keys.
API keys can have any combinations of these two roles now, allowing to limit their interactions:
* Can interact only with short URLs created with that API key.
* Can interact only with short URLs for a specific domain.
* [#833](https://github.com/shlinkio/shlink/issues/833) Added support to connect through unix socket when using an external MySQL, MariaDB or Postgres database.
It can be provided during the installation, or as the `DB_UNIX_SOCKET` env var for the docker image.
* [#869](https://github.com/shlinkio/shlink/issues/869) Added support for Mercure Hub 0.10.
* [#896](https://github.com/shlinkio/shlink/issues/896) Added support for unicode characters in custom slugs.
* [#930](https://github.com/shlinkio/shlink/issues/930) Added new `bin/set-option` script that allows changing individual configuration options on existing shlink instances.
* [#877](https://github.com/shlinkio/shlink/issues/877) Improved API tests on CORS, and "refined" middleware handling it.
### Changed
* [#912](https://github.com/shlinkio/shlink/issues/912) Changed error templates to be plain html files, removing the dependency on `league/plates` package.
* [#875](https://github.com/shlinkio/shlink/issues/875) Updated to `mezzio/mezzio-swoole` v3.1.
* [#952](https://github.com/shlinkio/shlink/issues/952) Simplified in-project docs, by keeping only the basics and linking to the websites docs for anything else.
### Deprecated
* [#917](https://github.com/shlinkio/shlink/issues/917) Deprecated `/{shortCode}/qr-code/{size}` URL, in favor of providing the size in the query instead, `/{shortCode}/qr-code?size={size}`.
* [#924](https://github.com/shlinkio/shlink/issues/924) Deprecated mechanism to provide config options to the docker image through volumes. Use the env vars instead as a direct replacement.
### Removed
* *Nothing*
### Fixed
* *Nothing*
## [2.4.2] - 2020-11-22
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#904](https://github.com/shlinkio/shlink/issues/904) Explicitly added missing "Domains" and "Integrations" tags to swagger docs.
* [#901](https://github.com/shlinkio/shlink/issues/901) Ensured domains which are not in use on any short URL are not returned on the list of domains.
* [#899](https://github.com/shlinkio/shlink/issues/899) Avoided filesystem errors produced while downloading geolite DB files on several shlink instances that share the same filesystem.
* [#827](https://github.com/shlinkio/shlink/issues/827) Fixed swoole config getting loaded in config cache if a console command is run before any web execution, when swoole extension is enabled, making subsequent non-swoole web requests fail.
## [2.4.1] - 2020-11-10
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#891](https://github.com/shlinkio/shlink/issues/891) Fixed error when running migrations in postgres due to incorrect return type hint.
* [#846](https://github.com/shlinkio/shlink/issues/846) Fixed base image used for the PHP-FPM dev container.
* [#867](https://github.com/shlinkio/shlink/issues/867) Fixed not-found redirects not using proper status (301 or 302) as configured during installation.
## [2.4.0] - 2020-11-08
### Added
* [#829](https://github.com/shlinkio/shlink/issues/829) Added support for QR codes in SVG format, by passing `?format=svg` to the QR code URL.
* [#820](https://github.com/shlinkio/shlink/issues/820) Added new option to force enabling or disabling URL validation on a per-URL basis.
Currently, there's a global config that tells if long URLs should be validated (by ensuring they are publicly accessible and return a 2xx status). However, this is either always applied or never applied.
Now, it is possible to enforce validation or enforce disabling validation when a new short URL is created or edited:
* On the `POST /short-url` and `PATCH /short-url/{shortCode}` endpoints, you can now pass `validateUrl: true/false` in order to enforce enabling or disabling validation, ignoring the global config. If the value is not provided, the global config is still normally applied.
* On the `short-url:generate` CLI command, you can pass `--validate-url` or `--no-validate-url` flags, in order to enforce enabling or disabling validation. If none of them is provided, the global config is still normally applied.
* [#838](https://github.com/shlinkio/shlink/issues/838) Added new endpoint and CLI command to list existing domains.
It returns both default domain and specific domains that were used for some short URLs.
* REST endpoint: `GET /rest/v2/domains`
* CLI Command: `domain:list`
* [#832](https://github.com/shlinkio/shlink/issues/832) Added support to customize the port in which the docker image listens by using the `PORT` env var or the `port` config option.
* [#860](https://github.com/shlinkio/shlink/issues/860) Added support to import links from bit.ly.
Run the command `short-urls:import bitly` and introduce requested information in order to import all your links.
Other sources will be supported in future releases.
### Changed
* [#836](https://github.com/shlinkio/shlink/issues/836) Added support for the `<field>-<dir>` notation while determining how to order the short URLs list, as in `?orderBy=shortCode-DESC`. This effectively deprecates the array notation (`?orderBy[shortCode]=DESC`), that will be removed in Shlink 3.0.0
* [#782](https://github.com/shlinkio/shlink/issues/782) Added code coverage to API tests.
* [#858](https://github.com/shlinkio/shlink/issues/858) Updated to latest infection version. Updated docker images to PHP 7.4.11 and swoole 4.5.5
* [#887](https://github.com/shlinkio/shlink/pull/887) Started tracking the API key used to create short URLs, in order to allow restrictions in future releases.
### Deprecated
* [#883](https://github.com/shlinkio/shlink/issues/883) Deprecated `POST /tags` endpoint and `tag:create` command, as tags are created automatically while creating short URLs.
### Removed
* *Nothing*
### Fixed
* [#837](https://github.com/shlinkio/shlink/issues/837) Drastically improved performance when creating a new shortUrl and providing `findIfExists = true`.
* [#878](https://github.com/shlinkio/shlink/issues/878) Added missing `gmp` extension to the official docker image.
## [2.3.0] - 2020-08-09
### Added
* [#746](https://github.com/shlinkio/shlink/issues/746) Allowed to configure the kind of redirect you want to use for your short URLs. You can either set:
* `302` redirects: Default behavior. Visitors always hit the server.
* `301` redirects: Better for SEO. Visitors hit the server the first time and then cache the redirect.
When selecting 301 redirects, you can also configure the time redirects are cached, to mitigate deviations in stats.
* [#734](https://github.com/shlinkio/shlink/issues/734) Added support to redirect to deeplinks and other links with schemas different from `http` and `https`.
* [#709](https://github.com/shlinkio/shlink/issues/709) Added multi-architecture builds for the docker image.
* [#707](https://github.com/shlinkio/shlink/issues/707) Added `--all` flag to `short-urls:list` command, which will print all existing URLs in one go, with no pagination.
It has one limitation, though. Because of the way the CLI tooling works, all rows in the table must be loaded in memory. If the amount of URLs is too high, the command may fail due to too much memory usage.
### Changed
* [#508](https://github.com/shlinkio/shlink/issues/508) Added mutation checks to database tests.
* [#790](https://github.com/shlinkio/shlink/issues/790) Updated to doctrine/migrations v3.
* [#798](https://github.com/shlinkio/shlink/issues/798) Updated to guzzlehttp/guzzle v7.
* [#822](https://github.com/shlinkio/shlink/issues/822) Updated docker image to use PHP 7.4.9 with Alpine 3.12 and swoole 4.5.2.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* *Nothing*
## [2.2.2] - 2020-06-08
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#769](https://github.com/shlinkio/shlink/issues/769) Fixed custom slugs not allowing valid URL characters, like `.`, `_` or `~`.
* [#781](https://github.com/shlinkio/shlink/issues/781) Fixed memory leak when loading visits for a tag which is used for big amounts of short URLs.
## [2.2.1] - 2020-05-11
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#764](https://github.com/shlinkio/shlink/issues/764) Fixed error when trying to match an existing short URL which does not have `validSince` and/or `validUntil`, but you are providing either one of them for the new one.
## [2.2.0] - 2020-05-09
### Added
* [#712](https://github.com/shlinkio/shlink/issues/712) Added support to integrate Shlink with a [mercure hub](https://mercure.rocks/) server.
Thanks to that, Shlink will be able to publish events that can be consumed in real time.
For now, two topics (events) are published, when new visits occur. Both include a payload with the visit and the shortUrl:
* A visit occurs on any short URL: `https://shlink.io/new-visit`.
* A visit occurs on short URLs with a specific short code: `https://shlink.io/new-visit/{shortCode}`.
The updates are only published when serving Shlink with swoole.
Also, Shlink exposes a new endpoint `GET /rest/v2/mercure-info`, which returns the public URL of the mercure hub, and a valid JWT that can be used to subscribe to updates.
* [#673](https://github.com/shlinkio/shlink/issues/673) Added new `[GET /visits]` rest endpoint which returns basic visits stats.
* [#674](https://github.com/shlinkio/shlink/issues/674) Added new `[GET /tags/{tag}/visits]` rest endpoint which returns visits by tag.
It works in the same way as the `[GET /short-urls/{shortCode}/visits]` one, returning the same response payload, and supporting the same query params, but the response is the list of visits in all short URLs which have provided tag.
* [#672](https://github.com/shlinkio/shlink/issues/672) Enhanced `[GET /tags]` rest endpoint so that it is possible to get basic stats info for every tag.
Now, if the `withStats=true` query param is provided, the response payload will include a new `stats` property which is a list with the amount of short URLs and visits for every tag.
Also, the `tag:list` CLI command has been changed and it always behaves like this.
* [#640](https://github.com/shlinkio/shlink/issues/640) Allowed to optionally disable visitors' IP address anonymization. This will make Shlink no longer be GDPR-compliant, but it's OK if you only plan to share your URLs in countries without this regulation.
### Changed
* [#692](https://github.com/shlinkio/shlink/issues/692) Drastically improved performance when loading visits. Specially noticeable when loading big result sets.
* [#657](https://github.com/shlinkio/shlink/issues/657) Updated how DB tests are run in travis by using docker containers which allow all engines to be covered.
* [#751](https://github.com/shlinkio/shlink/issues/751) Updated PHP and swoole versions used in docker image, and removed mssql-tools, as they are not needed.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#729](https://github.com/shlinkio/shlink/issues/729) Fixed weird error when fetching multiple visits result sets concurrently using mariadb or mysql.
* [#735](https://github.com/shlinkio/shlink/issues/735) Fixed error when cleaning metadata cache during installation when APCu is enabled.
* [#677](https://github.com/shlinkio/shlink/issues/677) Fixed `/health` endpoint returning `503` fail responses when the database connection has expired.
* [#732](https://github.com/shlinkio/shlink/issues/732) Fixed wrong client IP in access logs when serving app with swoole behind load balancer.
## [2.1.4] - 2020-04-30
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#742](https://github.com/shlinkio/shlink/issues/742) Allowed a custom GeoLite2 license key to be provided, in order to avoid download limits.
## [2.1.3] - 2020-04-09
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#712](https://github.com/shlinkio/shlink/issues/712) Fixed app set-up not clearing entities metadata cache.
* [#711](https://github.com/shlinkio/shlink/issues/711) Fixed `HEAD` requests returning a duplicated `Content-Length` header.
* [#716](https://github.com/shlinkio/shlink/issues/716) Fixed Twitter not properly displaying preview for final long URL.
* [#717](https://github.com/shlinkio/shlink/issues/717) Fixed DB connection expiring on task workers when using swoole.
* [#705](https://github.com/shlinkio/shlink/issues/705) Fixed how the short URL domain is inferred when generating QR codes, making sure the configured domain is respected even if the request is performed using a different one, and only when a custom domain is used, then that one is used instead.
## [2.1.2] - 2020-03-29
### Added
* *Nothing*
### Changed
* [#696](https://github.com/shlinkio/shlink/issues/696) Updated to infection v0.16.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#700](https://github.com/shlinkio/shlink/issues/700) Fixed migration not working with postgres.
* [#690](https://github.com/shlinkio/shlink/issues/690) Fixed tags being incorrectly sluggified when filtering short URL lists, making results not be the expected.
## [2.1.1] - 2020-03-28
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#697](https://github.com/shlinkio/shlink/issues/697) Recovered `.htaccess` file that was unintentionally removed in v2.1.0, making Shlink unusable with Apache.
## [2.1.0] - 2020-03-28
### Added
* [#626](https://github.com/shlinkio/shlink/issues/626) Added support for Microsoft SQL Server.
* [#556](https://github.com/shlinkio/shlink/issues/556) Short code lengths can now be customized, both globally and on a per-short URL basis.
* [#541](https://github.com/shlinkio/shlink/issues/541) Added a request ID that is returned on `X-Request-Id` header, can be provided from outside and is set in log entries.
* [#642](https://github.com/shlinkio/shlink/issues/642) IP geolocation is now performed over the non-anonymized IP address when using swoole.
* [#521](https://github.com/shlinkio/shlink/issues/521) The long URL for any existing short URL can now be edited using the `PATCH /short-urls/{shortCode}` endpoint.
### Changed
* [#656](https://github.com/shlinkio/shlink/issues/656) Updated to PHPUnit 9.
* [#641](https://github.com/shlinkio/shlink/issues/641) Added two new flags to the `visit:locate` command, `--retry` and `--all`.
* When `--retry` is provided, it will try to re-locate visits which IP address was originally considered not found, in case it was a temporal issue.
* When `--all` is provided together with `--retry`, it will try to re-locate all existing visits. A warning and confirmation are displayed, as this can have side effects.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#665](https://github.com/shlinkio/shlink/issues/665) Fixed `base_url_redirect_to` simplified config option not being properly parsed.
* [#663](https://github.com/shlinkio/shlink/issues/663) Fixed Shlink allowing short URLs to be created with an empty custom slug.
* [#678](https://github.com/shlinkio/shlink/issues/678) Fixed `db` commands not running in a non-interactive way.
## [2.0.5] - 2020-02-09
### Added
* [#651](https://github.com/shlinkio/shlink/issues/651) Documented how Shlink behaves when using multiple domains.
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#648](https://github.com/shlinkio/shlink/issues/648) Ensured any user can write in log files, in case shlink is run by several system users.
* [#650](https://github.com/shlinkio/shlink/issues/650) Ensured default domain is ignored when trying to create a short URL.
## [2.0.4] - 2020-02-02
### Added
* *Nothing*
### Changed
* [#577](https://github.com/shlinkio/shlink/issues/577) Wrapped params used to customize short URL lists into a DTO with implicit validation.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#620](https://github.com/shlinkio/shlink/issues/620) Ensured "controlled" errors (like validation errors and such) won't be logged with error level, preventing logs to be polluted.
* [#637](https://github.com/shlinkio/shlink/issues/637) Fixed several work flows in which short URLs with domain are handled form the API.
* [#644](https://github.com/shlinkio/shlink/issues/644) Fixed visits to short URL on non-default domain being linked to the URL on default domain with the same short code.
* [#643](https://github.com/shlinkio/shlink/issues/643) Fixed searching on short URL lists not taking into consideration the domain name.
## [2.0.3] - 2020-01-27
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#624](https://github.com/shlinkio/shlink/issues/624) Fixed order in which headers for remote IP detection are inspected.
* [#623](https://github.com/shlinkio/shlink/issues/623) Fixed short URLs metadata being impossible to reset.
* [#628](https://github.com/shlinkio/shlink/issues/628) Fixed `GET /short-urls/{shortCode}` REST endpoint returning a 404 for short URLs which are not enabled.
* [#621](https://github.com/shlinkio/shlink/issues/621) Fixed permission denied error when updating same GeoLite file version more than once.
## [2.0.2] - 2020-01-12
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#614](https://github.com/shlinkio/shlink/issues/614) Fixed `OPTIONS` requests including the `Origin` header not always returning an empty body with status 2xx.
* [#615](https://github.com/shlinkio/shlink/issues/615) Fixed query args with no value being lost from the long URL when users are redirected.
## [2.0.1] - 2020-01-10
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#607](https://github.com/shlinkio/shlink/issues/607) Added missing info on UPGRADE.md doc.
* [#610](https://github.com/shlinkio/shlink/issues/610) Fixed use of hardcoded quotes on a database migration which makes it fail on postgres.
* [#605](https://github.com/shlinkio/shlink/issues/605) Fixed crashes occurring when migrating from old Shlink versions with nullable DB columns that are assigned to non-nullable entity typed props.
## [2.0.0] - 2020-01-08
### Added
* [#429](https://github.com/shlinkio/shlink/issues/429) Added support for PHP 7.4
* [#529](https://github.com/shlinkio/shlink/issues/529) Created an UPGRADING.md file explaining how to upgrade from v1.x to v2.x
* [#594](https://github.com/shlinkio/shlink/issues/594) Updated external shlink packages, including installer v4.0, which adds the option to ask for the redis cluster config.
### Changed
* [#592](https://github.com/shlinkio/shlink/issues/592) Updated coding styles to use [shlinkio/php-coding-standard](https://github.com/shlinkio/php-coding-standard) v2.1.0.
* [#530](https://github.com/shlinkio/shlink/issues/530) Migrated project from deprecated `zendframework` components to the new `laminas` and `mezzio` ones.
### Deprecated
* *Nothing*
### Removed
* [#429](https://github.com/shlinkio/shlink/issues/429) Dropped support for PHP 7.2 and 7.3
* [#229](https://github.com/shlinkio/shlink/issues/229) Remove everything which was deprecated, including:
* Preview generation feature completely removed.
* Authentication against REST API using JWT is no longer supported.
See [UPGRADE](UPGRADE.md#from-v1x-to-v2x) doc in order to get details on how to migrate to this version.
### Fixed
* [#600](https://github.com/shlinkio/shlink/issues/600) Fixed health action so that it works with and without version in the path.

View file

@ -1,14 +1,10 @@
{
"type": "object",
"required": ["visitedUrl", "type"],
"required": ["type"],
"allOf": [{
"$ref": "./Visit.json"
}],
"properties": {
"visitedUrl": {
"type": ["string", "null"],
"description": "The originally visited URL that triggered the tracking of this visit"
},
"type": {
"type": "string",
"enum": [

View file

@ -1,6 +1,6 @@
{
"type": "object",
"required": ["referer", "date", "userAgent", "visitLocation"],
"required": ["referer", "date", "userAgent", "visitLocation", "potentialBot", "visitedUrl"],
"properties": {
"referer": {
"type": "string",
@ -21,6 +21,10 @@
"potentialBot": {
"type": "boolean",
"description": "Tells if Shlink thinks this visit comes potentially from a bot or crawler"
},
"visitedUrl": {
"type": ["string", "null"],
"description": "The originally visited URL that triggered the tracking of this visit"
}
}
}

View file

@ -100,7 +100,8 @@
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"visitLocation": null,
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": "https://t.co",
@ -115,14 +116,16 @@
"regionName": "California",
"timezone": "America/Los_Angeles"
},
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": null,
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "some_web_crawler/1.4",
"visitLocation": null,
"potentialBot": true
"potentialBot": true,
"visitedUrl": "https://s.test"
}
],
"pagination": {

View file

@ -103,7 +103,8 @@
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"visitLocation": null,
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": "https://t.co",
@ -118,14 +119,16 @@
"regionName": "California",
"timezone": "America/Los_Angeles"
},
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": null,
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "some_web_crawler/1.4",
"visitLocation": null,
"potentialBot": true
"potentialBot": true,
"visitedUrl": "https://s.test"
}
],
"pagination": {

View file

@ -103,7 +103,8 @@
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"visitLocation": null,
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": "https://t.co",
@ -118,14 +119,16 @@
"regionName": "California",
"timezone": "America/Los_Angeles"
},
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": null,
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "some_web_crawler/1.4",
"visitLocation": null,
"potentialBot": true
"potentialBot": true,
"visitedUrl": "https://s.test"
}
],
"pagination": {

View file

@ -94,7 +94,8 @@
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
"visitLocation": null,
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": "https://t.co",
@ -109,14 +110,16 @@
"regionName": "California",
"timezone": "America/Los_Angeles"
},
"potentialBot": false
"potentialBot": false,
"visitedUrl": "https://s.test"
},
{
"referer": null,
"date": "2015-08-20T05:05:03+04:00",
"userAgent": "some_web_crawler/1.4",
"visitLocation": null,
"potentialBot": true
"potentialBot": true,
"visitedUrl": "https://s.test"
}
],
"pagination": {

View file

@ -14,6 +14,8 @@ return [
Command\ShortUrl\GetShortUrlVisitsCommand::NAME => Command\ShortUrl\GetShortUrlVisitsCommand::class,
Command\ShortUrl\DeleteShortUrlCommand::NAME => Command\ShortUrl\DeleteShortUrlCommand::class,
Command\ShortUrl\DeleteShortUrlVisitsCommand::NAME => Command\ShortUrl\DeleteShortUrlVisitsCommand::class,
Command\ShortUrl\DeleteExpiredShortUrlsCommand::NAME =>
Command\ShortUrl\DeleteExpiredShortUrlsCommand::class,
Command\Visit\LocateVisitsCommand::NAME => Command\Visit\LocateVisitsCommand::class,
Command\Visit\DownloadGeoLiteDbCommand::NAME => Command\Visit\DownloadGeoLiteDbCommand::class,
@ -40,6 +42,8 @@ return [
Command\RedirectRule\ManageRedirectRulesCommand::NAME =>
Command\RedirectRule\ManageRedirectRulesCommand::class,
Command\Integration\MatomoSendVisitsCommand::NAME => Command\Integration\MatomoSendVisitsCommand::class,
],
],

View file

@ -8,6 +8,7 @@ use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Shlinkio\Shlink\Common\Doctrine\NoDbNameConnectionFactory;
use Shlinkio\Shlink\Core\Domain\DomainService;
use Shlinkio\Shlink\Core\Matomo;
use Shlinkio\Shlink\Core\Options\TrackingOptions;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectRuleService;
@ -45,6 +46,7 @@ return [
Command\ShortUrl\GetShortUrlVisitsCommand::class => ConfigAbstractFactory::class,
Command\ShortUrl\DeleteShortUrlCommand::class => ConfigAbstractFactory::class,
Command\ShortUrl\DeleteShortUrlVisitsCommand::class => ConfigAbstractFactory::class,
Command\ShortUrl\DeleteExpiredShortUrlsCommand::class => ConfigAbstractFactory::class,
Command\Visit\DownloadGeoLiteDbCommand::class => ConfigAbstractFactory::class,
Command\Visit\LocateVisitsCommand::class => ConfigAbstractFactory::class,
@ -70,6 +72,8 @@ return [
Command\Domain\GetDomainVisitsCommand::class => ConfigAbstractFactory::class,
Command\RedirectRule\ManageRedirectRulesCommand::class => ConfigAbstractFactory::class,
Command\Integration\MatomoSendVisitsCommand::class => ConfigAbstractFactory::class,
],
],
@ -96,6 +100,7 @@ return [
Command\ShortUrl\GetShortUrlVisitsCommand::class => [Visit\VisitsStatsHelper::class],
Command\ShortUrl\DeleteShortUrlCommand::class => [ShortUrl\DeleteShortUrlService::class],
Command\ShortUrl\DeleteShortUrlVisitsCommand::class => [ShortUrl\ShortUrlVisitsDeleter::class],
Command\ShortUrl\DeleteExpiredShortUrlsCommand::class => [ShortUrl\DeleteShortUrlService::class],
Command\Visit\DownloadGeoLiteDbCommand::class => [GeoLite\GeolocationDbUpdater::class],
Command\Visit\LocateVisitsCommand::class => [
@ -127,6 +132,11 @@ return [
RedirectRule\RedirectRuleHandler::class,
],
Command\Integration\MatomoSendVisitsCommand::class => [
Matomo\MatomoOptions::class,
Matomo\MatomoVisitSender::class,
],
Command\Db\CreateDatabaseCommand::class => [
LockFactory::class,
Util\ProcessRunner::class,

View file

@ -50,11 +50,11 @@ class ListKeysCommand extends Command
$enabledOnly = $input->getOption('enabled-only');
$rows = array_map(function (ApiKey $apiKey) use ($enabledOnly) {
$expiration = $apiKey->getExpirationDate();
$expiration = $apiKey->expirationDate;
$messagePattern = $this->determineMessagePattern($apiKey);
// Set columns for this row
$rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name() ?? '-')];
$rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name ?? '-')];
if (! $enabledOnly) {
$rowData[] = sprintf($messagePattern, $this->getEnabledSymbol($apiKey));
}

View file

@ -44,7 +44,7 @@ class GetDomainVisitsCommand extends AbstractVisitsListCommand
*/
protected function mapExtraFields(Visit $visit): array
{
$shortUrl = $visit->getShortUrl();
$shortUrl = $visit->shortUrl;
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
}
}

View file

@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Integration;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Matomo\MatomoOptions;
use Shlinkio\Shlink\Core\Matomo\MatomoVisitSenderInterface;
use Shlinkio\Shlink\Core\Matomo\VisitSendingProgressTrackerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use function Shlinkio\Shlink\Common\buildDateRange;
use function Shlinkio\Shlink\Core\dateRangeToHumanFriendly;
use function sprintf;
class MatomoSendVisitsCommand extends Command implements VisitSendingProgressTrackerInterface
{
public const NAME = 'integration:matomo:send-visits';
private readonly bool $matomoEnabled;
private SymfonyStyle $io;
public function __construct(MatomoOptions $matomoOptions, private readonly MatomoVisitSenderInterface $visitSender)
{
$this->matomoEnabled = $matomoOptions->enabled;
parent::__construct();
}
protected function configure(): void
{
$help = <<<HELP
This command allows you to send existing visits from this Shlink instance to the configured Matomo server.
Its intention is to allow you to configure Matomo at some point in time, and still have your whole visits
history tracked there.
This command will unconditionally send to Matomo all visits for a specific date range, so make sure you
provide the proper limits to avoid duplicated visits.
Send all visits created so far:
<info>%command.name%</info>
Send all visits created before 2024:
<info>%command.name% --until 2023-12-31</info>
Send all visits created after a specific day:
<info>%command.name% --since 2022-03-27</info>
Send all visits created during 2022:
<info>%command.name% --since 2022-01-01 --until 2022-12-31</info>
HELP;
$this
->setName(self::NAME)
->setDescription(sprintf(
'%sSend existing visits to the configured matomo instance',
$this->matomoEnabled ? '' : '[MATOMO INTEGRATION DISABLED] ',
))
->setHelp($help)
->addOption(
'since',
's',
InputOption::VALUE_REQUIRED,
'Only visits created since this date, inclusively, will be sent to Matomo',
)
->addOption(
'until',
'u',
InputOption::VALUE_REQUIRED,
'Only visits created until this date, inclusively, will be sent to Matomo',
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);
if (! $this->matomoEnabled) {
$this->io->warning('Matomo integration is not enabled in this Shlink instance');
return ExitCode::EXIT_WARNING;
}
// TODO Validate provided date formats
$since = $input->getOption('since');
$until = $input->getOption('until');
$dateRange = buildDateRange(
startDate: $since !== null ? Chronos::parse($since) : null,
endDate: $until !== null ? Chronos::parse($until) : null,
);
if ($input->isInteractive()) {
$this->io->warning([
'You are about to send visits from this Shlink instance to Matomo',
'Resolved date range -> ' . dateRangeToHumanFriendly($dateRange),
'Shlink will not check for already sent visits, which could result in some duplications. Make sure '
. 'you have verified only visits in the right date range are going to be sent.',
]);
if (! $this->io->confirm('Continue?', default: false)) {
return ExitCode::EXIT_WARNING;
}
}
$result = $this->visitSender->sendVisitsInDateRange($dateRange, $this);
match (true) {
$result->hasFailures() && $result->hasSuccesses() => $this->io->warning(
sprintf('%s visits sent to Matomo. %s failed.', $result->successfulVisits, $result->failedVisits),
),
$result->hasFailures() => $this->io->error(
sprintf('Failed to send %s visits to Matomo.', $result->failedVisits),
),
$result->hasSuccesses() => $this->io->success(
sprintf('%s visits sent to Matomo.', $result->successfulVisits),
),
default => $this->io->info('There was no visits matching provided date range.'),
};
return ExitCode::EXIT_SUCCESS;
}
public function success(int $index): void
{
$this->io->write('.');
}
public function error(int $index, Throwable $e): void
{
$this->io->write('<error>E</error>');
if ($this->io->isVerbose()) {
$this->getApplication()?->renderThrowable($e, $this->io);
}
}
}

View file

@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ExpiredShortUrlsConditions;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function sprintf;
class DeleteExpiredShortUrlsCommand extends Command
{
public const NAME = 'short-url:delete-expired';
public function __construct(private readonly DeleteShortUrlServiceInterface $deleteShortUrlService)
{
parent::__construct();
}
protected function configure(): void
{
$this
->setName(self::NAME)
->setDescription(
'Deletes all short URLs that are considered expired, because they have a validUntil date in the past',
)
->addOption(
'evaluate-max-visits',
mode: InputOption::VALUE_NONE,
description: 'Also take into consideration short URLs which have reached their max amount of visits.',
)
->addOption('force', 'f', InputOption::VALUE_NONE, 'Delete short URLs with no confirmation')
->addOption(
'dry-run',
mode: InputOption::VALUE_NONE,
description: 'Delete short URLs with no confirmation',
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$force = $input->getOption('force') || ! $input->isInteractive();
$dryRun = $input->getOption('dry-run');
$conditions = new ExpiredShortUrlsConditions(maxVisitsReached: $input->getOption('evaluate-max-visits'));
if (! $force && ! $dryRun) {
$io->warning([
'Careful!',
'You are about to perform a destructive operation that can result in deleted short URLs and visits.',
'This action cannot be undone. Proceed at your own risk',
]);
if (! $io->confirm('Continue?', default: false)) {
return ExitCode::EXIT_WARNING;
}
}
if ($dryRun) {
$result = $this->deleteShortUrlService->countExpiredShortUrls($conditions);
$io->success(sprintf('There are %s expired short URLs matching provided conditions', $result));
return ExitCode::EXIT_SUCCESS;
}
$result = $this->deleteShortUrlService->deleteExpiredShortUrls($conditions);
$io->success(sprintf('%s expired short URLs have been deleted', $result));
return ExitCode::EXIT_SUCCESS;
}
}

View file

@ -13,6 +13,7 @@ use Shlinkio\Shlink\Common\Paginator\Util\PagerfantaUtilsTrait;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Core\ShortUrl\Model\TagsMode;
use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlsParamsInputFilter;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlListServiceInterface;
@ -23,10 +24,10 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function array_keys;
use function array_map;
use function array_pad;
use function explode;
use function implode;
use function Shlinkio\Shlink\Core\ArrayUtils\map;
use function sprintf;
class ListShortUrlsCommand extends Command
@ -176,6 +177,9 @@ class ListShortUrlsCommand extends Command
return ExitCode::EXIT_SUCCESS;
}
/**
* @param array<string, callable(array $serializedShortUrl, ShortUrl $shortUrl): ?string> $columnsMap
*/
private function renderPage(
OutputInterface $output,
array $columnsMap,
@ -184,10 +188,10 @@ class ListShortUrlsCommand extends Command
): Paginator {
$shortUrls = $this->shortUrlService->listShortUrls($params);
$rows = array_map(function (ShortUrl $shortUrl) use ($columnsMap) {
$rawShortUrl = $this->transformer->transform($shortUrl);
return array_map(fn (callable $call) => $call($rawShortUrl, $shortUrl), $columnsMap);
}, [...$shortUrls]);
$rows = map([...$shortUrls], function (ShortUrlWithVisitsSummary $shortUrl) use ($columnsMap) {
$serializedShortUrl = $this->transformer->transform($shortUrl);
return map($columnsMap, fn (callable $call) => $call($serializedShortUrl, $shortUrl->shortUrl));
});
ShlinkTable::default($output)->render(
array_keys($columnsMap),
@ -209,6 +213,9 @@ class ListShortUrlsCommand extends Command
return $dir === null ? $field : sprintf('%s-%s', $field, $dir);
}
/**
* @return array<string, callable(array $serializedShortUrl, ShortUrl $shortUrl): ?string>
*/
private function resolveColumnsMap(InputInterface $input): array
{
$pickProp = static fn (string $prop): callable => static fn (array $shortUrl) => $shortUrl[$prop];
@ -229,11 +236,11 @@ class ListShortUrlsCommand extends Command
}
if ($input->getOption('show-api-key')) {
$columnsMap['API Key'] = static fn (array $_, ShortUrl $shortUrl): string =>
$shortUrl->authorApiKey()?->__toString() ?? '';
$shortUrl->authorApiKey?->__toString() ?? '';
}
if ($input->getOption('show-api-key-name')) {
$columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): ?string =>
$shortUrl->authorApiKey()?->name();
$shortUrl->authorApiKey?->name;
}
return $columnsMap;

View file

@ -44,7 +44,7 @@ class GetTagVisitsCommand extends AbstractVisitsListCommand
*/
protected function mapExtraFields(Visit $visit): array
{
$shortUrl = $visit->getShortUrl();
$shortUrl = $visit->shortUrl;
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
}
}

View file

@ -54,9 +54,12 @@ abstract class AbstractVisitsListCommand extends Command
$extraKeys = array_keys($extraFields);
$rowData = [
...$visit->jsonSerialize(),
'country' => $visit->getVisitLocation()?->getCountryName() ?? 'Unknown',
'city' => $visit->getVisitLocation()?->getCityName() ?? 'Unknown',
'referer' => $visit->referer,
'date' => $visit->date->toAtomString(),
'userAgent' => $visit->userAgent,
'potentialBot' => $visit->potentialBot,
'country' => $visit->getVisitLocation()?->countryName ?? 'Unknown',
'city' => $visit->getVisitLocation()?->cityName ?? 'Unknown',
...$extraFields,
];

View file

@ -40,7 +40,7 @@ class GetNonOrphanVisitsCommand extends AbstractVisitsListCommand
*/
protected function mapExtraFields(Visit $visit): array
{
$shortUrl = $visit->getShortUrl();
$shortUrl = $visit->shortUrl;
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
}
}

View file

@ -42,6 +42,6 @@ class GetOrphanVisitsCommand extends AbstractVisitsListCommand
*/
protected function mapExtraFields(Visit $visit): array
{
return ['type' => $visit->type()->value];
return ['type' => $visit->type->value];
}
}

View file

@ -132,7 +132,7 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
*/
public function geolocateVisit(Visit $visit): Location
{
$ipAddr = $visit->getRemoteAddr() ?? '?';
$ipAddr = $visit->remoteAddr ?? '?';
$this->io->write(sprintf('Processing IP <fg=blue>%s</>', $ipAddr));
try {
@ -154,9 +154,9 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void
{
if (! $visitLocation->isEmpty()) {
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->getCountryName()));
} elseif ($visit->hasRemoteAddr() && $visit->getRemoteAddr() !== IpAddress::LOCALHOST) {
if (! $visitLocation->isEmpty) {
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->countryName));
} elseif ($visit->hasRemoteAddr() && $visit->remoteAddr !== IpAddress::LOCALHOST) {
$this->io->writeln(' <comment>[Could not locate address]</comment>');
}
}

View file

@ -60,7 +60,7 @@ class GetDomainVisitsCommandTest extends TestCase
+---------+---------------------------+------------+---------+--------+---------------+
| Referer | Date | User agent | Country | City | Short Url |
+---------+---------------------------+------------+---------+--------+---------------+
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url |
| foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | the_short_url |
+---------+---------------------------+------------+---------+--------+---------------+
OUTPUT,

View file

@ -0,0 +1,135 @@
<?php
namespace ShlinkioTest\Shlink\CLI\Command\Integration;
use Exception;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\Integration\MatomoSendVisitsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Matomo\MatomoOptions;
use Shlinkio\Shlink\Core\Matomo\MatomoVisitSenderInterface;
use Shlinkio\Shlink\Core\Matomo\Model\SendVisitsResult;
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
class MatomoSendVisitsCommandTest extends TestCase
{
private MockObject & MatomoVisitSenderInterface $visitSender;
protected function setUp(): void
{
$this->visitSender = $this->createMock(MatomoVisitSenderInterface::class);
}
#[Test]
public function warningDisplayedIfIntegrationIsNotEnabled(): void
{
[$output, $exitCode] = $this->executeCommand(matomoEnabled: false);
self::assertStringContainsString('Matomo integration is not enabled in this Shlink instance', $output);
self::assertEquals(ExitCode::EXIT_WARNING, $exitCode);
}
#[Test]
#[TestWith([true])]
#[TestWith([false])]
public function warningIsOnlyDisplayedInInteractiveMode(bool $interactive): void
{
$this->visitSender->method('sendVisitsInDateRange')->willReturn(new SendVisitsResult());
[$output] = $this->executeCommand(['y'], ['interactive' => $interactive]);
if ($interactive) {
self::assertStringContainsString('You are about to send visits', $output);
} else {
self::assertStringNotContainsString('You are about to send visits', $output);
}
}
#[Test]
#[TestWith([true])]
#[TestWith([false])]
public function canCancelExecutionInInteractiveMode(bool $interactive): void
{
$this->visitSender->expects($this->exactly($interactive ? 0 : 1))->method('sendVisitsInDateRange')->willReturn(
new SendVisitsResult(),
);
$this->executeCommand(['n'], ['interactive' => $interactive]);
}
#[Test]
#[TestWith([new SendVisitsResult(), 'There was no visits matching provided date range'])]
#[TestWith([new SendVisitsResult(successfulVisits: 10), '10 visits sent to Matomo.'])]
#[TestWith([new SendVisitsResult(successfulVisits: 2), '2 visits sent to Matomo.'])]
#[TestWith([new SendVisitsResult(failedVisits: 238), 'Failed to send 238 visits to Matomo.'])]
#[TestWith([new SendVisitsResult(failedVisits: 18), 'Failed to send 18 visits to Matomo.'])]
#[TestWith([new SendVisitsResult(successfulVisits: 2, failedVisits: 35), '2 visits sent to Matomo. 35 failed.'])]
#[TestWith([new SendVisitsResult(successfulVisits: 81, failedVisits: 6), '81 visits sent to Matomo. 6 failed.'])]
public function expectedResultIsDisplayed(SendVisitsResult $result, string $expectedResultMessage): void
{
$this->visitSender->expects($this->once())->method('sendVisitsInDateRange')->willReturn($result);
[$output, $exitCode] = $this->executeCommand(['y']);
self::assertStringContainsString($expectedResultMessage, $output);
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
}
#[Test]
public function printsResultOfSendingVisits(): void
{
$this->visitSender->method('sendVisitsInDateRange')->willReturnCallback(
function (DateRange $_, MatomoSendVisitsCommand $command): SendVisitsResult {
// Call it a few times for an easier match of its result in the command putput
$command->success(0);
$command->success(1);
$command->success(2);
$command->error(3, new Exception('Error'));
$command->success(4);
$command->error(5, new Exception('Error'));
return new SendVisitsResult();
},
);
[$output] = $this->executeCommand(['y']);
self::assertStringContainsString('...E.E', $output);
}
#[Test]
#[TestWith([[], 'All time'])]
#[TestWith([['--since' => '2023-05-01'], 'Since 2023-05-01 00:00:00'])]
#[TestWith([['--until' => '2023-05-01'], 'Until 2023-05-01 00:00:00'])]
#[TestWith([
['--since' => '2023-05-01', '--until' => '2024-02-02 23:59:59'],
'Between 2023-05-01 00:00:00 and 2024-02-02 23:59:59',
])]
public function providedDateAreParsed(array $args, string $expectedMessage): void
{
[$output] = $this->executeCommand(['n'], args: $args);
self::assertStringContainsString('Resolved date range -> ' . $expectedMessage, $output);
}
/**
* @return array{string, int, MatomoSendVisitsCommand}
*/
private function executeCommand(
array $input = [],
array $options = [],
array $args = [],
bool $matomoEnabled = true,
): array {
$command = new MatomoSendVisitsCommand(new MatomoOptions(enabled: $matomoEnabled), $this->visitSender);
$commandTester = CliTestUtils::testerForCommand($command);
$commandTester->setInputs($input);
$commandTester->execute($args, $options);
$output = $commandTester->getDisplay();
$exitCode = $commandTester->getStatusCode();
return [$output, $exitCode, $command];
}
}

View file

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\ShortUrl\DeleteExpiredShortUrlsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ExpiredShortUrlsConditions;
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
use Symfony\Component\Console\Tester\CommandTester;
class DeleteExpiredShortUrlsCommandTest extends TestCase
{
private CommandTester $commandTester;
private MockObject & DeleteShortUrlServiceInterface $service;
protected function setUp(): void
{
$this->service = $this->createMock(DeleteShortUrlServiceInterface::class);
$this->commandTester = CliTestUtils::testerForCommand(new DeleteExpiredShortUrlsCommand($this->service));
}
#[Test]
public function warningIsDisplayedAndExecutionCanBeCancelled(): void
{
$this->service->expects($this->never())->method('countExpiredShortUrls');
$this->service->expects($this->never())->method('deleteExpiredShortUrls');
$this->commandTester->setInputs(['n']);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
$status = $this->commandTester->getStatusCode();
self::assertStringContainsString('Careful!', $output);
self::assertEquals(ExitCode::EXIT_WARNING, $status);
}
#[Test]
#[TestWith([[], [], true])]
#[TestWith([['--force' => true], [], false])]
#[TestWith([['-f' => true], [], false])]
#[TestWith([[], ['interactive' => false], false])]
public function deletionIsExecutedByDefault(array $input, array $options, bool $expectsWarning): void
{
$this->service->expects($this->never())->method('countExpiredShortUrls');
$this->service->expects($this->once())->method('deleteExpiredShortUrls')->willReturn(5);
$this->commandTester->setInputs(['y']);
$this->commandTester->execute($input, $options);
$output = $this->commandTester->getDisplay();
$status = $this->commandTester->getStatusCode();
if ($expectsWarning) {
self::assertStringContainsString('Careful!', $output);
} else {
self::assertStringNotContainsString('Careful!', $output);
}
self::assertStringContainsString('5 expired short URLs have been deleted', $output);
self::assertEquals(ExitCode::EXIT_SUCCESS, $status);
}
#[Test]
public function countIsExecutedDuringDryRun(): void
{
$this->service->expects($this->once())->method('countExpiredShortUrls')->willReturn(38);
$this->service->expects($this->never())->method('deleteExpiredShortUrls');
$this->commandTester->execute(['--dry-run' => true]);
$output = $this->commandTester->getDisplay();
$status = $this->commandTester->getStatusCode();
self::assertStringNotContainsString('Careful!', $output);
self::assertStringContainsString('There are 38 expired short URLs matching provided conditions', $output);
self::assertEquals(ExitCode::EXIT_SUCCESS, $status);
}
#[Test]
#[TestWith([[], new ExpiredShortUrlsConditions()])]
#[TestWith([['--evaluate-max-visits' => true], new ExpiredShortUrlsConditions(maxVisitsReached: true)])]
public function providesExpectedConditionsToService(array $extraInput, ExpiredShortUrlsConditions $conditions): void
{
$this->service->expects($this->once())->method('countExpiredShortUrls')->with($conditions)->willReturn(4);
$this->commandTester->execute(['--dry-run' => true, ...$extraInput]);
}
}

View file

@ -110,7 +110,7 @@ class GetShortUrlVisitsCommandTest extends TestCase
+---------+---------------------------+------------+---------+--------+
| Referer | Date | User agent | Country | City |
+---------+---------------------------+------------+---------+--------+
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid |
| foo | {$visit->date->toAtomString()} | bar | Spain | Madrid |
+---------+---------------------------+------------+---------+--------+
OUTPUT,

View file

@ -16,6 +16,7 @@ use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Core\ShortUrl\Model\TagsMode;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlListServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
@ -47,7 +48,7 @@ class ListShortUrlsCommandTest extends TestCase
// The paginator will return more than one page
$data = [];
for ($i = 0; $i < 50; $i++) {
$data[] = ShortUrl::withLongUrl('https://url_' . $i);
$data[] = ShortUrlWithVisitsSummary::fromShortUrl(ShortUrl::withLongUrl('https://url_' . $i));
}
$this->shortUrlService->expects($this->exactly(3))->method('listShortUrls')->withAnyParameters()
@ -69,7 +70,7 @@ class ListShortUrlsCommandTest extends TestCase
// The paginator will return more than one page
$data = [];
for ($i = 0; $i < 30; $i++) {
$data[] = ShortUrl::withLongUrl('https://url_' . $i);
$data[] = ShortUrlWithVisitsSummary::fromShortUrl(ShortUrl::withLongUrl('https://url_' . $i));
}
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
@ -111,11 +112,13 @@ class ListShortUrlsCommandTest extends TestCase
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
ShortUrlsParams::emptyInstance(),
)->willReturn(new Paginator(new ArrayAdapter([
ShortUrlWithVisitsSummary::fromShortUrl(
ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'https://foo.com',
'tags' => ['foo', 'bar', 'baz'],
'apiKey' => $apiKey,
])),
),
])));
$this->commandTester->setInputs(['y']);

View file

@ -57,7 +57,7 @@ class GetTagVisitsCommandTest extends TestCase
+---------+---------------------------+------------+---------+--------+---------------+
| Referer | Date | User agent | Country | City | Short Url |
+---------+---------------------------+------------+---------+--------+---------------+
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url |
| foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | the_short_url |
+---------+---------------------------+------------+---------+--------+---------------+
OUTPUT,

View file

@ -56,7 +56,7 @@ class GetNonOrphanVisitsCommandTest extends TestCase
+---------+---------------------------+------------+---------+--------+---------------+
| Referer | Date | User agent | Country | City | Short Url |
+---------+---------------------------+------------+---------+--------+---------------+
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url |
| foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | the_short_url |
+---------+---------------------------+------------+---------+--------+---------------+
OUTPUT,

View file

@ -54,7 +54,7 @@ class GetOrphanVisitsCommandTest extends TestCase
+---------+---------------------------+------------+---------+--------+----------+
| Referer | Date | User agent | Country | City | Type |
+---------+---------------------------+------------+---------+--------+----------+
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | base_url |
| foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | base_url |
+---------+---------------------------+------------+---------+--------+----------+
OUTPUT,

View file

@ -10,6 +10,7 @@ use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Common\Doctrine\EntityRepositoryFactory;
use Shlinkio\Shlink\Config\Factory\ValinorConfigFactory;
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
use Symfony\Component\Lock;
@ -57,6 +58,10 @@ return [
EntityRepositoryFactory::class,
ShortUrl\Entity\ShortUrl::class,
],
ShortUrl\Repository\ExpiredShortUrlsRepository::class => [
EntityRepositoryFactory::class,
ShortUrl\Entity\ShortUrl::class,
],
Tag\TagService::class => ConfigAbstractFactory::class,
@ -68,8 +73,7 @@ return [
Visit\Geolocation\VisitLocator::class => ConfigAbstractFactory::class,
Visit\Geolocation\VisitToLocationHelper::class => ConfigAbstractFactory::class,
Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class,
Visit\Transformer\OrphanVisitDataTransformer::class => InvokableFactory::class,
Visit\Repository\VisitLocationRepository::class => [
Visit\Repository\VisitIterationRepository::class => [
EntityRepositoryFactory::class,
Visit\Entity\Visit::class,
],
@ -77,6 +81,8 @@ return [
EntityRepositoryFactory::class,
Visit\Entity\Visit::class,
],
Visit\Listener\ShortUrlVisitsCountTracker::class => InvokableFactory::class,
Visit\Listener\OrphanVisitsCountTracker::class => InvokableFactory::class,
Util\DoctrineBatchHelper::class => ConfigAbstractFactory::class,
Util\RedirectResponseHelper::class => ConfigAbstractFactory::class,
@ -96,6 +102,7 @@ return [
Matomo\MatomoOptions::class => [ValinorConfigFactory::class, 'config.matomo'],
Matomo\MatomoTrackerBuilder::class => ConfigAbstractFactory::class,
Matomo\MatomoVisitSender::class => ConfigAbstractFactory::class,
],
'aliases' => [
@ -105,6 +112,11 @@ return [
ConfigAbstractFactory::class => [
Matomo\MatomoTrackerBuilder::class => [Matomo\MatomoOptions::class],
Matomo\MatomoVisitSender::class => [
Matomo\MatomoTrackerBuilder::class,
ShortUrlStringifier::class,
Visit\Repository\VisitIterationRepository::class,
],
ErrorHandler\NotFoundTypeResolverMiddleware::class => ['config.router.base_path'],
ErrorHandler\NotFoundTrackerMiddleware::class => [Visit\RequestTracker::class],
@ -138,7 +150,7 @@ return [
ShortUrl\Repository\ShortUrlListRepository::class,
Options\UrlShortenerOptions::class,
],
Visit\Geolocation\VisitLocator::class => ['em', Visit\Repository\VisitLocationRepository::class],
Visit\Geolocation\VisitLocator::class => ['em', Visit\Repository\VisitIterationRepository::class],
Visit\Geolocation\VisitToLocationHelper::class => [IpLocationResolverInterface::class],
Visit\VisitsStatsHelper::class => ['em'],
Tag\TagService::class => ['em'],
@ -146,6 +158,7 @@ return [
'em',
Options\DeleteShortUrlsOptions::class,
ShortUrl\ShortUrlResolver::class,
ShortUrl\Repository\ExpiredShortUrlsRepository::class,
],
ShortUrl\ShortUrlResolver::class => ['em', Options\UrlShortenerOptions::class],
ShortUrl\ShortUrlVisitsDeleter::class => [
@ -199,10 +212,7 @@ return [
],
ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => [Options\UrlShortenerOptions::class],
EventDispatcher\PublishingUpdatesGenerator::class => [
ShortUrl\Transformer\ShortUrlDataTransformer::class,
Visit\Transformer\OrphanVisitDataTransformer::class,
],
EventDispatcher\PublishingUpdatesGenerator::class => [ShortUrl\Transformer\ShortUrlDataTransformer::class],
Importer\ImportedLinksProcessor::class => [
'em',

View file

@ -67,6 +67,11 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
->fetchExtraLazy()
->build();
$builder->createOneToMany('visitsCounts', Visit\Entity\ShortUrlVisitsCount::class)
->mappedBy('shortUrl')
->fetchExtraLazy() // TODO Check if this makes sense
->build();
$builder->createManyToMany('tags', Tag\Entity\Tag::class)
->setJoinTable(determineTableName('short_urls_in_tags', $emConfig))
->addInverseJoinColumn('tag_id', 'id', onDelete: 'CASCADE')

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(determineTableName('orphan_visits_counts', $emConfig))
->setCustomRepositoryClass(Visit\Repository\OrphanVisitsCountRepository::class);
$builder->createField('id', Types::BIGINT)
->columnName('id')
->makePrimaryKey()
->generatedValue('IDENTITY')
->option('unsigned', true)
->build();
$builder->createField('potentialBot', Types::BOOLEAN)
->columnName('potential_bot')
->option('default', false)
->build();
$builder->createField('count', Types::BIGINT)
->columnName('count')
->option('unsigned', true)
->option('default', 1)
->build();
$builder->createField('slotId', Types::INTEGER)
->columnName('slot_id')
->option('unsigned', true)
->build();
$builder->addUniqueConstraint(['potential_bot', 'slot_id'], 'UQ_slot');
};

View file

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(determineTableName('short_url_visits_counts', $emConfig))
->setCustomRepositoryClass(Visit\Repository\ShortUrlVisitsCountRepository::class);
$builder->createField('id', Types::BIGINT)
->columnName('id')
->makePrimaryKey()
->generatedValue('IDENTITY')
->option('unsigned', true)
->build();
$builder->createField('potentialBot', Types::BOOLEAN)
->columnName('potential_bot')
->option('default', false)
->build();
$builder->createField('count', Types::BIGINT)
->columnName('count')
->option('unsigned', true)
->option('default', 1)
->build();
$builder->createField('slotId', Types::INTEGER)
->columnName('slot_id')
->option('unsigned', true)
->build();
$builder->createManyToOne('shortUrl', ShortUrl\Entity\ShortUrl::class)
->addJoinColumn('short_url_id', 'id', onDelete: 'CASCADE')
->build();
$builder->addUniqueConstraint(['short_url_id', 'potential_bot', 'slot_id'], 'UQ_slot_per_short_url');
};

View file

@ -12,7 +12,6 @@ use Shlinkio\Shlink\Common\Mercure\MercureHubPublishingHelper;
use Shlinkio\Shlink\Common\Mercure\MercureOptions;
use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelper;
use Shlinkio\Shlink\Core\Matomo\MatomoOptions;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\Visit\Geolocation\VisitLocator;
use Shlinkio\Shlink\Core\Visit\Geolocation\VisitToLocationHelper;
use Shlinkio\Shlink\EventDispatcher\Listener\EnabledListenerCheckerInterface;
@ -157,9 +156,8 @@ return (static function (): array {
EventDispatcher\Matomo\SendVisitToMatomo::class => [
'em',
'Logger_Shlink',
ShortUrlStringifier::class,
Matomo\MatomoOptions::class,
Matomo\MatomoTrackerBuilder::class,
Matomo\MatomoVisitSender::class,
],
EventDispatcher\UpdateGeoLiteDb::class => [

View file

@ -61,6 +61,23 @@ function parseDateRangeFromQuery(array $query, string $startDateName, string $en
return buildDateRange($startDate, $endDate);
}
function dateRangeToHumanFriendly(?DateRange $dateRange): string
{
$startDate = $dateRange?->startDate;
$endDate = $dateRange?->endDate;
return match (true) {
$startDate !== null && $endDate !== null => sprintf(
'Between %s and %s',
$startDate->toDateTimeString(),
$endDate->toDateTimeString(),
),
$startDate !== null => sprintf('Since %s', $startDate->toDateTimeString()),
$endDate !== null => sprintf('Until %s', $endDate->toDateTimeString()),
default => 'All time',
};
}
/**
* @return ($date is null ? null : Chronos)
*/

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;
/**
* Create the new short_url_visits_counts table that will track visit counts per short URL
*/
final class Version20240306132518 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->skipIf($schema->hasTable('short_url_visits_counts'));
$table = $schema->createTable('short_url_visits_counts');
$table->addColumn('id', Types::BIGINT, [
'unsigned' => true,
'autoincrement' => true,
'notnull' => true,
]);
$table->setPrimaryKey(['id']);
$table->addColumn('short_url_id', Types::BIGINT, [
'unsigned' => true,
'notnull' => true,
]);
$table->addForeignKeyConstraint('short_urls', ['short_url_id'], ['id'], [
'onDelete' => 'CASCADE',
'onUpdate' => 'RESTRICT',
]);
$table->addColumn('potential_bot', Types::BOOLEAN, ['default' => false]);
$table->addColumn('slot_id', Types::INTEGER, [
'unsigned' => true,
'notnull' => true,
'default' => 1,
]);
$table->addColumn('count', Types::BIGINT, [
'unsigned' => true,
'notnull' => true,
'default' => 1,
]);
$table->addUniqueIndex(['short_url_id', 'potential_bot', 'slot_id'], 'UQ_slot_per_short_url');
}
public function down(Schema $schema): void
{
$this->skipIf(! $schema->hasTable('short_url_visits_counts'));
$schema->dropTable('short_url_visits_counts');
}
public function isTransactional(): bool
{
return ! ($this->connection->getDatabasePlatform() instanceof MySQLPlatform);
}
}

View file

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Create initial entries in the short_url_visits_counts table for existing visits
*/
final class Version20240318084804 extends AbstractMigration
{
public function up(Schema $schema): void
{
$qb = $this->connection->createQueryBuilder();
$result = $qb->select('id')
->from('short_urls')
->executeQuery();
while ($shortUrlId = $result->fetchOne()) {
$visitsQb = $this->connection->createQueryBuilder();
$visitsQb->select('COUNT(id)')
->from('visits')
->where($visitsQb->expr()->eq('short_url_id', ':short_url_id'))
->andWhere($visitsQb->expr()->eq('potential_bot', ':potential_bot'))
->setParameter('short_url_id', $shortUrlId);
$botsCount = $visitsQb->setParameter('potential_bot', '1')->executeQuery()->fetchOne();
$nonBotsCount = $visitsQb->setParameter('potential_bot', '0')->executeQuery()->fetchOne();
if ($botsCount > 0) {
$this->insertCount($shortUrlId, $botsCount, potentialBot: true);
}
if ($nonBotsCount > 0) {
$this->insertCount($shortUrlId, $nonBotsCount, potentialBot: false);
}
}
}
private function insertCount(string|int $shortUrlId, string|int $count, bool $potentialBot): void
{
$this->connection->createQueryBuilder()
->insert('short_url_visits_counts')
->values([
'short_url_id' => ':short_url_id',
'count' => ':count',
'potential_bot' => ':potential_bot',
])
->setParameters([
'short_url_id' => $shortUrlId,
'count' => $count,
'potential_bot' => $potentialBot ? '1' : '0',
])
->executeStatement();
}
}

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;
/**
* Create a new orphan_visits_counts that will work similarly to the short_url_visits_counts
*/
final class Version20240331111103 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->skipIf($schema->hasTable('orphan_visits_counts'));
$table = $schema->createTable('orphan_visits_counts');
$table->addColumn('id', Types::BIGINT, [
'unsigned' => true,
'autoincrement' => true,
'notnull' => true,
]);
$table->setPrimaryKey(['id']);
$table->addColumn('potential_bot', Types::BOOLEAN, ['default' => false]);
$table->addColumn('slot_id', Types::INTEGER, [
'unsigned' => true,
'notnull' => true,
'default' => 1,
]);
$table->addColumn('count', Types::BIGINT, [
'unsigned' => true,
'notnull' => true,
'default' => 1,
]);
$table->addUniqueIndex(['potential_bot', 'slot_id'], 'UQ_slot');
}
public function down(Schema $schema): void
{
$this->skipIf(! $schema->hasTable('orphan_visits_counts'));
$schema->dropTable('orphan_visits_counts');
}
public function isTransactional(): bool
{
return ! ($this->connection->getDatabasePlatform() instanceof MySQLPlatform);
}
}

View file

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20240331111447 extends AbstractMigration
{
public function up(Schema $schema): void
{
$visitsQb = $this->connection->createQueryBuilder();
$visitsQb->select('COUNT(id)')
->from('visits')
->where($visitsQb->expr()->isNull('short_url_id'))
->andWhere($visitsQb->expr()->eq('potential_bot', ':potential_bot'));
$botsCount = $visitsQb->setParameter('potential_bot', '1')->executeQuery()->fetchOne();
$nonBotsCount = $visitsQb->setParameter('potential_bot', '0')->executeQuery()->fetchOne();
if ($botsCount > 0) {
$this->insertCount($botsCount, potentialBot: true);
}
if ($nonBotsCount > 0) {
$this->insertCount($nonBotsCount, potentialBot: false);
}
}
private function insertCount(string|int $count, bool $potentialBot): void
{
$this->connection->createQueryBuilder()
->insert('orphan_visits_counts')
->values([
'count' => ':count',
'potential_bot' => ':potential_bot',
])
->setParameters([
'count' => $count,
'potential_bot' => $potentialBot ? '1' : '0',
])
->executeStatement();
}
}

View file

@ -71,6 +71,7 @@ enum EnvVars: string
case REDIRECT_APPEND_EXTRA_PATH = 'REDIRECT_APPEND_EXTRA_PATH';
case TIMEZONE = 'TIMEZONE';
case MULTI_SEGMENT_SLUGS_ENABLED = 'MULTI_SEGMENT_SLUGS_ENABLED';
case MEMORY_LIMIT = 'MEMORY_LIMIT';
public function loadFromEnv(mixed $default = null): mixed
{

View file

@ -4,8 +4,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Config\PostProcessor;
use Fig\Http\Message\RequestMethodInterface;
use Mezzio\Router\Route;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Util\RedirectStatus;
@ -40,9 +38,7 @@ class ShortUrlMethodsProcessor
$redirectStatus = RedirectStatus::tryFrom(
$config['redirects']['redirect_status_code'] ?? 0,
) ?? DEFAULT_REDIRECT_STATUS_CODE;
$redirectRoute['allowed_methods'] = $redirectStatus->isLegacyStatus()
? [RequestMethodInterface::METHOD_GET]
: Route::HTTP_METHOD_ANY;
$redirectRoute['allowed_methods'] = $redirectStatus->allowedHttpMethods();
$config['routes'] = [...$rest, $redirectRoute];
return $config;

View file

@ -11,12 +11,12 @@ use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
class Domain extends AbstractEntity implements JsonSerializable, NotFoundRedirectConfigInterface
{
private ?string $baseUrlRedirect = null;
private ?string $regular404Redirect = null;
private ?string $invalidShortUrlRedirect = null;
private function __construct(public readonly string $authority)
{
private function __construct(
public readonly string $authority,
private ?string $baseUrlRedirect = null,
private ?string $regular404Redirect = null,
private ?string $invalidShortUrlRedirect = null,
) {
}
public static function withAuthority(string $authority): self

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher\Helper;
use Shlinkio\Shlink\Common\Middleware\RequestIdMiddleware;
use Shlinkio\Shlink\EventDispatcher\Util\RequestIdProviderInterface;
readonly class RequestIdProvider implements RequestIdProviderInterface
{
public function __construct(private RequestIdMiddleware $requestIdMiddleware)
{
}
public function currentRequestId(): string
{
return $this->requestIdMiddleware->currentRequestId();
}
}

View file

@ -17,7 +17,7 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
use Throwable;
class LocateVisit
readonly class LocateVisit
{
public function __construct(
private IpLocationResolverInterface $ipLocationResolver,
@ -55,7 +55,7 @@ class LocateVisit
}
$isLocatable = $originalIpAddress !== null || $visit->isLocatable();
$addr = $originalIpAddress ?? $visit->getRemoteAddr() ?? '';
$addr = $originalIpAddress ?? $visit->remoteAddr ?? '';
try {
$location = $isLocatable ? $this->ipLocationResolver->resolveIpLocation($addr) : Location::emptyInstance();

View file

@ -8,19 +8,17 @@ use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\Core\Matomo\MatomoOptions;
use Shlinkio\Shlink\Core\Matomo\MatomoTrackerBuilderInterface;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\Matomo\MatomoVisitSenderInterface;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Throwable;
class SendVisitToMatomo
readonly class SendVisitToMatomo
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
private readonly ShortUrlStringifier $shortUrlStringifier,
private readonly MatomoOptions $matomoOptions,
private readonly MatomoTrackerBuilderInterface $trackerBuilder,
private EntityManagerInterface $em,
private LoggerInterface $logger,
private MatomoOptions $matomoOptions,
private MatomoVisitSenderInterface $visitSender,
) {
}
@ -42,48 +40,10 @@ class SendVisitToMatomo
}
try {
$tracker = $this->trackerBuilder->buildMatomoTracker();
$tracker
->setUrl($this->resolveUrlToTrack($visit))
->setCustomTrackingParameter('type', $visit->type()->value)
->setUserAgent($visit->userAgent())
->setUrlReferrer($visit->referer());
$location = $visit->getVisitLocation();
if ($location !== null) {
$tracker
->setCity($location->getCityName())
->setCountry($location->getCountryName())
->setLatitude($location->getLatitude())
->setLongitude($location->getLongitude());
}
// Set not obfuscated IP if possible, as matomo handles obfuscation itself
$ip = $visitLocated->originalIpAddress ?? $visit->getRemoteAddr();
if ($ip !== null) {
$tracker->setIp($ip);
}
if ($visit->isOrphan()) {
$tracker->setCustomTrackingParameter('orphan', 'true');
}
// Send empty document title to avoid different actions to be created by matomo
$tracker->doTrackPageView('');
$this->visitSender->sendVisit($visit, $visitLocated->originalIpAddress);
} catch (Throwable $e) {
// Capture all exceptions to make sure this does not interfere with the regular execution
$this->logger->error('An error occurred while trying to send visit to Matomo. {e}', ['e' => $e]);
}
}
public function resolveUrlToTrack(Visit $visit): string
{
$shortUrl = $visit->getShortUrl();
if ($shortUrl === null) {
return $visit->visitedUrl() ?? '';
}
return $this->shortUrlStringifier->stringify($shortUrl);
}
}

View file

@ -9,18 +9,16 @@ use Shlinkio\Shlink\Common\UpdatePublishing\Update;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
final class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInterface
final readonly class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInterface
{
public function __construct(
private readonly DataTransformerInterface $shortUrlTransformer,
private readonly DataTransformerInterface $orphanVisitTransformer,
) {
public function __construct(private DataTransformerInterface $shortUrlTransformer)
{
}
public function newVisitUpdate(Visit $visit): Update
{
return Update::forTopicAndPayload(Topic::NEW_VISIT->value, [
'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()),
'shortUrl' => $this->shortUrlTransformer->transform($visit->shortUrl),
'visit' => $visit->jsonSerialize(),
]);
}
@ -28,13 +26,13 @@ final class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInte
public function newOrphanVisitUpdate(Visit $visit): Update
{
return Update::forTopicAndPayload(Topic::NEW_ORPHAN_VISIT->value, [
'visit' => $this->orphanVisitTransformer->transform($visit),
'visit' => $visit->jsonSerialize(),
]);
}
public function newShortUrlVisitUpdate(Visit $visit): Update
{
$shortUrl = $visit->getShortUrl();
$shortUrl = $visit->shortUrl;
$topic = Topic::newShortUrlVisit($shortUrl?->getShortCode());
return Update::forTopicAndPayload($topic, [

View file

@ -139,7 +139,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
$importedVisits = 0;
foreach ($iterable as $importedOrphanVisit) {
// Skip visits which are older than the most recent already imported visit's date
if ($mostRecentOrphanVisit?->getDate()->greaterThanOrEquals(normalizeDate($importedOrphanVisit->date))) {
if ($mostRecentOrphanVisit?->date->greaterThanOrEquals(normalizeDate($importedOrphanVisit->date))) {
continue;
}

View file

@ -7,11 +7,11 @@ namespace Shlinkio\Shlink\Core\Matomo;
use MatomoTracker;
use Shlinkio\Shlink\Core\Exception\RuntimeException;
class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface
readonly class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface
{
public const MATOMO_DEFAULT_TIMEOUT = 10; // Time in seconds
public function __construct(private readonly MatomoOptions $options)
public function __construct(private MatomoOptions $options)
{
}

View file

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Matomo;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Matomo\Model\SendVisitsResult;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Repository\VisitIterationRepositoryInterface;
use Throwable;
readonly class MatomoVisitSender implements MatomoVisitSenderInterface
{
public function __construct(
private MatomoTrackerBuilderInterface $trackerBuilder,
private ShortUrlStringifier $shortUrlStringifier,
private VisitIterationRepositoryInterface $visitIterationRepository,
) {
}
/**
* Sends all visits in provided date range to matomo, and returns the amount of affected visits
*/
public function sendVisitsInDateRange(
DateRange $dateRange,
VisitSendingProgressTrackerInterface|null $progressTracker = null,
): SendVisitsResult {
$visitsIterator = $this->visitIterationRepository->findAllVisits($dateRange);
$successfulVisits = 0;
$failedVisits = 0;
foreach ($visitsIterator as $index => $visit) {
try {
$this->sendVisit($visit);
$progressTracker?->success($index);
$successfulVisits++;
} catch (Throwable $e) {
$progressTracker?->error($index, $e);
$failedVisits++;
}
}
return new SendVisitsResult($successfulVisits, $failedVisits);
}
public function sendVisit(Visit $visit, ?string $originalIpAddress = null): void
{
$tracker = $this->trackerBuilder->buildMatomoTracker();
$tracker
->setUrl($this->resolveUrlToTrack($visit))
->setCustomTrackingParameter('type', $visit->type->value)
->setUserAgent($visit->userAgent)
->setUrlReferrer($visit->referer)
->setForceVisitDateTime($visit->date->setTimezone('UTC')->toDateTimeString());
$location = $visit->getVisitLocation();
if ($location !== null) {
$tracker
->setCity($location->cityName)
->setCountry($location->countryName)
->setLatitude($location->latitude)
->setLongitude($location->longitude);
}
// Set not obfuscated IP if possible, as matomo handles obfuscation itself
$ip = $originalIpAddress ?? $visit->remoteAddr;
if ($ip !== null) {
$tracker->setIp($ip);
}
if ($visit->isOrphan()) {
$tracker->setCustomTrackingParameter('orphan', 'true');
}
// Send the short URL title or an empty document title to avoid different actions to be created by matomo
$tracker->doTrackPageView($visit->shortUrl?->title() ?? '');
}
private function resolveUrlToTrack(Visit $visit): string
{
$shortUrl = $visit->shortUrl;
if ($shortUrl === null) {
return $visit->visitedUrl ?? '';
}
return $this->shortUrlStringifier->stringify($shortUrl);
}
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Matomo;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Matomo\Model\SendVisitsResult;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
interface MatomoVisitSenderInterface
{
/**
* Sends all visits in provided date range to matomo, and returns the amount of affected visits
*/
public function sendVisitsInDateRange(
DateRange $dateRange,
VisitSendingProgressTrackerInterface|null $progressTracker = null,
): SendVisitsResult;
public function sendVisit(Visit $visit, ?string $originalIpAddress = null): void;
}

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Matomo\Model;
use Countable;
final readonly class SendVisitsResult implements Countable
{
/**
* @param int<0, max> $successfulVisits
* @param int<0, max> $failedVisits
*/
public function __construct(public int $successfulVisits = 0, public int $failedVisits = 0)
{
}
public function hasSuccesses(): bool
{
return $this->successfulVisits > 0;
}
public function hasFailures(): bool
{
return $this->failedVisits > 0;
}
public function count(): int
{
return $this->successfulVisits + $this->failedVisits;
}
}

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Matomo;
use Throwable;
interface VisitSendingProgressTrackerInterface
{
public function success(int $index): void;
public function error(int $index, Throwable $e): void;
}

View file

@ -4,14 +4,21 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Model;
final class Ordering
final readonly class Ordering
{
private const DEFAULT_DIR = 'ASC';
private const DESC_DIR = 'DESC';
private const ASC_DIR = 'ASC';
private const DEFAULT_DIR = self::ASC_DIR;
private function __construct(public readonly ?string $field, public readonly string $direction)
public function __construct(public ?string $field = null, public string $direction = self::DEFAULT_DIR)
{
}
public static function none(): self
{
return new self();
}
/**
* @param array{string|null, string|null} $props
*/
@ -21,13 +28,13 @@ final class Ordering
return new self($field, $dir ?? self::DEFAULT_DIR);
}
public static function emptyInstance(): self
public static function fromFieldAsc(string $field): self
{
return self::fromTuple([null, null]);
return new self($field, self::ASC_DIR);
}
public function hasOrderField(): bool
public static function fromFieldDesc(string $field): self
{
return $this->field !== null;
return new self($field, self::DESC_DIR);
}
}

View file

@ -8,15 +8,18 @@ use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\Options\DeleteShortUrlsOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ExpiredShortUrlsConditions;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ExpiredShortUrlsRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class DeleteShortUrlService implements DeleteShortUrlServiceInterface
readonly class DeleteShortUrlService implements DeleteShortUrlServiceInterface
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly DeleteShortUrlsOptions $deleteShortUrlsOptions,
private readonly ShortUrlResolverInterface $urlResolver,
private EntityManagerInterface $em,
private DeleteShortUrlsOptions $deleteShortUrlsOptions,
private ShortUrlResolverInterface $urlResolver,
private ExpiredShortUrlsRepositoryInterface $expiredShortUrlsRepository,
) {
}
@ -43,10 +46,18 @@ class DeleteShortUrlService implements DeleteShortUrlServiceInterface
private function isThresholdReached(ShortUrl $shortUrl): bool
{
if (! $this->deleteShortUrlsOptions->checkVisitsThreshold) {
return false;
return $this->deleteShortUrlsOptions->checkVisitsThreshold && $shortUrl->reachedVisits(
$this->deleteShortUrlsOptions->visitsThreshold,
);
}
return $shortUrl->getVisitsCount() >= $this->deleteShortUrlsOptions->visitsThreshold;
public function deleteExpiredShortUrls(ExpiredShortUrlsConditions $conditions): int
{
return $this->expiredShortUrlsRepository->delete($conditions);
}
public function countExpiredShortUrls(ExpiredShortUrlsConditions $conditions): int
{
return $this->expiredShortUrlsRepository->dryCount($conditions);
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\ShortUrl\Model\ExpiredShortUrlsConditions;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
@ -19,4 +20,14 @@ interface DeleteShortUrlServiceInterface
bool $ignoreThreshold = false,
?ApiKey $apiKey = null,
): void;
/**
* Deletes short URLs that are considered expired based on provided conditions
*/
public function deleteExpiredShortUrls(ExpiredShortUrlsConditions $conditions): int;
/**
* Counts short URLs that are considered expired based on provided conditions, without really deleting them
*/
public function countExpiredShortUrls(ExpiredShortUrlsConditions $conditions): int;
}

View file

@ -19,11 +19,14 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
use Shlinkio\Shlink\Core\Tag\Entity\Tag;
use Shlinkio\Shlink\Core\Visit\Entity\ShortUrlVisitsCount;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use function array_map;
use function count;
use function Shlinkio\Shlink\Core\generateRandomShortCode;
use function Shlinkio\Shlink\Core\normalizeDate;
@ -32,29 +35,32 @@ use function sprintf;
class ShortUrl extends AbstractEntity
{
private string $longUrl;
private string $shortCode;
private Chronos $dateCreated;
/** @var Collection<int, Visit> & Selectable */
private Collection & Selectable $visits;
/** @var Collection<int, Tag> */
private Collection $tags;
private ?Chronos $validSince = null;
private ?Chronos $validUntil = null;
private ?int $maxVisits = null;
private ?Domain $domain = null;
private bool $customSlugWasProvided;
private int $shortCodeLength;
private ?string $importSource = null;
private ?string $importOriginalShortCode = null;
private ?ApiKey $authorApiKey = null;
private ?string $title = null;
private bool $titleWasAutoResolved = false;
private bool $crawlable = false;
private bool $forwardQuery = true;
private function __construct()
{
/**
* @param Collection<int, Tag> $tags
* @param Collection<int, Visit> & Selectable $visits
* @param Collection<int, ShortUrlVisitsCount> & Selectable $visitsCounts
*/
private function __construct(
private string $longUrl,
private string $shortCode,
private Chronos $dateCreated = new Chronos(),
private Collection $tags = new ArrayCollection(),
private Collection & Selectable $visits = new ArrayCollection(),
private Collection & Selectable $visitsCounts = new ArrayCollection(),
private ?Chronos $validSince = null,
private ?Chronos $validUntil = null,
private ?int $maxVisits = null,
private ?Domain $domain = null,
private bool $customSlugWasProvided = false,
private int $shortCodeLength = 0,
public readonly ?ApiKey $authorApiKey = null,
private ?string $title = null,
private bool $titleWasAutoResolved = false,
private bool $crawlable = false,
private bool $forwardQuery = true,
private ?string $importSource = null,
private ?string $importOriginalShortCode = null,
) {
}
/**
@ -78,31 +84,29 @@ class ShortUrl extends AbstractEntity
ShortUrlCreation $creation,
?ShortUrlRelationResolverInterface $relationResolver = null,
): self {
$instance = new self();
$relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver();
$shortCodeLength = $creation->shortCodeLength;
$instance->longUrl = $creation->getLongUrl();
$instance->dateCreated = Chronos::now();
$instance->visits = new ArrayCollection();
$instance->tags = $relationResolver->resolveTags($creation->tags);
$instance->validSince = $creation->validSince;
$instance->validUntil = $creation->validUntil;
$instance->maxVisits = $creation->maxVisits;
$instance->customSlugWasProvided = $creation->hasCustomSlug();
$instance->shortCodeLength = $creation->shortCodeLength;
$instance->shortCode = sprintf(
return new self(
longUrl: $creation->getLongUrl(),
shortCode: sprintf(
'%s%s',
$creation->pathPrefix ?? '',
$creation->customSlug ?? generateRandomShortCode($instance->shortCodeLength, $creation->shortUrlMode),
$creation->customSlug ?? generateRandomShortCode($shortCodeLength, $creation->shortUrlMode),
),
tags: $relationResolver->resolveTags($creation->tags),
validSince: $creation->validSince,
validUntil: $creation->validUntil,
maxVisits: $creation->maxVisits,
domain: $relationResolver->resolveDomain($creation->domain),
customSlugWasProvided: $creation->hasCustomSlug(),
shortCodeLength: $shortCodeLength,
authorApiKey: $creation->apiKey,
title: $creation->title,
titleWasAutoResolved: $creation->titleWasAutoResolved,
crawlable: $creation->crawlable,
forwardQuery: $creation->forwardQuery,
);
$instance->domain = $relationResolver->resolveDomain($creation->domain);
$instance->authorApiKey = $creation->apiKey;
$instance->title = $creation->title;
$instance->titleWasAutoResolved = $creation->titleWasAutoResolved;
$instance->crawlable = $creation->crawlable;
$instance->forwardQuery = $creation->forwardQuery;
return $instance;
}
public static function fromImport(
@ -123,11 +127,11 @@ class ShortUrl extends AbstractEntity
$instance = self::create(ShortUrlCreation::fromRawData($meta), $relationResolver);
$instance->importSource = $url->source->value;
$instance->importOriginalShortCode = $url->shortCode;
$instance->validSince = normalizeOptionalDate($url->meta->validSince);
$instance->validUntil = normalizeOptionalDate($url->meta->validUntil);
$instance->dateCreated = normalizeDate($url->createdAt);
$instance->importSource = $url->source->value;
$instance->importOriginalShortCode = $url->shortCode;
return $instance;
}
@ -178,48 +182,24 @@ class ShortUrl extends AbstractEntity
return $this->shortCode;
}
public function getDateCreated(): Chronos
{
return $this->dateCreated;
}
public function getDomain(): ?Domain
{
return $this->domain;
}
/**
* @return Collection<int, Tag>
*/
public function getTags(): Collection
public function forwardQuery(): bool
{
return $this->tags;
return $this->forwardQuery;
}
public function authorApiKey(): ?ApiKey
public function title(): ?string
{
return $this->authorApiKey;
return $this->title;
}
public function getValidSince(): ?Chronos
public function reachedVisits(int $visitsAmount): bool
{
return $this->validSince;
}
public function getValidUntil(): ?Chronos
{
return $this->validUntil;
}
public function getVisitsCount(): int
{
return count($this->visits);
}
public function nonBotVisitsCount(): int
{
$criteria = Criteria::create()->where(Criteria::expr()->eq('potentialBot', false));
return count($this->visits->matching($criteria));
return count($this->visits) >= $visitsAmount;
}
public function mostRecentImportedVisitDate(): ?Chronos
@ -229,7 +209,7 @@ class ShortUrl extends AbstractEntity
->setMaxResults(1);
$visit = $this->visits->matching($criteria)->last();
return $visit instanceof Visit ? $visit->getDate() : null;
return $visit instanceof Visit ? $visit->date : null;
}
/**
@ -242,26 +222,6 @@ class ShortUrl extends AbstractEntity
return $this;
}
public function getMaxVisits(): ?int
{
return $this->maxVisits;
}
public function title(): ?string
{
return $this->title;
}
public function crawlable(): bool
{
return $this->crawlable;
}
public function forwardQuery(): bool
{
return $this->forwardQuery;
}
/**
* @throws ShortCodeCannotBeRegeneratedException
*/
@ -282,7 +242,7 @@ class ShortUrl extends AbstractEntity
public function isEnabled(): bool
{
$maxVisitsReached = $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
$maxVisitsReached = $this->maxVisits !== null && $this->reachedVisits($this->maxVisits);
if ($maxVisitsReached) {
return false;
}
@ -300,4 +260,29 @@ class ShortUrl extends AbstractEntity
return true;
}
public function toArray(?VisitsSummary $precalculatedSummary = null): array
{
return [
'shortCode' => $this->shortCode,
'longUrl' => $this->longUrl,
'dateCreated' => $this->dateCreated->toAtomString(),
'tags' => array_map(static fn (Tag $tag) => $tag->__toString(), $this->tags->toArray()),
'meta' => [
'validSince' => $this->validSince?->toAtomString(),
'validUntil' => $this->validUntil?->toAtomString(),
'maxVisits' => $this->maxVisits,
],
'domain' => $this->domain,
'title' => $this->title,
'crawlable' => $this->crawlable,
'forwardQuery' => $this->forwardQuery,
'visitsSummary' => $precalculatedSummary ?? VisitsSummary::fromTotalAndNonBots(
count($this->visits),
count($this->visits->matching(
Criteria::create()->where(Criteria::expr()->eq('potentialBot', false)),
)),
),
];
}
}

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Model;
final readonly class ExpiredShortUrlsConditions
{
public function __construct(public bool $pastValidUntil = true, public bool $maxVisitsReached = false)
{
}
public function hasConditions(): bool
{
return $this->pastValidUntil || $this->maxVisitsReached;
}
}

View file

@ -2,8 +2,6 @@
namespace Shlinkio\Shlink\Core\ShortUrl\Model;
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
enum OrderableField: string
{
case LONG_URL = 'longUrl';
@ -12,17 +10,4 @@ enum OrderableField: string
case TITLE = 'title';
case VISITS = 'visits';
case NON_BOT_VISITS = 'nonBotVisits';
public static function isBasicField(string $value): bool
{
return contains(
$value,
[self::LONG_URL->value, self::SHORT_CODE->value, self::DATE_CREATED->value, self::TITLE->value],
);
}
public static function isVisitsField(string $value): bool
{
return $value === self::VISITS->value || $value === self::NON_BOT_VISITS->value;
}
}

View file

@ -68,7 +68,7 @@ final readonly class ShortUrlCreation implements TitleResolutionModelInterface
ShortUrlInputFilter::SHORT_CODE_LENGTH,
) ?? DEFAULT_SHORT_CODES_LENGTH,
apiKey: $inputFilter->getValue(ShortUrlInputFilter::API_KEY),
tags: $inputFilter->getValue(ShortUrlInputFilter::TAGS),
tags: $inputFilter->getValue(ShortUrlInputFilter::TAGS) ?? [],
title: $inputFilter->getValue(ShortUrlInputFilter::TITLE),
crawlable: $inputFilter->getValue(ShortUrlInputFilter::CRAWLABLE),
forwardQuery: getOptionalBoolFromInputFilter($inputFilter, ShortUrlInputFilter::FORWARD_QUERY) ?? true,

View file

@ -60,7 +60,7 @@ final readonly class ShortUrlEdition implements TitleResolutionModelInterface
maxVisitsPropWasProvided: array_key_exists(ShortUrlInputFilter::MAX_VISITS, $data),
maxVisits: getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS),
tagsPropWasProvided: array_key_exists(ShortUrlInputFilter::TAGS, $data),
tags: $inputFilter->getValue(ShortUrlInputFilter::TAGS),
tags: $inputFilter->getValue(ShortUrlInputFilter::TAGS) ?? [],
titlePropWasProvided: array_key_exists(ShortUrlInputFilter::TITLE, $data),
title: $inputFilter->getValue(ShortUrlInputFilter::TITLE),
crawlablePropWasProvided: array_key_exists(ShortUrlInputFilter::CRAWLABLE, $data),

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Model;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary;
final readonly class ShortUrlWithVisitsSummary
{
private function __construct(public ShortUrl $shortUrl, private ?VisitsSummary $visitsSummary = null)
{
}
/**
* @param array{shortUrl: ShortUrl, visits: string|int, nonBotVisits: string|int} $data
*/
public static function fromArray(array $data): self
{
return new self($data['shortUrl'], VisitsSummary::fromTotalAndNonBots(
(int) $data['visits'],
(int) $data['nonBotVisits'],
));
}
public static function fromShortUrl(ShortUrl $shortUrl): self
{
return new self($shortUrl);
}
public function toArray(): array
{
return $this->shortUrl->toArray($this->visitsSummary);
}
}

View file

@ -11,13 +11,13 @@ use Shlinkio\Shlink\Core\ShortUrl\Persistence\ShortUrlsListFiltering;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlListRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ShortUrlRepositoryAdapter implements AdapterInterface
readonly class ShortUrlRepositoryAdapter implements AdapterInterface
{
public function __construct(
private readonly ShortUrlListRepositoryInterface $repository,
private readonly ShortUrlsParams $params,
private readonly ?ApiKey $apiKey,
private readonly string $defaultDomain,
private ShortUrlListRepositoryInterface $repository,
private ShortUrlsParams $params,
private ?ApiKey $apiKey,
private string $defaultDomain,
) {
}

View file

@ -13,9 +13,9 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ShortUrlsListFiltering extends ShortUrlsCountFiltering
{
public function __construct(
public readonly ?int $limit,
public readonly ?int $offset,
public readonly Ordering $orderBy,
public readonly ?int $limit = null,
public readonly ?int $offset = null,
public readonly Ordering $orderBy = new Ordering(),
?string $searchTerm = null,
array $tags = [],
?TagsMode $tagsMode = null,

View file

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Repository;
use Cake\Chronos\Chronos;
use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ExpiredShortUrlsConditions;
use Shlinkio\Shlink\Core\Visit\Entity\ShortUrlVisitsCount;
use function sprintf;
class ExpiredShortUrlsRepository extends EntitySpecificationRepository implements ExpiredShortUrlsRepositoryInterface
{
/**
* @inheritDoc
*/
public function delete(ExpiredShortUrlsConditions $conditions): int
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->delete(ShortUrl::class, 's');
return $this->applyConditions($qb, $conditions, fn () => (int) $qb->getQuery()->execute());
}
/**
* @inheritDoc
*/
public function dryCount(ExpiredShortUrlsConditions $conditions): int
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COUNT(s.id)')
->from(ShortUrl::class, 's');
return $this->applyConditions($qb, $conditions, fn () => (int) $qb->getQuery()->getSingleScalarResult());
}
/**
* @param callable(): int $getResultFromQueryBuilder
*/
private function applyConditions(
QueryBuilder $qb,
ExpiredShortUrlsConditions $conditions,
callable $getResultFromQueryBuilder,
): int {
if (! $conditions->hasConditions()) {
return 0;
}
if ($conditions->pastValidUntil) {
$qb
->where($qb->expr()->andX(
$qb->expr()->isNotNull('s.validUntil'),
$qb->expr()->lt('s.validUntil', ':now'),
))
->setParameter('now', Chronos::now()->toDateTimeString());
}
if ($conditions->maxVisitsReached) {
$qb->orWhere($qb->expr()->andX(
$qb->expr()->isNotNull('s.maxVisits'),
$qb->expr()->lte(
's.maxVisits',
sprintf(
'(SELECT COALESCE(SUM(vc.count), 0) FROM %s as vc WHERE vc.shortUrl=s)',
ShortUrlVisitsCount::class,
),
),
));
}
return $getResultFromQueryBuilder();
}
}

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Repository;
use Shlinkio\Shlink\Core\ShortUrl\Model\ExpiredShortUrlsConditions;
interface ExpiredShortUrlsRepositoryInterface
{
/**
* Delete expired short URLs matching provided conditions
*/
public function delete(ExpiredShortUrlsConditions $conditions): int;
/**
* Count how many expired short URLs would be deleted for provided conditions
*/
public function dryCount(ExpiredShortUrlsConditions $conditions): int;
}

View file

@ -11,62 +11,69 @@ use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\OrderableField;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Core\ShortUrl\Model\TagsMode;
use Shlinkio\Shlink\Core\ShortUrl\Persistence\ShortUrlsCountFiltering;
use Shlinkio\Shlink\Core\ShortUrl\Persistence\ShortUrlsListFiltering;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Entity\ShortUrlVisitsCount;
use function array_column;
use function Shlinkio\Shlink\Core\ArrayUtils\map;
use function sprintf;
class ShortUrlListRepository extends EntitySpecificationRepository implements ShortUrlListRepositoryInterface
{
/**
* @return ShortUrl[]
* @return ShortUrlWithVisitsSummary[]
*/
public function findList(ShortUrlsListFiltering $filtering): array
{
$buildVisitsSubQuery = function (string $alias, bool $excludingBots): string {
$vqb = $this->getEntityManager()->createQueryBuilder();
$vqb->select('COALESCE(SUM(' . $alias . '.count), 0)')
->from(ShortUrlVisitsCount::class, $alias)
->where($vqb->expr()->eq($alias . '.shortUrl', 's'));
if ($excludingBots) {
$vqb->andWhere($vqb->expr()->eq($alias . '.potentialBot', ':potentialBot'));
}
return $vqb->getDQL();
};
$qb = $this->createListQueryBuilder($filtering);
$qb->select('DISTINCT s')
$qb->select(
'DISTINCT s AS shortUrl',
'(' . $buildVisitsSubQuery('v', excludingBots: false) . ') AS ' . OrderableField::VISITS->value,
'(' . $buildVisitsSubQuery('v2', excludingBots: true) . ') AS ' . OrderableField::NON_BOT_VISITS->value,
// This is added only to have a consistent order by title between database engines
'COALESCE(s.title, \'\') AS title',
)
->setMaxResults($filtering->limit)
->setFirstResult($filtering->offset);
->setFirstResult($filtering->offset)
// This param is used in one of the sub-queries, but needs to set in the parent query
->setParameter('potentialBot', false);
$this->processOrderByForList($qb, $filtering);
/** @var array{shortUrl: ShortUrl, visits: string, nonBotVisits: string}[] $result */
$result = $qb->getQuery()->getResult();
if (OrderableField::isVisitsField($filtering->orderBy->field ?? '')) {
return array_column($result, 0);
}
return $result;
return map($result, static fn (array $s) => ShortUrlWithVisitsSummary::fromArray($s));
}
private function processOrderByForList(QueryBuilder $qb, ShortUrlsListFiltering $filtering): void
{
// With no explicit order by, fallback to dateCreated-DESC
$fieldName = $filtering->orderBy->field;
if ($fieldName === null) {
$qb->orderBy('s.dateCreated', 'DESC');
return;
}
$direction = $filtering->orderBy->direction;
[$sort, $order] = match (true) {
// With no explicit order by, fallback to dateCreated-DESC
$fieldName === null => ['s.dateCreated', 'DESC'],
$fieldName === OrderableField::VISITS->value,
$fieldName === OrderableField::NON_BOT_VISITS->value,
$fieldName === OrderableField::TITLE->value => [$fieldName, $direction],
default => ['s.' . $fieldName, $direction],
};
$order = $filtering->orderBy->direction;
if (OrderableField::isBasicField($fieldName)) {
$qb->orderBy('s.' . $fieldName, $order);
} elseif (OrderableField::isVisitsField($fieldName)) {
$leftJoinConditions = [$qb->expr()->eq('v.shortUrl', 's')];
if ($fieldName === OrderableField::NON_BOT_VISITS->value) {
$leftJoinConditions[] = $qb->expr()->eq('v.potentialBot', 'false');
}
// FIXME This query is inefficient.
// Diagnostic: It might need to use a sub-query, as done with the tags list query.
$qb->addSelect('COUNT(DISTINCT v)')
->leftJoin('s.visits', 'v', Join::WITH, $qb->expr()->andX(...$leftJoinConditions))
->groupBy('s')
->orderBy('COUNT(DISTINCT v)', $order);
}
$qb->orderBy($sort, $order);
}
public function countList(ShortUrlsCountFiltering $filtering): int
@ -139,7 +146,10 @@ class ShortUrlListRepository extends EntitySpecificationRepository implements Sh
$qb->expr()->isNull('s.maxVisits'),
$qb->expr()->gt(
's.maxVisits',
sprintf('(SELECT COUNT(innerV.id) FROM %s as innerV WHERE innerV.shortUrl=s)', Visit::class),
sprintf(
'(SELECT COALESCE(SUM(vc.count), 0) FROM %s as vc WHERE vc.shortUrl=s)',
ShortUrlVisitsCount::class,
),
),
));
}

View file

@ -4,14 +4,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Repository;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Core\ShortUrl\Persistence\ShortUrlsCountFiltering;
use Shlinkio\Shlink\Core\ShortUrl\Persistence\ShortUrlsListFiltering;
interface ShortUrlListRepositoryInterface
{
/**
* @return ShortUrl[]
* @return ShortUrlWithVisitsSummary[]
*/
public function findList(ShortUrlsListFiltering $filtering): array;

View file

@ -6,22 +6,22 @@ namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Core\ShortUrl\Paginator\Adapter\ShortUrlRepositoryAdapter;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlListRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ShortUrlListService implements ShortUrlListServiceInterface
readonly class ShortUrlListService implements ShortUrlListServiceInterface
{
public function __construct(
private readonly ShortUrlListRepositoryInterface $repo,
private readonly UrlShortenerOptions $urlShortenerOptions,
private ShortUrlListRepositoryInterface $repo,
private UrlShortenerOptions $urlShortenerOptions,
) {
}
/**
* @return ShortUrl[]|Paginator
* @return ShortUrlWithVisitsSummary[]|Paginator
*/
public function listShortUrls(ShortUrlsParams $params, ?ApiKey $apiKey = null): Paginator
{

View file

@ -5,14 +5,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface ShortUrlListServiceInterface
{
/**
* @return ShortUrl[]|Paginator
* @return ShortUrlWithVisitsSummary[]|Paginator
*/
public function listShortUrls(ShortUrlsParams $params, ?ApiKey $apiKey = null): Paginator;
}

View file

@ -7,50 +7,26 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Transformer;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
use Shlinkio\Shlink\Core\Tag\Entity\Tag;
use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use function array_map;
class ShortUrlDataTransformer implements DataTransformerInterface
/**
* @fixme Do not implement DataTransformerInterface, but a separate interface
*/
readonly class ShortUrlDataTransformer implements DataTransformerInterface
{
public function __construct(private readonly ShortUrlStringifierInterface $stringifier)
public function __construct(private ShortUrlStringifierInterface $stringifier)
{
}
/**
* @param ShortUrl $shortUrl
* @param ShortUrlWithVisitsSummary|ShortUrl $data
*/
public function transform($shortUrl): array // phpcs:ignore
public function transform($data): array // phpcs:ignore
{
$shortUrl = $data instanceof ShortUrlWithVisitsSummary ? $data->shortUrl : $data;
return [
'shortCode' => $shortUrl->getShortCode(),
'shortUrl' => $this->stringifier->stringify($shortUrl),
'longUrl' => $shortUrl->getLongUrl(),
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
'tags' => array_map(static fn (Tag $tag) => $tag->__toString(), $shortUrl->getTags()->toArray()),
'meta' => $this->buildMeta($shortUrl),
'domain' => $shortUrl->getDomain(),
'title' => $shortUrl->title(),
'crawlable' => $shortUrl->crawlable(),
'forwardQuery' => $shortUrl->forwardQuery(),
'visitsSummary' => VisitsSummary::fromTotalAndNonBots(
$shortUrl->getVisitsCount(),
$shortUrl->nonBotVisitsCount(),
),
];
}
private function buildMeta(ShortUrl $shortUrl): array
{
$validSince = $shortUrl->getValidSince();
$validUntil = $shortUrl->getValidUntil();
$maxVisits = $shortUrl->getMaxVisits();
return [
'validSince' => $validSince?->toAtomString(),
'validUntil' => $validUntil?->toAtomString(),
'maxVisits' => $maxVisits,
...$data->toArray(),
];
}
}

View file

@ -24,7 +24,7 @@ final class TagsParams extends AbstractInfinitePaginableListParams
{
return new self(
$query['searchTerm'] ?? null,
Ordering::fromTuple(isset($query['orderBy']) ? parseOrderBy($query['orderBy']) : [null, null]),
isset($query['orderBy']) ? Ordering::fromTuple(parseOrderBy($query['orderBy'])) : Ordering::none(),
isset($query['page']) ? (int) $query['page'] : null,
isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null,
);

View file

@ -76,19 +76,19 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito
$buildVisitsSubQb = static function (bool $excludeBots, string $aggregateAlias) use ($conn) {
$visitsSubQb = $conn->createQueryBuilder();
$commonJoinCondition = $visitsSubQb->expr()->eq('v.short_url_id', 's.id');
$commonJoinCondition = $visitsSubQb->expr()->eq('sc.short_url_id', 'st.short_url_id');
$visitsJoin = ! $excludeBots
? $commonJoinCondition
: $visitsSubQb->expr()->and(
$commonJoinCondition,
$visitsSubQb->expr()->eq('v.potential_bot', $conn->quote('0')),
$visitsSubQb->expr()->eq('sc.potential_bot', $conn->quote('0')),
)->__toString();
return $visitsSubQb
->select('st.tag_id AS tag_id', 'COUNT(DISTINCT v.id) AS ' . $aggregateAlias)
->from('visits', 'v')
->join('v', 'short_urls', 's', $visitsJoin)
->join('s', 'short_urls_in_tags', 'st', $visitsSubQb->expr()->eq('st.short_url_id', 's.id'))
->select('st.tag_id AS tag_id', 'SUM(sc.count) AS ' . $aggregateAlias)
->from('short_url_visits_counts', 'sc')
->join('sc', 'short_urls_in_tags', 'st', $visitsJoin)
->join('sc', 'short_urls', 's', $visitsSubQb->expr()->eq('sc.short_url_id', 's.id'))
->groupBy('st.tag_id');
};
$allVisitsSubQb = $buildVisitsSubQb(false, 'visits');

View file

@ -2,6 +2,9 @@
namespace Shlinkio\Shlink\Core\Util;
use Fig\Http\Message\RequestMethodInterface;
use Mezzio\Router\Route;
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
enum RedirectStatus: int
@ -16,8 +19,13 @@ enum RedirectStatus: int
return contains($this, [self::STATUS_301, self::STATUS_308]);
}
public function isLegacyStatus(): bool
/**
* @return array<RequestMethodInterface::METHOD_*>|Route::HTTP_METHOD_ANY
*/
public function allowedHttpMethods(): array|null
{
return contains($this, [self::STATUS_301, self::STATUS_302]);
return contains($this, [self::STATUS_301, self::STATUS_302])
? [RequestMethodInterface::METHOD_GET]
: Route::HTTP_METHOD_ANY;
}
}

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Entity;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
class OrphanVisitsCount extends AbstractEntity
{
public function __construct(
public readonly bool $potentialBot = false,
public readonly int $slotId = 1,
public readonly string $count = '1',
) {
}
}

View file

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Entity;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
class ShortUrlVisitsCount extends AbstractEntity
{
public function __construct(
private readonly ShortUrl $shortUrl,
private readonly bool $potentialBot = false,
public readonly int $slotId = 1,
public readonly string $count = '1',
) {
}
}

View file

@ -20,100 +20,55 @@ use function Shlinkio\Shlink\Core\normalizeDate;
class Visit extends AbstractEntity implements JsonSerializable
{
private string $referer;
private Chronos $date;
private ?string $remoteAddr = null;
private ?string $visitedUrl = null;
private string $userAgent;
private VisitType $type;
private ?ShortUrl $shortUrl;
private ?VisitLocation $visitLocation = null;
private bool $potentialBot;
private function __construct(?ShortUrl $shortUrl, VisitType $type)
{
$this->shortUrl = $shortUrl;
$this->date = Chronos::now();
$this->type = $type;
private function __construct(
public readonly ?ShortUrl $shortUrl,
public readonly VisitType $type,
public readonly string $userAgent,
public readonly string $referer,
public readonly bool $potentialBot,
public readonly ?string $remoteAddr = null,
public readonly ?string $visitedUrl = null,
private ?VisitLocation $visitLocation = null,
public readonly Chronos $date = new Chronos(),
) {
}
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
{
$instance = new self($shortUrl, VisitType::VALID_SHORT_URL);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
}
public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self
{
return self::fromImportOrOrphanImport($importedVisit, VisitType::IMPORTED, $shortUrl);
}
public static function fromOrphanImport(ImportedShlinkOrphanVisit $importedVisit): self
{
$instance = self::fromImportOrOrphanImport(
$importedVisit,
VisitType::tryFrom($importedVisit->type) ?? VisitType::IMPORTED,
);
$instance->visitedUrl = $importedVisit->visitedUrl;
return $instance;
}
private static function fromImportOrOrphanImport(
ImportedShlinkVisit|ImportedShlinkOrphanVisit $importedVisit,
VisitType $type,
?ShortUrl $shortUrl = null,
): self {
$instance = new self($shortUrl, $type);
$instance->userAgent = $importedVisit->userAgent;
$instance->potentialBot = isCrawler($instance->userAgent);
$instance->referer = $importedVisit->referer;
$instance->date = normalizeDate($importedVisit->date);
$importedLocation = $importedVisit->location;
$instance->visitLocation = $importedLocation !== null ? VisitLocation::fromImport($importedLocation) : null;
return $instance;
return self::fromVisitor($shortUrl, VisitType::VALID_SHORT_URL, $visitor, $anonymize);
}
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
{
$instance = new self(null, VisitType::BASE_URL);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
return self::fromVisitor(null, VisitType::BASE_URL, $visitor, $anonymize);
}
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
{
$instance = new self(null, VisitType::INVALID_SHORT_URL);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
return self::fromVisitor(null, VisitType::INVALID_SHORT_URL, $visitor, $anonymize);
}
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
{
$instance = new self(null, VisitType::REGULAR_404);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
return self::fromVisitor(null, VisitType::REGULAR_404, $visitor, $anonymize);
}
private function hydrateFromVisitor(Visitor $visitor, bool $anonymize = true): void
private static function fromVisitor(?ShortUrl $shortUrl, VisitType $type, Visitor $visitor, bool $anonymize): self
{
$this->userAgent = $visitor->userAgent;
$this->referer = $visitor->referer;
$this->remoteAddr = $this->processAddress($anonymize, $visitor->remoteAddress);
$this->visitedUrl = $visitor->visitedUrl;
$this->potentialBot = $visitor->isPotentialBot();
return new self(
shortUrl: $shortUrl,
type: $type,
userAgent: $visitor->userAgent,
referer: $visitor->referer,
potentialBot: $visitor->isPotentialBot(),
remoteAddr: self::processAddress($visitor->remoteAddress, $anonymize),
visitedUrl: $visitor->visitedUrl,
);
}
private function processAddress(bool $anonymize, ?string $address): ?string
private static function processAddress(?string $address, bool $anonymize): ?string
{
// Localhost addresses do not need to be anonymized
// Localhost address does not need to be anonymized
if (! $anonymize || $address === null || $address === IpAddress::LOCALHOST) {
return $address;
}
@ -125,9 +80,35 @@ class Visit extends AbstractEntity implements JsonSerializable
}
}
public function getRemoteAddr(): ?string
public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self
{
return $this->remoteAddr;
return self::fromImportOrOrphanImport($importedVisit, VisitType::IMPORTED, $shortUrl);
}
public static function fromOrphanImport(ImportedShlinkOrphanVisit $importedVisit): self
{
return self::fromImportOrOrphanImport(
$importedVisit,
VisitType::tryFrom($importedVisit->type) ?? VisitType::IMPORTED,
);
}
private static function fromImportOrOrphanImport(
ImportedShlinkVisit|ImportedShlinkOrphanVisit $importedVisit,
VisitType $type,
?ShortUrl $shortUrl = null,
): self {
$importedLocation = $importedVisit->location;
return new self(
shortUrl: $shortUrl,
type: $type,
userAgent: $importedVisit->userAgent,
referer: $importedVisit->referer,
potentialBot: isCrawler($importedVisit->userAgent),
visitedUrl: $importedVisit instanceof ImportedShlinkOrphanVisit ? $importedVisit->visitedUrl : null,
visitLocation: $importedLocation !== null ? VisitLocation::fromImport($importedLocation) : null,
date: normalizeDate($importedVisit->date),
);
}
public function hasRemoteAddr(): bool
@ -135,11 +116,6 @@ class Visit extends AbstractEntity implements JsonSerializable
return ! empty($this->remoteAddr);
}
public function getShortUrl(): ?ShortUrl
{
return $this->shortUrl;
}
public function getVisitLocation(): ?VisitLocation
{
return $this->visitLocation;
@ -161,51 +137,32 @@ class Visit extends AbstractEntity implements JsonSerializable
return $this->shortUrl === null;
}
public function visitedUrl(): ?string
{
return $this->visitedUrl;
}
public function type(): VisitType
{
return $this->type;
}
/**
* Needed only for ArrayCollections to be able to apply criteria filtering
* @internal
*/
public function getType(): VisitType
{
return $this->type();
}
/**
* @internal
*/
public function getDate(): Chronos
{
return $this->date;
}
public function userAgent(): string
{
return $this->userAgent;
}
public function referer(): string
{
return $this->referer;
return $this->type;
}
public function jsonSerialize(): array
{
return [
$base = [
'referer' => $this->referer,
'date' => $this->date->toAtomString(),
'userAgent' => $this->userAgent,
'visitLocation' => $this->visitLocation,
'potentialBot' => $this->potentialBot,
'visitedUrl' => $this->visitedUrl,
];
if (! $this->isOrphan()) {
return $base;
}
return [
...$base,
'type' => $this->type->value,
];
}
}

View file

@ -11,89 +11,54 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
class VisitLocation extends AbstractEntity implements JsonSerializable
{
private string $countryCode;
private string $countryName;
private string $regionName;
private string $cityName;
private float $latitude;
private float $longitude;
private string $timezone;
private bool $isEmpty;
public readonly bool $isEmpty;
private function __construct()
{
private function __construct(
public readonly string $countryCode,
public readonly string $countryName,
public readonly string $regionName,
public readonly string $cityName,
public readonly float $latitude,
public readonly float $longitude,
public readonly string $timezone,
) {
$this->isEmpty = (
$countryCode === '' &&
$countryName === '' &&
$regionName === '' &&
$cityName === '' &&
$latitude === 0.0 &&
$longitude === 0.0 &&
$timezone === ''
);
}
public static function fromGeolocation(Location $location): self
{
$instance = new self();
$instance->countryCode = $location->countryCode;
$instance->countryName = $location->countryName;
$instance->regionName = $location->regionName;
$instance->cityName = $location->city;
$instance->latitude = $location->latitude;
$instance->longitude = $location->longitude;
$instance->timezone = $location->timeZone;
$instance->computeIsEmpty();
return $instance;
return new self(
countryCode: $location->countryCode,
countryName: $location->countryName,
regionName: $location->regionName,
cityName: $location->city,
latitude: $location->latitude,
longitude: $location->longitude,
timezone: $location->timeZone,
);
}
public static function fromImport(ImportedShlinkVisitLocation $location): self
{
$instance = new self();
$instance->countryCode = $location->countryCode;
$instance->countryName = $location->countryName;
$instance->regionName = $location->regionName;
$instance->cityName = $location->cityName;
$instance->latitude = $location->latitude;
$instance->longitude = $location->longitude;
$instance->timezone = $location->timezone;
$instance->computeIsEmpty();
return $instance;
}
private function computeIsEmpty(): void
{
$this->isEmpty = (
$this->countryCode === '' &&
$this->countryName === '' &&
$this->regionName === '' &&
$this->cityName === '' &&
$this->latitude === 0.0 &&
$this->longitude === 0.0 &&
$this->timezone === ''
return new self(
countryCode: $location->countryCode,
countryName: $location->countryName,
regionName: $location->regionName,
cityName: $location->cityName,
latitude: $location->latitude,
longitude: $location->longitude,
timezone: $location->timezone,
);
}
public function getCountryName(): string
{
return $this->countryName;
}
public function getLatitude(): float
{
return $this->latitude;
}
public function getLongitude(): float
{
return $this->longitude;
}
public function getCityName(): string
{
return $this->cityName;
}
public function isEmpty(): bool
{
return $this->isEmpty;
}
public function jsonSerialize(): array
{
return [

View file

@ -8,14 +8,14 @@ use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Visit\Repository\VisitLocationRepositoryInterface;
use Shlinkio\Shlink\Core\Visit\Repository\VisitIterationRepositoryInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
class VisitLocator implements VisitLocatorInterface
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly VisitLocationRepositoryInterface $repo,
private readonly VisitIterationRepositoryInterface $repo,
) {
}

View file

@ -26,7 +26,7 @@ class VisitToLocationHelper implements VisitToLocationHelperInterface
throw IpCannotBeLocatedException::forEmptyAddress();
}
$ipAddr = $visit->getRemoteAddr() ?? '';
$ipAddr = $visit->remoteAddr ?? '';
if ($ipAddr === IpAddress::LOCALHOST) {
throw IpCannotBeLocatedException::forLocalhost();
}

View file

@ -0,0 +1,145 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Listener;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use function rand;
final class OrphanVisitsCountTracker
{
/** @var object[] */
private array $entitiesToBeCreated = [];
public function onFlush(OnFlushEventArgs $args): void
{
// Track entities that are going to be created during this flush operation
$this->entitiesToBeCreated = $args->getObjectManager()->getUnitOfWork()->getScheduledEntityInsertions();
}
/**
* @throws Exception
*/
public function postFlush(PostFlushEventArgs $args): void
{
$em = $args->getObjectManager();
$entitiesToBeCreated = $this->entitiesToBeCreated;
// Reset tracked entities until next flush operation
$this->entitiesToBeCreated = [];
foreach ($entitiesToBeCreated as $entity) {
$this->trackVisitCount($em, $entity);
}
}
/**
* @throws Exception
*/
private function trackVisitCount(EntityManagerInterface $em, object $entity): void
{
// This is not an orphan visit
if (! $entity instanceof Visit || ! $entity->isOrphan()) {
return;
}
$visit = $entity;
$isBot = $visit->potentialBot;
$conn = $em->getConnection();
$platformClass = $conn->getDatabasePlatform();
match ($platformClass::class) {
PostgreSQLPlatform::class => $this->incrementForPostgres($conn, $isBot),
SQLitePlatform::class, SQLServerPlatform::class => $this->incrementForOthers($conn, $isBot),
default => $this->incrementForMySQL($conn, $isBot),
};
}
/**
* @throws Exception
*/
private function incrementForMySQL(Connection $conn, bool $potentialBot): void
{
$this->incrementWithPreparedStatement($conn, $potentialBot, <<<QUERY
INSERT INTO orphan_visits_counts (potential_bot, slot_id, count)
VALUES (:potential_bot, RAND() * 100, 1)
ON DUPLICATE KEY UPDATE count = count + 1;
QUERY);
}
/**
* @throws Exception
*/
private function incrementForPostgres(Connection $conn, bool $potentialBot): void
{
$this->incrementWithPreparedStatement($conn, $potentialBot, <<<QUERY
INSERT INTO orphan_visits_counts (potential_bot, slot_id, count)
VALUES (:potential_bot, random() * 100, 1)
ON CONFLICT (potential_bot, slot_id) DO UPDATE
SET count = orphan_visits_counts.count + 1;
QUERY);
}
/**
* @throws Exception
*/
private function incrementWithPreparedStatement(Connection $conn, bool $potentialBot, string $query): void
{
$statement = $conn->prepare($query);
$statement->bindValue('potential_bot', $potentialBot ? 1 : 0);
$statement->executeStatement();
}
/**
* @throws Exception
*/
private function incrementForOthers(Connection $conn, bool $potentialBot): void
{
$slotId = rand(1, 100);
// For engines without a specific UPSERT syntax, do a regular locked select followed by an insert or update
$qb = $conn->createQueryBuilder();
$qb->select('id')
->from('orphan_visits_counts')
->where($qb->expr()->and(
$qb->expr()->eq('potential_bot', ':potential_bot'),
$qb->expr()->eq('slot_id', ':slot_id'),
))
->setParameter('potential_bot', $potentialBot ? '1' : '0')
->setParameter('slot_id', $slotId)
->setMaxResults(1);
if ($conn->getDatabasePlatform()::class === SQLServerPlatform::class) {
$qb->forUpdate();
}
$visitsCountId = $qb->executeQuery()->fetchOne();
$writeQb = ! $visitsCountId
? $conn->createQueryBuilder()
->insert('orphan_visits_counts')
->values([
'potential_bot' => ':potential_bot',
'slot_id' => ':slot_id',
])
->setParameter('potential_bot', $potentialBot ? '1' : '0')
->setParameter('slot_id', $slotId)
: $conn->createQueryBuilder()
->update('orphan_visits_counts')
->set('count', 'count + 1')
->where($qb->expr()->eq('id', ':visits_count_id'))
->setParameter('visits_count_id', $visitsCountId);
$writeQb->executeStatement();
}
}

View file

@ -0,0 +1,160 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Listener;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use function rand;
final class ShortUrlVisitsCountTracker
{
/** @var object[] */
private array $entitiesToBeCreated = [];
public function onFlush(OnFlushEventArgs $args): void
{
// Track entities that are going to be created during this flush operation
$this->entitiesToBeCreated = $args->getObjectManager()->getUnitOfWork()->getScheduledEntityInsertions();
}
/**
* @throws Exception
*/
public function postFlush(PostFlushEventArgs $args): void
{
$em = $args->getObjectManager();
$entitiesToBeCreated = $this->entitiesToBeCreated;
// Reset tracked entities until next flush operation
$this->entitiesToBeCreated = [];
foreach ($entitiesToBeCreated as $entity) {
$this->trackVisitCount($em, $entity);
}
}
/**
* @throws Exception
*/
private function trackVisitCount(EntityManagerInterface $em, object $entity): void
{
// This is not a visit
if (!$entity instanceof Visit) {
return;
}
$visit = $entity;
// The short URL is not persisted yet or this is an orphan visit
$shortUrlId = $visit->shortUrl?->getId();
if ($shortUrlId === null || $shortUrlId === '') {
return;
}
$isBot = $visit->potentialBot;
$conn = $em->getConnection();
$platformClass = $conn->getDatabasePlatform();
match ($platformClass::class) {
PostgreSQLPlatform::class => $this->incrementForPostgres($conn, $shortUrlId, $isBot),
SQLitePlatform::class, SQLServerPlatform::class => $this->incrementForOthers($conn, $shortUrlId, $isBot),
default => $this->incrementForMySQL($conn, $shortUrlId, $isBot),
};
}
/**
* @throws Exception
*/
private function incrementForMySQL(Connection $conn, string $shortUrlId, bool $potentialBot): void
{
$this->incrementWithPreparedStatement($conn, $shortUrlId, $potentialBot, <<<QUERY
INSERT INTO short_url_visits_counts (short_url_id, potential_bot, slot_id, count)
VALUES (:short_url_id, :potential_bot, RAND() * 100, 1)
ON DUPLICATE KEY UPDATE count = count + 1;
QUERY);
}
/**
* @throws Exception
*/
private function incrementForPostgres(Connection $conn, string $shortUrlId, bool $potentialBot): void
{
$this->incrementWithPreparedStatement($conn, $shortUrlId, $potentialBot, <<<QUERY
INSERT INTO short_url_visits_counts (short_url_id, potential_bot, slot_id, count)
VALUES (:short_url_id, :potential_bot, random() * 100, 1)
ON CONFLICT (short_url_id, potential_bot, slot_id) DO UPDATE
SET count = short_url_visits_counts.count + 1;
QUERY);
}
/**
* @throws Exception
*/
private function incrementWithPreparedStatement(
Connection $conn,
string $shortUrlId,
bool $potentialBot,
string $query,
): void {
$statement = $conn->prepare($query);
$statement->bindValue('short_url_id', $shortUrlId);
$statement->bindValue('potential_bot', $potentialBot ? 1 : 0);
$statement->executeStatement();
}
/**
* @throws Exception
*/
private function incrementForOthers(Connection $conn, string $shortUrlId, bool $potentialBot): void
{
$slotId = rand(1, 100);
// For engines without a specific UPSERT syntax, do a regular locked select followed by an insert or update
$qb = $conn->createQueryBuilder();
$qb->select('id')
->from('short_url_visits_counts')
->where($qb->expr()->and(
$qb->expr()->eq('short_url_id', ':short_url_id'),
$qb->expr()->eq('potential_bot', ':potential_bot'),
$qb->expr()->eq('slot_id', ':slot_id'),
))
->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot ? '1' : '0')
->setParameter('slot_id', $slotId)
->setMaxResults(1);
if ($conn->getDatabasePlatform()::class === SQLServerPlatform::class) {
$qb->forUpdate();
}
$visitsCountId = $qb->executeQuery()->fetchOne();
$writeQb = ! $visitsCountId
? $conn->createQueryBuilder()
->insert('short_url_visits_counts')
->values([
'short_url_id' => ':short_url_id',
'potential_bot' => ':potential_bot',
'slot_id' => ':slot_id',
])
->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot ? '1' : '0')
->setParameter('slot_id', $slotId)
: $conn->createQueryBuilder()
->update('short_url_visits_counts')
->set('count', 'count + 1')
->where($qb->expr()->eq('id', ':visits_count_id'))
->setParameter('visits_count_id', $visitsCountId);
$writeQb->executeStatement();
}
}

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Core\Visit\Entity\OrphanVisitsCount;
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering;
use Shlinkio\Shlink\Rest\ApiKey\Role;
class OrphanVisitsCountRepository extends EntitySpecificationRepository implements OrphanVisitsCountRepositoryInterface
{
public function countOrphanVisits(VisitsCountFiltering $filtering): int
{
if ($filtering->apiKey?->hasRole(Role::NO_ORPHAN_VISITS)) {
return 0;
}
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COALESCE(SUM(vc.count), 0)')
->from(OrphanVisitsCount::class, 'vc');
if ($filtering->excludeBots) {
$qb->andWhere($qb->expr()->eq('vc.potentialBot', ':potentialBot'))
->setParameter('potentialBot', false);
}
return (int) $qb->getQuery()->getSingleScalarResult();
}
}

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering;
interface OrphanVisitsCountRepositoryInterface
{
public function countOrphanVisits(VisitsCountFiltering $filtering): int;
}

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Core\Visit\Entity\ShortUrlVisitsCount;
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering;
class ShortUrlVisitsCountRepository extends EntitySpecificationRepository implements
ShortUrlVisitsCountRepositoryInterface
{
public function countNonOrphanVisits(VisitsCountFiltering $filtering): int
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COALESCE(SUM(vc.count), 0)')
->from(ShortUrlVisitsCount::class, 'vc')
->join('vc.shortUrl', 's');
if ($filtering->excludeBots) {
$qb->andWhere($qb->expr()->eq('vc.potentialBot', ':potentialBot'))
->setParameter('potentialBot', false);
}
$this->applySpecification($qb, $filtering->apiKey?->spec(), 's');
return (int) $qb->getQuery()->getSingleScalarResult();
}
}

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering;
interface ShortUrlVisitsCountRepositoryInterface
{
public function countNonOrphanVisits(VisitsCountFiltering $filtering): int;
}

View file

@ -6,9 +6,14 @@ namespace Shlinkio\Shlink\Core\Visit\Repository;
use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
class VisitLocationRepository extends EntitySpecificationRepository implements VisitLocationRepositoryInterface
/**
* Allows iterating large amounts of visits in a memory-efficient way, to use in batch processes
*/
class VisitIterationRepository extends EntitySpecificationRepository implements VisitIterationRepositoryInterface
{
/**
* @return iterable<Visit>
@ -42,9 +47,18 @@ class VisitLocationRepository extends EntitySpecificationRepository implements V
/**
* @return iterable<Visit>
*/
public function findAllVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable
public function findAllVisits(?DateRange $dateRange = null, int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable
{
$qb = $this->createQueryBuilder('v');
if ($dateRange?->startDate !== null) {
$qb->andWhere($qb->expr()->gte('v.date', ':since'))
->setParameter('since', $dateRange->startDate, ChronosDateTimeType::CHRONOS_DATETIME);
}
if ($dateRange?->endDate !== null) {
$qb->andWhere($qb->expr()->lte('v.date', ':until'))
->setParameter('until', $dateRange->endDate, ChronosDateTimeType::CHRONOS_DATETIME);
}
return $this->visitsIterableForQuery($qb, $blockSize);
}

View file

@ -4,9 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
interface VisitLocationRepositoryInterface
interface VisitIterationRepositoryInterface
{
public const DEFAULT_BLOCK_SIZE = 10000;
@ -23,5 +24,5 @@ interface VisitLocationRepositoryInterface
/**
* @return iterable<Visit>
*/
public function findAllVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable;
public function findAllVisits(?DateRange $dateRange = null, int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable;
}

Some files were not shown because too many files have changed in this diff Show more