diff --git a/.github/workflows/ci-db-tests.yml b/.github/workflows/ci-db-tests.yml index dd797e836..8cea11f78 100644 --- a/.github/workflows/ci-db-tests.yml +++ b/.github/workflows/ci-db-tests.yml @@ -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 diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index ea26ccd78..d2cf4d9ae 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index e646df824..08738f515 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,47 @@ 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). +## [4.1.0] - 2024-04-14 +### Added +* [#1330](https://github.com/shlinkio/shlink/issues/1330) All visit-related endpoints now expose the `visitedUrl` prop for any visit. + + Previously, this was exposed only for orphan visits, since this can be an arbitrary value for those. + +* [#2077](https://github.com/shlinkio/shlink/issues/2077) When sending visits to Matomo, the short URL title is now used as document title in matomo. +* [#2059](https://github.com/shlinkio/shlink/issues/2059) Add new `short-url:delete-expired` command that can be used to programmatically delete expired short URLs. + + Expired short URLs are those that have a `validUntil` date in the past, or optionally, that have reached the max amount of visits. + + This command can be run periodically by those who create many disposable URLs which are valid only for a period of time, and then can be deleted to save space. + +* [#1925](https://github.com/shlinkio/shlink/issues/1925) Add new `integration:matomo:send-visits` console command that can be used to send existing visits to integrated Matomo instance. +* [#2087](https://github.com/shlinkio/shlink/issues/2087) Allow `memory_limit` to be configured via the new `MEMORY_LIMIT` env var or configuration option. + +### Changed +* [#2034](https://github.com/shlinkio/shlink/issues/2034) Modernize entities, using constructor property promotion and readonly wherever possible. +* [#2036](https://github.com/shlinkio/shlink/issues/2036) Deep performance improvement in some endpoints which involve counting visits: + + * listing short URLs ordered by visits counts. + * loading tags with stats. + * visits overview. + + This has been achieved by introducing a new table which tracks slotted visits counts. We can then `SUM` all counts for certain short URL, avoiding `COUNT(visits)` aggregates which are much less performant when there are a lot of visits. + +* [#2049](https://github.com/shlinkio/shlink/issues/2049) Request ID is now propagated to the background tasks/jobs scheduled during a request. + + This allows for a better traceability, as the logs generated during those jobs will have a matching UUID as the logs generated during the request the triggered the job. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#2095](https://github.com/shlinkio/shlink/issues/2095) Fix custom slugs not being properly imported from bitly +* Fix error when importing short URLs and visits from a Shlink 4.x instance + + ## [4.0.3] - 2024-03-15 ### Added * *Nothing* @@ -783,2073 +824,3 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ### Fixed * *Nothing* - - -## [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 `-` 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. - - -## [1.21.1] - 2020-01-02 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#596](https://github.com/shlinkio/shlink/issues/596) Fixed error when trying to download GeoLite2 database due to changes on how to get the database files. - - -## [1.21.0] - 2019-12-29 -### Added -* [#118](https://github.com/shlinkio/shlink/issues/118) API errors now implement the [problem details](https://tools.ietf.org/html/rfc7807) standard. - - In order to make it backwards compatible, two things have been done: - - * Both the old `error` and `message` properties have been kept on error response, containing the same values as the `type` and `detail` properties respectively. - * The API `v2` has been enabled. If an error occurs when calling the API with this version, the `error` and `message` properties will not be returned. - - > After Shlink v2 is released, both API versions will behave like API v2. - -* [#575](https://github.com/shlinkio/shlink/issues/575) Added support to filter short URL lists by date ranges. - - * The `GET /short-urls` endpoint now accepts the `startDate` and `endDate` query params. - * The `short-urls:list` command now allows `--startDate` and `--endDate` flags to be optionally provided. - -* [#338](https://github.com/shlinkio/shlink/issues/338) Added support to asynchronously notify external services via webhook, only when shlink is served with swoole. - - Configured webhooks will receive a POST request every time a URL receives a visit, including information about the short URL and the visit. - - The payload will look like this: - - ```json - { - "shortUrl": {}, - "visit": {} - } - ``` - - > The `shortUrl` and `visit` props have the same shape as it is defined in the [API spec](https://api-spec.shlink.io). - -### Changed -* [#492](https://github.com/shlinkio/shlink/issues/492) Updated to monolog 2, together with other dependencies, like Symfony 5 and infection-php. -* [#527](https://github.com/shlinkio/shlink/issues/527) Increased minimum required mutation score for unit tests to 80%. -* [#557](https://github.com/shlinkio/shlink/issues/557) Added a few php.ini configs for development and production docker images. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#570](https://github.com/shlinkio/shlink/issues/570) Fixed shlink version generated for docker images when building from `develop` branch. - - -## [1.20.3] - 2019-12-23 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#585](https://github.com/shlinkio/shlink/issues/585) Fixed `PHP Fatal error: Uncaught Error: Class 'Shlinkio\Shlink\LocalLockFactory' not found` happening when running some CLI commands. - - -## [1.20.2] - 2019-12-06 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#561](https://github.com/shlinkio/shlink/issues/561) Fixed `db:migrate` command failing because yaml extension is not installed, which makes config file not to be readable. -* [#562](https://github.com/shlinkio/shlink/issues/562) Fixed internal server error being returned when renaming a tag to another tag's name. Now a meaningful API error with status 409 is returned. -* [#555](https://github.com/shlinkio/shlink/issues/555) Fixed internal server error being returned when invalid dates are provided for new short URLs. Now a 400 is returned, as intended. - - -## [1.20.1] - 2019-11-17 -### Added -* [#519](https://github.com/shlinkio/shlink/issues/519) Documented how to customize web workers and task workers for the docker image. - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#512](https://github.com/shlinkio/shlink/issues/512) Fixed query params not being properly forwarded from short URL to long one. -* [#540](https://github.com/shlinkio/shlink/issues/540) Fixed errors thrown when creating short URLs if the original URL has an internationalized domain name and URL validation is enabled. -* [#528](https://github.com/shlinkio/shlink/issues/528) Ensured `db:create` and `db:migrate` commands do not silently fail when run as part of `install` or `update`. -* [#518](https://github.com/shlinkio/shlink/issues/518) Fixed service which updates Geolite db file to use a local lock instead of a shared one, since every shlink instance holds its own db instance. - - -## [1.20.0] - 2019-11-02 -### Added -* [#491](https://github.com/shlinkio/shlink/issues/491) Added improved short code generation logic. - - Now, short codes are truly random, which removes the guessability factor existing in previous versions. - - Generated short codes have 5 characters, and shlink makes sure they keep unique, while making it backwards-compatible. - -* [#418](https://github.com/shlinkio/shlink/issues/418) and [#419](https://github.com/shlinkio/shlink/issues/419) Added support to redirect any 404 error to a custom URL. - - It was already possible to configure this but only for invalid short URLs. Shlink now also support configuring redirects for the base URL and any other kind of "not found" error. - - The three URLs can be different, and it is already possible to pass them to the docker image via configuration or env vars. - - The installer also asks for these two new configuration options. - -* [#497](https://github.com/shlinkio/shlink/issues/497) Officially added support for MariaDB. - -### Changed -* [#458](https://github.com/shlinkio/shlink/issues/458) Updated coding styles to use [shlinkio/php-coding-standard](https://github.com/shlinkio/php-coding-standard) v2.0.0. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#507](https://github.com/shlinkio/shlink/issues/507) Fixed error with too long original URLs by increasing size to the maximum value (2048) based on [the standard](https://stackoverflow.com/a/417184). -* [#502](https://github.com/shlinkio/shlink/issues/502) Fixed error when providing the port as part of the domain on short URLs. -* [#509](https://github.com/shlinkio/shlink/issues/509) Fixed error when trying to generate a QR code for a short URL which uses a custom domain. -* [#522](https://github.com/shlinkio/shlink/issues/522) Highly mitigated errors thrown when lots of short URLs are created concurrently including new and existing tags. - - -## [1.19.0] - 2019-10-05 -### Added -* [#482](https://github.com/shlinkio/shlink/issues/482) Added support to serve shlink under a sub path. - - The `router.base_path` config option can be defined now to set the base path from which shlink is served. - - ```php - return [ - 'router' => [ - 'base_path' => '/foo/bar', - ], - ]; - ``` - - This option will also be available on shlink-installer 1.3.0, so the installer will ask for it. It can also be provided for the docker image as the `BASE_PATH` env var. - -* [#479](https://github.com/shlinkio/shlink/issues/479) Added preliminary support for multiple domains. - - Endpoints and commands which create short URLs support providing the `domain` now (via query param or CLI flag). If not provided, the short URLs will still be "attached" to the default domain. - - Custom slugs can be created on multiple domains, allowing to share links like `https://s.test/my-campaign` and `https://example.com/my-campaign`, under the same shlink instance. - - When resolving a short URL to redirect end users, the following rules are applied: - - * If the domain used for the request plus the short code/slug are found, the user is redirected to that long URL and the visit is tracked. - * If the domain is not known but the short code/slug is defined for default domain, the user is redirected there and the visit is tracked. - * In any other case, no redirection happens and no visit is tracked (if a fall back redirection is configured for not-found URLs, it will still happen). - -### Changed -* [#486](https://github.com/shlinkio/shlink/issues/486) Updated to [shlink-installer](https://github.com/shlinkio/shlink-installer) v2, which supports asking for base path in which shlink is served. - -### Deprecated -* *Nothing* - -### Removed -* [#435](https://github.com/shlinkio/shlink/issues/435) Removed translations for error pages. All error pages are in english now. - -### Fixed -* *Nothing* - - -## [1.18.1] - 2019-08-24 -### Added -* *Nothing* - -### Changed -* [#450](https://github.com/shlinkio/shlink/issues/450) Added PHP 7.4 to the build matrix, as an allowed-to-fail env. -* [#441](https://github.com/shlinkio/shlink/issues/441) and [#443](https://github.com/shlinkio/shlink/issues/443) Split some logic into independent modules. -* [#451](https://github.com/shlinkio/shlink/issues/451) Updated to infection 0.13. -* [#467](https://github.com/shlinkio/shlink/issues/467) Moved docker image config to main Shlink repo. - -### Deprecated -* [#428](https://github.com/shlinkio/shlink/issues/428) Deprecated preview-generation feature. It will keep working but it will be removed in Shlink v2.0.0 - -### Removed -* [#468](https://github.com/shlinkio/shlink/issues/468) Removed APCu extension from docker image. - -### Fixed -* [#449](https://github.com/shlinkio/shlink/issues/449) Fixed error when trying to save too big referrers on PostgreSQL. - - -## [1.18.0] - 2019-08-08 -### Added -* [#411](https://github.com/shlinkio/shlink/issues/411) Added new `meta` property on the `ShortUrl` REST API model. - - These endpoints are affected and include the new property when suitable: - - * `GET /short-urls` - List short URLs. - * `GET /short-urls/shorten` - Create a short URL (for integrations). - * `GET /short-urls/{shortCode}` - Get one short URL. - * `POST /short-urls` - Create short URL. - - The property includes the values `validSince`, `validUntil` and `maxVisits` in a single object. All of them are nullable. - - ```json - { - "validSince": "2016-01-01T00:00:00+02:00", - "validUntil": null, - "maxVisits": 100 - } - ``` - -* [#285](https://github.com/shlinkio/shlink/issues/285) Visit location resolution is now done asynchronously but in real time thanks to swoole task management. - - Now, when a short URL is visited, a task is enqueued to locate it. The user is immediately redirected to the long URL, and in the background, the visit is located, making stats to be available a couple of seconds after the visit without the requirement of cronjobs being run constantly. - - Sadly, this feature is not enabled when serving shlink via apache/nginx, where you should still rely on cronjobs. - -* [#384](https://github.com/shlinkio/shlink/issues/384) Improved how remote IP addresses are detected. - - This new set of headers is now also inspected looking for the IP address: - - * CF-Connecting-IP - * True-Client-IP - * X-Real-IP - -* [#440](https://github.com/shlinkio/shlink/pull/440) Created `db:create` command, which improves how the shlink database is created, with these benefits: - - * It sets up a lock which prevents the command to be run concurrently. - * It checks of the database does not exist, and creates it in that case. - * It checks if the database tables already exist, exiting gracefully in that case. - -* [#442](https://github.com/shlinkio/shlink/pull/442) Created `db:migrate` command, which improves doctrine's migrations command by generating a lock, preventing it to be run concurrently. - -### Changed -* [#430](https://github.com/shlinkio/shlink/issues/430) Updated to [shlinkio/php-coding-standard](https://github.com/shlinkio/php-coding-standard) 1.2.2 -* [#305](https://github.com/shlinkio/shlink/issues/305) Implemented changes which will allow Shlink to be truly clusterizable. -* [#262](https://github.com/shlinkio/shlink/issues/262) Increased mutation score to 75%. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#416](https://github.com/shlinkio/shlink/issues/416) Fixed error thrown when trying to locate visits after the GeoLite2 DB is downloaded for the first time. -* [#424](https://github.com/shlinkio/shlink/issues/424) Updated wkhtmltoimage to version 0.12.5 -* [#427](https://github.com/shlinkio/shlink/issues/427) and [#434](https://github.com/shlinkio/shlink/issues/434) Fixed shlink being unusable after a database error on swoole contexts. - - -## [1.17.0] - 2019-05-13 -### Added -* [#377](https://github.com/shlinkio/shlink/issues/377) Updated `visit:locate` command (formerly `visit:process`) to automatically update the GeoLite2 database if it is too old or it does not exist. - - This simplifies processing visits in a container-based infrastructure, since a fresh container is capable of getting an updated version of the file by itself. - - It also removes the need of asynchronously and programmatically updating the file, which deprecates the `visit:update-db` command. - -* [#373](https://github.com/shlinkio/shlink/issues/373) Added support for a simplified config. Specially useful to use with the docker container. - -### Changed -* [#56](https://github.com/shlinkio/shlink/issues/56) Simplified supported cache, requiring APCu always. - -### Deprecated -* [#406](https://github.com/shlinkio/shlink/issues/406) Deprecated `PUT /short-urls/{shortCode}` REST endpoint in favor of `PATCH /short-urls/{shortCode}`. - -### Removed -* [#385](https://github.com/shlinkio/shlink/issues/385) Dropped support for PHP 7.1 -* [#379](https://github.com/shlinkio/shlink/issues/379) Removed copyright from error templates. - -### Fixed -* *Nothing* - - -## [1.16.3] - 2019-03-30 -### Added -* *Nothing* - -### Changed -* [#153](https://github.com/shlinkio/shlink/issues/153) Updated to [doctrine/migrations](https://github.com/doctrine/migrations) version 2.0.0 -* [#376](https://github.com/shlinkio/shlink/issues/376) Allowed `visit:update-db` command to not return an error exit code even if download fails, by passing the `-i` flag. -* [#341](https://github.com/shlinkio/shlink/issues/341) Improved database tests so that they are executed against all supported database engines. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#382](https://github.com/shlinkio/shlink/issues/382) Fixed existing short URLs not properly checked when providing the `findIfExists` flag. - - -## [1.16.2] - 2019-03-05 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#368](https://github.com/shlinkio/shlink/issues/368) Fixed error produced when running a `SELECT COUNT(...)` with `ORDER BY` in PostgreSQL databases. - - -## [1.16.1] - 2019-02-26 -### Added -* *Nothing* - -### Changed -* [#363](https://github.com/shlinkio/shlink/issues/363) Updated to `shlinkio/php-coding-standard` version 1.1.0 - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#362](https://github.com/shlinkio/shlink/issues/362) Fixed all visits without an IP address being processed every time the `visit:process` command is executed. - - -## [1.16.0] - 2019-02-23 -### Added -* [#304](https://github.com/shlinkio/shlink/issues/304) Added health endpoint to check healthiness of the service. Useful in container-based infrastructures. - - Call [GET /rest/health] in order to get a response like this: - - ```http - HTTP/1.1 200 OK - Content-Type: application/health+json - Content-Length: 681 - - { - "status": "pass", - "version": "1.16.0", - "links": { - "about": "https://shlink.io", - "project": "https://github.com/shlinkio/shlink" - } - } - ``` - - The status code can be `200 OK` in case of success or `503 Service Unavailable` in case of error, while the `status` property will be one of `pass` or `fail`, as defined in the [Health check RFC](https://inadarei.github.io/rfc-healthcheck/). - -* [#279](https://github.com/shlinkio/shlink/issues/279) Added new `findIfExists` flag to the `[POST /short-url]` REST endpoint and the `short-urls:generate` CLI command. It can be used to return existing short URLs when found, instead of creating new ones. - - Thanks to this flag you won't need to remember if you created a short URL for a long one. It will just create it if needed or return the existing one if possible. - - The behavior might be a little bit counterintuitive when combined with other params. This is how the endpoint behaves when providing this new flag: - - * Only the long URL is provided: It will return the newest match or create a new short URL if none is found. - * Long url and custom slug are provided: It will return the short URL when both params match, return an error when the slug is in use for another long URL, or create a new short URL otherwise. - * Any of the above but including other params (tags, validSince, validUntil, maxVisits): It will behave the same as the previous two cases, but it will try to exactly match existing results using all the params. If any of them does not match, it will try to create a new short URL. - -* [#336](https://github.com/shlinkio/shlink/issues/336) Added an API test suite which performs API calls to an actual instance of the web service. - -### Changed -* [#342](https://github.com/shlinkio/shlink/issues/342) The installer no longer asks for a charset to be provided, and instead, it shuffles the base62 charset. -* [#320](https://github.com/shlinkio/shlink/issues/320) Replaced query builder by plain DQL for all queries which do not need to be dynamically generated. -* [#330](https://github.com/shlinkio/shlink/issues/330) No longer allow failures on PHP 7.3 envs during project CI build. -* [#335](https://github.com/shlinkio/shlink/issues/335) Renamed functional test suite to database test suite, since that better describes what it actually does. -* [#346](https://github.com/shlinkio/shlink/issues/346) Extracted installer as an independent tool. -* [#261](https://github.com/shlinkio/shlink/issues/261) Increased mutation score to 70%. - -### Deprecated -* [#351](https://github.com/shlinkio/shlink/issues/351) Deprecated `config:generate-charset` and `config:generate-secret` CLI commands. - -### Removed -* *Nothing* - -### Fixed -* [#317](https://github.com/shlinkio/shlink/issues/317) Fixed error while trying to generate previews because of global config file being deleted by mistake by build script. -* [#307](https://github.com/shlinkio/shlink/issues/307) Fixed memory leak while trying to process huge amounts of visits due to the query not being properly paginated. - - -## [1.15.1] - 2018-12-16 -### Added -* [#162](https://github.com/shlinkio/shlink/issues/162) Added non-rest endpoints to swagger definition. - -### Changed -* [#312](https://github.com/shlinkio/shlink/issues/312) Now all config files both in `php` and `json` format are loaded from `config/params` folder, easing users to provided customizations to docker image. -* [#226](https://github.com/shlinkio/shlink/issues/226) Updated how table are rendered in CLI commands, making use of new features in Symfony 4.2. -* [#321](https://github.com/shlinkio/shlink/issues/321) Extracted entities mappings from entities to external config files. -* [#308](https://github.com/shlinkio/shlink/issues/308) Automated docker image building. - -### Deprecated -* *Nothing* - -### Removed -* [#301](https://github.com/shlinkio/shlink/issues/301) Removed custom `AccessLogFactory` in favor of the implementation included in [zendframework/zend-expressive-swoole](https://github.com/zendframework/zend-expressive-swoole) v2.2.0 - -### Fixed -* [#309](https://github.com/shlinkio/shlink/issues/309) Added missing favicon to prevent 404 errors logged when an error page is loaded in a browser. -* [#310](https://github.com/shlinkio/shlink/issues/310) Fixed execution context not being properly detected, making `CloseDbConnectionMiddleware` to be always piped. Now the check is not even made, which simplifies everything. - - -## [1.15.0] - 2018-12-02 -### Added -* [#208](https://github.com/shlinkio/shlink/issues/208) Added initial support to run shlink using [swoole](https://www.swoole.co.uk/), a non-blocking IO server which improves the performance of shlink from 4 to 10 times. - - Run shlink with `./vendor/bin/zend-expressive-swoole start` to start-up the service, which will be exposed in port `8080`. - - Adding the `-d` flag, it will be started as a background service. Then you can use the `./vendor/bin/zend-expressive-swoole stop` command in order to stop it. - -* [#266](https://github.com/shlinkio/shlink/issues/266) Added pagination to `GET /short-urls/{shortCode}/visits` endpoint. - - In order to make it backwards compatible, it keeps returning all visits by default, but it now allows to provide the `page` and `itemsPerPage` query parameters in order to configure the number of items to get. - -### Changed -* [#267](https://github.com/shlinkio/shlink/issues/267) API responses and the CLI interface is no longer translated and uses english always. Only not found error templates are still translated. -* [#289](https://github.com/shlinkio/shlink/issues/289) Extracted coding standard rules to a separated package. -* [#273](https://github.com/shlinkio/shlink/issues/273) Improved code coverage in repository classes. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#278](https://github.com/shlinkio/shlink/pull/278) Added missing `X-Api-Key` header to the list of valid cross domain headers. -* [#295](https://github.com/shlinkio/shlink/pull/295) Fixed custom slugs so that they are case sensitive and do not try to lowercase provided values. - - -## [1.14.1] - 2018-11-17 -### Added -* *Nothing* - -### Changed -* [#260](https://github.com/shlinkio/shlink/issues/260) Increased mutation score to 65%. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#271](https://github.com/shlinkio/shlink/issues/271) Fixed memory leak produced when processing high amounts of visits at the same time. -* [#272](https://github.com/shlinkio/shlink/issues/272) Fixed errors produced when trying to process visits multiple times in parallel, by using a lock which ensures only one instance is run at a time. - - -## [1.14.0] - 2018-11-16 -### Added -* [#236](https://github.com/shlinkio/shlink/issues/236) Added option to define a redirection to a custom URL when a user hits an invalid short URL. - - It only affects URLs matched as "short URL" where the short code is invalid, not any 404 that happens in the app. For example, a request to the path `/foo/bar` will keep returning a 404. - - This new option will be asked by the installer both for new shlink installations and for any previous shlink version which is updated. - -* [#189](https://github.com/shlinkio/shlink/issues/189) and [#240](https://github.com/shlinkio/shlink/issues/240) Added new [GeoLite2](https://dev.maxmind.com/geoip/geoip2/geolite2/)-based geolocation service which is faster and more reliable than previous one. - - It does not have API limit problems, since it uses a local database file. - - Previous service is still used as a fallback in case GeoLite DB does not contain any IP address. - -### Changed -* [#241](https://github.com/shlinkio/shlink/issues/241) Fixed columns in `visit_locations` table, to be snake_case instead of camelCase. -* [#228](https://github.com/shlinkio/shlink/issues/228) Updated how exceptions are serialized into logs, by using monolog's `PsrLogMessageProcessor`. -* [#225](https://github.com/shlinkio/shlink/issues/225) Performance and maintainability slightly improved by enforcing via code sniffer that all global namespace classes, functions and constants are explicitly imported. -* [#196](https://github.com/shlinkio/shlink/issues/196) Reduced anemic model in entities, defining more expressive public APIs instead. -* [#249](https://github.com/shlinkio/shlink/issues/249) Added [functional-php](https://github.com/lstrojny/functional-php) to ease collections handling. -* [#253](https://github.com/shlinkio/shlink/issues/253) Increased `user_agent` column length in `visits` table to 512. -* [#256](https://github.com/shlinkio/shlink/issues/256) Updated to Infection v0.11. -* [#202](https://github.com/shlinkio/shlink/issues/202) Added missing response examples to OpenAPI docs. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#223](https://github.com/shlinkio/shlink/issues/223) Fixed PHPStan errors produced with symfony/console 4.1.5 - - -## [1.13.2] - 2018-10-18 -### Added -* [#233](https://github.com/shlinkio/shlink/issues/233) Added PHP 7.3 to build matrix allowing its failure. - -### Changed -* [#235](https://github.com/shlinkio/shlink/issues/235) Improved update instructions (thanks to [tivyhosting](https://github.com/tivyhosting)). - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#237](https://github.com/shlinkio/shlink/issues/233) Solved errors when trying to geo-locate `null` IP addresses. - - Also improved how visitor IP addresses are discovered, thanks to [akrabat/ip-address-middleware](https://github.com/akrabat/ip-address-middleware) package. - - -## [1.13.1] - 2018-10-16 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#231](https://github.com/shlinkio/shlink/issues/197) Fixed error when processing visits. - - -## [1.13.0] - 2018-10-06 -### Added -* [#197](https://github.com/shlinkio/shlink/issues/197) Added [cakephp/chronos](https://book.cakephp.org/3.0/en/chronos.html) library for date manipulations. -* [#214](https://github.com/shlinkio/shlink/issues/214) Improved build script, which allows builds to be done without "jumping" outside the project directory, and generates smaller dist files. - - It also allows automating the dist file generation in travis-ci builds. - -* [#207](https://github.com/shlinkio/shlink/issues/207) Added two new config options which are asked during installation process. The config options already existed in previous shlink version, but you had to manually set their values. - - These are the new options: - - * Visits threshold to allow short URLs to be deleted. - * Check the visits threshold when trying to delete a short URL via REST API. - -### Changed -* [#211](https://github.com/shlinkio/shlink/issues/211) Extracted installer to its own module, which will simplify moving it to a separated package in the future. -* [#200](https://github.com/shlinkio/shlink/issues/200) and [#201](https://github.com/shlinkio/shlink/issues/201) Renamed REST Action classes and CLI Command classes to use the concept of `ShortUrl` instead of the concept of `ShortCode` when referring to the entity, and left the `short code` concept to the identifier which is used as a unique code for a specific `Short URL`. -* [#181](https://github.com/shlinkio/shlink/issues/181) When importing the configuration from a previous shlink installation, it no longer asks to import every block. Instead, it is capable of detecting only new config options introduced in the new version, and ask only for those. - - If no new options are found and you have selected to import config, no further questions will be asked and shlink will just import the old config. - -### Deprecated -* [#205](https://github.com/shlinkio/shlink/issues/205) Deprecated `[POST /authenticate]` endpoint, and allowed any API request to be automatically authenticated using the `X-Api-Key` header with a valid API key. - - This effectively deprecates the `Authorization: Bearer ` authentication form, but it will keep working. - -* As of [#200](https://github.com/shlinkio/shlink/issues/200) and [#201](https://github.com/shlinkio/shlink/issues/201) REST urls have changed from `/short-codes/...` to `/short-urls/...`, and the command namespaces have changed from `short-code:...` to `short-url:...`. - - In both cases, backwards compatibility has been retained and the old ones are aliases for the new ones, but the old ones are considered deprecated. - -### Removed -* *Nothing* - -### Fixed -* [#203](https://github.com/shlinkio/shlink/issues/203) Fixed some warnings thrown while unzipping distributable files. -* [#206](https://github.com/shlinkio/shlink/issues/206) An error is now thrown during installation if any required param is left empty, making the installer display a message and ask again until a value is set. - - -## [1.12.0] - 2018-09-15 -### Added -* [#187](https://github.com/shlinkio/shlink/issues/187) Included an API endpoint and a CLI command to delete short URLs. - - Due to the implicit danger of this operation, the deletion includes a safety check. URLs cannot be deleted if they have more than a specific amount of visits. - - The visits threshold is set to **15** by default and currently it has to be manually changed. In future versions the installation/update process will ask you about the value of the visits threshold. - - In order to change it, open the `config/autoload/delete_short_urls.global.php` file, which has this structure: - - ```php - return [ - - 'delete_short_urls' => [ - 'visits_threshold' => 15, - 'check_visits_threshold' => true, - ], - - ]; - ``` - - Properties are self explanatory. Change `check_visits_threshold` to `false` to completely disable this safety check, and change the value of `visits_threshold` to allow short URLs with a different number of visits to be deleted. - - Once changed, delete the `data/cache/app_config.php` file (if any) to let shlink know about the new values. - - This check is implicit for the API endpoint, but can be "disabled" for the CLI command, which will ask you when trying to delete a URL which has reached to threshold in order to force the deletion. - -* [#183](https://github.com/shlinkio/shlink/issues/183) and [#190](https://github.com/shlinkio/shlink/issues/190) Included important documentation improvements in the repository itself. You no longer need to go to the website in order to see how to install or use shlink. -* [#186](https://github.com/shlinkio/shlink/issues/186) Added a small robots.txt file that prevents 404 errors to be logged due to search engines trying to index the domain where shlink is located. Thanks to [@robwent](https://github.com/robwent) for the contribution. - -### Changed -* [#145](https://github.com/shlinkio/shlink/issues/145) Shlink now obfuscates IP addresses from visitors by replacing the latest octet by `0`, which does not affect geolocation and allows it to fulfil the GDPR. - - Other known services follow this same approach, like [Google Analytics](https://support.google.com/analytics/answer/2763052?hl=en) or [Matomo](https://matomo.org/docs/privacy/#step-1-automatically-anonymize-visitor-ips) - -* [#182](https://github.com/shlinkio/shlink/issues/182) The short URL creation API endpoints now return the same model used for lists and details endpoints. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#188](https://github.com/shlinkio/shlink/issues/188) Shlink now allows multiple short URLs to be created that resolve to the same long URL. - - -## [1.11.0] - 2018-08-13 -### Added -* [#170](https://github.com/shlinkio/shlink/issues/170) and [#171](https://github.com/shlinkio/shlink/issues/171) Updated `[GET /short-codes]` and `[GET /short-codes/{shortCode}]` endpoints to return more meaningful information and make their response consistent. - - The short URLs are now represented by this object in both cases: - - ```json - { - "shortCode": "12Kb3", - "shortUrl": "https://s.test/12Kb3", - "longUrl": "https://shlink.io", - "dateCreated": "2016-05-01T20:34:16+02:00", - "visitsCount": 1029, - "tags": [ - "shlink" - ], - "originalUrl": "https://shlink.io" - } - ``` - - The `originalUrl` property is considered deprecated and has been kept for backward compatibility purposes. It holds the same value as the `longUrl` property. - -### Changed -* *Nothing* - -### Deprecated -* The `originalUrl` property in `[GET /short-codes]` and `[GET /short-codes/{shortCode}]` endpoints is now deprecated and replaced by the `longUrl` property. - -### Removed -* *Nothing* - -### Fixed -* *Nothing* - - -## [1.10.2] - 2018-08-04 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#177](https://github.com/shlinkio/shlink/issues/177) Fixed `[GET] /short-codes` endpoint returning a 500 status code when trying to filter by `tags` and `searchTerm` at the same time. -* [#175](https://github.com/shlinkio/shlink/issues/175) Fixed error introduced in previous version, where you could end up banned from the service used to resolve IP address locations. - - In order to fix that, just fill [this form](http://ip-api.com/docs/unban) including your server's IP address and your server should be unbanned. - - In order to prevent this, after resolving 150 IP addresses, shlink now waits 1 minute before trying to resolve any more addresses. - - -## [1.10.1] - 2018-08-02 -### Added -* *Nothing* - -### Changed -* [#167](https://github.com/shlinkio/shlink/issues/167) Shlink version is now set at build time to avoid older version numbers to be kept in newer builds. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#165](https://github.com/shlinkio/shlink/issues/165) Fixed custom slugs failing when they are longer than 10 characters. -* [#166](https://github.com/shlinkio/shlink/issues/166) Fixed unusual edge case in which visits were not properly counted when ordering by visit and filtering by search term in `[GET] /short-codes` API endpoint. -* [#174](https://github.com/shlinkio/shlink/issues/174) Fixed geolocation not working due to a deprecation on used service. -* [#172](https://github.com/shlinkio/shlink/issues/172) Documented missing filtering params for `[GET] /short-codes/{shortCode}/visits` API endpoint, which allow the list to be filtered by date range. - - For example: `https://s.test/rest/v1/short-urls/abc123/visits?startDate=2017-05-23&endDate=2017-10-05` - -* [#169](https://github.com/shlinkio/shlink/issues/169) Fixed unhandled error when parsing `ShortUrlMeta` and date fields are already `DateTime` instances. - - -## [1.10.0] - 2018-07-09 -### Added -* [#161](https://github.com/shlinkio/shlink/issues/161) AddED support for shlink to be run with [swoole](https://www.swoole.co.uk/) via [zend-expressive-swoole](https://github.com/zendframework/zend-expressive-swoole) package - -### Changed -* [#159](https://github.com/shlinkio/shlink/issues/159) Updated CHANGELOG to follow the [keep-a-changelog](https://keepachangelog.com) format -* [#160](https://github.com/shlinkio/shlink/issues/160) Update infection to v0.9 and phpstan to v 0.10 - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* *Nothing* - - -## [1.9.1] - 2018-06-18 -### Added -* [#155](https://github.com/shlinkio/shlink/issues/155) Improved the pagination object returned in lists, including more meaningful properties. - - * Old structure: - - ```json - { - "pagination": { - "currentPage": 1, - "pagesCount": 2 - } - } - ``` - - * New structure: - - ```json - { - "pagination": { - "currentPage": 2, - "pagesCount": 13, - "itemsPerPage": 10, - "itemsInCurrentPage": 10, - "totalItems": 126 - } - } - ``` - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#154](https://github.com/shlinkio/shlink/issues/154) Fixed sizes of every result page when filtering by searchTerm -* [#157](https://github.com/shlinkio/shlink/issues/157) Background commands executed by installation process now respect the originally used php binary - - -## [1.9.0] - 2018-05-07 -### Added -* [#147](https://github.com/shlinkio/shlink/issues/147) Allowed short URLs to be created on the fly using a single API request, including the API key in a query param. - - This eases integration with third party services. - - With this feature, a simple request to a URL like `https://s.test/rest/v1/short-codes/shorten?apiKey=[YOUR_API_KEY]&longUrl=[URL_TO_BE_SHORTENED]` would return the shortened one in JSON or plain text format. - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#139](https://github.com/shlinkio/shlink/issues/139) Ensured all core actions log exceptions - - -## [1.8.1] - 2018-04-07 -### Added -* *Nothing* - -### Changed -* [#141](https://github.com/shlinkio/shlink/issues/141) Removed workaround used in `PathVersionMiddleware`, since the bug in zend-stratigility has been fixed. - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#140](https://github.com/shlinkio/shlink/issues/140) Fixed warning thrown during installation while trying to include doctrine script - - -## [1.8.0] - 2018-03-29 -### Added -* [#125](https://github.com/shlinkio/shlink/issues/125) Implemented a path which returns a 1px image instead of a redirection. - - Useful to track emails. Just add an image pointing to a URL like `https://s.test/abc123/track` to any email and an invisible image will be generated tracking every time the email is opened. - -* [#132](https://github.com/shlinkio/shlink/issues/132) Added infection to improve tests - -### Changed -* [#130](https://github.com/shlinkio/shlink/issues/130) Updated to Expressive 3 -* [#137](https://github.com/shlinkio/shlink/issues/137) Updated symfony components to v4 - -### Deprecated -* *Nothing* - -### Removed -* [#131](https://github.com/shlinkio/shlink/issues/131) Dropped support for PHP 7 - -### Fixed -* *Nothing* - - -## [1.7.2] - 2018-03-26 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#135](https://github.com/shlinkio/shlink/issues/135) Fixed `PathVersionMiddleware` being ignored when using expressive 2.2 - - -## [1.7.1] - 2018-03-21 -### Added -* *Nothing* - -### Changed -* [#128](https://github.com/shlinkio/shlink/issues/128) Upgraded to expressive 2.2 - - This will ease the upcoming update to expressive 3 - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#126](https://github.com/shlinkio/shlink/issues/126) Fixed `E_USER_DEPRECATED` errors triggered when using Expressive 2.2 - - -## [1.7.0] - 2018-01-21 -### Added -* [#88](https://github.com/shlinkio/shlink/issues/88) Allowed tracking of short URLs to be disabled by including a configurable query param -* [#108](https://github.com/shlinkio/shlink/issues/108) Allowed metadata to be defined when creating short codes - -### Changed -* [#113](https://github.com/shlinkio/shlink/issues/113) Updated CLI commands to use `SymfonyStyle` -* [#112](https://github.com/shlinkio/shlink/issues/112) Enabled Lazy loading in CLI commands -* [#117](https://github.com/shlinkio/shlink/issues/117) Every module which throws exceptions has now its own `ExceptionInterface` extending `Throwable` -* [#115](https://github.com/shlinkio/shlink/issues/115) Added phpstan to build matrix on PHP >=7.1 envs -* [#114](https://github.com/shlinkio/shlink/issues/114) Replaced [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv) dev requirement by [symfony/dotenv](https://github.com/symfony/dotenv) - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* *Nothing* - - -## [1.6.2] - 2017-10-25 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#109](https://github.com/shlinkio/shlink/issues/109) Fixed installation error due to typo in latest migration - - -## [1.6.1] - 2017-10-24 -### Added -* *Nothing* - -### Changed -* [#110](https://github.com/shlinkio/shlink/issues/110) Created `.gitattributes` file to define files to be excluded from distributable package - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* *Nothing* - - -## [1.6.0] - 2017-10-23 -### Added -* [#44](https://github.com/shlinkio/shlink/issues/44) Now it is possible to set custom slugs for short URLs instead of using a generated short code -* [#47](https://github.com/shlinkio/shlink/issues/47) Allowed to limit short URLs availability by date range -* [#48](https://github.com/shlinkio/shlink/issues/48) Allowed to limit the number of visits to a short URL -* [#105](https://github.com/shlinkio/shlink/pull/105) Added option to enable/disable URL validation by response status code - -### Changed -* [#27](https://github.com/shlinkio/shlink/issues/27) Added repository functional tests with dbunit -* [#101](https://github.com/shlinkio/shlink/issues/101) Now specific actions just capture very specific exceptions, and let the `ErrorHandler` catch any other unhandled exception -* [#104](https://github.com/shlinkio/shlink/issues/104) Used different templates for *requested-short-code-does-not-exist* and *route-could-not-be-match* -* [#99](https://github.com/shlinkio/shlink/issues/99) Replaced usages of `AnnotatedFactory` by `ConfigAbstractFactory` -* [#100](https://github.com/shlinkio/shlink/issues/100) Updated templates engine. Replaced twig by plates -* [#102](https://github.com/shlinkio/shlink/issues/102) Improved coding standards strictness - -### Deprecated -* *Nothing* - -### Removed -* [#86](https://github.com/shlinkio/shlink/issues/86) Dropped support for PHP 5 - -### Fixed -* [#103](https://github.com/shlinkio/shlink/issues/103) `NotFoundDelegate` now returns proper content types based on accepted content - - -## [1.5.0] - 2017-07-16 -### Added -* [#95](https://github.com/shlinkio/shlink/issues/95) Added tags CRUD to CLI -* [#59](https://github.com/shlinkio/shlink/issues/59) Added tags CRUD to REST -* [#66](https://github.com/shlinkio/shlink/issues/66) Allowed certain information to be imported from and older shlink instance directory when updating - -### Changed -* [#96](https://github.com/shlinkio/shlink/issues/96) Added namespace to functions -* [#76](https://github.com/shlinkio/shlink/issues/76) Added response examples to swagger docs -* [#93](https://github.com/shlinkio/shlink/issues/93) Improved cross domain management by using the `ImplicitOptionsMiddleware` - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#92](https://github.com/shlinkio/shlink/issues/92) Fixed formatted dates, using an ISO compliant format - - -## [1.4.0] - 2017-03-25 -### Added -* *Nothing* - -### Changed -* [#89](https://github.com/shlinkio/shlink/issues/89) Updated to expressive 2 - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* *Nothing* - - -## [1.3.1] - 2017-01-22 -### Added -* *Nothing* - -### Changed -* [#82](https://github.com/shlinkio/shlink/issues/82) Enabled `FastRoute` routes cache -* [#85](https://github.com/shlinkio/shlink/issues/85) Updated year in license file -* [#81](https://github.com/shlinkio/shlink/issues/81) Added docker containers config - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#83](https://github.com/shlinkio/shlink/issues/83) Fixed short codes list: search in tags when filtering by query string -* [#79](https://github.com/shlinkio/shlink/issues/79) Increased the number of followed redirects -* [#75](https://github.com/shlinkio/shlink/issues/75) Applied `PathVersionMiddleware` only to rest routes defining it by configuration instead of code -* [#77](https://github.com/shlinkio/shlink/issues/77) Allowed defining database server hostname and port - - -## [1.3.0] - 2016-10-23 -### Added -* [#67](https://github.com/shlinkio/shlink/issues/67) Allowed to order the short codes list -* [#60](https://github.com/shlinkio/shlink/issues/60) Accepted JSON requests in REST and used a body parser middleware to set the request's `parsedBody` -* [#72](https://github.com/shlinkio/shlink/issues/72) When listing API keys from CLI, use yellow color for enabled keys that have expired -* [#58](https://github.com/shlinkio/shlink/issues/58) Allowed to filter short URLs by tag -* [#69](https://github.com/shlinkio/shlink/issues/69) Allowed to filter short URLs by text query -* [#73](https://github.com/shlinkio/shlink/issues/73) Added tag-related endpoints to swagger file -* [#63](https://github.com/shlinkio/shlink/issues/63) Added path versioning to REST API routes - -### Changed -* [#71](https://github.com/shlinkio/shlink/issues/71) Separated swagger docs into multiple files - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* *Nothing* - - -## [1.2.2] - 2016-08-29 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* Fixed minor bugs on CORS requests - - -## [1.2.1] - 2016-08-21 -### Added -* *Nothing* - -### Changed -* *Nothing* - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#62](https://github.com/shlinkio/shlink/issues/62) Fixed cross-domain requests in REST API - - -## [1.2.0] - 2016-08-21 -### Added -* [#45](https://github.com/shlinkio/shlink/issues/45) Allowed to define tags on short codes, to improve filtering and classification -* [#7](https://github.com/shlinkio/shlink/issues/7) Added website previews while listing available URLs -* [#57](https://github.com/shlinkio/shlink/issues/57) Added database migrations system to improve updating between versions -* [#31](https://github.com/shlinkio/shlink/issues/31) Added support for other database management systems by improving the `EntityManager` factory -* [#51](https://github.com/shlinkio/shlink/issues/51) Generated build process to create app package and ease distribution -* [#38](https://github.com/shlinkio/shlink/issues/38) Defined installation script. It will request dynamic data on the fly so that there is no need to define env vars -* [#55](https://github.com/shlinkio/shlink/issues/55) Created update script which does not try to create a new database - -### Changed -* [#54](https://github.com/shlinkio/shlink/issues/54) Added cache namespace to prevent name collisions with other apps in the same environment -* [#29](https://github.com/shlinkio/shlink/issues/29) Used the [acelaya/ze-content-based-error-handler](https://github.com/acelaya/ze-content-based-error-handler) package instead of custom error handler implementation - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#53](https://github.com/shlinkio/shlink/issues/53) Fixed entities database interoperability -* [#52](https://github.com/shlinkio/shlink/issues/52) Added missing htaccess file for apache environments - - -## [1.1.0] - 2016-08-09 -### Added -* [#46](https://github.com/shlinkio/shlink/issues/46) Defined a route that returns a QR code representing the shortened URL. - - In order to get the QR code URL, use a pattern like `https://s.test/abc123/qr-code` - -* [#32](https://github.com/shlinkio/shlink/issues/32) Added support for other cache adapters by improving the Cache factory -* [#14](https://github.com/shlinkio/shlink/issues/14) Added logger and enabled errors logging -* [#13](https://github.com/shlinkio/shlink/issues/13) Improved REST authentication - -### Changed -* [#41](https://github.com/shlinkio/shlink/issues/41) Cached the "short code" => "URL" map to prevent extra DB hits -* [#39](https://github.com/shlinkio/shlink/issues/39) Changed copyright from "Alejandro Celaya" to "Shlink" in error pages -* [#42](https://github.com/shlinkio/shlink/issues/42) REST endpoints that need to find *something* now return a 404 when it is not found -* [#35](https://github.com/shlinkio/shlink/issues/35) Updated CLI commands to use the same PHP namespace as the one used for the command name - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* [#40](https://github.com/shlinkio/shlink/issues/40) Taken into account the `X-Forwarded-For` header in order to get the visitor information, in case the server is behind a load balancer or proxy - - -## [1.0.0] - 2016-08-01 -### Added -* [#33](https://github.com/shlinkio/shlink/issues/33) Created a command that generates a short code charset by randomizing the default one -* [#23](https://github.com/shlinkio/shlink/issues/23) Translated application literals -* [#21](https://github.com/shlinkio/shlink/issues/21) Allowed to filter visits by date range -* [#4](https://github.com/shlinkio/shlink/issues/4) Added installation steps -* [#12](https://github.com/shlinkio/shlink/issues/12) Improved code coverage - -### Changed -* [#15](https://github.com/shlinkio/shlink/issues/15) HTTP requests now return JSON/HTML responses for errors (4xx and 5xx) based on `Accept` header -* [#22](https://github.com/shlinkio/shlink/issues/22) Now visits locations data is saved on a `visit_locations` table -* [#20](https://github.com/shlinkio/shlink/issues/20) Injected cross domain headers in response only if the `Origin` header is present in the request -* [#11](https://github.com/shlinkio/shlink/issues/11) Separated code into multiple modules -* [#18](https://github.com/shlinkio/shlink/issues/18) Grouped routable middleware in an Action namespace -* [#6](https://github.com/shlinkio/shlink/issues/6) Project no longer depends on [zendframework/zend-expressive-helpers](https://github.com/zendframework/zend-expressive-helpers) package -* [#30](https://github.com/shlinkio/shlink/issues/30) Replaced the "services" first level config entry by "dependencies", in order to fulfill default Expressive naming -* [#25](https://github.com/shlinkio/shlink/issues/25) Replaced "Middleware" suffix on routable middlewares by "Action" -* [#19](https://github.com/shlinkio/shlink/issues/19) Changed the vendor and app namespace from `Acelaya\UrlShortener` to `Shlinkio\Shlink` - -### Deprecated -* *Nothing* - -### Removed -* [#36](https://github.com/shlinkio/shlink/issues/36) Removed hhvm from the CI matrix since it doesn't support array constants and will fail - -### Fixed -* [#24](https://github.com/shlinkio/shlink/issues/24) Prevented duplicated short codes errors because of the case insensitive behavior on MySQL - - -## [0.2.0] - 2016-08-01 -### Added -* [#8](https://github.com/shlinkio/shlink/issues/8) Created a REST API -* [#10](https://github.com/shlinkio/shlink/issues/10) Added more CLI functionality -* [#5](https://github.com/shlinkio/shlink/issues/5) Created a CHANGELOG file - -### Changed -* [#9](https://github.com/shlinkio/shlink/issues/9) Used [symfony/console](https://github.com/symfony/console) to dispatch console requests, instead of trying to integrate the process with expressive - -### Deprecated -* *Nothing* - -### Removed -* *Nothing* - -### Fixed -* *Nothing* diff --git a/bin/roadrunner-worker.php b/bin/roadrunner-worker.php index c4a89a855..8466c6833 100644 --- a/bin/roadrunner-worker.php +++ b/bin/roadrunner-worker.php @@ -4,6 +4,7 @@ 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 @@ } } } else { - $container->get(RoadRunnerTaskConsumerToListener::class)->listenForTasks(); + $requestIdMiddleware = $container->get(RequestIdMiddleware::class); + $container->get(RoadRunnerTaskConsumerToListener::class)->listenForTasks( + fn (string $requestId) => $requestIdMiddleware->setCurrentRequestId($requestId), + ); } })(); diff --git a/composer.json b/composer.json index a35dd7271..517caf0ca 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/autoload/entity-manager.global.php b/config/autoload/entity-manager.global.php index 3eb43edf2..c3aa66327 100644 --- a/config/autoload/entity-manager.global.php +++ b/config/autoload/entity-manager.global.php @@ -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 @@ '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(), ], diff --git a/config/autoload/installer.global.php b/config/autoload/installer.global.php index b6a796798..4ebd67164 100644 --- a/config/autoload/installer.global.php +++ b/config/autoload/installer.global.php @@ -12,6 +12,7 @@ 'installer' => [ 'enabled_options' => [ Option\Server\RuntimeConfigOption::class, + Option\Server\MemoryLimitConfigOption::class, Option\Database\DatabaseDriverConfigOption::class, Option\Database\DatabaseNameConfigOption::class, Option\Database\DatabaseHostConfigOption::class, diff --git a/config/autoload/logger.global.php b/config/autoload/logger.global.php index 67b737ae0..c7d7d7576 100644 --- a/config/autoload/logger.global.php +++ b/config/autoload/logger.global.php @@ -4,6 +4,7 @@ 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\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 @@ '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], + ], + ]; })(); diff --git a/config/autoload/rabbit.local.php.dist b/config/autoload/rabbit.local.php.dist index b758528e4..d19f82c6a 100644 --- a/config/autoload/rabbit.local.php.dist +++ b/config/autoload/rabbit.local.php.dist @@ -7,7 +7,7 @@ return [ 'rabbitmq' => [ 'enabled' => true, 'host' => 'shlink_rabbitmq', - 'port' => '5673', + 'port' => '5672', 'user' => 'rabbit', 'password' => 'rabbit', ], diff --git a/config/container.php b/config/container.php index 5d2631732..bfab77634 100644 --- a/config/container.php +++ b/config/container.php @@ -12,6 +12,8 @@ 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())); diff --git a/data/infra/php.ini b/data/infra/php.ini index 64838d111..46ad43bbe 100644 --- a/data/infra/php.ini +++ b/data/infra/php.ini @@ -1,6 +1,5 @@ display_errors=On error_reporting=-1 -memory_limit=-1 log_errors_max_len=0 zend.assertions=1 assert.exception=1 diff --git a/docker-compose.yml b/docker-compose.yml index ccc5fc2d9..5bccfd482 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/docker/config/php.ini b/docker/config/php.ini index 248e508d7..f6c718d02 100644 --- a/docker/config/php.ini +++ b/docker/config/php.ini @@ -1,4 +1,3 @@ log_errors_max_len=0 zend.assertions=1 assert.exception=1 -memory_limit=512M diff --git a/docs/async-api/async-api.json b/docs/async-api/async-api.json index 7cd838a84..83c424ea9 100644 --- a/docs/async-api/async-api.json +++ b/docs/async-api/async-api.json @@ -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": [ diff --git a/docs/changelog-archive/CHANGELOG-1.x.md b/docs/changelog-archive/CHANGELOG-1.x.md new file mode 100644 index 000000000..337279997 --- /dev/null +++ b/docs/changelog-archive/CHANGELOG-1.x.md @@ -0,0 +1,1167 @@ +# CHANGELOG 1.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). + +## [1.21.1] - 2020-01-02 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#596](https://github.com/shlinkio/shlink/issues/596) Fixed error when trying to download GeoLite2 database due to changes on how to get the database files. + + +## [1.21.0] - 2019-12-29 +### Added +* [#118](https://github.com/shlinkio/shlink/issues/118) API errors now implement the [problem details](https://tools.ietf.org/html/rfc7807) standard. + + In order to make it backwards compatible, two things have been done: + + * Both the old `error` and `message` properties have been kept on error response, containing the same values as the `type` and `detail` properties respectively. + * The API `v2` has been enabled. If an error occurs when calling the API with this version, the `error` and `message` properties will not be returned. + + > After Shlink v2 is released, both API versions will behave like API v2. + +* [#575](https://github.com/shlinkio/shlink/issues/575) Added support to filter short URL lists by date ranges. + + * The `GET /short-urls` endpoint now accepts the `startDate` and `endDate` query params. + * The `short-urls:list` command now allows `--startDate` and `--endDate` flags to be optionally provided. + +* [#338](https://github.com/shlinkio/shlink/issues/338) Added support to asynchronously notify external services via webhook, only when shlink is served with swoole. + + Configured webhooks will receive a POST request every time a URL receives a visit, including information about the short URL and the visit. + + The payload will look like this: + + ```json + { + "shortUrl": {}, + "visit": {} + } + ``` + + > The `shortUrl` and `visit` props have the same shape as it is defined in the [API spec](https://api-spec.shlink.io). + +### Changed +* [#492](https://github.com/shlinkio/shlink/issues/492) Updated to monolog 2, together with other dependencies, like Symfony 5 and infection-php. +* [#527](https://github.com/shlinkio/shlink/issues/527) Increased minimum required mutation score for unit tests to 80%. +* [#557](https://github.com/shlinkio/shlink/issues/557) Added a few php.ini configs for development and production docker images. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#570](https://github.com/shlinkio/shlink/issues/570) Fixed shlink version generated for docker images when building from `develop` branch. + + +## [1.20.3] - 2019-12-23 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#585](https://github.com/shlinkio/shlink/issues/585) Fixed `PHP Fatal error: Uncaught Error: Class 'Shlinkio\Shlink\LocalLockFactory' not found` happening when running some CLI commands. + + +## [1.20.2] - 2019-12-06 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#561](https://github.com/shlinkio/shlink/issues/561) Fixed `db:migrate` command failing because yaml extension is not installed, which makes config file not to be readable. +* [#562](https://github.com/shlinkio/shlink/issues/562) Fixed internal server error being returned when renaming a tag to another tag's name. Now a meaningful API error with status 409 is returned. +* [#555](https://github.com/shlinkio/shlink/issues/555) Fixed internal server error being returned when invalid dates are provided for new short URLs. Now a 400 is returned, as intended. + + +## [1.20.1] - 2019-11-17 +### Added +* [#519](https://github.com/shlinkio/shlink/issues/519) Documented how to customize web workers and task workers for the docker image. + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#512](https://github.com/shlinkio/shlink/issues/512) Fixed query params not being properly forwarded from short URL to long one. +* [#540](https://github.com/shlinkio/shlink/issues/540) Fixed errors thrown when creating short URLs if the original URL has an internationalized domain name and URL validation is enabled. +* [#528](https://github.com/shlinkio/shlink/issues/528) Ensured `db:create` and `db:migrate` commands do not silently fail when run as part of `install` or `update`. +* [#518](https://github.com/shlinkio/shlink/issues/518) Fixed service which updates Geolite db file to use a local lock instead of a shared one, since every shlink instance holds its own db instance. + + +## [1.20.0] - 2019-11-02 +### Added +* [#491](https://github.com/shlinkio/shlink/issues/491) Added improved short code generation logic. + + Now, short codes are truly random, which removes the guessability factor existing in previous versions. + + Generated short codes have 5 characters, and shlink makes sure they keep unique, while making it backwards-compatible. + +* [#418](https://github.com/shlinkio/shlink/issues/418) and [#419](https://github.com/shlinkio/shlink/issues/419) Added support to redirect any 404 error to a custom URL. + + It was already possible to configure this but only for invalid short URLs. Shlink now also support configuring redirects for the base URL and any other kind of "not found" error. + + The three URLs can be different, and it is already possible to pass them to the docker image via configuration or env vars. + + The installer also asks for these two new configuration options. + +* [#497](https://github.com/shlinkio/shlink/issues/497) Officially added support for MariaDB. + +### Changed +* [#458](https://github.com/shlinkio/shlink/issues/458) Updated coding styles to use [shlinkio/php-coding-standard](https://github.com/shlinkio/php-coding-standard) v2.0.0. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#507](https://github.com/shlinkio/shlink/issues/507) Fixed error with too long original URLs by increasing size to the maximum value (2048) based on [the standard](https://stackoverflow.com/a/417184). +* [#502](https://github.com/shlinkio/shlink/issues/502) Fixed error when providing the port as part of the domain on short URLs. +* [#509](https://github.com/shlinkio/shlink/issues/509) Fixed error when trying to generate a QR code for a short URL which uses a custom domain. +* [#522](https://github.com/shlinkio/shlink/issues/522) Highly mitigated errors thrown when lots of short URLs are created concurrently including new and existing tags. + + +## [1.19.0] - 2019-10-05 +### Added +* [#482](https://github.com/shlinkio/shlink/issues/482) Added support to serve shlink under a sub path. + + The `router.base_path` config option can be defined now to set the base path from which shlink is served. + + ```php + return [ + 'router' => [ + 'base_path' => '/foo/bar', + ], + ]; + ``` + + This option will also be available on shlink-installer 1.3.0, so the installer will ask for it. It can also be provided for the docker image as the `BASE_PATH` env var. + +* [#479](https://github.com/shlinkio/shlink/issues/479) Added preliminary support for multiple domains. + + Endpoints and commands which create short URLs support providing the `domain` now (via query param or CLI flag). If not provided, the short URLs will still be "attached" to the default domain. + + Custom slugs can be created on multiple domains, allowing to share links like `https://s.test/my-campaign` and `https://example.com/my-campaign`, under the same shlink instance. + + When resolving a short URL to redirect end users, the following rules are applied: + + * If the domain used for the request plus the short code/slug are found, the user is redirected to that long URL and the visit is tracked. + * If the domain is not known but the short code/slug is defined for default domain, the user is redirected there and the visit is tracked. + * In any other case, no redirection happens and no visit is tracked (if a fall back redirection is configured for not-found URLs, it will still happen). + +### Changed +* [#486](https://github.com/shlinkio/shlink/issues/486) Updated to [shlink-installer](https://github.com/shlinkio/shlink-installer) v2, which supports asking for base path in which shlink is served. + +### Deprecated +* *Nothing* + +### Removed +* [#435](https://github.com/shlinkio/shlink/issues/435) Removed translations for error pages. All error pages are in english now. + +### Fixed +* *Nothing* + + +## [1.18.1] - 2019-08-24 +### Added +* *Nothing* + +### Changed +* [#450](https://github.com/shlinkio/shlink/issues/450) Added PHP 7.4 to the build matrix, as an allowed-to-fail env. +* [#441](https://github.com/shlinkio/shlink/issues/441) and [#443](https://github.com/shlinkio/shlink/issues/443) Split some logic into independent modules. +* [#451](https://github.com/shlinkio/shlink/issues/451) Updated to infection 0.13. +* [#467](https://github.com/shlinkio/shlink/issues/467) Moved docker image config to main Shlink repo. + +### Deprecated +* [#428](https://github.com/shlinkio/shlink/issues/428) Deprecated preview-generation feature. It will keep working but it will be removed in Shlink v2.0.0 + +### Removed +* [#468](https://github.com/shlinkio/shlink/issues/468) Removed APCu extension from docker image. + +### Fixed +* [#449](https://github.com/shlinkio/shlink/issues/449) Fixed error when trying to save too big referrers on PostgreSQL. + + +## [1.18.0] - 2019-08-08 +### Added +* [#411](https://github.com/shlinkio/shlink/issues/411) Added new `meta` property on the `ShortUrl` REST API model. + + These endpoints are affected and include the new property when suitable: + + * `GET /short-urls` - List short URLs. + * `GET /short-urls/shorten` - Create a short URL (for integrations). + * `GET /short-urls/{shortCode}` - Get one short URL. + * `POST /short-urls` - Create short URL. + + The property includes the values `validSince`, `validUntil` and `maxVisits` in a single object. All of them are nullable. + + ```json + { + "validSince": "2016-01-01T00:00:00+02:00", + "validUntil": null, + "maxVisits": 100 + } + ``` + +* [#285](https://github.com/shlinkio/shlink/issues/285) Visit location resolution is now done asynchronously but in real time thanks to swoole task management. + + Now, when a short URL is visited, a task is enqueued to locate it. The user is immediately redirected to the long URL, and in the background, the visit is located, making stats to be available a couple of seconds after the visit without the requirement of cronjobs being run constantly. + + Sadly, this feature is not enabled when serving shlink via apache/nginx, where you should still rely on cronjobs. + +* [#384](https://github.com/shlinkio/shlink/issues/384) Improved how remote IP addresses are detected. + + This new set of headers is now also inspected looking for the IP address: + + * CF-Connecting-IP + * True-Client-IP + * X-Real-IP + +* [#440](https://github.com/shlinkio/shlink/pull/440) Created `db:create` command, which improves how the shlink database is created, with these benefits: + + * It sets up a lock which prevents the command to be run concurrently. + * It checks of the database does not exist, and creates it in that case. + * It checks if the database tables already exist, exiting gracefully in that case. + +* [#442](https://github.com/shlinkio/shlink/pull/442) Created `db:migrate` command, which improves doctrine's migrations command by generating a lock, preventing it to be run concurrently. + +### Changed +* [#430](https://github.com/shlinkio/shlink/issues/430) Updated to [shlinkio/php-coding-standard](https://github.com/shlinkio/php-coding-standard) 1.2.2 +* [#305](https://github.com/shlinkio/shlink/issues/305) Implemented changes which will allow Shlink to be truly clusterizable. +* [#262](https://github.com/shlinkio/shlink/issues/262) Increased mutation score to 75%. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#416](https://github.com/shlinkio/shlink/issues/416) Fixed error thrown when trying to locate visits after the GeoLite2 DB is downloaded for the first time. +* [#424](https://github.com/shlinkio/shlink/issues/424) Updated wkhtmltoimage to version 0.12.5 +* [#427](https://github.com/shlinkio/shlink/issues/427) and [#434](https://github.com/shlinkio/shlink/issues/434) Fixed shlink being unusable after a database error on swoole contexts. + + +## [1.17.0] - 2019-05-13 +### Added +* [#377](https://github.com/shlinkio/shlink/issues/377) Updated `visit:locate` command (formerly `visit:process`) to automatically update the GeoLite2 database if it is too old or it does not exist. + + This simplifies processing visits in a container-based infrastructure, since a fresh container is capable of getting an updated version of the file by itself. + + It also removes the need of asynchronously and programmatically updating the file, which deprecates the `visit:update-db` command. + +* [#373](https://github.com/shlinkio/shlink/issues/373) Added support for a simplified config. Specially useful to use with the docker container. + +### Changed +* [#56](https://github.com/shlinkio/shlink/issues/56) Simplified supported cache, requiring APCu always. + +### Deprecated +* [#406](https://github.com/shlinkio/shlink/issues/406) Deprecated `PUT /short-urls/{shortCode}` REST endpoint in favor of `PATCH /short-urls/{shortCode}`. + +### Removed +* [#385](https://github.com/shlinkio/shlink/issues/385) Dropped support for PHP 7.1 +* [#379](https://github.com/shlinkio/shlink/issues/379) Removed copyright from error templates. + +### Fixed +* *Nothing* + + +## [1.16.3] - 2019-03-30 +### Added +* *Nothing* + +### Changed +* [#153](https://github.com/shlinkio/shlink/issues/153) Updated to [doctrine/migrations](https://github.com/doctrine/migrations) version 2.0.0 +* [#376](https://github.com/shlinkio/shlink/issues/376) Allowed `visit:update-db` command to not return an error exit code even if download fails, by passing the `-i` flag. +* [#341](https://github.com/shlinkio/shlink/issues/341) Improved database tests so that they are executed against all supported database engines. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#382](https://github.com/shlinkio/shlink/issues/382) Fixed existing short URLs not properly checked when providing the `findIfExists` flag. + + +## [1.16.2] - 2019-03-05 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#368](https://github.com/shlinkio/shlink/issues/368) Fixed error produced when running a `SELECT COUNT(...)` with `ORDER BY` in PostgreSQL databases. + + +## [1.16.1] - 2019-02-26 +### Added +* *Nothing* + +### Changed +* [#363](https://github.com/shlinkio/shlink/issues/363) Updated to `shlinkio/php-coding-standard` version 1.1.0 + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#362](https://github.com/shlinkio/shlink/issues/362) Fixed all visits without an IP address being processed every time the `visit:process` command is executed. + + +## [1.16.0] - 2019-02-23 +### Added +* [#304](https://github.com/shlinkio/shlink/issues/304) Added health endpoint to check healthiness of the service. Useful in container-based infrastructures. + + Call [GET /rest/health] in order to get a response like this: + + ```http + HTTP/1.1 200 OK + Content-Type: application/health+json + Content-Length: 681 + + { + "status": "pass", + "version": "1.16.0", + "links": { + "about": "https://shlink.io", + "project": "https://github.com/shlinkio/shlink" + } + } + ``` + + The status code can be `200 OK` in case of success or `503 Service Unavailable` in case of error, while the `status` property will be one of `pass` or `fail`, as defined in the [Health check RFC](https://inadarei.github.io/rfc-healthcheck/). + +* [#279](https://github.com/shlinkio/shlink/issues/279) Added new `findIfExists` flag to the `[POST /short-url]` REST endpoint and the `short-urls:generate` CLI command. It can be used to return existing short URLs when found, instead of creating new ones. + + Thanks to this flag you won't need to remember if you created a short URL for a long one. It will just create it if needed or return the existing one if possible. + + The behavior might be a little bit counterintuitive when combined with other params. This is how the endpoint behaves when providing this new flag: + + * Only the long URL is provided: It will return the newest match or create a new short URL if none is found. + * Long url and custom slug are provided: It will return the short URL when both params match, return an error when the slug is in use for another long URL, or create a new short URL otherwise. + * Any of the above but including other params (tags, validSince, validUntil, maxVisits): It will behave the same as the previous two cases, but it will try to exactly match existing results using all the params. If any of them does not match, it will try to create a new short URL. + +* [#336](https://github.com/shlinkio/shlink/issues/336) Added an API test suite which performs API calls to an actual instance of the web service. + +### Changed +* [#342](https://github.com/shlinkio/shlink/issues/342) The installer no longer asks for a charset to be provided, and instead, it shuffles the base62 charset. +* [#320](https://github.com/shlinkio/shlink/issues/320) Replaced query builder by plain DQL for all queries which do not need to be dynamically generated. +* [#330](https://github.com/shlinkio/shlink/issues/330) No longer allow failures on PHP 7.3 envs during project CI build. +* [#335](https://github.com/shlinkio/shlink/issues/335) Renamed functional test suite to database test suite, since that better describes what it actually does. +* [#346](https://github.com/shlinkio/shlink/issues/346) Extracted installer as an independent tool. +* [#261](https://github.com/shlinkio/shlink/issues/261) Increased mutation score to 70%. + +### Deprecated +* [#351](https://github.com/shlinkio/shlink/issues/351) Deprecated `config:generate-charset` and `config:generate-secret` CLI commands. + +### Removed +* *Nothing* + +### Fixed +* [#317](https://github.com/shlinkio/shlink/issues/317) Fixed error while trying to generate previews because of global config file being deleted by mistake by build script. +* [#307](https://github.com/shlinkio/shlink/issues/307) Fixed memory leak while trying to process huge amounts of visits due to the query not being properly paginated. + + +## [1.15.1] - 2018-12-16 +### Added +* [#162](https://github.com/shlinkio/shlink/issues/162) Added non-rest endpoints to swagger definition. + +### Changed +* [#312](https://github.com/shlinkio/shlink/issues/312) Now all config files both in `php` and `json` format are loaded from `config/params` folder, easing users to provided customizations to docker image. +* [#226](https://github.com/shlinkio/shlink/issues/226) Updated how table are rendered in CLI commands, making use of new features in Symfony 4.2. +* [#321](https://github.com/shlinkio/shlink/issues/321) Extracted entities mappings from entities to external config files. +* [#308](https://github.com/shlinkio/shlink/issues/308) Automated docker image building. + +### Deprecated +* *Nothing* + +### Removed +* [#301](https://github.com/shlinkio/shlink/issues/301) Removed custom `AccessLogFactory` in favor of the implementation included in [zendframework/zend-expressive-swoole](https://github.com/zendframework/zend-expressive-swoole) v2.2.0 + +### Fixed +* [#309](https://github.com/shlinkio/shlink/issues/309) Added missing favicon to prevent 404 errors logged when an error page is loaded in a browser. +* [#310](https://github.com/shlinkio/shlink/issues/310) Fixed execution context not being properly detected, making `CloseDbConnectionMiddleware` to be always piped. Now the check is not even made, which simplifies everything. + + +## [1.15.0] - 2018-12-02 +### Added +* [#208](https://github.com/shlinkio/shlink/issues/208) Added initial support to run shlink using [swoole](https://www.swoole.co.uk/), a non-blocking IO server which improves the performance of shlink from 4 to 10 times. + + Run shlink with `./vendor/bin/zend-expressive-swoole start` to start-up the service, which will be exposed in port `8080`. + + Adding the `-d` flag, it will be started as a background service. Then you can use the `./vendor/bin/zend-expressive-swoole stop` command in order to stop it. + +* [#266](https://github.com/shlinkio/shlink/issues/266) Added pagination to `GET /short-urls/{shortCode}/visits` endpoint. + + In order to make it backwards compatible, it keeps returning all visits by default, but it now allows to provide the `page` and `itemsPerPage` query parameters in order to configure the number of items to get. + +### Changed +* [#267](https://github.com/shlinkio/shlink/issues/267) API responses and the CLI interface is no longer translated and uses english always. Only not found error templates are still translated. +* [#289](https://github.com/shlinkio/shlink/issues/289) Extracted coding standard rules to a separated package. +* [#273](https://github.com/shlinkio/shlink/issues/273) Improved code coverage in repository classes. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#278](https://github.com/shlinkio/shlink/pull/278) Added missing `X-Api-Key` header to the list of valid cross domain headers. +* [#295](https://github.com/shlinkio/shlink/pull/295) Fixed custom slugs so that they are case sensitive and do not try to lowercase provided values. + + +## [1.14.1] - 2018-11-17 +### Added +* *Nothing* + +### Changed +* [#260](https://github.com/shlinkio/shlink/issues/260) Increased mutation score to 65%. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#271](https://github.com/shlinkio/shlink/issues/271) Fixed memory leak produced when processing high amounts of visits at the same time. +* [#272](https://github.com/shlinkio/shlink/issues/272) Fixed errors produced when trying to process visits multiple times in parallel, by using a lock which ensures only one instance is run at a time. + + +## [1.14.0] - 2018-11-16 +### Added +* [#236](https://github.com/shlinkio/shlink/issues/236) Added option to define a redirection to a custom URL when a user hits an invalid short URL. + + It only affects URLs matched as "short URL" where the short code is invalid, not any 404 that happens in the app. For example, a request to the path `/foo/bar` will keep returning a 404. + + This new option will be asked by the installer both for new shlink installations and for any previous shlink version which is updated. + +* [#189](https://github.com/shlinkio/shlink/issues/189) and [#240](https://github.com/shlinkio/shlink/issues/240) Added new [GeoLite2](https://dev.maxmind.com/geoip/geoip2/geolite2/)-based geolocation service which is faster and more reliable than previous one. + + It does not have API limit problems, since it uses a local database file. + + Previous service is still used as a fallback in case GeoLite DB does not contain any IP address. + +### Changed +* [#241](https://github.com/shlinkio/shlink/issues/241) Fixed columns in `visit_locations` table, to be snake_case instead of camelCase. +* [#228](https://github.com/shlinkio/shlink/issues/228) Updated how exceptions are serialized into logs, by using monolog's `PsrLogMessageProcessor`. +* [#225](https://github.com/shlinkio/shlink/issues/225) Performance and maintainability slightly improved by enforcing via code sniffer that all global namespace classes, functions and constants are explicitly imported. +* [#196](https://github.com/shlinkio/shlink/issues/196) Reduced anemic model in entities, defining more expressive public APIs instead. +* [#249](https://github.com/shlinkio/shlink/issues/249) Added [functional-php](https://github.com/lstrojny/functional-php) to ease collections handling. +* [#253](https://github.com/shlinkio/shlink/issues/253) Increased `user_agent` column length in `visits` table to 512. +* [#256](https://github.com/shlinkio/shlink/issues/256) Updated to Infection v0.11. +* [#202](https://github.com/shlinkio/shlink/issues/202) Added missing response examples to OpenAPI docs. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#223](https://github.com/shlinkio/shlink/issues/223) Fixed PHPStan errors produced with symfony/console 4.1.5 + + +## [1.13.2] - 2018-10-18 +### Added +* [#233](https://github.com/shlinkio/shlink/issues/233) Added PHP 7.3 to build matrix allowing its failure. + +### Changed +* [#235](https://github.com/shlinkio/shlink/issues/235) Improved update instructions (thanks to [tivyhosting](https://github.com/tivyhosting)). + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#237](https://github.com/shlinkio/shlink/issues/233) Solved errors when trying to geo-locate `null` IP addresses. + + Also improved how visitor IP addresses are discovered, thanks to [akrabat/ip-address-middleware](https://github.com/akrabat/ip-address-middleware) package. + + +## [1.13.1] - 2018-10-16 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#231](https://github.com/shlinkio/shlink/issues/197) Fixed error when processing visits. + + +## [1.13.0] - 2018-10-06 +### Added +* [#197](https://github.com/shlinkio/shlink/issues/197) Added [cakephp/chronos](https://book.cakephp.org/3.0/en/chronos.html) library for date manipulations. +* [#214](https://github.com/shlinkio/shlink/issues/214) Improved build script, which allows builds to be done without "jumping" outside the project directory, and generates smaller dist files. + + It also allows automating the dist file generation in travis-ci builds. + +* [#207](https://github.com/shlinkio/shlink/issues/207) Added two new config options which are asked during installation process. The config options already existed in previous shlink version, but you had to manually set their values. + + These are the new options: + + * Visits threshold to allow short URLs to be deleted. + * Check the visits threshold when trying to delete a short URL via REST API. + +### Changed +* [#211](https://github.com/shlinkio/shlink/issues/211) Extracted installer to its own module, which will simplify moving it to a separated package in the future. +* [#200](https://github.com/shlinkio/shlink/issues/200) and [#201](https://github.com/shlinkio/shlink/issues/201) Renamed REST Action classes and CLI Command classes to use the concept of `ShortUrl` instead of the concept of `ShortCode` when referring to the entity, and left the `short code` concept to the identifier which is used as a unique code for a specific `Short URL`. +* [#181](https://github.com/shlinkio/shlink/issues/181) When importing the configuration from a previous shlink installation, it no longer asks to import every block. Instead, it is capable of detecting only new config options introduced in the new version, and ask only for those. + + If no new options are found and you have selected to import config, no further questions will be asked and shlink will just import the old config. + +### Deprecated +* [#205](https://github.com/shlinkio/shlink/issues/205) Deprecated `[POST /authenticate]` endpoint, and allowed any API request to be automatically authenticated using the `X-Api-Key` header with a valid API key. + + This effectively deprecates the `Authorization: Bearer ` authentication form, but it will keep working. + +* As of [#200](https://github.com/shlinkio/shlink/issues/200) and [#201](https://github.com/shlinkio/shlink/issues/201) REST urls have changed from `/short-codes/...` to `/short-urls/...`, and the command namespaces have changed from `short-code:...` to `short-url:...`. + + In both cases, backwards compatibility has been retained and the old ones are aliases for the new ones, but the old ones are considered deprecated. + +### Removed +* *Nothing* + +### Fixed +* [#203](https://github.com/shlinkio/shlink/issues/203) Fixed some warnings thrown while unzipping distributable files. +* [#206](https://github.com/shlinkio/shlink/issues/206) An error is now thrown during installation if any required param is left empty, making the installer display a message and ask again until a value is set. + + +## [1.12.0] - 2018-09-15 +### Added +* [#187](https://github.com/shlinkio/shlink/issues/187) Included an API endpoint and a CLI command to delete short URLs. + + Due to the implicit danger of this operation, the deletion includes a safety check. URLs cannot be deleted if they have more than a specific amount of visits. + + The visits threshold is set to **15** by default and currently it has to be manually changed. In future versions the installation/update process will ask you about the value of the visits threshold. + + In order to change it, open the `config/autoload/delete_short_urls.global.php` file, which has this structure: + + ```php + return [ + + 'delete_short_urls' => [ + 'visits_threshold' => 15, + 'check_visits_threshold' => true, + ], + + ]; + ``` + + Properties are self explanatory. Change `check_visits_threshold` to `false` to completely disable this safety check, and change the value of `visits_threshold` to allow short URLs with a different number of visits to be deleted. + + Once changed, delete the `data/cache/app_config.php` file (if any) to let shlink know about the new values. + + This check is implicit for the API endpoint, but can be "disabled" for the CLI command, which will ask you when trying to delete a URL which has reached to threshold in order to force the deletion. + +* [#183](https://github.com/shlinkio/shlink/issues/183) and [#190](https://github.com/shlinkio/shlink/issues/190) Included important documentation improvements in the repository itself. You no longer need to go to the website in order to see how to install or use shlink. +* [#186](https://github.com/shlinkio/shlink/issues/186) Added a small robots.txt file that prevents 404 errors to be logged due to search engines trying to index the domain where shlink is located. Thanks to [@robwent](https://github.com/robwent) for the contribution. + +### Changed +* [#145](https://github.com/shlinkio/shlink/issues/145) Shlink now obfuscates IP addresses from visitors by replacing the latest octet by `0`, which does not affect geolocation and allows it to fulfil the GDPR. + + Other known services follow this same approach, like [Google Analytics](https://support.google.com/analytics/answer/2763052?hl=en) or [Matomo](https://matomo.org/docs/privacy/#step-1-automatically-anonymize-visitor-ips) + +* [#182](https://github.com/shlinkio/shlink/issues/182) The short URL creation API endpoints now return the same model used for lists and details endpoints. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#188](https://github.com/shlinkio/shlink/issues/188) Shlink now allows multiple short URLs to be created that resolve to the same long URL. + + +## [1.11.0] - 2018-08-13 +### Added +* [#170](https://github.com/shlinkio/shlink/issues/170) and [#171](https://github.com/shlinkio/shlink/issues/171) Updated `[GET /short-codes]` and `[GET /short-codes/{shortCode}]` endpoints to return more meaningful information and make their response consistent. + + The short URLs are now represented by this object in both cases: + + ```json + { + "shortCode": "12Kb3", + "shortUrl": "https://s.test/12Kb3", + "longUrl": "https://shlink.io", + "dateCreated": "2016-05-01T20:34:16+02:00", + "visitsCount": 1029, + "tags": [ + "shlink" + ], + "originalUrl": "https://shlink.io" + } + ``` + + The `originalUrl` property is considered deprecated and has been kept for backward compatibility purposes. It holds the same value as the `longUrl` property. + +### Changed +* *Nothing* + +### Deprecated +* The `originalUrl` property in `[GET /short-codes]` and `[GET /short-codes/{shortCode}]` endpoints is now deprecated and replaced by the `longUrl` property. + +### Removed +* *Nothing* + +### Fixed +* *Nothing* + + +## [1.10.2] - 2018-08-04 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#177](https://github.com/shlinkio/shlink/issues/177) Fixed `[GET] /short-codes` endpoint returning a 500 status code when trying to filter by `tags` and `searchTerm` at the same time. +* [#175](https://github.com/shlinkio/shlink/issues/175) Fixed error introduced in previous version, where you could end up banned from the service used to resolve IP address locations. + + In order to fix that, just fill [this form](http://ip-api.com/docs/unban) including your server's IP address and your server should be unbanned. + + In order to prevent this, after resolving 150 IP addresses, shlink now waits 1 minute before trying to resolve any more addresses. + + +## [1.10.1] - 2018-08-02 +### Added +* *Nothing* + +### Changed +* [#167](https://github.com/shlinkio/shlink/issues/167) Shlink version is now set at build time to avoid older version numbers to be kept in newer builds. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#165](https://github.com/shlinkio/shlink/issues/165) Fixed custom slugs failing when they are longer than 10 characters. +* [#166](https://github.com/shlinkio/shlink/issues/166) Fixed unusual edge case in which visits were not properly counted when ordering by visit and filtering by search term in `[GET] /short-codes` API endpoint. +* [#174](https://github.com/shlinkio/shlink/issues/174) Fixed geolocation not working due to a deprecation on used service. +* [#172](https://github.com/shlinkio/shlink/issues/172) Documented missing filtering params for `[GET] /short-codes/{shortCode}/visits` API endpoint, which allow the list to be filtered by date range. + + For example: `https://s.test/rest/v1/short-urls/abc123/visits?startDate=2017-05-23&endDate=2017-10-05` + +* [#169](https://github.com/shlinkio/shlink/issues/169) Fixed unhandled error when parsing `ShortUrlMeta` and date fields are already `DateTime` instances. + + +## [1.10.0] - 2018-07-09 +### Added +* [#161](https://github.com/shlinkio/shlink/issues/161) AddED support for shlink to be run with [swoole](https://www.swoole.co.uk/) via [zend-expressive-swoole](https://github.com/zendframework/zend-expressive-swoole) package + +### Changed +* [#159](https://github.com/shlinkio/shlink/issues/159) Updated CHANGELOG to follow the [keep-a-changelog](https://keepachangelog.com) format +* [#160](https://github.com/shlinkio/shlink/issues/160) Update infection to v0.9 and phpstan to v 0.10 + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* *Nothing* + + +## [1.9.1] - 2018-06-18 +### Added +* [#155](https://github.com/shlinkio/shlink/issues/155) Improved the pagination object returned in lists, including more meaningful properties. + + * Old structure: + + ```json + { + "pagination": { + "currentPage": 1, + "pagesCount": 2 + } + } + ``` + + * New structure: + + ```json + { + "pagination": { + "currentPage": 2, + "pagesCount": 13, + "itemsPerPage": 10, + "itemsInCurrentPage": 10, + "totalItems": 126 + } + } + ``` + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#154](https://github.com/shlinkio/shlink/issues/154) Fixed sizes of every result page when filtering by searchTerm +* [#157](https://github.com/shlinkio/shlink/issues/157) Background commands executed by installation process now respect the originally used php binary + + +## [1.9.0] - 2018-05-07 +### Added +* [#147](https://github.com/shlinkio/shlink/issues/147) Allowed short URLs to be created on the fly using a single API request, including the API key in a query param. + + This eases integration with third party services. + + With this feature, a simple request to a URL like `https://s.test/rest/v1/short-codes/shorten?apiKey=[YOUR_API_KEY]&longUrl=[URL_TO_BE_SHORTENED]` would return the shortened one in JSON or plain text format. + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#139](https://github.com/shlinkio/shlink/issues/139) Ensured all core actions log exceptions + + +## [1.8.1] - 2018-04-07 +### Added +* *Nothing* + +### Changed +* [#141](https://github.com/shlinkio/shlink/issues/141) Removed workaround used in `PathVersionMiddleware`, since the bug in zend-stratigility has been fixed. + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#140](https://github.com/shlinkio/shlink/issues/140) Fixed warning thrown during installation while trying to include doctrine script + + +## [1.8.0] - 2018-03-29 +### Added +* [#125](https://github.com/shlinkio/shlink/issues/125) Implemented a path which returns a 1px image instead of a redirection. + + Useful to track emails. Just add an image pointing to a URL like `https://s.test/abc123/track` to any email and an invisible image will be generated tracking every time the email is opened. + +* [#132](https://github.com/shlinkio/shlink/issues/132) Added infection to improve tests + +### Changed +* [#130](https://github.com/shlinkio/shlink/issues/130) Updated to Expressive 3 +* [#137](https://github.com/shlinkio/shlink/issues/137) Updated symfony components to v4 + +### Deprecated +* *Nothing* + +### Removed +* [#131](https://github.com/shlinkio/shlink/issues/131) Dropped support for PHP 7 + +### Fixed +* *Nothing* + + +## [1.7.2] - 2018-03-26 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#135](https://github.com/shlinkio/shlink/issues/135) Fixed `PathVersionMiddleware` being ignored when using expressive 2.2 + + +## [1.7.1] - 2018-03-21 +### Added +* *Nothing* + +### Changed +* [#128](https://github.com/shlinkio/shlink/issues/128) Upgraded to expressive 2.2 + + This will ease the upcoming update to expressive 3 + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#126](https://github.com/shlinkio/shlink/issues/126) Fixed `E_USER_DEPRECATED` errors triggered when using Expressive 2.2 + + +## [1.7.0] - 2018-01-21 +### Added +* [#88](https://github.com/shlinkio/shlink/issues/88) Allowed tracking of short URLs to be disabled by including a configurable query param +* [#108](https://github.com/shlinkio/shlink/issues/108) Allowed metadata to be defined when creating short codes + +### Changed +* [#113](https://github.com/shlinkio/shlink/issues/113) Updated CLI commands to use `SymfonyStyle` +* [#112](https://github.com/shlinkio/shlink/issues/112) Enabled Lazy loading in CLI commands +* [#117](https://github.com/shlinkio/shlink/issues/117) Every module which throws exceptions has now its own `ExceptionInterface` extending `Throwable` +* [#115](https://github.com/shlinkio/shlink/issues/115) Added phpstan to build matrix on PHP >=7.1 envs +* [#114](https://github.com/shlinkio/shlink/issues/114) Replaced [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv) dev requirement by [symfony/dotenv](https://github.com/symfony/dotenv) + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* *Nothing* + + +## [1.6.2] - 2017-10-25 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#109](https://github.com/shlinkio/shlink/issues/109) Fixed installation error due to typo in latest migration + + +## [1.6.1] - 2017-10-24 +### Added +* *Nothing* + +### Changed +* [#110](https://github.com/shlinkio/shlink/issues/110) Created `.gitattributes` file to define files to be excluded from distributable package + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* *Nothing* + + +## [1.6.0] - 2017-10-23 +### Added +* [#44](https://github.com/shlinkio/shlink/issues/44) Now it is possible to set custom slugs for short URLs instead of using a generated short code +* [#47](https://github.com/shlinkio/shlink/issues/47) Allowed to limit short URLs availability by date range +* [#48](https://github.com/shlinkio/shlink/issues/48) Allowed to limit the number of visits to a short URL +* [#105](https://github.com/shlinkio/shlink/pull/105) Added option to enable/disable URL validation by response status code + +### Changed +* [#27](https://github.com/shlinkio/shlink/issues/27) Added repository functional tests with dbunit +* [#101](https://github.com/shlinkio/shlink/issues/101) Now specific actions just capture very specific exceptions, and let the `ErrorHandler` catch any other unhandled exception +* [#104](https://github.com/shlinkio/shlink/issues/104) Used different templates for *requested-short-code-does-not-exist* and *route-could-not-be-match* +* [#99](https://github.com/shlinkio/shlink/issues/99) Replaced usages of `AnnotatedFactory` by `ConfigAbstractFactory` +* [#100](https://github.com/shlinkio/shlink/issues/100) Updated templates engine. Replaced twig by plates +* [#102](https://github.com/shlinkio/shlink/issues/102) Improved coding standards strictness + +### Deprecated +* *Nothing* + +### Removed +* [#86](https://github.com/shlinkio/shlink/issues/86) Dropped support for PHP 5 + +### Fixed +* [#103](https://github.com/shlinkio/shlink/issues/103) `NotFoundDelegate` now returns proper content types based on accepted content + + +## [1.5.0] - 2017-07-16 +### Added +* [#95](https://github.com/shlinkio/shlink/issues/95) Added tags CRUD to CLI +* [#59](https://github.com/shlinkio/shlink/issues/59) Added tags CRUD to REST +* [#66](https://github.com/shlinkio/shlink/issues/66) Allowed certain information to be imported from and older shlink instance directory when updating + +### Changed +* [#96](https://github.com/shlinkio/shlink/issues/96) Added namespace to functions +* [#76](https://github.com/shlinkio/shlink/issues/76) Added response examples to swagger docs +* [#93](https://github.com/shlinkio/shlink/issues/93) Improved cross domain management by using the `ImplicitOptionsMiddleware` + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#92](https://github.com/shlinkio/shlink/issues/92) Fixed formatted dates, using an ISO compliant format + + +## [1.4.0] - 2017-03-25 +### Added +* *Nothing* + +### Changed +* [#89](https://github.com/shlinkio/shlink/issues/89) Updated to expressive 2 + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* *Nothing* + + +## [1.3.1] - 2017-01-22 +### Added +* *Nothing* + +### Changed +* [#82](https://github.com/shlinkio/shlink/issues/82) Enabled `FastRoute` routes cache +* [#85](https://github.com/shlinkio/shlink/issues/85) Updated year in license file +* [#81](https://github.com/shlinkio/shlink/issues/81) Added docker containers config + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#83](https://github.com/shlinkio/shlink/issues/83) Fixed short codes list: search in tags when filtering by query string +* [#79](https://github.com/shlinkio/shlink/issues/79) Increased the number of followed redirects +* [#75](https://github.com/shlinkio/shlink/issues/75) Applied `PathVersionMiddleware` only to rest routes defining it by configuration instead of code +* [#77](https://github.com/shlinkio/shlink/issues/77) Allowed defining database server hostname and port + + +## [1.3.0] - 2016-10-23 +### Added +* [#67](https://github.com/shlinkio/shlink/issues/67) Allowed to order the short codes list +* [#60](https://github.com/shlinkio/shlink/issues/60) Accepted JSON requests in REST and used a body parser middleware to set the request's `parsedBody` +* [#72](https://github.com/shlinkio/shlink/issues/72) When listing API keys from CLI, use yellow color for enabled keys that have expired +* [#58](https://github.com/shlinkio/shlink/issues/58) Allowed to filter short URLs by tag +* [#69](https://github.com/shlinkio/shlink/issues/69) Allowed to filter short URLs by text query +* [#73](https://github.com/shlinkio/shlink/issues/73) Added tag-related endpoints to swagger file +* [#63](https://github.com/shlinkio/shlink/issues/63) Added path versioning to REST API routes + +### Changed +* [#71](https://github.com/shlinkio/shlink/issues/71) Separated swagger docs into multiple files + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* *Nothing* + + +## [1.2.2] - 2016-08-29 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* Fixed minor bugs on CORS requests + + +## [1.2.1] - 2016-08-21 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#62](https://github.com/shlinkio/shlink/issues/62) Fixed cross-domain requests in REST API + + +## [1.2.0] - 2016-08-21 +### Added +* [#45](https://github.com/shlinkio/shlink/issues/45) Allowed to define tags on short codes, to improve filtering and classification +* [#7](https://github.com/shlinkio/shlink/issues/7) Added website previews while listing available URLs +* [#57](https://github.com/shlinkio/shlink/issues/57) Added database migrations system to improve updating between versions +* [#31](https://github.com/shlinkio/shlink/issues/31) Added support for other database management systems by improving the `EntityManager` factory +* [#51](https://github.com/shlinkio/shlink/issues/51) Generated build process to create app package and ease distribution +* [#38](https://github.com/shlinkio/shlink/issues/38) Defined installation script. It will request dynamic data on the fly so that there is no need to define env vars +* [#55](https://github.com/shlinkio/shlink/issues/55) Created update script which does not try to create a new database + +### Changed +* [#54](https://github.com/shlinkio/shlink/issues/54) Added cache namespace to prevent name collisions with other apps in the same environment +* [#29](https://github.com/shlinkio/shlink/issues/29) Used the [acelaya/ze-content-based-error-handler](https://github.com/acelaya/ze-content-based-error-handler) package instead of custom error handler implementation + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#53](https://github.com/shlinkio/shlink/issues/53) Fixed entities database interoperability +* [#52](https://github.com/shlinkio/shlink/issues/52) Added missing htaccess file for apache environments + + +## [1.1.0] - 2016-08-09 +### Added +* [#46](https://github.com/shlinkio/shlink/issues/46) Defined a route that returns a QR code representing the shortened URL. + + In order to get the QR code URL, use a pattern like `https://s.test/abc123/qr-code` + +* [#32](https://github.com/shlinkio/shlink/issues/32) Added support for other cache adapters by improving the Cache factory +* [#14](https://github.com/shlinkio/shlink/issues/14) Added logger and enabled errors logging +* [#13](https://github.com/shlinkio/shlink/issues/13) Improved REST authentication + +### Changed +* [#41](https://github.com/shlinkio/shlink/issues/41) Cached the "short code" => "URL" map to prevent extra DB hits +* [#39](https://github.com/shlinkio/shlink/issues/39) Changed copyright from "Alejandro Celaya" to "Shlink" in error pages +* [#42](https://github.com/shlinkio/shlink/issues/42) REST endpoints that need to find *something* now return a 404 when it is not found +* [#35](https://github.com/shlinkio/shlink/issues/35) Updated CLI commands to use the same PHP namespace as the one used for the command name + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#40](https://github.com/shlinkio/shlink/issues/40) Taken into account the `X-Forwarded-For` header in order to get the visitor information, in case the server is behind a load balancer or proxy + + +## [1.0.0] - 2016-08-01 +### Added +* [#33](https://github.com/shlinkio/shlink/issues/33) Created a command that generates a short code charset by randomizing the default one +* [#23](https://github.com/shlinkio/shlink/issues/23) Translated application literals +* [#21](https://github.com/shlinkio/shlink/issues/21) Allowed to filter visits by date range +* [#4](https://github.com/shlinkio/shlink/issues/4) Added installation steps +* [#12](https://github.com/shlinkio/shlink/issues/12) Improved code coverage + +### Changed +* [#15](https://github.com/shlinkio/shlink/issues/15) HTTP requests now return JSON/HTML responses for errors (4xx and 5xx) based on `Accept` header +* [#22](https://github.com/shlinkio/shlink/issues/22) Now visits locations data is saved on a `visit_locations` table +* [#20](https://github.com/shlinkio/shlink/issues/20) Injected cross domain headers in response only if the `Origin` header is present in the request +* [#11](https://github.com/shlinkio/shlink/issues/11) Separated code into multiple modules +* [#18](https://github.com/shlinkio/shlink/issues/18) Grouped routable middleware in an Action namespace +* [#6](https://github.com/shlinkio/shlink/issues/6) Project no longer depends on [zendframework/zend-expressive-helpers](https://github.com/zendframework/zend-expressive-helpers) package +* [#30](https://github.com/shlinkio/shlink/issues/30) Replaced the "services" first level config entry by "dependencies", in order to fulfill default Expressive naming +* [#25](https://github.com/shlinkio/shlink/issues/25) Replaced "Middleware" suffix on routable middlewares by "Action" +* [#19](https://github.com/shlinkio/shlink/issues/19) Changed the vendor and app namespace from `Acelaya\UrlShortener` to `Shlinkio\Shlink` + +### Deprecated +* *Nothing* + +### Removed +* [#36](https://github.com/shlinkio/shlink/issues/36) Removed hhvm from the CI matrix since it doesn't support array constants and will fail + +### Fixed +* [#24](https://github.com/shlinkio/shlink/issues/24) Prevented duplicated short codes errors because of the case insensitive behavior on MySQL + + +## [0.2.0] - 2016-08-01 +### Added +* [#8](https://github.com/shlinkio/shlink/issues/8) Created a REST API +* [#10](https://github.com/shlinkio/shlink/issues/10) Added more CLI functionality +* [#5](https://github.com/shlinkio/shlink/issues/5) Created a CHANGELOG file + +### Changed +* [#9](https://github.com/shlinkio/shlink/issues/9) Used [symfony/console](https://github.com/symfony/console) to dispatch console requests, instead of trying to integrate the process with expressive + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* *Nothing* +* diff --git a/docs/changelog-archive/CHANGELOG-2.x.md b/docs/changelog-archive/CHANGELOG-2.x.md new file mode 100644 index 000000000..72bb6f5d9 --- /dev/null +++ b/docs/changelog-archive/CHANGELOG-2.x.md @@ -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 `-` 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. diff --git a/docs/swagger/definitions/OrphanVisit.json b/docs/swagger/definitions/OrphanVisit.json index a8b4954a6..897c60497 100644 --- a/docs/swagger/definitions/OrphanVisit.json +++ b/docs/swagger/definitions/OrphanVisit.json @@ -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": [ diff --git a/docs/swagger/definitions/Visit.json b/docs/swagger/definitions/Visit.json index ecb6b9f9b..c4589bb18 100644 --- a/docs/swagger/definitions/Visit.json +++ b/docs/swagger/definitions/Visit.json @@ -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" } } } diff --git a/docs/swagger/paths/v1_short-urls_{shortCode}_visits.json b/docs/swagger/paths/v1_short-urls_{shortCode}_visits.json index 71e701488..f3799f13b 100644 --- a/docs/swagger/paths/v1_short-urls_{shortCode}_visits.json +++ b/docs/swagger/paths/v1_short-urls_{shortCode}_visits.json @@ -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": { diff --git a/docs/swagger/paths/v2_domains_{domain}_visits.json b/docs/swagger/paths/v2_domains_{domain}_visits.json index d3acf60e9..a477cb8e7 100644 --- a/docs/swagger/paths/v2_domains_{domain}_visits.json +++ b/docs/swagger/paths/v2_domains_{domain}_visits.json @@ -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": { diff --git a/docs/swagger/paths/v2_tags_{tag}_visits.json b/docs/swagger/paths/v2_tags_{tag}_visits.json index 2a0148ec2..1f3dabf2b 100644 --- a/docs/swagger/paths/v2_tags_{tag}_visits.json +++ b/docs/swagger/paths/v2_tags_{tag}_visits.json @@ -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": { diff --git a/docs/swagger/paths/v2_visits_non-orphan.json b/docs/swagger/paths/v2_visits_non-orphan.json index da0bdd14f..65b11252f 100644 --- a/docs/swagger/paths/v2_visits_non-orphan.json +++ b/docs/swagger/paths/v2_visits_non-orphan.json @@ -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": { diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 94237c15b..2ee33a1d7 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -14,6 +14,8 @@ 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 @@ Command\RedirectRule\ManageRedirectRulesCommand::NAME => Command\RedirectRule\ManageRedirectRulesCommand::class, + + Command\Integration\MatomoSendVisitsCommand::NAME => Command\Integration\MatomoSendVisitsCommand::class, ], ], diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 282a1db5b..f9b90daca 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -8,6 +8,7 @@ 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 @@ 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 @@ Command\Domain\GetDomainVisitsCommand::class => ConfigAbstractFactory::class, Command\RedirectRule\ManageRedirectRulesCommand::class => ConfigAbstractFactory::class, + + Command\Integration\MatomoSendVisitsCommand::class => ConfigAbstractFactory::class, ], ], @@ -96,6 +100,7 @@ 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 @@ RedirectRule\RedirectRuleHandler::class, ], + Command\Integration\MatomoSendVisitsCommand::class => [ + Matomo\MatomoOptions::class, + Matomo\MatomoVisitSender::class, + ], + Command\Db\CreateDatabaseCommand::class => [ LockFactory::class, Util\ProcessRunner::class, diff --git a/module/CLI/src/Command/Api/ListKeysCommand.php b/module/CLI/src/Command/Api/ListKeysCommand.php index fab020877..40ae8eef8 100644 --- a/module/CLI/src/Command/Api/ListKeysCommand.php +++ b/module/CLI/src/Command/Api/ListKeysCommand.php @@ -50,11 +50,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $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)); } diff --git a/module/CLI/src/Command/Domain/GetDomainVisitsCommand.php b/module/CLI/src/Command/Domain/GetDomainVisitsCommand.php index 8d2eb8c99..dd3797f87 100644 --- a/module/CLI/src/Command/Domain/GetDomainVisitsCommand.php +++ b/module/CLI/src/Command/Domain/GetDomainVisitsCommand.php @@ -44,7 +44,7 @@ protected function getVisitsPaginator(InputInterface $input, DateRange $dateRang */ protected function mapExtraFields(Visit $visit): array { - $shortUrl = $visit->getShortUrl(); + $shortUrl = $visit->shortUrl; return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)]; } } diff --git a/module/CLI/src/Command/Integration/MatomoSendVisitsCommand.php b/module/CLI/src/Command/Integration/MatomoSendVisitsCommand.php new file mode 100644 index 000000000..ba9a794e6 --- /dev/null +++ b/module/CLI/src/Command/Integration/MatomoSendVisitsCommand.php @@ -0,0 +1,140 @@ +matomoEnabled = $matomoOptions->enabled; + parent::__construct(); + } + + protected function configure(): void + { + $help = <<%command.name% + + Send all visits created before 2024: + %command.name% --until 2023-12-31 + + Send all visits created after a specific day: + %command.name% --since 2022-03-27 + + Send all visits created during 2022: + %command.name% --since 2022-01-01 --until 2022-12-31 + 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('E'); + if ($this->io->isVerbose()) { + $this->getApplication()?->renderThrowable($e, $this->io); + } + } +} diff --git a/module/CLI/src/Command/ShortUrl/DeleteExpiredShortUrlsCommand.php b/module/CLI/src/Command/ShortUrl/DeleteExpiredShortUrlsCommand.php new file mode 100644 index 000000000..109beff78 --- /dev/null +++ b/module/CLI/src/Command/ShortUrl/DeleteExpiredShortUrlsCommand.php @@ -0,0 +1,75 @@ +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; + } +} diff --git a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php index a318e6e42..4e3a7706c 100644 --- a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php +++ b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php @@ -13,6 +13,7 @@ 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\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 @@ protected function execute(InputInterface $input, OutputInterface $output): int return ExitCode::EXIT_SUCCESS; } + /** + * @param array $columnsMap + */ private function renderPage( OutputInterface $output, array $columnsMap, @@ -184,10 +188,10 @@ private function renderPage( ): 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 @@ private function processOrderBy(InputInterface $input): ?string return $dir === null ? $field : sprintf('%s-%s', $field, $dir); } + /** + * @return array + */ private function resolveColumnsMap(InputInterface $input): array { $pickProp = static fn (string $prop): callable => static fn (array $shortUrl) => $shortUrl[$prop]; @@ -229,11 +236,11 @@ private function resolveColumnsMap(InputInterface $input): array } 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; diff --git a/module/CLI/src/Command/Tag/GetTagVisitsCommand.php b/module/CLI/src/Command/Tag/GetTagVisitsCommand.php index 290a172a6..1dfd0ba9b 100644 --- a/module/CLI/src/Command/Tag/GetTagVisitsCommand.php +++ b/module/CLI/src/Command/Tag/GetTagVisitsCommand.php @@ -44,7 +44,7 @@ protected function getVisitsPaginator(InputInterface $input, DateRange $dateRang */ protected function mapExtraFields(Visit $visit): array { - $shortUrl = $visit->getShortUrl(); + $shortUrl = $visit->shortUrl; return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)]; } } diff --git a/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php b/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php index bd20a4ae5..d3a49c53e 100644 --- a/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php +++ b/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php @@ -54,9 +54,12 @@ private function resolveRowsAndHeaders(Paginator $paginator): array $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, ]; diff --git a/module/CLI/src/Command/Visit/GetNonOrphanVisitsCommand.php b/module/CLI/src/Command/Visit/GetNonOrphanVisitsCommand.php index 0dd32f3ec..1462620da 100644 --- a/module/CLI/src/Command/Visit/GetNonOrphanVisitsCommand.php +++ b/module/CLI/src/Command/Visit/GetNonOrphanVisitsCommand.php @@ -40,7 +40,7 @@ protected function getVisitsPaginator(InputInterface $input, DateRange $dateRang */ protected function mapExtraFields(Visit $visit): array { - $shortUrl = $visit->getShortUrl(); + $shortUrl = $visit->shortUrl; return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)]; } } diff --git a/module/CLI/src/Command/Visit/GetOrphanVisitsCommand.php b/module/CLI/src/Command/Visit/GetOrphanVisitsCommand.php index 7beae19aa..d495db77c 100644 --- a/module/CLI/src/Command/Visit/GetOrphanVisitsCommand.php +++ b/module/CLI/src/Command/Visit/GetOrphanVisitsCommand.php @@ -42,6 +42,6 @@ protected function getVisitsPaginator(InputInterface $input, DateRange $dateRang */ protected function mapExtraFields(Visit $visit): array { - return ['type' => $visit->type()->value]; + return ['type' => $visit->type->value]; } } diff --git a/module/CLI/src/Command/Visit/LocateVisitsCommand.php b/module/CLI/src/Command/Visit/LocateVisitsCommand.php index 09e535560..596b287e9 100644 --- a/module/CLI/src/Command/Visit/LocateVisitsCommand.php +++ b/module/CLI/src/Command/Visit/LocateVisitsCommand.php @@ -132,7 +132,7 @@ protected function lockedExecute(InputInterface $input, OutputInterface $output) */ public function geolocateVisit(Visit $visit): Location { - $ipAddr = $visit->getRemoteAddr() ?? '?'; + $ipAddr = $visit->remoteAddr ?? '?'; $this->io->write(sprintf('Processing IP %s', $ipAddr)); try { @@ -154,9 +154,9 @@ public function geolocateVisit(Visit $visit): Location public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void { - if (! $visitLocation->isEmpty()) { - $this->io->writeln(sprintf(' [Address located in "%s"]', $visitLocation->getCountryName())); - } elseif ($visit->hasRemoteAddr() && $visit->getRemoteAddr() !== IpAddress::LOCALHOST) { + if (! $visitLocation->isEmpty) { + $this->io->writeln(sprintf(' [Address located in "%s"]', $visitLocation->countryName)); + } elseif ($visit->hasRemoteAddr() && $visit->remoteAddr !== IpAddress::LOCALHOST) { $this->io->writeln(' [Could not locate address]'); } } diff --git a/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php index 7f4bd0768..6563abc06 100644 --- a/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php +++ b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php @@ -60,7 +60,7 @@ public function outputIsProperlyGenerated(): void +---------+---------------------------+------------+---------+--------+---------------+ | 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, diff --git a/module/CLI/test/Command/Integration/MatomoSendVisitsCommandTest.php b/module/CLI/test/Command/Integration/MatomoSendVisitsCommandTest.php new file mode 100644 index 000000000..e3a527332 --- /dev/null +++ b/module/CLI/test/Command/Integration/MatomoSendVisitsCommandTest.php @@ -0,0 +1,135 @@ +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]; + } +} diff --git a/module/CLI/test/Command/ShortUrl/DeleteExpiredShortUrlsCommandTest.php b/module/CLI/test/Command/ShortUrl/DeleteExpiredShortUrlsCommandTest.php new file mode 100644 index 000000000..ea5800645 --- /dev/null +++ b/module/CLI/test/Command/ShortUrl/DeleteExpiredShortUrlsCommandTest.php @@ -0,0 +1,91 @@ +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]); + } +} diff --git a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php index f93ab5ecc..ba6735ba5 100644 --- a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php @@ -110,7 +110,7 @@ public function outputIsProperlyGenerated(): void +---------+---------------------------+------------+---------+--------+ | Referer | Date | User agent | Country | City | +---------+---------------------------+------------+---------+--------+ - | foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | + | foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | +---------+---------------------------+------------+---------+--------+ OUTPUT, diff --git a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php index 6859ec137..6016da1c1 100644 --- a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php @@ -16,6 +16,7 @@ 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 @@ public function loadingMorePagesCallsListMoreTimes(): void // 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 @@ public function havingMorePagesButAnsweringNoCallsListJustOnce(): void // 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 @@ public function provideOptionalFlagsMakesNewColumnsToBeIncluded( $this->shortUrlService->expects($this->once())->method('listShortUrls')->with( ShortUrlsParams::emptyInstance(), )->willReturn(new Paginator(new ArrayAdapter([ - ShortUrl::create(ShortUrlCreation::fromRawData([ - 'longUrl' => 'https://foo.com', - 'tags' => ['foo', 'bar', 'baz'], - 'apiKey' => $apiKey, - ])), + ShortUrlWithVisitsSummary::fromShortUrl( + ShortUrl::create(ShortUrlCreation::fromRawData([ + 'longUrl' => 'https://foo.com', + 'tags' => ['foo', 'bar', 'baz'], + 'apiKey' => $apiKey, + ])), + ), ]))); $this->commandTester->setInputs(['y']); diff --git a/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php b/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php index a2dc059f6..9b79f5098 100644 --- a/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php +++ b/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php @@ -57,7 +57,7 @@ public function outputIsProperlyGenerated(): void +---------+---------------------------+------------+---------+--------+---------------+ | 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, diff --git a/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php index 439b33bdf..0462c2c0a 100644 --- a/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php @@ -56,7 +56,7 @@ public function outputIsProperlyGenerated(): void +---------+---------------------------+------------+---------+--------+---------------+ | 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, diff --git a/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php index a9e2a50cf..29914b616 100644 --- a/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php @@ -54,7 +54,7 @@ public function outputIsProperlyGenerated(array $args, bool $includesType): void +---------+---------------------------+------------+---------+--------+----------+ | 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, diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index ed64a30e0..5fcc8e44b 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -10,6 +10,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ Matomo\MatomoOptions::class => [ValinorConfigFactory::class, 'config.matomo'], Matomo\MatomoTrackerBuilder::class => ConfigAbstractFactory::class, + Matomo\MatomoVisitSender::class => ConfigAbstractFactory::class, ], 'aliases' => [ @@ -105,6 +112,11 @@ 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 @@ 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 @@ '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 @@ ], 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', diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php index 358ee6bde..b159da133 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php @@ -67,6 +67,11 @@ ->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') diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Visit.Entity.OrphanVisitsCount.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Visit.Entity.OrphanVisitsCount.php new file mode 100644 index 000000000..dbdf9e9ab --- /dev/null +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Visit.Entity.OrphanVisitsCount.php @@ -0,0 +1,41 @@ +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'); +}; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Visit.Entity.ShortUrlVisitsCount.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Visit.Entity.ShortUrlVisitsCount.php new file mode 100644 index 000000000..07977a50b --- /dev/null +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Visit.Entity.ShortUrlVisitsCount.php @@ -0,0 +1,45 @@ +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'); +}; diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index 8fc534d43..f401f255a 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -12,7 +12,6 @@ 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 @@ EventDispatcher\Matomo\SendVisitToMatomo::class => [ 'em', 'Logger_Shlink', - ShortUrlStringifier::class, Matomo\MatomoOptions::class, - Matomo\MatomoTrackerBuilder::class, + Matomo\MatomoVisitSender::class, ], EventDispatcher\UpdateGeoLiteDb::class => [ diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php index 5ba45ac21..2e238e997 100644 --- a/module/Core/functions/functions.php +++ b/module/Core/functions/functions.php @@ -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) */ diff --git a/module/Core/migrations/Version20240306132518.php b/module/Core/migrations/Version20240306132518.php new file mode 100644 index 000000000..a2960e9b7 --- /dev/null +++ b/module/Core/migrations/Version20240306132518.php @@ -0,0 +1,65 @@ +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); + } +} diff --git a/module/Core/migrations/Version20240318084804.php b/module/Core/migrations/Version20240318084804.php new file mode 100644 index 000000000..6ff25facc --- /dev/null +++ b/module/Core/migrations/Version20240318084804.php @@ -0,0 +1,58 @@ +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(); + } +} diff --git a/module/Core/migrations/Version20240331111103.php b/module/Core/migrations/Version20240331111103.php new file mode 100644 index 000000000..ec0a50877 --- /dev/null +++ b/module/Core/migrations/Version20240331111103.php @@ -0,0 +1,56 @@ +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); + } +} diff --git a/module/Core/migrations/Version20240331111447.php b/module/Core/migrations/Version20240331111447.php new file mode 100644 index 000000000..333656c47 --- /dev/null +++ b/module/Core/migrations/Version20240331111447.php @@ -0,0 +1,45 @@ +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(); + } +} diff --git a/module/Core/src/Config/EnvVars.php b/module/Core/src/Config/EnvVars.php index e5df95326..bae68e848 100644 --- a/module/Core/src/Config/EnvVars.php +++ b/module/Core/src/Config/EnvVars.php @@ -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 { diff --git a/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php b/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php index 42f00889d..a73c584d1 100644 --- a/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php +++ b/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php @@ -4,8 +4,6 @@ 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 @@ public function __invoke(array $config): array $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; diff --git a/module/Core/src/Domain/Entity/Domain.php b/module/Core/src/Domain/Entity/Domain.php index 4e6ea8656..b3d2b7340 100644 --- a/module/Core/src/Domain/Entity/Domain.php +++ b/module/Core/src/Domain/Entity/Domain.php @@ -11,12 +11,12 @@ 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 diff --git a/module/Core/src/EventDispatcher/Helper/RequestIdProvider.php b/module/Core/src/EventDispatcher/Helper/RequestIdProvider.php new file mode 100644 index 000000000..2ff529ee2 --- /dev/null +++ b/module/Core/src/EventDispatcher/Helper/RequestIdProvider.php @@ -0,0 +1,20 @@ +requestIdMiddleware->currentRequestId(); + } +} diff --git a/module/Core/src/EventDispatcher/LocateVisit.php b/module/Core/src/EventDispatcher/LocateVisit.php index f139c0f53..6f7fb7e88 100644 --- a/module/Core/src/EventDispatcher/LocateVisit.php +++ b/module/Core/src/EventDispatcher/LocateVisit.php @@ -17,7 +17,7 @@ use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface; use Throwable; -class LocateVisit +readonly class LocateVisit { public function __construct( private IpLocationResolverInterface $ipLocationResolver, @@ -55,7 +55,7 @@ private function locateVisit(string $visitId, ?string $originalIpAddress, Visit } $isLocatable = $originalIpAddress !== null || $visit->isLocatable(); - $addr = $originalIpAddress ?? $visit->getRemoteAddr() ?? ''; + $addr = $originalIpAddress ?? $visit->remoteAddr ?? ''; try { $location = $isLocatable ? $this->ipLocationResolver->resolveIpLocation($addr) : Location::emptyInstance(); diff --git a/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php b/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php index ad9660cba..5a85aed43 100644 --- a/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php +++ b/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php @@ -8,19 +8,17 @@ 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 @@ public function __invoke(VisitLocated $visitLocated): void } 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); - } } diff --git a/module/Core/src/EventDispatcher/PublishingUpdatesGenerator.php b/module/Core/src/EventDispatcher/PublishingUpdatesGenerator.php index 9acdfd048..82ada6e17 100644 --- a/module/Core/src/EventDispatcher/PublishingUpdatesGenerator.php +++ b/module/Core/src/EventDispatcher/PublishingUpdatesGenerator.php @@ -9,18 +9,16 @@ 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 @@ public function newVisitUpdate(Visit $visit): Update 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, [ diff --git a/module/Core/src/Importer/ImportedLinksProcessor.php b/module/Core/src/Importer/ImportedLinksProcessor.php index 7a9c3b92c..16da0a09f 100644 --- a/module/Core/src/Importer/ImportedLinksProcessor.php +++ b/module/Core/src/Importer/ImportedLinksProcessor.php @@ -139,7 +139,7 @@ private function importOrphanVisits(StyleInterface $io, iterable $orphanVisits): $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; } diff --git a/module/Core/src/Matomo/MatomoTrackerBuilder.php b/module/Core/src/Matomo/MatomoTrackerBuilder.php index 4bad67990..e006271bc 100644 --- a/module/Core/src/Matomo/MatomoTrackerBuilder.php +++ b/module/Core/src/Matomo/MatomoTrackerBuilder.php @@ -7,11 +7,11 @@ 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) { } diff --git a/module/Core/src/Matomo/MatomoVisitSender.php b/module/Core/src/Matomo/MatomoVisitSender.php new file mode 100644 index 000000000..d2a4484a6 --- /dev/null +++ b/module/Core/src/Matomo/MatomoVisitSender.php @@ -0,0 +1,91 @@ +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); + } +} diff --git a/module/Core/src/Matomo/MatomoVisitSenderInterface.php b/module/Core/src/Matomo/MatomoVisitSenderInterface.php new file mode 100644 index 000000000..e1b1c3cbc --- /dev/null +++ b/module/Core/src/Matomo/MatomoVisitSenderInterface.php @@ -0,0 +1,22 @@ + $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; + } +} diff --git a/module/Core/src/Matomo/VisitSendingProgressTrackerInterface.php b/module/Core/src/Matomo/VisitSendingProgressTrackerInterface.php new file mode 100644 index 000000000..94686992d --- /dev/null +++ b/module/Core/src/Matomo/VisitSendingProgressTrackerInterface.php @@ -0,0 +1,14 @@ +field !== null; + return new self($field, self::DESC_DIR); } } diff --git a/module/Core/src/ShortUrl/DeleteShortUrlService.php b/module/Core/src/ShortUrl/DeleteShortUrlService.php index 16fe6ac05..b65381aab 100644 --- a/module/Core/src/ShortUrl/DeleteShortUrlService.php +++ b/module/Core/src/ShortUrl/DeleteShortUrlService.php @@ -8,15 +8,18 @@ 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 @@ public function deleteByShortCode( private function isThresholdReached(ShortUrl $shortUrl): bool { - if (! $this->deleteShortUrlsOptions->checkVisitsThreshold) { - return false; - } + return $this->deleteShortUrlsOptions->checkVisitsThreshold && $shortUrl->reachedVisits( + $this->deleteShortUrlsOptions->visitsThreshold, + ); + } + + public function deleteExpiredShortUrls(ExpiredShortUrlsConditions $conditions): int + { + return $this->expiredShortUrlsRepository->delete($conditions); + } - return $shortUrl->getVisitsCount() >= $this->deleteShortUrlsOptions->visitsThreshold; + public function countExpiredShortUrls(ExpiredShortUrlsConditions $conditions): int + { + return $this->expiredShortUrlsRepository->dryCount($conditions); } } diff --git a/module/Core/src/ShortUrl/DeleteShortUrlServiceInterface.php b/module/Core/src/ShortUrl/DeleteShortUrlServiceInterface.php index 0a7420f13..32eaffa12 100644 --- a/module/Core/src/ShortUrl/DeleteShortUrlServiceInterface.php +++ b/module/Core/src/ShortUrl/DeleteShortUrlServiceInterface.php @@ -5,6 +5,7 @@ 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 @@ public function deleteByShortCode( 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; } diff --git a/module/Core/src/ShortUrl/Entity/ShortUrl.php b/module/Core/src/ShortUrl/Entity/ShortUrl.php index 8a5772056..084acabda 100644 --- a/module/Core/src/ShortUrl/Entity/ShortUrl.php +++ b/module/Core/src/ShortUrl/Entity/ShortUrl.php @@ -19,11 +19,14 @@ 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 @@ class ShortUrl extends AbstractEntity { - private string $longUrl; - private string $shortCode; - private Chronos $dateCreated; - /** @var Collection & Selectable */ - private Collection & Selectable $visits; - /** @var Collection */ - 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 $tags + * @param Collection & Selectable $visits + * @param Collection & 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 @@ public static function create( ShortUrlCreation $creation, ?ShortUrlRelationResolverInterface $relationResolver = null, ): self { - $instance = new self(); $relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver(); - - $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( - '%s%s', - $creation->pathPrefix ?? '', - $creation->customSlug ?? generateRandomShortCode($instance->shortCodeLength, $creation->shortUrlMode), + $shortCodeLength = $creation->shortCodeLength; + + return new self( + longUrl: $creation->getLongUrl(), + shortCode: sprintf( + '%s%s', + $creation->pathPrefix ?? '', + $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 @@ public static function fromImport( $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 @@ public function getShortCode(): string return $this->shortCode; } - public function getDateCreated(): Chronos - { - return $this->dateCreated; - } - public function getDomain(): ?Domain { return $this->domain; } - /** - * @return Collection - */ - public function getTags(): Collection - { - return $this->tags; - } - - public function authorApiKey(): ?ApiKey - { - return $this->authorApiKey; - } - - public function getValidSince(): ?Chronos - { - return $this->validSince; - } - - public function getValidUntil(): ?Chronos + public function forwardQuery(): bool { - return $this->validUntil; + return $this->forwardQuery; } - public function getVisitsCount(): int + public function title(): ?string { - return count($this->visits); + return $this->title; } - public function nonBotVisitsCount(): int + public function reachedVisits(int $visitsAmount): bool { - $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 @@ public function mostRecentImportedVisitDate(): ?Chronos ->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 @@ public function setVisits(Collection & Selectable $visits): self 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 @@ public function regenerateShortCode(ShortUrlMode $mode): void 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 @@ public function isEnabled(): bool 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)), + )), + ), + ]; + } } diff --git a/module/Core/src/ShortUrl/Model/ExpiredShortUrlsConditions.php b/module/Core/src/ShortUrl/Model/ExpiredShortUrlsConditions.php new file mode 100644 index 000000000..d4f0c0639 --- /dev/null +++ b/module/Core/src/ShortUrl/Model/ExpiredShortUrlsConditions.php @@ -0,0 +1,17 @@ +pastValidUntil || $this->maxVisitsReached; + } +} diff --git a/module/Core/src/ShortUrl/Model/OrderableField.php b/module/Core/src/ShortUrl/Model/OrderableField.php index 685f6f12a..a40537424 100644 --- a/module/Core/src/ShortUrl/Model/OrderableField.php +++ b/module/Core/src/ShortUrl/Model/OrderableField.php @@ -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; - } } diff --git a/module/Core/src/ShortUrl/Model/ShortUrlCreation.php b/module/Core/src/ShortUrl/Model/ShortUrlCreation.php index b0c87f99a..f53360754 100644 --- a/module/Core/src/ShortUrl/Model/ShortUrlCreation.php +++ b/module/Core/src/ShortUrl/Model/ShortUrlCreation.php @@ -68,7 +68,7 @@ public static function fromRawData(array $data, UrlShortenerOptions $options = n 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, diff --git a/module/Core/src/ShortUrl/Model/ShortUrlEdition.php b/module/Core/src/ShortUrl/Model/ShortUrlEdition.php index 36a99f5f6..6296f84d6 100644 --- a/module/Core/src/ShortUrl/Model/ShortUrlEdition.php +++ b/module/Core/src/ShortUrl/Model/ShortUrlEdition.php @@ -60,7 +60,7 @@ public static function fromRawData(array $data): self 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), diff --git a/module/Core/src/ShortUrl/Model/ShortUrlWithVisitsSummary.php b/module/Core/src/ShortUrl/Model/ShortUrlWithVisitsSummary.php new file mode 100644 index 000000000..50efaaee6 --- /dev/null +++ b/module/Core/src/ShortUrl/Model/ShortUrlWithVisitsSummary.php @@ -0,0 +1,36 @@ +shortUrl->toArray($this->visitsSummary); + } +} diff --git a/module/Core/src/ShortUrl/Paginator/Adapter/ShortUrlRepositoryAdapter.php b/module/Core/src/ShortUrl/Paginator/Adapter/ShortUrlRepositoryAdapter.php index 83ce8bd9f..56f8e5a50 100644 --- a/module/Core/src/ShortUrl/Paginator/Adapter/ShortUrlRepositoryAdapter.php +++ b/module/Core/src/ShortUrl/Paginator/Adapter/ShortUrlRepositoryAdapter.php @@ -11,13 +11,13 @@ 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, ) { } diff --git a/module/Core/src/ShortUrl/Persistence/ShortUrlsListFiltering.php b/module/Core/src/ShortUrl/Persistence/ShortUrlsListFiltering.php index db8b9a705..589947dd1 100644 --- a/module/Core/src/ShortUrl/Persistence/ShortUrlsListFiltering.php +++ b/module/Core/src/ShortUrl/Persistence/ShortUrlsListFiltering.php @@ -13,9 +13,9 @@ 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, diff --git a/module/Core/src/ShortUrl/Repository/ExpiredShortUrlsRepository.php b/module/Core/src/ShortUrl/Repository/ExpiredShortUrlsRepository.php new file mode 100644 index 000000000..0b796971e --- /dev/null +++ b/module/Core/src/ShortUrl/Repository/ExpiredShortUrlsRepository.php @@ -0,0 +1,77 @@ +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(); + } +} diff --git a/module/Core/src/ShortUrl/Repository/ExpiredShortUrlsRepositoryInterface.php b/module/Core/src/ShortUrl/Repository/ExpiredShortUrlsRepositoryInterface.php new file mode 100644 index 000000000..96032065f --- /dev/null +++ b/module/Core/src/ShortUrl/Repository/ExpiredShortUrlsRepositoryInterface.php @@ -0,0 +1,20 @@ +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; - } - - $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); - } + $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], + }; + + $qb->orderBy($sort, $order); } public function countList(ShortUrlsCountFiltering $filtering): int @@ -81,7 +88,7 @@ private function createListQueryBuilder(ShortUrlsCountFiltering $filtering): Que { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->from(ShortUrl::class, 's') - ->where('1=1'); + ->where('1=1'); $dateRange = $filtering->dateRange; if ($dateRange?->startDate !== null) { @@ -139,7 +146,10 @@ private function createListQueryBuilder(ShortUrlsCountFiltering $filtering): Que $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, + ), ), )); } diff --git a/module/Core/src/ShortUrl/Repository/ShortUrlListRepositoryInterface.php b/module/Core/src/ShortUrl/Repository/ShortUrlListRepositoryInterface.php index 130e0db73..db3f8017d 100644 --- a/module/Core/src/ShortUrl/Repository/ShortUrlListRepositoryInterface.php +++ b/module/Core/src/ShortUrl/Repository/ShortUrlListRepositoryInterface.php @@ -4,14 +4,14 @@ 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; diff --git a/module/Core/src/ShortUrl/ShortUrlListService.php b/module/Core/src/ShortUrl/ShortUrlListService.php index 60f56554e..d86c4988f 100644 --- a/module/Core/src/ShortUrl/ShortUrlListService.php +++ b/module/Core/src/ShortUrl/ShortUrlListService.php @@ -6,22 +6,22 @@ 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 { diff --git a/module/Core/src/ShortUrl/ShortUrlListServiceInterface.php b/module/Core/src/ShortUrl/ShortUrlListServiceInterface.php index ef7b31c2c..ffbb1374f 100644 --- a/module/Core/src/ShortUrl/ShortUrlListServiceInterface.php +++ b/module/Core/src/ShortUrl/ShortUrlListServiceInterface.php @@ -5,14 +5,14 @@ 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; } diff --git a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php index ea694c61c..413f5a690 100644 --- a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php +++ b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php @@ -7,50 +7,26 @@ 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(), ]; } } diff --git a/module/Core/src/Tag/Model/TagsParams.php b/module/Core/src/Tag/Model/TagsParams.php index 422f9da16..d094bcc0f 100644 --- a/module/Core/src/Tag/Model/TagsParams.php +++ b/module/Core/src/Tag/Model/TagsParams.php @@ -24,7 +24,7 @@ public static function fromRawData(array $query): self { 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, ); diff --git a/module/Core/src/Tag/Repository/TagRepository.php b/module/Core/src/Tag/Repository/TagRepository.php index 0f113776c..c63d461d8 100644 --- a/module/Core/src/Tag/Repository/TagRepository.php +++ b/module/Core/src/Tag/Repository/TagRepository.php @@ -76,19 +76,19 @@ public function findTagsWithInfo(?TagsListFiltering $filtering = null): array $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'); diff --git a/module/Core/src/Util/RedirectStatus.php b/module/Core/src/Util/RedirectStatus.php index f561e2125..defea11b0 100644 --- a/module/Core/src/Util/RedirectStatus.php +++ b/module/Core/src/Util/RedirectStatus.php @@ -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 @@ public function allowsCache(): bool return contains($this, [self::STATUS_301, self::STATUS_308]); } - public function isLegacyStatus(): bool + /** + * @return array|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; } } diff --git a/module/Core/src/Visit/Entity/OrphanVisitsCount.php b/module/Core/src/Visit/Entity/OrphanVisitsCount.php new file mode 100644 index 000000000..8199b1744 --- /dev/null +++ b/module/Core/src/Visit/Entity/OrphanVisitsCount.php @@ -0,0 +1,17 @@ +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,19 +80,40 @@ private function processAddress(bool $anonymize, ?string $address): ?string } } - public function getRemoteAddr(): ?string + public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self { - return $this->remoteAddr; + return self::fromImportOrOrphanImport($importedVisit, VisitType::IMPORTED, $shortUrl); } - public function hasRemoteAddr(): bool + public static function fromOrphanImport(ImportedShlinkOrphanVisit $importedVisit): self { - return ! empty($this->remoteAddr); + return self::fromImportOrOrphanImport( + $importedVisit, + VisitType::tryFrom($importedVisit->type) ?? VisitType::IMPORTED, + ); } - public function getShortUrl(): ?ShortUrl + 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 { - return $this->shortUrl; + return ! empty($this->remoteAddr); } public function getVisitLocation(): ?VisitLocation @@ -161,51 +137,32 @@ public function isOrphan(): bool 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, ]; } } diff --git a/module/Core/src/Visit/Entity/VisitLocation.php b/module/Core/src/Visit/Entity/VisitLocation.php index 2b3b854e3..1f1db6861 100644 --- a/module/Core/src/Visit/Entity/VisitLocation.php +++ b/module/Core/src/Visit/Entity/VisitLocation.php @@ -11,89 +11,54 @@ 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; - - private function __construct() - { + public readonly bool $isEmpty; + + 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 [ diff --git a/module/Core/src/Visit/Geolocation/VisitLocator.php b/module/Core/src/Visit/Geolocation/VisitLocator.php index 4b3b8e22b..63cb61372 100644 --- a/module/Core/src/Visit/Geolocation/VisitLocator.php +++ b/module/Core/src/Visit/Geolocation/VisitLocator.php @@ -8,14 +8,14 @@ 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, ) { } diff --git a/module/Core/src/Visit/Geolocation/VisitToLocationHelper.php b/module/Core/src/Visit/Geolocation/VisitToLocationHelper.php index 2a261019d..9d614a7be 100644 --- a/module/Core/src/Visit/Geolocation/VisitToLocationHelper.php +++ b/module/Core/src/Visit/Geolocation/VisitToLocationHelper.php @@ -26,7 +26,7 @@ public function resolveVisitLocation(Visit $visit): Location throw IpCannotBeLocatedException::forEmptyAddress(); } - $ipAddr = $visit->getRemoteAddr() ?? ''; + $ipAddr = $visit->remoteAddr ?? ''; if ($ipAddr === IpAddress::LOCALHOST) { throw IpCannotBeLocatedException::forLocalhost(); } diff --git a/module/Core/src/Visit/Listener/OrphanVisitsCountTracker.php b/module/Core/src/Visit/Listener/OrphanVisitsCountTracker.php new file mode 100644 index 000000000..157d587e0 --- /dev/null +++ b/module/Core/src/Visit/Listener/OrphanVisitsCountTracker.php @@ -0,0 +1,145 @@ +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, <<incrementWithPreparedStatement($conn, $potentialBot, <<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(); + } +} diff --git a/module/Core/src/Visit/Listener/ShortUrlVisitsCountTracker.php b/module/Core/src/Visit/Listener/ShortUrlVisitsCountTracker.php new file mode 100644 index 000000000..f62ddb3df --- /dev/null +++ b/module/Core/src/Visit/Listener/ShortUrlVisitsCountTracker.php @@ -0,0 +1,160 @@ +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, <<incrementWithPreparedStatement($conn, $shortUrlId, $potentialBot, <<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(); + } +} diff --git a/module/Core/src/Visit/Repository/OrphanVisitsCountRepository.php b/module/Core/src/Visit/Repository/OrphanVisitsCountRepository.php new file mode 100644 index 000000000..d5e7670cf --- /dev/null +++ b/module/Core/src/Visit/Repository/OrphanVisitsCountRepository.php @@ -0,0 +1,31 @@ +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(); + } +} diff --git a/module/Core/src/Visit/Repository/OrphanVisitsCountRepositoryInterface.php b/module/Core/src/Visit/Repository/OrphanVisitsCountRepositoryInterface.php new file mode 100644 index 000000000..adbbe0764 --- /dev/null +++ b/module/Core/src/Visit/Repository/OrphanVisitsCountRepositoryInterface.php @@ -0,0 +1,12 @@ +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(); + } +} diff --git a/module/Core/src/Visit/Repository/ShortUrlVisitsCountRepositoryInterface.php b/module/Core/src/Visit/Repository/ShortUrlVisitsCountRepositoryInterface.php new file mode 100644 index 000000000..fd966039f --- /dev/null +++ b/module/Core/src/Visit/Repository/ShortUrlVisitsCountRepositoryInterface.php @@ -0,0 +1,12 @@ + @@ -42,9 +47,18 @@ public function findVisitsWithEmptyLocation(int $blockSize = self::DEFAULT_BLOCK /** * @return iterable */ - 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); } diff --git a/module/Core/src/Visit/Repository/VisitLocationRepositoryInterface.php b/module/Core/src/Visit/Repository/VisitIterationRepositoryInterface.php similarity index 71% rename from module/Core/src/Visit/Repository/VisitLocationRepositoryInterface.php rename to module/Core/src/Visit/Repository/VisitIterationRepositoryInterface.php index 083d61f22..d4ffb864a 100644 --- a/module/Core/src/Visit/Repository/VisitLocationRepositoryInterface.php +++ b/module/Core/src/Visit/Repository/VisitIterationRepositoryInterface.php @@ -4,9 +4,10 @@ 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 @@ public function findVisitsWithEmptyLocation(int $blockSize = self::DEFAULT_BLOCK /** * @return iterable */ - public function findAllVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable; + public function findAllVisits(?DateRange $dateRange = null, int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable; } diff --git a/module/Core/src/Visit/Spec/CountOfNonOrphanVisits.php b/module/Core/src/Visit/Spec/CountOfNonOrphanVisits.php index a7b2a1d6c..d81cd21b8 100644 --- a/module/Core/src/Visit/Spec/CountOfNonOrphanVisits.php +++ b/module/Core/src/Visit/Spec/CountOfNonOrphanVisits.php @@ -13,7 +13,7 @@ class CountOfNonOrphanVisits extends BaseSpecification { - public function __construct(private VisitsCountFiltering $filtering) + public function __construct(private readonly VisitsCountFiltering $filtering) { parent::__construct(); } diff --git a/module/Core/src/Visit/Transformer/OrphanVisitDataTransformer.php b/module/Core/src/Visit/Transformer/OrphanVisitDataTransformer.php deleted file mode 100644 index cf1c8bc91..000000000 --- a/module/Core/src/Visit/Transformer/OrphanVisitDataTransformer.php +++ /dev/null @@ -1,23 +0,0 @@ -jsonSerialize(); - $serializedVisit['visitedUrl'] = $visit->visitedUrl(); - $serializedVisit['type'] = $visit->type()->value; - - return $serializedVisit; - } -} diff --git a/module/Core/src/Visit/VisitsStatsHelper.php b/module/Core/src/Visit/VisitsStatsHelper.php index 480435c1d..65d9a1c80 100644 --- a/module/Core/src/Visit/VisitsStatsHelper.php +++ b/module/Core/src/Visit/VisitsStatsHelper.php @@ -17,6 +17,8 @@ use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface; use Shlinkio\Shlink\Core\Tag\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Repository\TagRepository; +use Shlinkio\Shlink\Core\Visit\Entity\OrphanVisitsCount; +use Shlinkio\Shlink\Core\Visit\Entity\ShortUrlVisitsCount; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams; use Shlinkio\Shlink\Core\Visit\Model\VisitsParams; @@ -28,28 +30,33 @@ use Shlinkio\Shlink\Core\Visit\Paginator\Adapter\TagVisitsPaginatorAdapter; use Shlinkio\Shlink\Core\Visit\Persistence\OrphanVisitsCountFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering; -use Shlinkio\Shlink\Core\Visit\Repository\VisitRepository; +use Shlinkio\Shlink\Core\Visit\Repository\OrphanVisitsCountRepository; +use Shlinkio\Shlink\Core\Visit\Repository\ShortUrlVisitsCountRepository; use Shlinkio\Shlink\Core\Visit\Repository\VisitRepositoryInterface; use Shlinkio\Shlink\Rest\Entity\ApiKey; -class VisitsStatsHelper implements VisitsStatsHelperInterface +readonly class VisitsStatsHelper implements VisitsStatsHelperInterface { - public function __construct(private readonly EntityManagerInterface $em) + public function __construct(private EntityManagerInterface $em) { } public function getVisitsStats(?ApiKey $apiKey = null): VisitsStats { - /** @var VisitRepository $visitsRepo */ - $visitsRepo = $this->em->getRepository(Visit::class); + /** @var OrphanVisitsCountRepository $orphanVisitsCountRepo */ + $orphanVisitsCountRepo = $this->em->getRepository(OrphanVisitsCount::class); + /** @var ShortUrlVisitsCountRepository $visitsCountRepo */ + $visitsCountRepo = $this->em->getRepository(ShortUrlVisitsCount::class); return new VisitsStats( - nonOrphanVisitsTotal: $visitsRepo->countNonOrphanVisits(new VisitsCountFiltering(apiKey: $apiKey)), - orphanVisitsTotal: $visitsRepo->countOrphanVisits(new OrphanVisitsCountFiltering(apiKey: $apiKey)), - nonOrphanVisitsNonBots: $visitsRepo->countNonOrphanVisits( + nonOrphanVisitsTotal: $visitsCountRepo->countNonOrphanVisits(new VisitsCountFiltering(apiKey: $apiKey)), + orphanVisitsTotal: $orphanVisitsCountRepo->countOrphanVisits( + new OrphanVisitsCountFiltering(apiKey: $apiKey), + ), + nonOrphanVisitsNonBots: $visitsCountRepo->countNonOrphanVisits( new VisitsCountFiltering(excludeBots: true, apiKey: $apiKey), ), - orphanVisitsNonBots: $visitsRepo->countOrphanVisits( + orphanVisitsNonBots: $orphanVisitsCountRepo->countOrphanVisits( new OrphanVisitsCountFiltering(excludeBots: true, apiKey: $apiKey), ), ); diff --git a/module/Core/src/Visit/VisitsTracker.php b/module/Core/src/Visit/VisitsTracker.php index 9e4b88dfd..f2e26493d 100644 --- a/module/Core/src/Visit/VisitsTracker.php +++ b/module/Core/src/Visit/VisitsTracker.php @@ -12,12 +12,12 @@ use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\Visitor; -class VisitsTracker implements VisitsTrackerInterface +readonly class VisitsTracker implements VisitsTrackerInterface { public function __construct( - private readonly ORM\EntityManagerInterface $em, - private readonly EventDispatcherInterface $eventDispatcher, - private readonly TrackingOptions $options, + private ORM\EntityManagerInterface $em, + private EventDispatcherInterface $eventDispatcher, + private TrackingOptions $options, ) { } @@ -72,8 +72,13 @@ private function trackVisit(callable $createVisit, Visitor $visitor): void } $visit = $createVisit($visitor->normalizeForTrackingOptions($this->options)); - $this->em->persist($visit); - $this->em->flush(); + + // Wrap persisting and flushing the visit in a transaction, so that the ShortUrlVisitsCountTracker performs + // changes inside that very same transaction atomically + $this->em->wrapInTransaction(function () use ($visit): void { + $this->em->persist($visit); + $this->em->flush(); + }); $this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress)); } diff --git a/module/Core/test-db/ShortUrl/Repository/DeleteExpiredShortUrlsRepositoryTest.php b/module/Core/test-db/ShortUrl/Repository/DeleteExpiredShortUrlsRepositoryTest.php new file mode 100644 index 000000000..d90ad2569 --- /dev/null +++ b/module/Core/test-db/ShortUrl/Repository/DeleteExpiredShortUrlsRepositoryTest.php @@ -0,0 +1,100 @@ +getEntityManager(); + $this->repository = new ExpiredShortUrlsRepository($em, $em->getClassMetadata(ShortUrl::class)); + } + + #[Test] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: false, maxVisitsReached: false), 0])] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: true, maxVisitsReached: false), 7])] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: false, maxVisitsReached: true), 6])] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: true, maxVisitsReached: true), 9])] + public function deletesExpectedAmountOfShortUrls( + ExpiredShortUrlsConditions $conditions, + int $expectedDeletedShortUrls, + ): void { + $createdShortUrls = $this->createDataSet(); + + self::assertEquals($expectedDeletedShortUrls, $this->repository->delete($conditions)); + self::assertEquals( + $createdShortUrls - $expectedDeletedShortUrls, + $this->getEntityManager()->getRepository(ShortUrl::class)->count(), + ); + } + + #[Test] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: false, maxVisitsReached: false), 0])] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: true, maxVisitsReached: false), 7])] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: false, maxVisitsReached: true), 6])] + #[TestWith([new ExpiredShortUrlsConditions(pastValidUntil: true, maxVisitsReached: true), 9])] + public function countsExpectedAmountOfShortUrls( + ExpiredShortUrlsConditions $conditions, + int $expectedShortUrlsCount, + ): void { + $createdShortUrls = $this->createDataSet(); + + self::assertEquals($expectedShortUrlsCount, $this->repository->dryCount($conditions)); + self::assertEquals($createdShortUrls, $this->getEntityManager()->getRepository(ShortUrl::class)->count()); + } + + private function createDataSet(): int + { + // Create some non-expired short URLs + $this->createShortUrls(5); + $this->createShortUrls(2, [ShortUrlInputFilter::VALID_UNTIL => Chronos::now()->addDays(1)->toAtomString()]); + $this->createShortUrls(3, [ShortUrlInputFilter::MAX_VISITS => 4], visitsPerShortUrl: 2); + + // Create some short URLs with a valid date in the past + $this->createShortUrls(3, [ShortUrlInputFilter::VALID_UNTIL => Chronos::now()->subDays(1)->toAtomString()]); + + // Create some short URLs which reached the max amount of visits + $this->createShortUrls(2, [ShortUrlInputFilter::MAX_VISITS => 3], visitsPerShortUrl: 3); + + // Create some short URLs with a valid date in the past which also reached the max amount of visits + $this->createShortUrls(4, [ + ShortUrlInputFilter::VALID_UNTIL => Chronos::now()->subDays(1)->toAtomString(), + ShortUrlInputFilter::MAX_VISITS => 3, + ], visitsPerShortUrl: 4); + + $this->getEntityManager()->flush(); + + return 5 + 2 + 3 + 3 + 2 + 4; + } + + private function createShortUrls(int $amountOfShortUrls, array $metadata = [], int $visitsPerShortUrl = 0): void + { + for ($i = 0; $i < $amountOfShortUrls; $i++) { + $shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([ + ShortUrlInputFilter::LONG_URL => 'https://shlink.io', + ...$metadata, + ])); + $this->getEntityManager()->persist($shortUrl); + + for ($j = 0; $j < $visitsPerShortUrl; $j++) { + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance())); + } + } + } +} diff --git a/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php b/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php index b359e35d5..959249568 100644 --- a/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php +++ b/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php @@ -13,6 +13,7 @@ use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Model\OrderableField; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation; +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; @@ -25,6 +26,7 @@ use function array_map; use function count; use function range; +use function Shlinkio\Shlink\Core\ArrayUtils\map; class ShortUrlListRepositoryTest extends DatabaseTestCase { @@ -85,62 +87,54 @@ public function findListProperlyFiltersResult(): void $this->getEntityManager()->flush(); - $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::emptyInstance(), 'foo', ['bar']), - ); + $result = $this->repo->findList(new ShortUrlsListFiltering(searchTerm: 'foo', tags: ['bar'])); self::assertCount(1, $result); self::assertEquals(1, $this->repo->countList(new ShortUrlsCountFiltering('foo', ['bar']))); - self::assertSame($foo, $result[0]); + self::assertSame($foo, $result[0]->shortUrl); // Assert searched text also applies to tags - $result = $this->repo->findList(new ShortUrlsListFiltering(null, null, Ordering::emptyInstance(), 'bar')); + $result = $this->repo->findList(new ShortUrlsListFiltering(searchTerm: 'bar')); self::assertCount(2, $result); self::assertEquals(2, $this->repo->countList(new ShortUrlsCountFiltering('bar'))); - self::assertContains($foo, $result); + self::assertContains($foo, map($result, fn (ShortUrlWithVisitsSummary $s) => $s->shortUrl)); - $result = $this->repo->findList(new ShortUrlsListFiltering(null, null, Ordering::emptyInstance())); + $result = $this->repo->findList(new ShortUrlsListFiltering()); self::assertCount(3, $result); - $result = $this->repo->findList(new ShortUrlsListFiltering(2, null, Ordering::emptyInstance())); + $result = $this->repo->findList(new ShortUrlsListFiltering(limit: 2)); self::assertCount(2, $result); - $result = $this->repo->findList(new ShortUrlsListFiltering(2, 1, Ordering::emptyInstance())); + $result = $this->repo->findList(new ShortUrlsListFiltering(limit: 2, offset: 1)); self::assertCount(2, $result); - self::assertCount(1, $this->repo->findList(new ShortUrlsListFiltering(2, 2, Ordering::emptyInstance()))); + self::assertCount(1, $this->repo->findList(new ShortUrlsListFiltering(limit: 2, offset: 2))); - $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::fromTuple([OrderableField::VISITS->value, 'DESC'])), - ); + $result = $this->repo->findList(new ShortUrlsListFiltering( + orderBy: Ordering::fromFieldDesc(OrderableField::VISITS->value), + )); self::assertCount(3, $result); - self::assertSame($bar, $result[0]); + self::assertSame($bar, $result[0]->shortUrl); - $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::fromTuple( - [OrderableField::NON_BOT_VISITS->value, 'DESC'], - )), - ); + $result = $this->repo->findList(new ShortUrlsListFiltering( + orderBy: Ordering::fromFieldDesc(OrderableField::NON_BOT_VISITS->value), + )); self::assertCount(3, $result); - self::assertSame($foo2, $result[0]); + self::assertSame($foo2, $result[0]->shortUrl); - $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::emptyInstance(), null, [], null, DateRange::until( - Chronos::now()->subDays(2), - )), - ); - self::assertCount(1, $result); - self::assertEquals(1, $this->repo->countList(new ShortUrlsCountFiltering(null, [], null, DateRange::until( - Chronos::now()->subDays(2), - )))); - self::assertSame($foo2, $result[0]); - - self::assertCount(2, $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::emptyInstance(), null, [], null, DateRange::since( - Chronos::now()->subDays(2), - )), + $result = $this->repo->findList(new ShortUrlsListFiltering( + dateRange: DateRange::until(Chronos::now()->subDays(2)), )); + self::assertCount(1, $result); + self::assertEquals(1, $this->repo->countList(new ShortUrlsCountFiltering( + dateRange: DateRange::until(Chronos::now()->subDays(2)), + ))); + self::assertSame($foo2, $result[0]->shortUrl); + + self::assertCount(2, $this->repo->findList(new ShortUrlsListFiltering( + dateRange: DateRange::since(Chronos::now()->subDays(2)), + ))); self::assertEquals(2, $this->repo->countList( - new ShortUrlsCountFiltering(null, [], null, DateRange::since(Chronos::now()->subDays(2))), + new ShortUrlsCountFiltering(dateRange: DateRange::since(Chronos::now()->subDays(2))), )); } @@ -154,15 +148,13 @@ public function findListProperlyMapsFieldNamesToColumnNamesWhenOrdering(): void $this->getEntityManager()->flush(); - $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::fromTuple(['longUrl', 'ASC'])), - ); + $result = $this->repo->findList(new ShortUrlsListFiltering(orderBy: Ordering::fromFieldAsc('longUrl'))); self::assertCount(count($urls), $result); - self::assertEquals('https://a', $result[0]->getLongUrl()); - self::assertEquals('https://b', $result[1]->getLongUrl()); - self::assertEquals('https://c', $result[2]->getLongUrl()); - self::assertEquals('https://z', $result[3]->getLongUrl()); + self::assertEquals('https://a', $result[0]->shortUrl->getLongUrl()); + self::assertEquals('https://b', $result[1]->shortUrl->getLongUrl()); + self::assertEquals('https://c', $result[2]->shortUrl->getLongUrl()); + self::assertEquals('https://z', $result[3]->shortUrl->getLongUrl()); } #[Test] @@ -196,81 +188,55 @@ public function findListReturnsOnlyThoseWithMatchingTags(): void $this->getEntityManager()->flush(); - self::assertCount(5, $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::emptyInstance(), null, ['foo', 'bar']), - )); + self::assertCount(5, $this->repo->findList(new ShortUrlsListFiltering(tags: ['foo', 'bar']))); self::assertCount(5, $this->repo->findList(new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), - null, - ['foo', 'bar'], - TagsMode::ANY, + tags: ['foo', 'bar'], + tagsMode: TagsMode::ANY, ))); self::assertCount(1, $this->repo->findList(new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), - null, - ['foo', 'bar'], - TagsMode::ALL, + tags: ['foo', 'bar'], + tagsMode: TagsMode::ALL, ))); - self::assertEquals(5, $this->repo->countList(new ShortUrlsCountFiltering(null, ['foo', 'bar']))); - self::assertEquals(5, $this->repo->countList(new ShortUrlsCountFiltering(null, ['foo', 'bar'], TagsMode::ANY))); - self::assertEquals(1, $this->repo->countList(new ShortUrlsCountFiltering(null, ['foo', 'bar'], TagsMode::ALL))); - - self::assertCount(4, $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::emptyInstance(), null, ['bar', 'baz']), + self::assertEquals(5, $this->repo->countList(new ShortUrlsCountFiltering(tags: ['foo', 'bar']))); + self::assertEquals(5, $this->repo->countList( + new ShortUrlsCountFiltering(tags: ['foo', 'bar'], tagsMode: TagsMode::ANY), + )); + self::assertEquals(1, $this->repo->countList( + new ShortUrlsCountFiltering(tags: ['foo', 'bar'], tagsMode: TagsMode::ALL), )); + + self::assertCount(4, $this->repo->findList(new ShortUrlsListFiltering(tags: ['bar', 'baz']))); self::assertCount(4, $this->repo->findList(new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), - null, - ['bar', 'baz'], - TagsMode::ANY, + tags: ['bar', 'baz'], + tagsMode: TagsMode::ANY, ))); self::assertCount(2, $this->repo->findList(new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), - null, - ['bar', 'baz'], - TagsMode::ALL, + tags: ['bar', 'baz'], + tagsMode: TagsMode::ALL, ))); - self::assertEquals(4, $this->repo->countList(new ShortUrlsCountFiltering(null, ['bar', 'baz']))); + self::assertEquals(4, $this->repo->countList(new ShortUrlsCountFiltering(tags: ['bar', 'baz']))); self::assertEquals(4, $this->repo->countList( - new ShortUrlsCountFiltering(null, ['bar', 'baz'], TagsMode::ANY), + new ShortUrlsCountFiltering(tags: ['bar', 'baz'], tagsMode: TagsMode::ANY), )); self::assertEquals(2, $this->repo->countList( - new ShortUrlsCountFiltering(null, ['bar', 'baz'], TagsMode::ALL), + new ShortUrlsCountFiltering(tags: ['bar', 'baz'], tagsMode: TagsMode::ALL), )); - self::assertCount(5, $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::emptyInstance(), null, ['foo', 'bar', 'baz']), - )); + self::assertCount(5, $this->repo->findList(new ShortUrlsListFiltering(tags: ['foo', 'bar', 'baz']))); self::assertCount(5, $this->repo->findList(new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), - null, - ['foo', 'bar', 'baz'], - TagsMode::ANY, + tags: ['foo', 'bar', 'baz'], + tagsMode: TagsMode::ANY, ))); self::assertCount(0, $this->repo->findList(new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), - null, - ['foo', 'bar', 'baz'], - TagsMode::ALL, + tags: ['foo', 'bar', 'baz'], + tagsMode: TagsMode::ALL, ))); - self::assertEquals(5, $this->repo->countList(new ShortUrlsCountFiltering(null, ['foo', 'bar', 'baz']))); + self::assertEquals(5, $this->repo->countList(new ShortUrlsCountFiltering(tags: ['foo', 'bar', 'baz']))); self::assertEquals(5, $this->repo->countList( - new ShortUrlsCountFiltering(null, ['foo', 'bar', 'baz'], TagsMode::ANY), + new ShortUrlsCountFiltering(tags: ['foo', 'bar', 'baz'], tagsMode: TagsMode::ANY), )); self::assertEquals(0, $this->repo->countList( - new ShortUrlsCountFiltering(null, ['foo', 'bar', 'baz'], TagsMode::ALL), + new ShortUrlsCountFiltering(tags: ['foo', 'bar', 'baz'], tagsMode: TagsMode::ALL), )); } @@ -296,9 +262,6 @@ public function findListReturnsOnlyThoseWithMatchingDomains(): void $this->getEntityManager()->flush(); $buildFiltering = static fn (string $searchTerm) => new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), searchTerm: $searchTerm, defaultDomain: 'deFaulT-domain.com', ); @@ -341,9 +304,6 @@ public function findListReturnsOnlyThoseWithoutExcludedUrls(): void $filtering = static fn (bool $excludeMaxVisitsReached, bool $excludePastValidUntil) => new ShortUrlsListFiltering( - null, - null, - Ordering::emptyInstance(), excludeMaxVisitsReached: $excludeMaxVisitsReached, excludePastValidUntil: $excludePastValidUntil, ); diff --git a/module/Core/test-db/Tag/Repository/TagRepositoryTest.php b/module/Core/test-db/Tag/Repository/TagRepositoryTest.php index 770771421..77f6aa6aa 100644 --- a/module/Core/test-db/Tag/Repository/TagRepositoryTest.php +++ b/module/Core/test-db/Tag/Repository/TagRepositoryTest.php @@ -135,11 +135,11 @@ public static function provideFilters(): iterable ['baz', 1, 3, 2], ]]; yield 'ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple([OrderableField::TAG->value, 'ASC'])), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::TAG->value)), $defaultList, ]; - yield 'DESC ordering' => [new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::TAG->value, 'DESC'], + yield 'DESC ordering' => [new TagsListFiltering(null, null, null, Ordering::fromFieldDesc( + OrderableField::TAG->value, )), [ ['foo', 2, 4, 3], ['baz', 1, 3, 2], @@ -147,9 +147,7 @@ public static function provideFilters(): iterable ['another', 0, 0, 0], ]]; yield 'short URLs count ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::SHORT_URLS_COUNT->value, 'ASC'], - )), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::SHORT_URLS_COUNT->value)), [ ['another', 0, 0, 0], ['baz', 1, 3, 2], @@ -158,9 +156,7 @@ public static function provideFilters(): iterable ], ]; yield 'short URLs count DESC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::SHORT_URLS_COUNT->value, 'DESC'], - )), + new TagsListFiltering(null, null, null, Ordering::fromFieldDesc(OrderableField::SHORT_URLS_COUNT->value)), [ ['bar', 3, 3, 2], ['foo', 2, 4, 3], @@ -169,7 +165,7 @@ public static function provideFilters(): iterable ], ]; yield 'visits count ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple([OrderableField::VISITS->value, 'ASC'])), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::VISITS->value)), [ ['another', 0, 0, 0], ['bar', 3, 3, 2], @@ -178,9 +174,7 @@ public static function provideFilters(): iterable ], ]; yield 'non-bot visits count ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::NON_BOT_VISITS->value, 'ASC'], - )), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::NON_BOT_VISITS->value)), [ ['another', 0, 0, 0], ['bar', 3, 3, 2], @@ -189,7 +183,7 @@ public static function provideFilters(): iterable ], ]; yield 'visits count DESC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple([OrderableField::VISITS->value, 'DESC'])), + new TagsListFiltering(null, null, null, Ordering::fromFieldDesc(OrderableField::VISITS->value)), [ ['foo', 2, 4, 3], ['bar', 3, 3, 2], @@ -204,8 +198,8 @@ public static function provideFilters(): iterable ['baz', 1, 3, 2], ['foo', 1, 3, 2], ]]; - yield 'combined' => [new TagsListFiltering(1, null, null, Ordering::fromTuple( - [OrderableField::SHORT_URLS_COUNT->value, 'DESC'], + yield 'combined' => [new TagsListFiltering(1, null, null, Ordering::fromFieldDesc( + OrderableField::SHORT_URLS_COUNT->value, ), ApiKey::fromMeta( ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()), )), [ diff --git a/module/Core/test-db/Visit/Listener/OrphanVisitsCountTrackerTest.php b/module/Core/test-db/Visit/Listener/OrphanVisitsCountTrackerTest.php new file mode 100644 index 000000000..20302c755 --- /dev/null +++ b/module/Core/test-db/Visit/Listener/OrphanVisitsCountTrackerTest.php @@ -0,0 +1,69 @@ +repo = $this->getEntityManager()->getRepository(OrphanVisitsCount::class); + } + + #[Test] + public function createsNewEntriesWhenNoneExist(): void + { + $visit = Visit::forBasePath(Visitor::emptyInstance()); + $this->getEntityManager()->persist($visit); + $this->getEntityManager()->flush(); + + /** @var OrphanVisitsCount[] $result */ + $result = $this->repo->findAll(); + + self::assertCount(1, $result); + self::assertEquals('1', $result[0]->count); + self::assertGreaterThanOrEqual(0, $result[0]->slotId); + self::assertLessThanOrEqual(100, $result[0]->slotId); + } + + #[Test] + public function editsExistingEntriesWhenAlreadyExist(): void + { + for ($i = 0; $i <= 100; $i++) { + $this->getEntityManager()->persist(new OrphanVisitsCount(slotId: $i)); + } + $this->getEntityManager()->flush(); + + $visit = Visit::forRegularNotFound(Visitor::emptyInstance()); + $this->getEntityManager()->persist($visit); + $this->getEntityManager()->flush(); + + // Clear entity manager to force it to get fresh data from the database + // This is needed because the tracker inserts natively, bypassing the entity manager + $this->getEntityManager()->clear(); + + /** @var OrphanVisitsCount[] $result */ + $result = $this->repo->findAll(); + $itemsWithCountBiggerThanOnce = array_values(array_filter( + $result, + static fn (OrphanVisitsCount $item) => ((int) $item->count) > 1, + )); + + self::assertCount(101, $result); + self::assertCount(1, $itemsWithCountBiggerThanOnce); + self::assertEquals('2', $itemsWithCountBiggerThanOnce[0]->count); + } +} diff --git a/module/Core/test-db/Visit/Listener/ShortUrlVisitsCountTrackerTest.php b/module/Core/test-db/Visit/Listener/ShortUrlVisitsCountTrackerTest.php new file mode 100644 index 000000000..29f5d5d1f --- /dev/null +++ b/module/Core/test-db/Visit/Listener/ShortUrlVisitsCountTrackerTest.php @@ -0,0 +1,76 @@ +repo = $this->getEntityManager()->getRepository(ShortUrlVisitsCount::class); + } + + #[Test] + public function createsNewEntriesWhenNoneExist(): void + { + $shortUrl = ShortUrl::createFake(); + $this->getEntityManager()->persist($shortUrl); + + $visit = Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance()); + $this->getEntityManager()->persist($visit); + $this->getEntityManager()->flush(); + + /** @var ShortUrlVisitsCount[] $result */ + $result = $this->repo->findBy(['shortUrl' => $shortUrl]); + + self::assertCount(1, $result); + self::assertEquals('1', $result[0]->count); + self::assertGreaterThanOrEqual(0, $result[0]->slotId); + self::assertLessThanOrEqual(100, $result[0]->slotId); + } + + #[Test] + public function editsExistingEntriesWhenAlreadyExist(): void + { + $shortUrl = ShortUrl::createFake(); + $this->getEntityManager()->persist($shortUrl); + + for ($i = 0; $i <= 100; $i++) { + $this->getEntityManager()->persist(new ShortUrlVisitsCount($shortUrl, slotId: $i)); + } + $this->getEntityManager()->flush(); + + $visit = Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance()); + $this->getEntityManager()->persist($visit); + $this->getEntityManager()->flush(); + + // Clear entity manager to force it to get fresh data from the database + // This is needed because the tracker inserts natively, bypassing the entity manager + $this->getEntityManager()->clear(); + + /** @var ShortUrlVisitsCount[] $result */ + $result = $this->repo->findBy(['shortUrl' => $shortUrl]); + $itemsWithCountBiggerThanOnce = array_values(array_filter( + $result, + static fn (ShortUrlVisitsCount $item) => ((int) $item->count) > 1, + )); + + self::assertCount(101, $result); + self::assertCount(1, $itemsWithCountBiggerThanOnce); + self::assertEquals('2', $itemsWithCountBiggerThanOnce[0]->count); + } +} diff --git a/module/Core/test-db/Visit/Repository/VisitLocationRepositoryTest.php b/module/Core/test-db/Visit/Repository/VisitIterationRepositoryTest.php similarity index 55% rename from module/Core/test-db/Visit/Repository/VisitLocationRepositoryTest.php rename to module/Core/test-db/Visit/Repository/VisitIterationRepositoryTest.php index c5aadf1f6..6d3d4b394 100644 --- a/module/Core/test-db/Visit/Repository/VisitLocationRepositoryTest.php +++ b/module/Core/test-db/Visit/Repository/VisitIterationRepositoryTest.php @@ -4,27 +4,29 @@ namespace ShlinkioDbTest\Shlink\Core\Visit\Repository; +use Cake\Chronos\Chronos; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation; use Shlinkio\Shlink\Core\Visit\Model\Visitor; -use Shlinkio\Shlink\Core\Visit\Repository\VisitLocationRepository; +use Shlinkio\Shlink\Core\Visit\Repository\VisitIterationRepository; use Shlinkio\Shlink\IpGeolocation\Model\Location; use Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase; use function array_map; use function range; -class VisitLocationRepositoryTest extends DatabaseTestCase +class VisitIterationRepositoryTest extends DatabaseTestCase { - private VisitLocationRepository $repo; + private VisitIterationRepository $repo; protected function setUp(): void { $em = $this->getEntityManager(); - $this->repo = new VisitLocationRepository($em, $em->getClassMetadata(Visit::class)); + $this->repo = new VisitIterationRepository($em, $em->getClassMetadata(Visit::class)); } #[Test, DataProvider('provideBlockSize')] @@ -33,7 +35,9 @@ public function findVisitsReturnsProperVisits(int $blockSize): void $shortUrl = ShortUrl::createFake(); $this->getEntityManager()->persist($shortUrl); + $unmodifiedDate = Chronos::now(); for ($i = 0; $i < 6; $i++) { + Chronos::setTestNow($unmodifiedDate->subDays($i)); // Enforce a different day for every visit $visit = Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance()); if ($i >= 2) { @@ -44,15 +48,34 @@ public function findVisitsReturnsProperVisits(int $blockSize): void $this->getEntityManager()->persist($visit); } + Chronos::setTestNow(); $this->getEntityManager()->flush(); $withEmptyLocation = $this->repo->findVisitsWithEmptyLocation($blockSize); $unlocated = $this->repo->findUnlocatedVisits($blockSize); - $all = $this->repo->findAllVisits($blockSize); + $all = $this->repo->findAllVisits(blockSize: $blockSize); + $lastThreeDays = $this->repo->findAllVisits( + dateRange: DateRange::since(Chronos::now()->subDays(2)->startOfDay()), + blockSize: $blockSize, + ); + $firstTwoDays = $this->repo->findAllVisits( + dateRange: DateRange::until(Chronos::now()->subDays(4)->endOfDay()), + blockSize: $blockSize, + ); + $daysInBetween = $this->repo->findAllVisits( + dateRange: DateRange::between( + startDate: Chronos::now()->subDays(5)->startOfDay(), + endDate: Chronos::now()->subDays(2)->endOfDay(), + ), + blockSize: $blockSize, + ); self::assertCount(2, [...$unlocated]); self::assertCount(4, [...$withEmptyLocation]); self::assertCount(6, [...$all]); + self::assertCount(3, [...$lastThreeDays]); + self::assertCount(2, [...$firstTwoDays]); + self::assertCount(4, [...$daysInBetween]); } public static function provideBlockSize(): iterable diff --git a/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php b/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php index 90496e1e7..9dc18390f 100644 --- a/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php +++ b/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php @@ -6,7 +6,6 @@ use Cake\Chronos\Chronos; use PHPUnit\Framework\Attributes\Test; -use ReflectionObject; use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; @@ -14,6 +13,8 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter; use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver; +use Shlinkio\Shlink\Core\Visit\Entity\OrphanVisitsCount; +use Shlinkio\Shlink\Core\Visit\Entity\ShortUrlVisitsCount; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitType; use Shlinkio\Shlink\Core\Visit\Model\Visitor; @@ -21,6 +22,8 @@ use Shlinkio\Shlink\Core\Visit\Persistence\OrphanVisitsListFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering; +use Shlinkio\Shlink\Core\Visit\Repository\OrphanVisitsCountRepository; +use Shlinkio\Shlink\Core\Visit\Repository\ShortUrlVisitsCountRepository; use Shlinkio\Shlink\Core\Visit\Repository\VisitRepository; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; @@ -36,11 +39,19 @@ class VisitRepositoryTest extends DatabaseTestCase { private VisitRepository $repo; + private ShortUrlVisitsCountRepository $countRepo; + private OrphanVisitsCountRepository $orphanCountRepo; private PersistenceShortUrlRelationResolver $relationResolver; protected function setUp(): void { $this->repo = $this->getEntityManager()->getRepository(Visit::class); + + // Testing the visits count repositories in this very same test, helps checking the fact that results should + // match what VisitRepository returns + $this->countRepo = $this->getEntityManager()->getRepository(ShortUrlVisitsCount::class); + $this->orphanCountRepo = $this->getEntityManager()->getRepository(OrphanVisitsCount::class); + $this->relationResolver = new PersistenceShortUrlRelationResolver($this->getEntityManager()); } @@ -308,12 +319,21 @@ public function countVisitsReturnsExpectedResultBasedOnApiKey(): void $this->getEntityManager()->flush(); self::assertEquals(4 + 5 + 7, $this->repo->countNonOrphanVisits(new VisitsCountFiltering())); + self::assertEquals(4 + 5 + 7, $this->countRepo->countNonOrphanVisits(new VisitsCountFiltering())); self::assertEquals(4, $this->repo->countNonOrphanVisits(new VisitsCountFiltering(apiKey: $apiKey1))); + self::assertEquals(4, $this->countRepo->countNonOrphanVisits(new VisitsCountFiltering(apiKey: $apiKey1))); self::assertEquals(5 + 7, $this->repo->countNonOrphanVisits(new VisitsCountFiltering(apiKey: $apiKey2))); + self::assertEquals(5 + 7, $this->countRepo->countNonOrphanVisits(new VisitsCountFiltering(apiKey: $apiKey2))); self::assertEquals(4 + 7, $this->repo->countNonOrphanVisits(new VisitsCountFiltering(apiKey: $domainApiKey))); + self::assertEquals(4 + 7, $this->countRepo->countNonOrphanVisits(new VisitsCountFiltering( + apiKey: $domainApiKey, + ))); self::assertEquals(0, $this->repo->countOrphanVisits(new OrphanVisitsCountFiltering( apiKey: $noOrphanVisitsApiKey, ))); + self::assertEquals(0, $this->orphanCountRepo->countOrphanVisits(new OrphanVisitsCountFiltering( + apiKey: $noOrphanVisitsApiKey, + ))); self::assertEquals(4, $this->repo->countNonOrphanVisits(new VisitsCountFiltering(DateRange::since( Chronos::parse('2016-01-05')->startOfDay(), )))); @@ -323,9 +343,18 @@ public function countVisitsReturnsExpectedResultBasedOnApiKey(): void self::assertEquals(1, $this->repo->countNonOrphanVisits(new VisitsCountFiltering(DateRange::since( Chronos::parse('2016-01-07')->startOfDay(), ), false, $apiKey2))); - self::assertEquals(3 + 5, $this->repo->countNonOrphanVisits(new VisitsCountFiltering(null, true, $apiKey2))); + self::assertEquals(3 + 5, $this->repo->countNonOrphanVisits( + new VisitsCountFiltering(excludeBots: true, apiKey: $apiKey2), + )); + self::assertEquals(3 + 5, $this->countRepo->countNonOrphanVisits( + new VisitsCountFiltering(excludeBots: true, apiKey: $apiKey2), + )); self::assertEquals(4, $this->repo->countOrphanVisits(new OrphanVisitsCountFiltering())); + self::assertEquals(4, $this->orphanCountRepo->countOrphanVisits(new OrphanVisitsCountFiltering())); self::assertEquals(3, $this->repo->countOrphanVisits(new OrphanVisitsCountFiltering(excludeBots: true))); + self::assertEquals(3, $this->orphanCountRepo->countOrphanVisits( + new OrphanVisitsCountFiltering(excludeBots: true), + )); } #[Test] @@ -341,15 +370,15 @@ public function findOrphanVisitsReturnsExpectedResult(): void $botsCount = 3; for ($i = 0; $i < 6; $i++) { $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forBasePath($botsCount < 1 ? Visitor::emptyInstance() : Visitor::botInstance()), + fn () => Visit::forBasePath($botsCount < 1 ? Visitor::emptyInstance() : Visitor::botInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forInvalidShortUrl(Visitor::emptyInstance()), + fn () => Visit::forInvalidShortUrl(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forRegularNotFound(Visitor::emptyInstance()), + fn () => Visit::forRegularNotFound(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); @@ -399,15 +428,15 @@ public function countOrphanVisitsReturnsExpectedResult(): void for ($i = 0; $i < 6; $i++) { $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forBasePath(Visitor::emptyInstance()), + fn () => Visit::forBasePath(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forInvalidShortUrl(Visitor::emptyInstance()), + fn () => Visit::forInvalidShortUrl(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forRegularNotFound(Visitor::emptyInstance()), + fn () => Visit::forRegularNotFound(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); } @@ -415,6 +444,7 @@ public function countOrphanVisitsReturnsExpectedResult(): void $this->getEntityManager()->flush(); self::assertEquals(18, $this->repo->countOrphanVisits(new OrphanVisitsCountFiltering())); + self::assertEquals(18, $this->orphanCountRepo->countOrphanVisits(new OrphanVisitsCountFiltering())); self::assertEquals(18, $this->repo->countOrphanVisits(new OrphanVisitsCountFiltering(DateRange::allTime()))); self::assertEquals(9, $this->repo->countOrphanVisits( new OrphanVisitsCountFiltering(DateRange::since(Chronos::parse('2020-01-04'))), @@ -535,7 +565,7 @@ private function createVisitsForShortUrl(ShortUrl $shortUrl, int $amount = 6, in { for ($i = 0; $i < $amount; $i++) { $visit = $this->setDateOnVisit( - Visit::forValidShortUrl( + fn () => Visit::forValidShortUrl( $shortUrl, $botsAmount < 1 ? Visitor::emptyInstance() : Visitor::botInstance(), ), @@ -547,12 +577,14 @@ private function createVisitsForShortUrl(ShortUrl $shortUrl, int $amount = 6, in } } - private function setDateOnVisit(Visit $visit, Chronos $date): Visit + /** + * @param callable(): Visit $createVisit + */ + private function setDateOnVisit(callable $createVisit, Chronos $date): Visit { - $ref = new ReflectionObject($visit); - $dateProp = $ref->getProperty('date'); - $dateProp->setAccessible(true); - $dateProp->setValue($visit, $date); + Chronos::setTestNow($date); + $visit = $createVisit(); + Chronos::setTestNow(); return $visit; } diff --git a/module/Core/test/EventDispatcher/Helper/RequestIdProviderTest.php b/module/Core/test/EventDispatcher/Helper/RequestIdProviderTest.php new file mode 100644 index 000000000..eb66f49e7 --- /dev/null +++ b/module/Core/test/EventDispatcher/Helper/RequestIdProviderTest.php @@ -0,0 +1,44 @@ +middleware = new RequestIdMiddleware(); + $this->provider = new RequestIdProvider($this->middleware); + } + + #[Test] + public function requestIdTrackedByMiddlewareIsForwarded(): void + { + $initialId = $this->middleware->currentRequestId(); + self::assertEquals($initialId, $this->provider->currentRequestId()); + + $handler = $this->createMock(RequestHandlerInterface::class); + $handler->method('handle')->willReturn(new Response()); + $this->middleware->process(ServerRequestFactory::fromGlobals(), $handler); + $idAfterProcessingRequest = $this->middleware->currentRequestId(); + self::assertNotEquals($idAfterProcessingRequest, $initialId); + self::assertEquals($idAfterProcessingRequest, $this->provider->currentRequestId()); + + $manuallySetId = 'foobar'; + $this->middleware->setCurrentRequestId($manuallySetId); + self::assertNotEquals($manuallySetId, $idAfterProcessingRequest); + self::assertEquals($manuallySetId, $this->provider->currentRequestId()); + } +} diff --git a/module/Core/test/EventDispatcher/LocateVisitTest.php b/module/Core/test/EventDispatcher/LocateVisitTest.php index ddadde842..63595a6ce 100644 --- a/module/Core/test/EventDispatcher/LocateVisitTest.php +++ b/module/Core/test/EventDispatcher/LocateVisitTest.php @@ -157,7 +157,7 @@ public static function provideNonLocatableVisits(): iterable #[Test, DataProvider('provideIpAddresses')] public function locatableVisitsResolveToLocation(Visit $visit, ?string $originalIpAddress): void { - $ipAddr = $originalIpAddress ?? $visit->getRemoteAddr(); + $ipAddr = $originalIpAddress ?? $visit->remoteAddr; $location = new Location('', '', '', '', 0.0, 0.0, ''); $event = new UrlVisited('123', $originalIpAddress); diff --git a/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php b/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php index 94c666232..10726273e 100644 --- a/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php +++ b/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php @@ -6,7 +6,6 @@ use Doctrine\ORM\EntityManagerInterface; use Exception; -use MatomoTracker; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; @@ -15,34 +14,28 @@ use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\Matomo\SendVisitToMatomo; use Shlinkio\Shlink\Core\Matomo\MatomoOptions; -use Shlinkio\Shlink\Core\Matomo\MatomoTrackerBuilderInterface; -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\Validation\ShortUrlInputFilter; +use Shlinkio\Shlink\Core\Matomo\MatomoVisitSenderInterface; use Shlinkio\Shlink\Core\Visit\Entity\Visit; -use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation; use Shlinkio\Shlink\Core\Visit\Model\Visitor; -use Shlinkio\Shlink\IpGeolocation\Model\Location; class SendVisitToMatomoTest extends TestCase { private MockObject & EntityManagerInterface $em; private MockObject & LoggerInterface $logger; - private MockObject & MatomoTrackerBuilderInterface $trackerBuilder; + private MockObject & MatomoVisitSenderInterface $visitSender; protected function setUp(): void { $this->em = $this->createMock(EntityManagerInterface::class); $this->logger = $this->createMock(LoggerInterface::class); - $this->trackerBuilder = $this->createMock(MatomoTrackerBuilderInterface::class); + $this->visitSender = $this->createMock(MatomoVisitSenderInterface::class); } #[Test] public function visitIsNotSentWhenMatomoIsDisabled(): void { $this->em->expects($this->never())->method('find'); - $this->trackerBuilder->expects($this->never())->method('buildMatomoTracker'); + $this->visitSender->expects($this->never())->method('sendVisit'); $this->logger->expects($this->never())->method('error'); $this->logger->expects($this->never())->method('warning'); @@ -53,7 +46,7 @@ public function visitIsNotSentWhenMatomoIsDisabled(): void public function visitIsNotSentWhenItDoesNotExist(): void { $this->em->expects($this->once())->method('find')->willReturn(null); - $this->trackerBuilder->expects($this->never())->method('buildMatomoTracker'); + $this->visitSender->expects($this->never())->method('sendVisit'); $this->logger->expects($this->never())->method('error'); $this->logger->expects($this->once())->method('warning')->with( 'Tried to send visit with id "{visitId}" to matomo, but it does not exist.', @@ -63,97 +56,24 @@ public function visitIsNotSentWhenItDoesNotExist(): void ($this->listener())(new VisitLocated('123')); } - #[Test, DataProvider('provideTrackerMethods')] - public function visitIsSentWhenItExists(Visit $visit, ?string $originalIpAddress, array $invokedMethods): void + #[Test, DataProvider('provideOriginalIpAddress')] + public function visitIsSentWhenItExists(?string $originalIpAddress): void { $visitId = '123'; - - $tracker = $this->createMock(MatomoTracker::class); - $tracker->expects($this->once())->method('setUrl')->willReturn($tracker); - $tracker->expects($this->once())->method('setUserAgent')->willReturn($tracker); - $tracker->expects($this->once())->method('setUrlReferrer')->willReturn($tracker); - $tracker->expects($this->once())->method('doTrackPageView')->with(''); - - if ($visit->isOrphan()) { - $tracker->expects($this->exactly(2))->method('setCustomTrackingParameter')->willReturnMap([ - ['type', $visit->type()->value, $tracker], - ['orphan', 'true', $tracker], - ]); - } else { - $tracker->expects($this->once())->method('setCustomTrackingParameter')->with( - 'type', - $visit->type()->value, - )->willReturn($tracker); - } - - foreach ($invokedMethods as $invokedMethod) { - $tracker->expects($this->once())->method($invokedMethod)->willReturn($tracker); - } + $visit = Visit::forBasePath(Visitor::emptyInstance()); $this->em->expects($this->once())->method('find')->with(Visit::class, $visitId)->willReturn($visit); - $this->trackerBuilder->expects($this->once())->method('buildMatomoTracker')->willReturn($tracker); + $this->visitSender->expects($this->once())->method('sendVisit')->with($visit, $originalIpAddress); $this->logger->expects($this->never())->method('error'); $this->logger->expects($this->never())->method('warning'); ($this->listener())(new VisitLocated($visitId, $originalIpAddress)); } - public static function provideTrackerMethods(): iterable - { - yield 'unlocated orphan visit' => [Visit::forBasePath(Visitor::emptyInstance()), null, []]; - yield 'located regular visit' => [ - Visit::forValidShortUrl(ShortUrl::withLongUrl('https://shlink.io'), Visitor::emptyInstance()) - ->locate(VisitLocation::fromGeolocation(new Location( - countryCode: 'countryCode', - countryName: 'countryName', - regionName: 'regionName', - city: 'city', - latitude: 123, - longitude: 123, - timeZone: 'timeZone', - ))), - '1.2.3.4', - ['setCity', 'setCountry', 'setLatitude', 'setLongitude', 'setIp'], - ]; - yield 'fallback IP' => [Visit::forBasePath(new Visitor('', '', '1.2.3.4', '')), null, ['setIp']]; - } - - #[Test, DataProvider('provideUrlsToTrack')] - public function properUrlIsTracked(Visit $visit, string $expectedTrackedUrl): void - { - $visitId = '123'; - - $tracker = $this->createMock(MatomoTracker::class); - $tracker->expects($this->once())->method('setUrl')->with($expectedTrackedUrl)->willReturn($tracker); - $tracker->expects($this->once())->method('setUserAgent')->willReturn($tracker); - $tracker->expects($this->once())->method('setUrlReferrer')->willReturn($tracker); - $tracker->expects($this->any())->method('setCustomTrackingParameter')->willReturn($tracker); - $tracker->expects($this->once())->method('doTrackPageView'); - - $this->em->expects($this->once())->method('find')->with(Visit::class, $visitId)->willReturn($visit); - $this->trackerBuilder->expects($this->once())->method('buildMatomoTracker')->willReturn($tracker); - $this->logger->expects($this->never())->method('error'); - $this->logger->expects($this->never())->method('warning'); - - ($this->listener())(new VisitLocated($visitId)); - } - - public static function provideUrlsToTrack(): iterable + public static function provideOriginalIpAddress(): iterable { - yield 'orphan visit without visited URL' => [Visit::forBasePath(Visitor::emptyInstance()), '']; - yield 'orphan visit with visited URL' => [ - Visit::forBasePath(new Visitor('', '', null, 'https://s.test/foo')), - 'https://s.test/foo', - ]; - yield 'non-orphan visit' => [ - Visit::forValidShortUrl(ShortUrl::create( - ShortUrlCreation::fromRawData([ - ShortUrlInputFilter::LONG_URL => 'https://shlink.io', - ShortUrlInputFilter::CUSTOM_SLUG => 'bar', - ]), - ), Visitor::emptyInstance()), - 'http://s2.test/bar', - ]; + yield 'no original IP address' => [null]; + yield 'original IP address' => ['1.2.3.4']; } #[Test] @@ -165,7 +85,7 @@ public function logsErrorWhenTrackingFails(): void $this->em->expects($this->once())->method('find')->with(Visit::class, $visitId)->willReturn( $this->createMock(Visit::class), ); - $this->trackerBuilder->expects($this->once())->method('buildMatomoTracker')->willThrowException($e); + $this->visitSender->expects($this->once())->method('sendVisit')->willThrowException($e); $this->logger->expects($this->never())->method('warning'); $this->logger->expects($this->once())->method('error')->with( 'An error occurred while trying to send visit to Matomo. {e}', @@ -180,9 +100,8 @@ private function listener(bool $enabled = true): SendVisitToMatomo return new SendVisitToMatomo( $this->em, $this->logger, - new ShortUrlStringifier(['hostname' => 's2.test']), new MatomoOptions(enabled: $enabled), - $this->trackerBuilder, + $this->visitSender, ); } } diff --git a/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php b/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php index 545c5b47c..bd3c82c83 100644 --- a/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php +++ b/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php @@ -4,6 +4,7 @@ namespace ShlinkioTest\Shlink\Core\EventDispatcher; +use Cake\Chronos\Chronos; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; @@ -18,20 +19,27 @@ use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary; use Shlinkio\Shlink\Core\Visit\Model\VisitType; -use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer; class PublishingUpdatesGeneratorTest extends TestCase { private PublishingUpdatesGenerator $generator; + private Chronos $now; protected function setUp(): void { + $this->now = Chronos::now(); + Chronos::setTestNow($this->now); + $this->generator = new PublishingUpdatesGenerator( new ShortUrlDataTransformer(new ShortUrlStringifier([])), - new OrphanVisitDataTransformer(), ); } + protected function tearDown(): void + { + Chronos::setTestNow(); + } + #[Test, DataProvider('provideMethod')] public function visitIsProperlySerializedIntoUpdate(string $method, string $expectedTopic, ?string $title): void { @@ -51,7 +59,7 @@ public function visitIsProperlySerializedIntoUpdate(string $method, string $expe 'shortCode' => $shortUrl->getShortCode(), 'shortUrl' => 'http:/' . $shortUrl->getShortCode(), 'longUrl' => 'https://longUrl', - 'dateCreated' => $shortUrl->getDateCreated()->toAtomString(), + 'dateCreated' => $this->now->toAtomString(), 'tags' => [], 'meta' => [ 'validSince' => null, @@ -68,8 +76,9 @@ public function visitIsProperlySerializedIntoUpdate(string $method, string $expe 'referer' => '', 'userAgent' => '', 'visitLocation' => null, - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'potentialBot' => false, + 'visitedUrl' => '', ], ], $update->payload); } @@ -91,10 +100,10 @@ public function orphanVisitIsProperlySerializedIntoUpdate(Visit $orphanVisit): v 'referer' => '', 'userAgent' => '', 'visitLocation' => null, - 'date' => $orphanVisit->getDate()->toAtomString(), + 'date' => $orphanVisit->date->toAtomString(), 'potentialBot' => false, - 'visitedUrl' => $orphanVisit->visitedUrl(), - 'type' => $orphanVisit->type()->value, + 'visitedUrl' => $orphanVisit->visitedUrl, + 'type' => $orphanVisit->type->value, ], ], $update->payload); } @@ -124,7 +133,7 @@ public function shortUrlIsProperlySerializedIntoUpdate(): void 'shortCode' => $shortUrl->getShortCode(), 'shortUrl' => 'http:/' . $shortUrl->getShortCode(), 'longUrl' => 'https://longUrl', - 'dateCreated' => $shortUrl->getDateCreated()->toAtomString(), + 'dateCreated' => $this->now->toAtomString(), 'tags' => [], 'meta' => [ 'validSince' => null, @@ -132,7 +141,7 @@ public function shortUrlIsProperlySerializedIntoUpdate(): void 'maxVisits' => null, ], 'domain' => null, - 'title' => $shortUrl->title(), + 'title' => 'The title', 'crawlable' => false, 'forwardQuery' => true, 'visitsSummary' => VisitsSummary::fromTotalAndNonBots(0, 0), diff --git a/module/Core/test/Importer/ImportedLinksProcessorTest.php b/module/Core/test/Importer/ImportedLinksProcessorTest.php index 7c8a17d12..a18165637 100644 --- a/module/Core/test/Importer/ImportedLinksProcessorTest.php +++ b/module/Core/test/Importer/ImportedLinksProcessorTest.php @@ -244,7 +244,7 @@ public function visitsArePersistedWithProperShortUrl(ShortUrl $originalShortUrl, $this->em->expects($this->once())->method('persist')->willReturnCallback( static fn (Visit $visit) => Assert::assertSame( $foundShortUrl ?? $originalShortUrl, - $visit->getShortUrl(), + $visit->shortUrl, ), ); diff --git a/module/Core/test/Matomo/MatomoVisitSenderTest.php b/module/Core/test/Matomo/MatomoVisitSenderTest.php new file mode 100644 index 000000000..816c8eeaf --- /dev/null +++ b/module/Core/test/Matomo/MatomoVisitSenderTest.php @@ -0,0 +1,173 @@ +trackerBuilder = $this->createMock(MatomoTrackerBuilderInterface::class); + $this->visitIterationRepository = $this->createMock(VisitIterationRepositoryInterface::class); + + $this->visitSender = new MatomoVisitSender( + $this->trackerBuilder, + new ShortUrlStringifier(['hostname' => 's2.test']), + $this->visitIterationRepository, + ); + } + + #[Test, DataProvider('provideTrackerMethods')] + public function visitIsSentToMatomo(Visit $visit, ?string $originalIpAddress, array $invokedMethods): void + { + $tracker = $this->createMock(MatomoTracker::class); + $tracker->expects($this->once())->method('setUrl')->willReturn($tracker); + $tracker->expects($this->once())->method('setUserAgent')->willReturn($tracker); + $tracker->expects($this->once())->method('setUrlReferrer')->willReturn($tracker); + $tracker->expects($this->once())->method('doTrackPageView')->with($visit->shortUrl?->title() ?? ''); + $tracker->expects($this->once())->method('setForceVisitDateTime')->with( + $visit->date->setTimezone('UTC')->toDateTimeString(), + ); + + if ($visit->isOrphan()) { + $tracker->expects($this->exactly(2))->method('setCustomTrackingParameter')->willReturnMap([ + ['type', $visit->type->value, $tracker], + ['orphan', 'true', $tracker], + ]); + } else { + $tracker->expects($this->once())->method('setCustomTrackingParameter')->with( + 'type', + $visit->type->value, + )->willReturn($tracker); + } + + foreach ($invokedMethods as $invokedMethod) { + $tracker->expects($this->once())->method($invokedMethod)->willReturn($tracker); + } + + $this->trackerBuilder->expects($this->once())->method('buildMatomoTracker')->willReturn($tracker); + + $this->visitSender->sendVisit($visit, $originalIpAddress); + } + + public static function provideTrackerMethods(): iterable + { + yield 'unlocated orphan visit' => [Visit::forBasePath(Visitor::emptyInstance()), null, []]; + yield 'located regular visit' => [ + Visit::forValidShortUrl(ShortUrl::withLongUrl('https://shlink.io'), Visitor::emptyInstance()) + ->locate(VisitLocation::fromGeolocation(new Location( + countryCode: 'countryCode', + countryName: 'countryName', + regionName: 'regionName', + city: 'city', + latitude: 123, + longitude: 123, + timeZone: 'timeZone', + ))), + '1.2.3.4', + ['setCity', 'setCountry', 'setLatitude', 'setLongitude', 'setIp'], + ]; + yield 'fallback IP' => [Visit::forBasePath(new Visitor('', '', '1.2.3.4', '')), null, ['setIp']]; + } + + #[Test, DataProvider('provideUrlsToTrack')] + public function properUrlIsTracked(Visit $visit, string $expectedTrackedUrl): void + { + $tracker = $this->createMock(MatomoTracker::class); + $tracker->expects($this->once())->method('setUrl')->with($expectedTrackedUrl)->willReturn($tracker); + $tracker->expects($this->once())->method('setUserAgent')->willReturn($tracker); + $tracker->expects($this->once())->method('setUrlReferrer')->willReturn($tracker); + $tracker->expects($this->any())->method('setCustomTrackingParameter')->willReturn($tracker); + $tracker->expects($this->once())->method('doTrackPageView'); + $tracker->expects($this->once())->method('setForceVisitDateTime')->with( + $visit->date->setTimezone('UTC')->toDateTimeString(), + ); + + $this->trackerBuilder->expects($this->once())->method('buildMatomoTracker')->willReturn($tracker); + + $this->visitSender->sendVisit($visit); + } + + public static function provideUrlsToTrack(): iterable + { + yield 'orphan visit without visited URL' => [Visit::forBasePath(Visitor::emptyInstance()), '']; + yield 'orphan visit with visited URL' => [ + Visit::forBasePath(new Visitor('', '', null, 'https://s.test/foo')), + 'https://s.test/foo', + ]; + yield 'non-orphan visit' => [ + Visit::forValidShortUrl(ShortUrl::create( + ShortUrlCreation::fromRawData([ + ShortUrlInputFilter::LONG_URL => 'https://shlink.io', + ShortUrlInputFilter::CUSTOM_SLUG => 'bar', + ]), + ), Visitor::emptyInstance()), + 'http://s2.test/bar', + ]; + } + + #[Test] + public function multipleVisitsCanBeSent(): void + { + $dateRange = DateRange::allTime(); + $visitor = Visitor::emptyInstance(); + $bot = Visitor::botInstance(); + + $this->visitIterationRepository->expects($this->once())->method('findAllVisits')->with($dateRange)->willReturn([ + Visit::forBasePath($bot), + Visit::forValidShortUrl(ShortUrl::createFake(), $visitor), + Visit::forInvalidShortUrl($visitor), + ]); + + $tracker = $this->createMock(MatomoTracker::class); + $tracker->method('setUrl')->willReturn($tracker); + $tracker->method('setUserAgent')->willReturn($tracker); + $tracker->method('setUrlReferrer')->willReturn($tracker); + $tracker->method('setCustomTrackingParameter')->willReturn($tracker); + + $callCount = 0; + $this->trackerBuilder->expects($this->exactly(3))->method('buildMatomoTracker')->willReturnCallback( + function () use (&$callCount, $tracker) { + $callCount++; + + if ($callCount === 2) { + throw new Exception('Error'); + } + + return $tracker; + }, + ); + + $result = $this->visitSender->sendVisitsInDateRange($dateRange); + + self::assertEquals(2, $result->successfulVisits); + self::assertEquals(1, $result->failedVisits); + self::assertCount(3, $result); + self::assertTrue($result->hasSuccesses()); + self::assertTrue($result->hasFailures()); + } +} diff --git a/module/Core/test/ShortUrl/DeleteShortUrlServiceTest.php b/module/Core/test/ShortUrl/DeleteShortUrlServiceTest.php index 3ac9897c2..4788818e8 100644 --- a/module/Core/test/ShortUrl/DeleteShortUrlServiceTest.php +++ b/module/Core/test/ShortUrl/DeleteShortUrlServiceTest.php @@ -13,7 +13,9 @@ use Shlinkio\Shlink\Core\Options\DeleteShortUrlsOptions; use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlService; 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\ExpiredShortUrlsRepository; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\Visitor; @@ -26,6 +28,7 @@ class DeleteShortUrlServiceTest extends TestCase { private MockObject & EntityManagerInterface $em; private MockObject & ShortUrlResolverInterface $urlResolver; + private MockObject & ExpiredShortUrlsRepository $expiredShortUrlsRepository; private string $shortCode; protected function setUp(): void @@ -39,6 +42,8 @@ protected function setUp(): void $this->urlResolver = $this->createMock(ShortUrlResolverInterface::class); $this->urlResolver->method('resolveShortUrl')->willReturn($shortUrl); + + $this->expiredShortUrlsRepository = $this->createMock(ExpiredShortUrlsRepository::class); } #[Test] @@ -94,11 +99,33 @@ public function deleteByShortCodeDeletesUrlWhenThresholdIsNotReached(): void $service->deleteByShortCode(ShortUrlIdentifier::fromShortCodeAndDomain($this->shortCode)); } + #[Test] + public function deleteExpiredShortUrlsDelegatesToRepository(): void + { + $conditions = new ExpiredShortUrlsConditions(); + $this->expiredShortUrlsRepository->expects($this->once())->method('delete')->with($conditions)->willReturn(5); + + $result = $this->createService()->deleteExpiredShortUrls($conditions); + + self::assertEquals(5, $result); + } + + #[Test] + public function countExpiredShortUrlsDelegatesToRepository(): void + { + $conditions = new ExpiredShortUrlsConditions(); + $this->expiredShortUrlsRepository->expects($this->once())->method('dryCount')->with($conditions)->willReturn(2); + + $result = $this->createService()->countExpiredShortUrls($conditions); + + self::assertEquals(2, $result); + } + private function createService(bool $checkVisitsThreshold = true, int $visitsThreshold = 5): DeleteShortUrlService { return new DeleteShortUrlService($this->em, new DeleteShortUrlsOptions( $visitsThreshold, $checkVisitsThreshold, - ), $this->urlResolver); + ), $this->urlResolver, $this->expiredShortUrlsRepository); } } diff --git a/module/Core/test/ShortUrl/ShortUrlServiceTest.php b/module/Core/test/ShortUrl/ShortUrlServiceTest.php index ae73ba335..669015a06 100644 --- a/module/Core/test/ShortUrl/ShortUrlServiceTest.php +++ b/module/Core/test/ShortUrl/ShortUrlServiceTest.php @@ -70,9 +70,11 @@ public function updateShortUrlUpdatesProvidedData( ); self::assertSame($shortUrl, $result); - self::assertEquals($shortUrlEdit->validSince, $shortUrl->getValidSince()); - self::assertEquals($shortUrlEdit->validUntil, $shortUrl->getValidUntil()); - self::assertEquals($shortUrlEdit->maxVisits, $shortUrl->getMaxVisits()); + ['validSince' => $since, 'validUntil' => $until, 'maxVisits' => $maxVisits] = $shortUrl->toArray()['meta']; + + self::assertEquals($shortUrlEdit->validSince?->toAtomString(), $since); + self::assertEquals($shortUrlEdit->validUntil?->toAtomString(), $until); + self::assertEquals($shortUrlEdit->maxVisits, $maxVisits); self::assertEquals($shortUrlEdit->longUrl ?? $originalLongUrl, $shortUrl->getLongUrl()); } diff --git a/module/Core/test/Visit/Entity/VisitLocationTest.php b/module/Core/test/Visit/Entity/VisitLocationTest.php index 46400f708..5f5c458dc 100644 --- a/module/Core/test/Visit/Entity/VisitLocationTest.php +++ b/module/Core/test/Visit/Entity/VisitLocationTest.php @@ -18,7 +18,7 @@ public function isEmptyReturnsTrueWhenAllValuesAreEmpty(array $args, bool $isEmp $payload = new Location(...$args); $location = VisitLocation::fromGeolocation($payload); - self::assertEquals($isEmpty, $location->isEmpty()); + self::assertEquals($isEmpty, $location->isEmpty); } public static function provideArgs(): iterable diff --git a/module/Core/test/Visit/Entity/VisitTest.php b/module/Core/test/Visit/Entity/VisitTest.php index 5eb885271..923b2e6b7 100644 --- a/module/Core/test/Visit/Entity/VisitTest.php +++ b/module/Core/test/Visit/Entity/VisitTest.php @@ -4,13 +4,18 @@ namespace ShlinkioTest\Shlink\Core\Visit\Entity; +use Laminas\Diactoros\ServerRequestFactory; +use Laminas\Diactoros\Uri; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Common\Util\IpAddress; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\Visit\Entity\Visit; +use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation; use Shlinkio\Shlink\Core\Visit\Model\Visitor; +use Shlinkio\Shlink\Core\Visit\Model\VisitType; +use Shlinkio\Shlink\IpGeolocation\Model\Location; class VisitTest extends TestCase { @@ -21,10 +26,11 @@ public function isProperlyJsonSerialized(string $userAgent, bool $expectedToBePo self::assertEquals([ 'referer' => 'some site', - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'userAgent' => $userAgent, 'visitLocation' => null, 'potentialBot' => $expectedToBePotentialBot, + 'visitedUrl' => $visit->visitedUrl, ], $visit->jsonSerialize()); } @@ -40,6 +46,62 @@ public static function provideUserAgents(): iterable yield 'Guzzle' => ['guzzlehttp', true]; } + #[Test, DataProvider('provideOrphanVisits')] + public function isProperlyJsonSerializedWhenOrphan(Visit $visit, array $expectedResult): void + { + self::assertEquals($expectedResult, $visit->jsonSerialize()); + } + + public static function provideOrphanVisits(): iterable + { + yield 'base path visit' => [ + $visit = Visit::forBasePath(Visitor::emptyInstance()), + [ + 'referer' => '', + 'date' => $visit->date->toAtomString(), + 'userAgent' => '', + 'visitLocation' => null, + 'potentialBot' => false, + 'visitedUrl' => '', + 'type' => VisitType::BASE_URL->value, + ], + ]; + yield 'invalid short url visit' => [ + $visit = Visit::forInvalidShortUrl(Visitor::fromRequest( + ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'foo') + ->withHeader('Referer', 'bar') + ->withUri(new Uri('https://example.com/foo')), + )), + [ + 'referer' => 'bar', + 'date' => $visit->date->toAtomString(), + 'userAgent' => 'foo', + 'visitLocation' => null, + 'potentialBot' => false, + 'visitedUrl' => 'https://example.com/foo', + 'type' => VisitType::INVALID_SHORT_URL->value, + ], + ]; + yield 'regular 404 visit' => [ + $visit = Visit::forRegularNotFound( + Visitor::fromRequest( + ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'user-agent') + ->withHeader('Referer', 'referer') + ->withUri(new Uri('https://s.test/foo/bar')), + ), + )->locate($location = VisitLocation::fromGeolocation(Location::emptyInstance())), + [ + 'referer' => 'referer', + 'date' => $visit->date->toAtomString(), + 'userAgent' => 'user-agent', + 'visitLocation' => $location, + 'potentialBot' => false, + 'visitedUrl' => 'https://s.test/foo/bar', + 'type' => VisitType::REGULAR_404->value, + ], + ]; + } + #[Test, DataProvider('provideAddresses')] public function addressIsAnonymizedWhenRequested(bool $anonymize, ?string $address, ?string $expectedAddress): void { @@ -49,7 +111,7 @@ public function addressIsAnonymizedWhenRequested(bool $anonymize, ?string $addre $anonymize, ); - self::assertEquals($expectedAddress, $visit->getRemoteAddr()); + self::assertEquals($expectedAddress, $visit->remoteAddr); } public static function provideAddresses(): iterable diff --git a/module/Core/test/Visit/Geolocation/VisitLocatorTest.php b/module/Core/test/Visit/Geolocation/VisitLocatorTest.php index 1d3af228b..f1d86f636 100644 --- a/module/Core/test/Visit/Geolocation/VisitLocatorTest.php +++ b/module/Core/test/Visit/Geolocation/VisitLocatorTest.php @@ -17,7 +17,7 @@ use Shlinkio\Shlink\Core\Visit\Geolocation\VisitGeolocationHelperInterface; use Shlinkio\Shlink\Core\Visit\Geolocation\VisitLocator; use Shlinkio\Shlink\Core\Visit\Model\Visitor; -use Shlinkio\Shlink\Core\Visit\Repository\VisitLocationRepositoryInterface; +use Shlinkio\Shlink\Core\Visit\Repository\VisitIterationRepositoryInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; use function array_map; @@ -30,12 +30,12 @@ class VisitLocatorTest extends TestCase { private VisitLocator $visitService; private MockObject & EntityManager $em; - private MockObject & VisitLocationRepositoryInterface $repo; + private MockObject & VisitIterationRepositoryInterface $repo; protected function setUp(): void { $this->em = $this->createMock(EntityManager::class); - $this->repo = $this->createMock(VisitLocationRepositoryInterface::class); + $this->repo = $this->createMock(VisitIterationRepositoryInterface::class); $this->visitService = new VisitLocator($this->em, $this->repo); } diff --git a/module/Core/test/Visit/Transformer/OrphanVisitDataTransformerTest.php b/module/Core/test/Visit/Transformer/OrphanVisitDataTransformerTest.php deleted file mode 100644 index 527f4fc92..000000000 --- a/module/Core/test/Visit/Transformer/OrphanVisitDataTransformerTest.php +++ /dev/null @@ -1,85 +0,0 @@ -transformer = new OrphanVisitDataTransformer(); - } - - #[Test, DataProvider('provideVisits')] - public function visitsAreParsedAsExpected(Visit $visit, array $expectedResult): void - { - $result = $this->transformer->transform($visit); - - self::assertEquals($expectedResult, $result); - } - - public static function provideVisits(): iterable - { - yield 'base path visit' => [ - $visit = Visit::forBasePath(Visitor::emptyInstance()), - [ - 'referer' => '', - 'date' => $visit->getDate()->toAtomString(), - 'userAgent' => '', - 'visitLocation' => null, - 'potentialBot' => false, - 'visitedUrl' => '', - 'type' => VisitType::BASE_URL->value, - ], - ]; - yield 'invalid short url visit' => [ - $visit = Visit::forInvalidShortUrl(Visitor::fromRequest( - ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'foo') - ->withHeader('Referer', 'bar') - ->withUri(new Uri('https://example.com/foo')), - )), - [ - 'referer' => 'bar', - 'date' => $visit->getDate()->toAtomString(), - 'userAgent' => 'foo', - 'visitLocation' => null, - 'potentialBot' => false, - 'visitedUrl' => 'https://example.com/foo', - 'type' => VisitType::INVALID_SHORT_URL->value, - ], - ]; - yield 'regular 404 visit' => [ - $visit = Visit::forRegularNotFound( - Visitor::fromRequest( - ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'user-agent') - ->withHeader('Referer', 'referer') - ->withUri(new Uri('https://s.test/foo/bar')), - ), - )->locate($location = VisitLocation::fromGeolocation(Location::emptyInstance())), - [ - 'referer' => 'referer', - 'date' => $visit->getDate()->toAtomString(), - 'userAgent' => 'user-agent', - 'visitLocation' => $location, - 'potentialBot' => false, - 'visitedUrl' => 'https://s.test/foo/bar', - 'type' => VisitType::REGULAR_404->value, - ], - ]; - } -} diff --git a/module/Core/test/Visit/VisitsStatsHelperTest.php b/module/Core/test/Visit/VisitsStatsHelperTest.php index f6bb5464b..c1aa07475 100644 --- a/module/Core/test/Visit/VisitsStatsHelperTest.php +++ b/module/Core/test/Visit/VisitsStatsHelperTest.php @@ -22,6 +22,8 @@ use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Tag\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Repository\TagRepository; +use Shlinkio\Shlink\Core\Visit\Entity\OrphanVisitsCount; +use Shlinkio\Shlink\Core\Visit\Entity\ShortUrlVisitsCount; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams; use Shlinkio\Shlink\Core\Visit\Model\Visitor; @@ -31,6 +33,8 @@ use Shlinkio\Shlink\Core\Visit\Persistence\OrphanVisitsListFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering; +use Shlinkio\Shlink\Core\Visit\Repository\OrphanVisitsCountRepository; +use Shlinkio\Shlink\Core\Visit\Repository\ShortUrlVisitsCountRepository; use Shlinkio\Shlink\Core\Visit\Repository\VisitRepository; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelper; use Shlinkio\Shlink\Rest\Entity\ApiKey; @@ -54,9 +58,9 @@ protected function setUp(): void #[Test, DataProvider('provideCounts')] public function returnsExpectedVisitsStats(int $expectedCount, ?ApiKey $apiKey): void { - $repo = $this->createMock(VisitRepository::class); $callCount = 0; - $repo->expects($this->exactly(2))->method('countNonOrphanVisits')->willReturnCallback( + $visitsCountRepo = $this->createMock(ShortUrlVisitsCountRepository::class); + $visitsCountRepo->expects($this->exactly(2))->method('countNonOrphanVisits')->willReturnCallback( function (VisitsCountFiltering $options) use ($expectedCount, $apiKey, &$callCount) { Assert::assertEquals($callCount !== 0, $options->excludeBots); Assert::assertEquals($apiKey, $options->apiKey); @@ -65,10 +69,16 @@ function (VisitsCountFiltering $options) use ($expectedCount, $apiKey, &$callCou return $expectedCount * 3; }, ); - $repo->expects($this->exactly(2))->method('countOrphanVisits')->with( + + $orphanVisitsCountRepo = $this->createMock(OrphanVisitsCountRepository::class); + $orphanVisitsCountRepo->expects($this->exactly(2))->method('countOrphanVisits')->with( $this->isInstanceOf(VisitsCountFiltering::class), )->willReturn($expectedCount); - $this->em->expects($this->once())->method('getRepository')->with(Visit::class)->willReturn($repo); + + $this->em->expects($this->exactly(2))->method('getRepository')->willReturnMap([ + [OrphanVisitsCount::class, $orphanVisitsCountRepo], + [ShortUrlVisitsCount::class, $visitsCountRepo], + ]); $stats = $this->helper->getVisitsStats($apiKey); diff --git a/module/Core/test/Visit/VisitsTrackerTest.php b/module/Core/test/Visit/VisitsTrackerTest.php index 32cd10a88..d6cbf4bff 100644 --- a/module/Core/test/Visit/VisitsTrackerTest.php +++ b/module/Core/test/Visit/VisitsTrackerTest.php @@ -25,6 +25,8 @@ class VisitsTrackerTest extends TestCase protected function setUp(): void { $this->em = $this->createMock(EntityManager::class); + $this->em->method('wrapInTransaction')->willReturnCallback(fn (callable $callback) => $callback()); + $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); } diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index 9396dd381..d334d5b04 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -89,10 +89,7 @@ 'config.url_shortener.domain.hostname', ], Action\Visit\GlobalVisitsAction::class => [Visit\VisitsStatsHelper::class], - Action\Visit\OrphanVisitsAction::class => [ - Visit\VisitsStatsHelper::class, - Visit\Transformer\OrphanVisitDataTransformer::class, - ], + Action\Visit\OrphanVisitsAction::class => [Visit\VisitsStatsHelper::class], Action\Visit\DeleteOrphanVisitsAction::class => [Visit\VisitsDeleter::class], Action\Visit\NonOrphanVisitsAction::class => [Visit\VisitsStatsHelper::class], Action\ShortUrl\ListShortUrlsAction::class => [ diff --git a/module/Rest/src/Action/Visit/OrphanVisitsAction.php b/module/Rest/src/Action/Visit/OrphanVisitsAction.php index 572441975..0224022d4 100644 --- a/module/Rest/src/Action/Visit/OrphanVisitsAction.php +++ b/module/Rest/src/Action/Visit/OrphanVisitsAction.php @@ -8,7 +8,6 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Common\Paginator\Util\PagerfantaUtilsTrait; -use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; @@ -21,10 +20,8 @@ class OrphanVisitsAction extends AbstractRestAction protected const ROUTE_PATH = '/visits/orphan'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; - public function __construct( - private readonly VisitsStatsHelperInterface $visitsHelper, - private readonly DataTransformerInterface $orphanVisitTransformer, - ) { + public function __construct(private readonly VisitsStatsHelperInterface $visitsHelper) + { } public function handle(ServerRequestInterface $request): ResponseInterface @@ -34,7 +31,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface $visits = $this->visitsHelper->orphanVisits($params, $apiKey); return new JsonResponse([ - 'visits' => $this->serializePaginator($visits, $this->orphanVisitTransformer), + 'visits' => $this->serializePaginator($visits), ]); } } diff --git a/module/Rest/src/Entity/ApiKey.php b/module/Rest/src/Entity/ApiKey.php index 9ad3fcf45..46548dcf6 100644 --- a/module/Rest/src/Entity/ApiKey.php +++ b/module/Rest/src/Entity/ApiKey.php @@ -17,21 +17,17 @@ class ApiKey extends AbstractEntity { - private string $key; - private ?Chronos $expirationDate = null; - private bool $enabled; - /** @var Collection */ - private Collection $roles; - private ?string $name = null; - /** + * @param Collection $roles * @throws Exception */ - private function __construct(string $key) - { - $this->key = $key; - $this->enabled = true; - $this->roles = new ArrayCollection(); + private function __construct( + private string $key, + public readonly ?string $name = null, + public readonly ?Chronos $expirationDate = null, + private bool $enabled = true, + private Collection $roles = new ArrayCollection(), + ) { } /** @@ -47,10 +43,7 @@ public static function create(): ApiKey */ public static function fromMeta(ApiKeyMeta $meta): self { - $apiKey = new self($meta->key); - $apiKey->name = $meta->name; - $apiKey->expirationDate = $meta->expirationDate; - + $apiKey = new self($meta->key, $meta->name, $meta->expirationDate); foreach ($meta->roleDefinitions as $roleDefinition) { $apiKey->registerRole($roleDefinition); } @@ -58,21 +51,11 @@ public static function fromMeta(ApiKeyMeta $meta): self return $apiKey; } - public function getExpirationDate(): ?Chronos - { - return $this->expirationDate; - } - public function isExpired(): bool { return $this->expirationDate !== null && $this->expirationDate->lessThan(Chronos::now()); } - public function name(): ?string - { - return $this->name; - } - public function isEnabled(): bool { return $this->enabled; diff --git a/module/Rest/test-api/Action/ListShortUrlsTest.php b/module/Rest/test-api/Action/ListShortUrlsTest.php index c3b9b41e8..e3fc49a66 100644 --- a/module/Rest/test-api/Action/ListShortUrlsTest.php +++ b/module/Rest/test-api/Action/ListShortUrlsTest.php @@ -201,12 +201,12 @@ public static function provideFilteredLists(): iterable self::SHORT_URL_SHLINK_WITH_TITLE, ], 'valid_api_key']; yield [['orderBy' => 'title-DESC'], [ + self::SHORT_URL_SHLINK_WITH_TITLE, self::SHORT_URL_META, self::SHORT_URL_CUSTOM_SLUG, self::SHORT_URL_DOCS, self::SHORT_URL_CUSTOM_DOMAIN, self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN, - self::SHORT_URL_SHLINK_WITH_TITLE, ], 'valid_api_key']; yield [['startDate' => Chronos::parse('2018-12-01')->toAtomString()], [ self::SHORT_URL_CUSTOM_DOMAIN, diff --git a/module/Rest/test-api/Fixtures/VisitsFixture.php b/module/Rest/test-api/Fixtures/VisitsFixture.php index 6076f95e9..9972e3a87 100644 --- a/module/Rest/test-api/Fixtures/VisitsFixture.php +++ b/module/Rest/test-api/Fixtures/VisitsFixture.php @@ -8,7 +8,6 @@ use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\DependentFixtureInterface; use Doctrine\Persistence\ObjectManager; -use ReflectionObject; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\Visitor; @@ -50,27 +49,31 @@ public function load(ObjectManager $manager): void ); $manager->persist($this->setVisitDate( - Visit::forBasePath(new Visitor('shlink-tests-agent', 'https://s.test', '1.2.3.4', '')), + fn () => Visit::forBasePath(new Visitor('shlink-tests-agent', 'https://s.test', '1.2.3.4', '')), '2020-01-01', )); $manager->persist($this->setVisitDate( - Visit::forRegularNotFound(new Visitor('shlink-tests-agent', 'https://s.test/foo/bar', '1.2.3.4', '')), + fn () => Visit::forRegularNotFound( + new Visitor('shlink-tests-agent', 'https://s.test/foo/bar', '1.2.3.4', ''), + ), '2020-02-01', )); $manager->persist($this->setVisitDate( - Visit::forInvalidShortUrl(new Visitor('cf-facebook', 'https://s.test/foo', '1.2.3.4', 'foo.com')), + fn () => Visit::forInvalidShortUrl(new Visitor('cf-facebook', 'https://s.test/foo', '1.2.3.4', 'foo.com')), '2020-03-01', )); $manager->flush(); } - private function setVisitDate(Visit $visit, string $date): Visit + /** + * @param callable(): Visit $createVisit + */ + private function setVisitDate(callable $createVisit, string $date): Visit { - $ref = new ReflectionObject($visit); - $dateProp = $ref->getProperty('date'); - $dateProp->setAccessible(true); - $dateProp->setValue($visit, Chronos::parse($date)); + Chronos::setTestNow($date); + $visit = $createVisit(); + Chronos::setTestNow(); return $visit; } diff --git a/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php b/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php index efa14caaa..d5bdfef94 100644 --- a/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php +++ b/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php @@ -11,7 +11,6 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Common\Paginator\Paginator; -use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams; @@ -26,14 +25,11 @@ class OrphanVisitsActionTest extends TestCase { private OrphanVisitsAction $action; private MockObject & VisitsStatsHelperInterface $visitsHelper; - private MockObject & DataTransformerInterface $orphanVisitTransformer; protected function setUp(): void { $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class); - $this->orphanVisitTransformer = $this->createMock(DataTransformerInterface::class); - - $this->action = new OrphanVisitsAction($this->visitsHelper, $this->orphanVisitTransformer); + $this->action = new OrphanVisitsAction($this->visitsHelper); } #[Test] @@ -45,9 +41,6 @@ public function requestIsHandled(): void $this->isInstanceOf(OrphanVisitsParams::class), )->willReturn(new Paginator(new ArrayAdapter($visits))); $visitsAmount = count($visits); - $this->orphanVisitTransformer->expects($this->exactly($visitsAmount))->method('transform')->with( - $this->isInstanceOf(Visit::class), - )->willReturn([]); /** @var JsonResponse $response */ $response = $this->action->handle( diff --git a/module/Rest/test/Service/ApiKeyServiceTest.php b/module/Rest/test/Service/ApiKeyServiceTest.php index f45e6ca51..453640702 100644 --- a/module/Rest/test/Service/ApiKeyServiceTest.php +++ b/module/Rest/test/Service/ApiKeyServiceTest.php @@ -44,8 +44,8 @@ public function apiKeyIsProperlyCreated(?Chronos $date, ?string $name, array $ro ApiKeyMeta::fromParams(name: $name, expirationDate: $date, roleDefinitions: $roles), ); - self::assertEquals($date, $key->getExpirationDate()); - self::assertEquals($name, $key->name()); + self::assertEquals($date, $key->expirationDate); + self::assertEquals($name, $key->name); foreach ($roles as $roleDefinition) { self::assertTrue($key->hasRole($roleDefinition->role)); } diff --git a/phpunit-db.xml b/phpunit-db.xml index b883d8caf..17e748b87 100644 --- a/phpunit-db.xml +++ b/phpunit-db.xml @@ -20,6 +20,7 @@ ./module/*/src/Spec ./module/*/src/**/Spec ./module/*/src/**/**/Spec + ./module/Core/src/Visit/Listener/*.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9c85d2c48..30f2286db 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -30,6 +30,7 @@ ./module/Core/src/Spec ./module/Core/src/**/Spec ./module/Core/src/**/**/Spec + ./module/Core/src/Visit/Listener/*.php