@@ -363,6 +363,101 @@ public function getAmountOfSocialMediaReferrers(FilterDto $filter): array
363363 return $ result ;
364364 }
365365
366+ /**
367+ * [
368+ * [
369+ * 'referrer_domain' => 'x.com',
370+ * 'count' => 123,
371+ * 'identified_count' => 34,
372+ * 'children' => [QueryResult],
373+ * ],
374+ * [
375+ * 'referrer_domain' => 'openai.com',
376+ * 'count' => 25,
377+ * 'identified_count' => 12,
378+ * 'children' => [QueryResult],
379+ * ],
380+ * ]
381+ *
382+ * @param FilterDto $filter
383+ * @return array
384+ * @throws ExceptionDbal
385+ */
386+ public function getReferrers (FilterDto $ filter ): array
387+ {
388+ $ readable = GeneralUtility::makeInstance (Readable::class);
389+ $ grouped = [];
390+ foreach ($ this ->getAmountOfReferrerDomains ($ filter ) as $ row ) {
391+ $ row ['identified_count ' ] = $ this ->extendRowIdentified ($ row , $ filter );
392+ $ row = $ this ->extendRowWithChildren ($ row , $ filter );
393+ $ key = $ readable ->getKeyFromHost ($ row ['referrer_domain ' ]) ?: 'other ' ;
394+ $ grouped [$ key ][] = $ row ;
395+ }
396+ return $ grouped ;
397+ }
398+
399+ /**
400+ * [
401+ * [
402+ * 'referrer_domain' => 'x.com',
403+ * 'count' => 123,
404+ * ],
405+ * [
406+ * 'referrer_domain' => 'openai.com',
407+ * 'count' => 25,
408+ * ],
409+ * ]
410+ *
411+ * @param FilterDto $filter
412+ * @return array
413+ * @throws ExceptionDbal
414+ */
415+ protected function getAmountOfReferrerDomains (FilterDto $ filter ): array
416+ {
417+ $ connection = DatabaseUtility::getConnectionForTable (Pagevisit::TABLE_NAME );
418+ $ sql = 'select substring_index(substring_index(referrer, \':// \', -1), \'/ \', 1) referrer_domain, count(*) count ' ;
419+ $ sql .= ' from ' . Pagevisit::TABLE_NAME . ' pv ' ;
420+ $ sql .= ' where pv.deleted = 0 and pv.hidden = 0 and pv.referrer != \'\'' ;
421+ $ sql .= $ this ->extendWhereClauseWithFilterSearchterms ($ filter , 'pv ' , 'referrer ' );
422+ $ sql .= $ this ->extendWhereClauseWithFilterTime ($ filter );
423+ $ sql .= $ this ->extendWhereClauseWithFilterSite ($ filter );
424+ $ sql .= $ this ->extendWhereClauseWithFilterDomain ($ filter );
425+ $ sql .= ' group by referrer_domain order by count desc; ' ;
426+ $ rows = $ connection ->executeQuery ($ sql )->fetchAllAssociative ();
427+ return $ rows ;
428+ }
429+
430+ protected function extendRowIdentified (array $ row , FilterDto $ filter ): int
431+ {
432+ $ connection = DatabaseUtility::getConnectionForTable (Pagevisit::TABLE_NAME );
433+ $ sql = 'select count(*) identified_count ' ;
434+ $ sql .= ' from tx_lux_domain_model_visitor v ' ;
435+ $ sql .= ' where v.identified = 1 ' ;
436+ $ sql .= ' and exists ( ' ;
437+ $ sql .= 'select 1 ' ;
438+ $ sql .= ' from tx_lux_domain_model_pagevisit pv ' ;
439+ $ sql .= ' where pv.visitor = v.uid and pv.referrer like "https:// ' . $ row ['referrer_domain ' ] . '%" ' ;
440+ $ sql .= $ this ->extendWhereClauseWithFilterTime ($ filter , true , 'pv ' );
441+ $ sql .= $ this ->extendWhereClauseWithFilterSite ($ filter , 'pv ' );
442+ $ sql .= ') ' ;
443+ return (int )$ connection ->executeQuery ($ sql )->fetchOne ();
444+ }
445+
446+ protected function extendRowWithChildren (array $ row , FilterDto $ filter ): array
447+ {
448+ $ query = $ this ->createQuery ();
449+ $ logicalAnd = [
450+ $ query ->like ('referrer ' , 'https:// ' . $ row ['referrer_domain ' ] . '% ' ),
451+ ];
452+ $ logicalAnd = $ this ->extendLogicalAndWithFilterConstraintsForCrdate ($ filter , $ query , $ logicalAnd );
453+ $ logicalAnd = $ this ->extendLogicalAndWithFilterConstraintsForSite ($ filter , $ query , $ logicalAnd );
454+ $ query ->matching ($ query ->logicalAnd (...$ logicalAnd ));
455+ $ query ->setOrderings (['crdate ' => QueryInterface::ORDER_DESCENDING ]);
456+ $ query ->setLimit ($ row ['count ' ]);
457+ $ row ['children ' ] = $ query ->execute ();
458+ return $ row ;
459+ }
460+
366461 /**
367462 * Get social media amount of referrers from link shortener (part of luxenterprise)
368463 *
@@ -380,6 +475,25 @@ protected function getAmountOfSocialMediaReferrersFromShorteners(array $result,
380475 return $ result ;
381476 }
382477
478+ public function getReferrerCategoryAmounts (FilterDto $ filter ): array
479+ {
480+ $ readable = GeneralUtility::makeInstance (Readable::class);
481+ $ amounts = [];
482+ foreach ($ readable ->getAllKeysWithDomainsForQuery () as $ key => $ valueRegEx ) {
483+ $ connection = DatabaseUtility::getConnectionForTable (Pagevisit::TABLE_NAME );
484+ $ sql = 'SELECT count(*) as count ' ;
485+ $ sql .= ' from ' . Pagevisit::TABLE_NAME . ' pv ' ;
486+ $ sql .= ' where referrer RLIKE \'^https://( ' . $ valueRegEx . ')/ \'' ;
487+ $ sql .= $ this ->extendWhereClauseWithFilterSearchterms ($ filter , 'pv ' , 'referrer ' );
488+ $ sql .= $ this ->extendWhereClauseWithFilterTime ($ filter );
489+ $ sql .= $ this ->extendWhereClauseWithFilterSite ($ filter );
490+ $ sql .= $ this ->extendWhereClauseWithFilterDomain ($ filter );
491+ $ amounts [$ key ] = $ connection ->executeQuery ($ sql )->fetchOne ();
492+ }
493+ arsort ($ amounts );
494+ return $ amounts ;
495+ }
496+
383497 /**
384498 * @param FilterDto $filter
385499 * @return array
@@ -509,4 +623,33 @@ public function compareAmountPerPage(int $pageIdentifier, FilterDto $filter1, Fi
509623 $ amount2 = $ this ->findAmountPerPage ($ pageIdentifier , $ filter2 );
510624 return $ amount1 - $ amount2 ;
511625 }
626+
627+ /**
628+ * Domain is misleading here - this function is reused to search for given domains by a category
629+ *
630+ * @param FilterDto $filter
631+ * @param string $table
632+ * @return string
633+ */
634+ protected function extendWhereClauseWithFilterDomain (FilterDto $ filter , string $ table = '' ): string
635+ {
636+ $ sql = '' ;
637+ if ($ filter ->isDomainSet ()) {
638+ $ field = 'referrer ' ;
639+ if ($ table !== '' ) {
640+ $ field = $ table . '. ' . $ field ;
641+ }
642+ $ readable = GeneralUtility::makeInstance (Readable::class);
643+ $ domains = $ readable ->getDomainsFromCategory ($ filter ->getDomain ());
644+
645+ if ($ domains !== []) {
646+ $ conditions = [];
647+ foreach ($ domains as $ domain ) {
648+ $ conditions [] = $ field . ' LIKE "https:// ' . $ domain . '%" ' ;
649+ }
650+ $ sql .= ' and ( ' . implode (' OR ' , $ conditions ) . ') ' ;
651+ }
652+ }
653+ return $ sql ;
654+ }
512655}
0 commit comments