memories/lib/Db/TimelineQueryDays.php

91 lines
2.7 KiB
PHP
Raw Normal View History

2022-09-11 02:05:04 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\Exif;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\DB\QueryBuilder\IQueryBuilder;
trait TimelineQueryDays {
protected IDBConnection $connection;
/**
* Process the days response
* @param array $days
*/
private function processDays(&$days) {
foreach($days as &$row) {
$row["dayid"] = intval($row["dayid"]);
$row["count"] = intval($row["count"]);
}
return $days;
}
/** Get the base query builder for days */
2022-09-12 01:03:40 +00:00
private function makeQueryDays(
2022-09-11 02:05:04 +00:00
IQueryBuilder &$query,
$whereFilecache
) {
// Get all entries also present in filecache
$query->select('m.dayid', $query->func()->count('m.fileid', 'count'))
->from('memories', 'm')
->innerJoin('m', 'filecache', 'f',
$query->expr()->andX(
$query->expr()->eq('f.fileid', 'm.fileid'),
$whereFilecache
));
// Group and sort by dayid
$query->groupBy('m.dayid')
->orderBy('m.dayid', 'DESC');
return $query;
}
/**
* Get the days response from the database for the timeline
* @param IConfig $config
* @param string $userId
*/
public function getDays(
IConfig &$config,
2022-09-12 01:33:38 +00:00
string $user,
array $queryTransforms = []
2022-09-12 01:06:16 +00:00
): array {
2022-09-11 02:05:04 +00:00
2022-09-12 01:03:40 +00:00
// Filter by path starting with timeline path
2022-09-11 02:05:04 +00:00
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$query = $this->connection->getQueryBuilder();
2022-09-12 01:03:40 +00:00
$this->makeQueryDays($query, $query->expr()->like(
'f.path', $query->createNamedParameter($path)
));
// Filter by user
2022-09-12 03:03:04 +00:00
$query->andWhere($query->expr()->eq('m.uid', $query->createNamedParameter($user)));
2022-09-11 02:05:04 +00:00
2022-09-12 01:33:38 +00:00
// Apply all transformations
foreach ($queryTransforms as &$transform) {
2022-09-12 03:03:04 +00:00
$transform($query, $user);
2022-09-12 01:33:38 +00:00
}
2022-09-11 02:05:04 +00:00
$rows = $query->executeQuery()->fetchAll();
return $this->processDays($rows);
}
/**
* Get the days response from the database for one folder
* @param int $folderId
*/
public function getDaysFolder(int $folderId) {
$query = $this->connection->getQueryBuilder();
2022-09-12 01:03:40 +00:00
$this->makeQueryDays($query, $query->expr()->orX(
2022-09-11 02:05:04 +00:00
$query->expr()->eq('f.parent', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)),
$query->expr()->eq('f.fileid', $query->createNamedParameter($folderId, IQueryBuilder::PARAM_INT)),
2022-09-12 01:03:40 +00:00
));
2022-09-11 02:05:04 +00:00
$rows = $query->executeQuery()->fetchAll();
return $this->processDays($rows);
}
}