Get information about external referrers (i.e. search engines, websites & campaigns)

@param $idaction
@param $actionType
@param Logaggregator $logAggregator
@param $limitBeforeGrouping
@return DataTable
764056
protected function queryExternalReferrers($idaction, $actionType, $logAggregator, $limitBeforeGrouping = false)
    {
        $rankingQuery = new RankingQuery($limitBeforeGrouping ? $limitBeforeGrouping : $this->limitBeforeGrouping);

        // we generate a single column that contains the interesting data for each referrer.
        // the reason we cannot group by referer_* becomes clear when we look at search engine keywords.
        // referer_url contains the url from the search engine, referer_keyword the keyword we want to
        // group by. when we group by both, we don't get a single column for the keyword but instead
        // one column per keyword + search engine url. this way, we could not get the top keywords using
        // the ranking query.
        $dimensions = array('referrer_data', 'referer_type');
        $rankingQuery->addLabelColumn('referrer_data');
        $selects = array(
            'CASE log_visit.referer_type
				WHEN ' . Common::REFERRER_TYPE_DIRECT_ENTRY . ' THEN \'\'
				WHEN ' . Common::REFERRER_TYPE_SEARCH_ENGINE . ' THEN log_visit.referer_keyword
				WHEN ' . Common::REFERRER_TYPE_SOCIAL_NETWORK . ' THEN log_visit.referer_name
				WHEN ' . Common::REFERRER_TYPE_WEBSITE . ' THEN log_visit.referer_url
				WHEN ' . Common::REFERRER_TYPE_CAMPAIGN . ' THEN CONCAT(log_visit.referer_name, \' \', log_visit.referer_keyword)
			END AS `referrer_data`');

        // get one limited group per referrer type
        $rankingQuery->partitionResultIntoMultipleGroups('referer_type', array(
                                                                              Common::REFERRER_TYPE_DIRECT_ENTRY,
                                                                              Common::REFERRER_TYPE_SEARCH_ENGINE,
                                                                              Common::REFERRER_TYPE_SOCIAL_NETWORK,
                                                                              Common::REFERRER_TYPE_WEBSITE,
                                                                              Common::REFERRER_TYPE_CAMPAIGN
                                                                         ));

        $type = $this->getColumnTypeSuffix($actionType);
        $where = 'visit_entry_idaction_' . $type . ' = ' . intval($idaction);

        $metrics = array(Metrics::INDEX_NB_VISITS);
        $data = $logAggregator->queryVisitsByDimension($dimensions, $where, $selects, $metrics, $rankingQuery);

        $referrerData = array();
        $referrerSubData = array();

        foreach ($data as $referrerType => &$subData) {
            $referrerData[$referrerType] = array(Metrics::INDEX_NB_VISITS => 0);
            if ($referrerType != Common::REFERRER_TYPE_DIRECT_ENTRY) {
                $referrerSubData[$referrerType] = array();
            }

            foreach ($subData as &$row) {
                if ($referrerType == Common::REFERRER_TYPE_SEARCH_ENGINE && empty($row['referrer_data'])) {
                    $row['referrer_data'] = \Piwik\Plugins\Referrers\API::LABEL_KEYWORD_NOT_DEFINED;
                }

                $referrerData[$referrerType][Metrics::INDEX_NB_VISITS] += $row[Metrics::INDEX_NB_VISITS];

                $label = $row['referrer_data'];
                if ($label) {
                    $referrerSubData[$referrerType][$label] = array(
                        Metrics::INDEX_NB_VISITS => $row[Metrics::INDEX_NB_VISITS]
                    );
                }
            }
        }

        $array = new DataArray($referrerData, $referrerSubData);
        return $array->asDataTable();
    }
1075794
protected function dispatchChildEvent(Website $website, $childType, $action, $details = [])
    {
        $event = new LogResourceChildUpdateEvent(
            $website->getResourceNode(),
            $childType,
            $action,
            $details
        );

        return $this->dispatch($event);
    }
674643
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        if (!$targetEntity->reflClass->implementsInterface(\WellCommerce\Bundle\LocaleBundle\Entity\LocaleAwareInterface::class)) {
            return "";
        }

        return $targetTableAlias . '.locale = ' . $this->getParameter('locale');
    }
761608
public static function filterReferences($content, $callback, $limit = -1, &$count = 0)
    {
        $content = static::filterUrls($content, $callback, $limit, $count);
        $content = static::filterImports($content, $callback, $limit, $count, false);
        $content = static::filterIEFilters($content, $callback, $limit, $count);

        return $content;
    }
185335
public List<ComponentDto> selectByQuery(DbSession session, ComponentQuery query, int offset, int limit) {
    return selectByQueryImpl(session, null, query, offset, limit);
  }
698706
public static function activiteTopTenSiteView(Application $app, $dmin, $dmax, $sbas_id, $list_coll_id)
    {
        $databox = $app->findDataboxById($sbas_id);
        $conn = $databox->get_connection();
        $result = [];
        $datefilter = module_report_sqlfilter::constructDateFilter($dmin, $dmax);

        $params = [];
        $params = array_merge($params, $datefilter['params']);


/*
        $sql = "
            SELECT tt.referrer, SUM(1) AS nb_view
            FROM (
                SELECT DISTINCT(log_date.id), referrer
                FROM (log_view)
                    INNER JOIN log AS log_date FORCE INDEX (date_site) ON (log_view.log_id = log_date.id)
                    INNER JOIN log_colls FORCE INDEX (couple) ON (log_date.id = log_colls.log_id)
                WHERE " . $datefilter['sql'] . "" .
            (('' !== $collfilter['sql']) ?  " AND (" . $collfilter['sql'] . ")" : '')
            . ") AS tt
            GROUP BY referrer
            ORDER BY nb_view DESC ";
*/
        $sql = "SELECT tt.referrer, SUM(1) AS nb_view\n"
            . " FROM (\n"
            . "     SELECT DISTINCT(log_date.id), referrer\n"
            . "     FROM (log_view)\n"
            . "        INNER JOIN log AS log_date FORCE INDEX (date_site) ON (log_view.log_id = log_date.id)\n"
            . "     WHERE " . $datefilter['sql']
            . ") AS tt\n"
            . " GROUP BY referrer\n"
            . " ORDER BY nb_view DESC ";

// no_file_put_contents("/tmp/report.txt", sprintf("%s (%s)\n%s\n\n", __FILE__, __LINE__, $sql), FILE_APPEND);

        $stmt = $conn->prepare($sql);
        $stmt->execute($params);
        $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $stmt->closeCursor();

        foreach ($rs as $row) {
            ($row['referrer'] != 'NO REFERRER') ?
                    $host = parent::getHost($row['referrer']) :
                    $host = 'NO REFERRER';
            if ( ! isset($result[$host]['nb']))
                $result[$host]['nb'] = 0;
            if ( ! isset($result[$host]['lib']))
                $result[$host]['lib'] = $host;
            $result[$host]['nb']+= ( (int) $row['nb_view']);
            $result[$host]['id'] = "false";
        }

        return $result;
    }
537828
public function allowedActions($limitToClass = null)
    {
        $actions = parent::allowedActions($limitToClass);
        $defaultMapping = $this->stat('allowed_actions');
        $siteConfig = SiteConfig::current_site_config();
        foreach ($defaultMapping as $map) {
            $key = ucfirst($map . 'Action');
            if ($siteConfig->$key) {
                self::$allowed_actions[] = $siteConfig->$key;
            }
        }
        if (is_array($actions)) {
            return array_merge($actions, self::$allowed_actions);
        }

        return self::$allowed_actions;
    }
1150088
private static function getCachedActionRow($idAction, $actionType)
    {
        $cacheLabel = self::getCachedActionRowKey($idAction, $actionType);

        if (!isset(self::$cacheParsedAction[$cacheLabel])) {
            // This can happen when
            // - We select an entry page ID that was only seen yesterday, so wasn't selected in the first query
            // - We count time spent on a page, when this page was only seen yesterday
            return false;
        }

        return self::$cacheParsedAction[$cacheLabel];
    }
500403
public function retrieveBackendOperations($transaction, $limit = false)
    {
        $parentTransaction = $this->getTransactionByTransactionId(
            $transaction->getParentTransactionId(),
            $transaction::NAME != MaestroTransaction::NAME ? $transaction::NAME : CreditCardTransaction::NAME
        );
        if (!is_null($parentTransaction) && (
            !$limit ||
            !$this->isFinal($parentTransaction[Transaction::PARAM_PAYMENT][Transaction::PARAM_TRANSACTION_TYPE])
            )) {
            $transaction->setParentTransactionType(
                $parentTransaction[Transaction::PARAM_PAYMENT][Transaction::PARAM_TRANSACTION_TYPE]
            );
        } else {
            return false;
        }

        $operations = false;
        if ($transaction->getBackendOperationForPay() && (!$limit ||
            $transaction->getParentTransactionType() == Transaction::TYPE_AUTHORIZATION)) {
            $operations[Operation::PAY] = self::CAPTURE_BUTTON_TEXT;
        }
        if ($transaction->getBackendOperationForCancel()) {
            if (stristr($transaction->getBackendOperationForCancel(), 'refund')) {
                $operations[Operation::CANCEL] = self::REFUND_BUTTON_TEXT;
            } else {
                $operations[Operation::CANCEL] = self::CANCEL_BUTTON_TEXT;
            }
        }
        if ($transaction->getBackendOperationForRefund()) {
            $operations[Operation::REFUND] = self::REFUND_BUTTON_TEXT;
        }
        if ($transaction->getBackendOperationForCredit() || $transaction->getSepaCredit()) {
            if ($limit && $transaction->getSepaCredit()) {
                $operations[Operation::CREDIT] = self::REFUND_BUTTON_TEXT;
            } elseif (!$limit) {
                $operations[Operation::CREDIT] = self::CREDIT_BUTTON_TEXT;
            }
        }

        return $operations;
    }
1131263
public function getLookupResults($type, $filter = '', $limit = 10)
    {
        return $this->getRepository()->getValueList($type, $filter, $limit);
    }
149816
public function getAggregation(
        ConnectionInterface $connection,
        $addedAggregateNames = []
    ) {
        if (!$this->annotations) {
            return [];
        }
        $hasLimit = ($this->offset || $this->limit);
        $hasExistingAnnotations = false;
        foreach ($this->annotations as $alias => $annotation) {
            $hasExistingAnnotations = ($hasExistingAnnotations ||
                !in_array($alias, $addedAggregateNames));
        }

        // we have of $hasExistingAnnotations  we need to make the core query a
        // subquery and aggregate over it.
        if ($hasExistingAnnotations || $hasLimit || $this->distinct ||
            is_array($this->groupBy)) {
            $outQuery = new AggregateQuery($this->model);
            $innerQuery = $this->deepClone();

            $innerQuery->selectRelected = false;

            if (!$hasLimit && !$this->distinctFields) {
                // Queries with distinct_fields need ordering and when a limit
                // is applied we must take the slice from the ordered query.
                // Otherwise no need for ordering, so clear.
                $innerQuery->clearOrdering(true);
            }

            if (!$innerQuery->distinct) {
                // if we are using default columns and we already have
                // aggregate annotations existing
                // then we must make sure the inner
                // query is grouped by the main model's primary key. However,
                // clearing the select clause can alter results if distinct is
                // used.
                if ($innerQuery->useDefaultCols && $hasExistingAnnotations) {
                    $innerQuery->groupBy = [
                        $this->getMeta()->primaryKey
                            ->getColExpression($innerQuery->getInitialAlias()),
                    ];
                }
                $innerQuery->useDefaultCols = false;
            }

            // add annotations to the outerquery todo
            foreach ($innerQuery->annotations as $alias => $annotation) {
                if ($annotation->isSummarize()) {
                    $outQuery->annotations[$alias] = $annotation;
                    unset($innerQuery->annotations[$alias]);
                }
            }

            if ($innerQuery->select == [] && !$innerQuery->useDefaultCols) {
                $innerQuery->select = [
                    $this->getMeta()->primaryKey->getColExpression(
                        $innerQuery->getInitialAlias()
                    ),
                ];
            }

            $outQuery->addSubQuery($innerQuery, $connection);
        } else {
            $outQuery = $this;
            $outQuery->select = [];
            $outQuery->useDefaultCols = false;
        }
        $outQuery->clearOrdering(true);
        $outQuery->clearLimits();
        $outQuery->selectRelected = false;

        $results = $outQuery->getSqlCompiler($connection)
            ->executeSql()->fetch();

        $result = [];
        foreach (array_combine(
                     array_keys($outQuery->annotations),
                     array_values($results)
                 ) as $key => $item) {
            $result[$key] = $item;
        }

        return $result;
    }
338712
public function beforeGetFinalisedQuery(DataQuery $dataQuery, $queriedColumns = [], SQLSelect $sqlSelect)
    {
        // Get metadata and SQL from join table
        $hasManyRelation = $this->getParentRelationship($dataQuery);
        $joinTableSQLSelect = $hasManyRelation->dataQuery()->query();
        $joinTableSQL = $joinTableSQLSelect->sql($joinTableParameters);
        $joinTableColumns = array_keys($joinTableSQLSelect->getSelect()); // Get aliases (keys) only
        $joinTableAlias = $this->getJoinAlias();

        // Get fields to join on
        $localKey = $this->getLocalKey();
        $schema = DataObject::getSchema();
        $baseTable = $schema->baseDataClass($dataQuery->dataClass());
        $childField = $schema->sqlColumnForField($baseTable, 'ID');

        // Add select fields
        foreach ($joinTableColumns as $joinTableColumn) {
            $sqlSelect->selectField(
                "\"{$joinTableAlias}\".\"{$joinTableColumn}\"",
                "{$joinTableAlias}_{$joinTableColumn}"
            );
        }

        // Apply join and record sql for later insertion (at end of replacements)
        // By using a string placeholder $$_SUBQUERY_$$ we protect field/table rewrites from interfering twice
        // on the already-finalised inner list
        $sqlSelect->addInnerJoin(
            '(SELECT $$_SUBQUERY_$$)',
            "\"{$joinTableAlias}\".\"{$localKey}\" = {$childField}",
            $joinTableAlias,
            20,
            $joinTableParameters
        );
        $dataQuery->setQueryParam('Foreign.JoinTableSQL', $joinTableSQL);

        // After this join, and prior to afterGetFinalisedQuery, $sqlSelect will be populated with the
        // necessary sql rewrites (versioned, etc) that effect the base table.
        // By using a placeholder for the subquery we can protect the subquery (already rewritten)
        // from being re-written a second time. However we DO want the join predicate (above) to be rewritten.
        // See http://php.net/manual/en/function.str-replace.php#refsect1-function.str-replace-notes
        // for the reason we only add the final substitution at the end of getFinalisedQuery()
    }
629762
private EntityLinks getPluginFor(Class<?> type) {

		return delegates.getPluginFor(type) //
				.orElseThrow(() -> new IllegalArgumentException(
						String.format("Cannot determine link for %s! No EntityLinks instance found supporting the domain type!",
								type.getName())));
	}
207132
private function decidePaginatorFormat(
        DoctrinePaginator $paginator,
        string $parameterType,
        int $limitPerPage,
        int $page
    ) {
        if (Pagerfanta::class === $parameterType) {
            $paginator = new Pagerfanta(new DoctrineORMAdapter($paginator->getQuery()));
            $paginator->setMaxPerPage($limitPerPage);
            $paginator->setCurrentPage($page);
        }
        if (PaginationInterface::class === $parameterType) {
            $knp = new KnpPaginator();
            $paginator = $knp->paginate($paginator->getQuery(), $page, $limitPerPage);
        }

        return $paginator;
    }
962441
public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerActionName)
    {
        $this->loadIdsFromLogActionTable();

        $visitAction = array(
            'idvisit'           => $visitor->getVisitorColumn('idvisit'),
            'idsite'            => $this->request->getIdSite(),
            'idvisitor'         => $visitor->getVisitorColumn('idvisitor'),
            'idaction_url'      => $this->getIdActionUrl(),
            'idaction_url_ref'  => $idReferrerActionUrl,
            'idaction_name_ref' => $idReferrerActionName
        );

        /** @var ActionDimension[] $dimensions */
        $dimensions = ActionDimension::getAllDimensions();

        foreach ($dimensions as $dimension) {
            $value = $dimension->onNewAction($this->request, $visitor, $this);

            if ($value !== false) {
                if (is_float($value)) {
                    $value = Common::forceDotAsSeparatorForDecimalPoint($value);
                }

                $visitAction[$dimension->getColumnName()] = $value;
            }
        }

        // idaction_name is NULLable. we only set it when applicable
        if ($this->isActionHasActionName()) {
            $visitAction['idaction_name'] = (int)$this->getIdActionName();
        }

        foreach ($this->actionIdsCached as $field => $idAction) {
            $visitAction[$field] = ($idAction === false) ? 0 : $idAction;
        }

        $customValue = $this->getCustomFloatValue();
        if ($customValue !== false && $customValue !== null && $customValue !== '') {
            $visitAction[self::DB_COLUMN_CUSTOM_FLOAT] = Common::forceDotAsSeparatorForDecimalPoint($customValue);
        }

        $visitAction = array_merge($visitAction, $this->customFields);

        $this->idLinkVisitAction = $this->getModel()->createAction($visitAction);

        $visitAction['idlink_va'] = $this->idLinkVisitAction;

        Common::printDebug("Inserted new action:");
        $visitActionDebug = $visitAction;
        $visitActionDebug['idvisitor'] = bin2hex($visitActionDebug['idvisitor']);
        Common::printDebug($visitActionDebug);
    }
308838
public function GetForRole($RoleID = '', $Offset = '0', $Limit = '50') {
      if (!is_array($RoleID))
         $RoleID = array($RoleID);
      
      $Offset = is_numeric($Offset) ? $Offset : 0;
      if ($Offset < 0)
         $Offset = 0;

      $Limit = is_numeric($Limit) ? $Limit : 0;
      if ($Limit < 0)
         $Limit = 0;
      
      $this->ActivityQuery();
      $Result = $this->SQL
         ->Join('UserRole ur', 'a.ActivityUserID = ur.UserID')
         ->WhereIn('ur.RoleID', $RoleID)
         ->Where('t.Public', '1')
         ->OrderBy('a.DateInserted', 'desc')
         ->Limit($Limit, $Offset)
         ->Get();
         
      $this->EventArguments['Data'] =& $Result;
      $this->FireEvent('AfterGet');
      
      return $Result;
   }
1093496
public function search(SearchQuery $searchQuery, $returnType = BaseSearch::SEARCH_RESULT)
    {
        $response = static::newResponse($returnType);

        /** @var yii\db\ActiveRecord|SearchableEntity $mainEntityClassName */
        $mainEntityClassName = $searchQuery->mainEntityClassName;

        $activeQuery = $mainEntityClassName::find();

        $success = false;

        // combine query
        $e = new SearchEvent(
            $searchQuery,
            $returnType,
            $response,
            [
                'params' => [
                    'activeQuery' => &$activeQuery,
                ],
            ]
        );
        $this->trigger(self::EVENT_BEFORE_PAGINATION, $e);

        // Pagination
        $pages = $searchQuery->getPagination();
        if ($returnType !== BaseSearch::SEARCH_COUNT && $pages !== null) {
            if ($pages->totalCount === 0) {
                $pages->totalCount = (int) $activeQuery->count();
            }
            $searchQuery->limit = $pages->limit;
            $searchQuery->offset = $pages->offset;
        }

        $e = new SearchEvent(
            $searchQuery,
            $returnType,
            $response,
            [
                'params' => [
                    'activeQuery' => &$activeQuery,
                    'pages' => $pages,
                ],
            ]
        );
        $this->trigger(self::EVENT_AFTER_PAGINATION, $e);

        $response->searchQuery = $searchQuery;

        switch ($returnType) {
            case BaseSearch::SEARCH_COUNT:
                /** @var CountResponse $response */
                $response->count = (int) $activeQuery->count();
                $success = true;
                break;
            case BaseSearch::SEARCH_QUERY:
                /** @var QueryResponse $response */
                $response->query = $activeQuery;
                $success = true;
                break;
            case BaseSearch::SEARCH_RESULT_ARRAY:
            case BaseSearch::SEARCH_RESULT:
                /** @var ResultResponse $response */
                // query for debug
                $response->query = $activeQuery;
                if (is_object($pages)) {
                    $response->pages = $pages;
                }
                if ($returnType === BaseSearch::SEARCH_RESULT_ARRAY) {
                    $activeQuery->asArray(true);
                }
                $response->entities = $activeQuery->all();
                $e = new SearchEvent(
                    $searchQuery,
                    $returnType,
                    $response,
                    [
                        'params' => [
                            'activeQuery' => &$activeQuery,
                        ],
                    ]
                );
                $this->trigger(self::EVENT_AFTER_FIND, $e);
                $success = true;
                break;
            case BaseSearch::SEARCH_IDS:
                /** @var IdsResponse $response */
                $activeQuery->select(
                    array_map(
                        function ($item) use ($mainEntityClassName) {
                            return $mainEntityClassName::tableName() . '.' . $item;
                        },
                        $mainEntityClassName::primaryKey()
                    )
                );
                $response->ids = $activeQuery
                    ->distinct(true)
                    ->asArray(true)
                    ->column();
                $response->query = $activeQuery;
                $success = true;
                break;
        }

        return $response->end($success);
    }