Fix SQL query for pageviewsperpage widget

The existing query was not fully working as intended.
Also it did not work with proper MySQL sql_mode settings.

This is fixed by building a proper query which delivers expected and
deterministic results.

We now always have latest records first.

Also there is no need to fetch the sys_language_uid, as we only fetch
localized record if only one language is allowed. That way we can just
check configuration and use the configuration to do language overlay.

Also there was no need for an join, therefore query was reduced to
necessary stuff.

Relates:  #35
This commit is contained in:
Daniel Siepmann 2020-08-12 15:54:03 +02:00
parent 27e0623794
commit 930903e39f

View file

@ -125,28 +125,22 @@ class PageviewsPerPage implements ChartDataProviderInterface
} }
$result = $this->queryBuilder $result = $this->queryBuilder
->selectLiteral('count(tx_tracking_pageview.pid) as total') ->selectLiteral(
->addSelect('pages.uid', 'tx_tracking_pageview.sys_language_uid') $this->queryBuilder->expr()->count('pid', 'total'),
$this->queryBuilder->expr()->max('uid', 'latest')
)
->addSelect('pid')
->from('tx_tracking_pageview') ->from('tx_tracking_pageview')
->leftJoin(
'tx_tracking_pageview',
'pages',
'pages',
$this->queryBuilder->expr()->eq(
'tx_tracking_pageview.pid',
$this->queryBuilder->quoteIdentifier('pages.uid')
)
)
->where(... $constraints) ->where(... $constraints)
->groupBy('tx_tracking_pageview.pid') ->groupBy('pid')
->orderBy('total', 'desc') ->orderBy('total', 'desc')
->addOrderBy('tx_tracking_pageview.uid', 'desc') ->addOrderBy('latest', 'desc')
->setMaxResults($this->maxResults) ->setMaxResults($this->maxResults)
->execute() ->execute()
->fetchAll(); ->fetchAll();
foreach ($result as $row) { foreach ($result as $row) {
$labels[] = $this->getRecordTitle($row['uid'], $row['sys_language_uid']); $labels[] = $this->getRecordTitle($row['pid']);
$data[] = $row['total']; $data[] = $row['total'];
} }
@ -156,11 +150,11 @@ class PageviewsPerPage implements ChartDataProviderInterface
]; ];
} }
private function getRecordTitle(int $uid, int $sysLanguageUid): string private function getRecordTitle(int $uid): string
{ {
$record = BackendUtility::getRecord('pages', $uid); $record = BackendUtility::getRecord('pages', $uid);
if ($sysLanguageUid > 0 && count($this->languageLimitation) === 1 && $record !== null) { if (count($this->languageLimitation) === 1 && $record !== null) {
$record = $this->pageRepository->getRecordOverlay('pages', $record, $sysLanguageUid); $record = $this->pageRepository->getRecordOverlay('pages', $record, $this->languageLimitation[0]);
} }
return strip_tags(BackendUtility::getRecordTitle('pages', $record, true)); return strip_tags(BackendUtility::getRecordTitle('pages', $record, true));