@@ -130,6 +130,23 @@ public function findLatestPagevisitsWithCompanies(FilterDto $filter): QueryResul
130130 return $ query ->execute ();
131131 }
132132
133+ public function findByReferrerDomain (FilterDto $ filter ): array
134+ {
135+ if (MathUtility::canBeInterpretedAsInteger ($ filter ->isSearchtermSet ()) === false ) {
136+ throw new ArgumentsException ('Filter searchterm must be filled ' , 1752775565 );
137+ }
138+
139+ $ sql = 'select pv.uid from ' . Pagevisit::TABLE_NAME . ' pv '
140+ . ' where pv.referrer like "https:// ' . $ filter ->getSearchterm () . '%" '
141+ . $ this ->extendWhereClauseWithFilterTime ($ filter , true , 'pv ' )
142+ . $ this ->extendWhereClauseWithFilterSite ($ filter , 'pv ' )
143+ . ' order by pv.crdate desc '
144+ . ' limit ' . ($ filter ->isLimitSet () ? $ filter ->getLimit () : 750 );
145+ $ connection = DatabaseUtility::getConnectionForTable (Pagevisit::TABLE_NAME );
146+ $ pagevisitIdentifiers = $ connection ->executeQuery ($ sql )->fetchFirstColumn ();
147+ return $ this ->convertIdentifiersToObjects ($ pagevisitIdentifiers , Pagevisit::TABLE_NAME );
148+ }
149+
133150 /**
134151 * @param DateTime $start
135152 * @param DateTime $end
@@ -363,6 +380,101 @@ public function getAmountOfSocialMediaReferrers(FilterDto $filter): array
363380 return $ result ;
364381 }
365382
383+ /**
384+ * [
385+ * [
386+ * 'referrer_domain' => 'x.com',
387+ * 'count' => 123,
388+ * 'identified_count' => 34,
389+ * 'children' => [QueryResult],
390+ * ],
391+ * [
392+ * 'referrer_domain' => 'openai.com',
393+ * 'count' => 25,
394+ * 'identified_count' => 12,
395+ * 'children' => [QueryResult],
396+ * ],
397+ * ]
398+ *
399+ * @param FilterDto $filter
400+ * @return array
401+ * @throws ExceptionDbal
402+ */
403+ public function getReferrers (FilterDto $ filter ): array
404+ {
405+ $ readable = GeneralUtility::makeInstance (Readable::class);
406+ $ grouped = [];
407+ foreach ($ this ->getAmountOfReferrerDomains ($ filter ) as $ row ) {
408+ $ row ['identified_count ' ] = $ this ->extendRowIdentified ($ row , $ filter );
409+ $ row = $ this ->extendRowWithChildren ($ row , $ filter );
410+ $ key = $ readable ->getKeyFromHost ($ row ['referrer_domain ' ]) ?: 'other ' ;
411+ $ grouped [$ key ][] = $ row ;
412+ }
413+ return $ grouped ;
414+ }
415+
416+ /**
417+ * [
418+ * [
419+ * 'referrer_domain' => 'x.com',
420+ * 'count' => 123,
421+ * ],
422+ * [
423+ * 'referrer_domain' => 'openai.com',
424+ * 'count' => 25,
425+ * ],
426+ * ]
427+ *
428+ * @param FilterDto $filter
429+ * @return array
430+ * @throws ExceptionDbal
431+ */
432+ protected function getAmountOfReferrerDomains (FilterDto $ filter ): array
433+ {
434+ $ connection = DatabaseUtility::getConnectionForTable (Pagevisit::TABLE_NAME );
435+ $ sql = 'select substring_index(substring_index(referrer, \':// \', -1), \'/ \', 1) referrer_domain, count(*) count ' ;
436+ $ sql .= ' from ' . Pagevisit::TABLE_NAME . ' pv ' ;
437+ $ sql .= ' where pv.deleted = 0 and pv.hidden = 0 and pv.referrer != \'\'' ;
438+ $ sql .= $ this ->extendWhereClauseWithFilterSearchterms ($ filter , 'pv ' , 'referrer ' );
439+ $ sql .= $ this ->extendWhereClauseWithFilterTime ($ filter );
440+ $ sql .= $ this ->extendWhereClauseWithFilterSite ($ filter );
441+ $ sql .= $ this ->extendWhereClauseWithFilterDomain ($ filter );
442+ $ sql .= ' group by referrer_domain order by count desc; ' ;
443+ $ rows = $ connection ->executeQuery ($ sql )->fetchAllAssociative ();
444+ return $ rows ;
445+ }
446+
447+ protected function extendRowIdentified (array $ row , FilterDto $ filter ): int
448+ {
449+ $ connection = DatabaseUtility::getConnectionForTable (Pagevisit::TABLE_NAME );
450+ $ sql = 'select count(*) identified_count ' ;
451+ $ sql .= ' from tx_lux_domain_model_visitor v ' ;
452+ $ sql .= ' where v.identified = 1 ' ;
453+ $ sql .= ' and exists ( ' ;
454+ $ sql .= 'select 1 ' ;
455+ $ sql .= ' from tx_lux_domain_model_pagevisit pv ' ;
456+ $ sql .= ' where pv.visitor = v.uid and pv.referrer like "https:// ' . $ row ['referrer_domain ' ] . '%" ' ;
457+ $ sql .= $ this ->extendWhereClauseWithFilterTime ($ filter , true , 'pv ' );
458+ $ sql .= $ this ->extendWhereClauseWithFilterSite ($ filter , 'pv ' );
459+ $ sql .= ') ' ;
460+ return (int )$ connection ->executeQuery ($ sql )->fetchOne ();
461+ }
462+
463+ protected function extendRowWithChildren (array $ row , FilterDto $ filter ): array
464+ {
465+ $ query = $ this ->createQuery ();
466+ $ logicalAnd = [
467+ $ query ->like ('referrer ' , 'https:// ' . $ row ['referrer_domain ' ] . '% ' ),
468+ ];
469+ $ logicalAnd = $ this ->extendLogicalAndWithFilterConstraintsForCrdate ($ filter , $ query , $ logicalAnd );
470+ $ logicalAnd = $ this ->extendLogicalAndWithFilterConstraintsForSite ($ filter , $ query , $ logicalAnd );
471+ $ query ->matching ($ query ->logicalAnd (...$ logicalAnd ));
472+ $ query ->setOrderings (['crdate ' => QueryInterface::ORDER_DESCENDING ]);
473+ $ query ->setLimit ($ row ['count ' ]);
474+ $ row ['children ' ] = $ query ->execute ();
475+ return $ row ;
476+ }
477+
366478 /**
367479 * Get social media amount of referrers from link shortener (part of luxenterprise)
368480 *
@@ -380,6 +492,25 @@ protected function getAmountOfSocialMediaReferrersFromShorteners(array $result,
380492 return $ result ;
381493 }
382494
495+ public function getReferrerCategoryAmounts (FilterDto $ filter ): array
496+ {
497+ $ readable = GeneralUtility::makeInstance (Readable::class);
498+ $ amounts = [];
499+ foreach ($ readable ->getAllKeysWithDomainsForQuery () as $ key => $ valueRegEx ) {
500+ $ connection = DatabaseUtility::getConnectionForTable (Pagevisit::TABLE_NAME );
501+ $ sql = 'SELECT count(*) as count ' ;
502+ $ sql .= ' from ' . Pagevisit::TABLE_NAME . ' pv ' ;
503+ $ sql .= ' where referrer RLIKE \'^https://( ' . $ valueRegEx . ')/ \'' ;
504+ $ sql .= $ this ->extendWhereClauseWithFilterSearchterms ($ filter , 'pv ' , 'referrer ' );
505+ $ sql .= $ this ->extendWhereClauseWithFilterTime ($ filter );
506+ $ sql .= $ this ->extendWhereClauseWithFilterSite ($ filter );
507+ $ sql .= $ this ->extendWhereClauseWithFilterDomain ($ filter );
508+ $ amounts [$ key ] = $ connection ->executeQuery ($ sql )->fetchOne ();
509+ }
510+ arsort ($ amounts );
511+ return $ amounts ;
512+ }
513+
383514 /**
384515 * @param FilterDto $filter
385516 * @return array
@@ -509,4 +640,33 @@ public function compareAmountPerPage(int $pageIdentifier, FilterDto $filter1, Fi
509640 $ amount2 = $ this ->findAmountPerPage ($ pageIdentifier , $ filter2 );
510641 return $ amount1 - $ amount2 ;
511642 }
643+
644+ /**
645+ * Domain is misleading here - this function is reused to search for given domains by a category
646+ *
647+ * @param FilterDto $filter
648+ * @param string $table
649+ * @return string
650+ */
651+ protected function extendWhereClauseWithFilterDomain (FilterDto $ filter , string $ table = '' ): string
652+ {
653+ $ sql = '' ;
654+ if ($ filter ->isDomainSet ()) {
655+ $ field = 'referrer ' ;
656+ if ($ table !== '' ) {
657+ $ field = $ table . '. ' . $ field ;
658+ }
659+ $ readable = GeneralUtility::makeInstance (Readable::class);
660+ $ domains = $ readable ->getDomainsFromCategory ($ filter ->getDomain ());
661+
662+ if ($ domains !== []) {
663+ $ conditions = [];
664+ foreach ($ domains as $ domain ) {
665+ $ conditions [] = $ field . ' LIKE "https:// ' . $ domain . '%" ' ;
666+ }
667+ $ sql .= ' and ( ' . implode (' OR ' , $ conditions ) . ') ' ;
668+ }
669+ }
670+ return $ sql ;
671+ }
512672}
0 commit comments