Skip to content

Commit

Permalink
[FEATURE] Remove all domains from referrer diagram where referrers ar…
Browse files Browse the repository at this point in the history
…e from local instance

So we now exclude all domains from all site configs in the where clause

Related: https://projekte.in2code.de/issues/62273
  • Loading branch information
einpraegsam committed Mar 19, 2024
1 parent ea713b3 commit 018835c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Classes/Domain/Repository/PagevisitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand Down
43 changes: 43 additions & 0 deletions Classes/Domain/Service/SiteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 018835c

Please sign in to comment.