diff --git a/Classes/Domain/Repository/PagevisitRepository.php b/Classes/Domain/Repository/PagevisitRepository.php index 37366c07..945825e6 100644 --- a/Classes/Domain/Repository/PagevisitRepository.php +++ b/Classes/Domain/Repository/PagevisitRepository.php @@ -15,6 +15,7 @@ use In2code\Lux\Domain\Model\Visitor; use In2code\Lux\Domain\Service\Referrer\Readable; use In2code\Lux\Domain\Service\Referrer\SocialMedia; +use In2code\Lux\Domain\Service\SiteService; use In2code\Lux\Exception\ArgumentsException; use In2code\Lux\Utility\ArrayUtility; use In2code\Lux\Utility\DatabaseUtility; @@ -310,20 +311,20 @@ public function findAmountPerPageAndVisitor(int $pageIdentifier, Visitor $visito * @param FilterDto $filter * @param int $limit * @return array - * @throws Exception * @throws ExceptionDbal + * @throws ExceptionDbalDriver */ public function getAmountOfReferrers(FilterDto $filter, int $limit = 100): array { + $siteService = GeneralUtility::makeInstance(SiteService::class); $connection = DatabaseUtility::getConnectionForTable(Pagevisit::TABLE_NAME); - $domainLike = addcslashes(FrontendUtility::getCurrentDomain(), '_%'); $sql = 'select referrer, count(referrer) count from ' . Pagevisit::TABLE_NAME - . ' where referrer != ""' - . ' and referrer not like ' . $connection->quote('%' . $domainLike . '%') + . ' where referrer != \'\'' + . ' and referrer not regexp "' . $siteService->getAllDomainsForWhereClause() . '"' . $this->extendWhereClauseWithFilterTime($filter) . $this->extendWhereClauseWithFilterSite($filter) . ' group by referrer having (count > 1) order by count desc limit ' . $limit; - $records = (array)$connection->executeQuery($sql)->fetchAllAssociative(); + $records = $connection->executeQuery($sql)->fetchAllAssociative(); $result = []; foreach ($records as $record) { $readableReferrer = GeneralUtility::makeInstance(Readable::class, $record['referrer']); diff --git a/Classes/Domain/Service/SiteService.php b/Classes/Domain/Service/SiteService.php index 4554b975..6107dc7b 100644 --- a/Classes/Domain/Service/SiteService.php +++ b/Classes/Domain/Service/SiteService.php @@ -4,6 +4,9 @@ namespace In2code\Lux\Domain\Service; use In2code\Lux\Utility\BackendUtility; +use In2code\Lux\Utility\FrontendUtility; +use In2code\Lux\Utility\StringUtility; +use In2code\Lux\Utility\UrlUtility; use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore; use TYPO3\CMS\Core\Exception\SiteNotFoundException; use TYPO3\CMS\Core\Site\Entity\Site; @@ -23,6 +26,7 @@ public function __construct(SiteFinder $siteFinder) * @param int $languageId normally sys_language_uid * @param int $pageIdentifier pid * @return string "de" or "en" + * @throws SiteNotFoundException */ public function getLanguageCodeFromLanguageAndPageIdentifier(int $languageId, int $pageIdentifier): string { @@ -68,6 +72,45 @@ public function getSiteIdentifierFromPageIdentifier(int $pageIdentifier): string return ''; } + /** + * Get all domains without protocol from all site configs + * [ + * 'www.domain.org', + * 'stage.domain.org', + * ] + * + * @return array + */ + public function getAllDomains(): array + { + $sites = $this->siteFinder->getAllSites(); + $domains = []; + foreach ($sites as $site) { + $url = $site->getBase()->__toString(); + $url = UrlUtility::removeProtocolFromDomain($url); + $domains[] = StringUtility::cleanString($url, true, './_-'); + } + return $domains; + } + + /** + * Get all domains from all site configs for a sql query with regexp (splitted by |) + * + * @param bool $addCurrentDomain + * @return string + */ + public function getAllDomainsForWhereClause(bool $addCurrentDomain = true): string + { + $domains = $this->getAllDomains(); + if ($addCurrentDomain) { + $domains = array_merge( + $domains, + [StringUtility::cleanString(FrontendUtility::getCurrentDomain(), true, './_-')] + ); + } + return implode('|', $domains); + } + public function getFirstDomain(): string { $site = self::getDefaultSite();