2022-09-11 02:05:04 +00:00
|
|
|
<?php
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-09-11 02:05:04 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
2023-03-24 04:57:54 +00:00
|
|
|
use OCA\Memories\ClustersBackend;
|
2023-08-21 06:07:07 +00:00
|
|
|
use OCA\Memories\Exif;
|
2022-09-11 02:05:04 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2022-10-19 17:10:36 +00:00
|
|
|
use OCP\IDBConnection;
|
2022-09-11 02:05:04 +00:00
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
trait TimelineQueryDays
|
|
|
|
{
|
2023-04-14 05:49:12 +00:00
|
|
|
use TimelineQueryCTE;
|
|
|
|
|
2022-09-11 02:05:04 +00:00
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
|
|
|
/**
|
2022-10-19 17:10:36 +00:00
|
|
|
* Get the days response from the database for the timeline.
|
|
|
|
*
|
2023-03-23 21:45:56 +00:00
|
|
|
* @param bool $recursive Whether to get the days recursively
|
|
|
|
* @param bool $archive Whether to get the days only from the archive folder
|
|
|
|
* @param array $queryTransforms An array of query transforms to apply to the query
|
2022-10-06 19:24:45 +00:00
|
|
|
*
|
|
|
|
* @return array The days response
|
2022-09-11 02:05:04 +00:00
|
|
|
*/
|
|
|
|
public function getDays(
|
2022-09-24 01:54:14 +00:00
|
|
|
bool $recursive,
|
2022-09-25 23:02:26 +00:00
|
|
|
bool $archive,
|
2022-09-12 01:33:38 +00:00
|
|
|
array $queryTransforms = []
|
2022-09-12 01:06:16 +00:00
|
|
|
): array {
|
2022-09-11 02:05:04 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2022-09-12 01:03:40 +00:00
|
|
|
|
2022-09-24 01:54:14 +00:00
|
|
|
// Get all entries also present in filecache
|
|
|
|
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
|
|
|
|
$query->select('m.dayid', $count)
|
|
|
|
->from('memories', 'm')
|
2022-10-19 17:10:36 +00:00
|
|
|
;
|
2022-09-24 01:54:14 +00:00
|
|
|
|
|
|
|
// Group and sort by dayid
|
|
|
|
$query->groupBy('m.dayid')
|
2022-10-19 17:10:36 +00:00
|
|
|
->orderBy('m.dayid', 'DESC')
|
|
|
|
;
|
2022-09-11 02:05:04 +00:00
|
|
|
|
2022-09-12 01:33:38 +00:00
|
|
|
// Apply all transformations
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->applyAllTransforms($queryTransforms, $query, true);
|
2022-09-12 01:33:38 +00:00
|
|
|
|
2023-04-16 22:46:26 +00:00
|
|
|
// JOIN with filecache for existing files
|
|
|
|
$query = $this->joinFilecache($query, null, $recursive, $archive);
|
|
|
|
|
|
|
|
// FETCH all days
|
|
|
|
$rows = $this->executeQueryWithCTEs($query)->fetchAll();
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-09-11 02:05:04 +00:00
|
|
|
return $this->processDays($rows);
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:24:45 +00:00
|
|
|
/**
|
2022-10-19 17:10:36 +00:00
|
|
|
* Get the day response from the database for the timeline.
|
|
|
|
*
|
2023-10-14 23:35:06 +00:00
|
|
|
* @param int[] $dayIds The day ids to fetch
|
2023-10-14 23:03:59 +00:00
|
|
|
* @param bool $recursive If the query should be recursive
|
|
|
|
* @param bool $archive If the query should include only the archive folder
|
|
|
|
* @param bool $hidden If the query should include hidden files
|
2023-10-14 23:35:06 +00:00
|
|
|
* @param bool $monthView If the query should be in month view (dayIds are monthIds)
|
2023-10-14 23:03:59 +00:00
|
|
|
* @param array $queryTransforms The query transformations to apply
|
2022-10-19 17:10:36 +00:00
|
|
|
*
|
2022-10-06 19:24:45 +00:00
|
|
|
* @return array An array of day responses
|
2022-09-11 02:05:04 +00:00
|
|
|
*/
|
2022-09-24 01:54:14 +00:00
|
|
|
public function getDay(
|
2023-10-14 23:35:06 +00:00
|
|
|
array $dayIds,
|
2022-09-24 01:54:14 +00:00
|
|
|
bool $recursive,
|
2022-09-25 23:02:26 +00:00
|
|
|
bool $archive,
|
2023-10-03 00:22:05 +00:00
|
|
|
bool $hidden,
|
2023-10-14 23:35:06 +00:00
|
|
|
bool $monthView,
|
2022-09-24 01:54:14 +00:00
|
|
|
array $queryTransforms = []
|
|
|
|
): array {
|
2023-10-14 23:35:06 +00:00
|
|
|
// Check if we have any dayIds
|
|
|
|
if (empty($dayIds)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make new query
|
2022-09-11 02:05:04 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2022-09-24 01:54:14 +00:00
|
|
|
|
|
|
|
// Get all entries also present in filecache
|
|
|
|
$fileid = $query->createFunction('DISTINCT m.fileid');
|
|
|
|
|
|
|
|
// We don't actually use m.datetaken here, but postgres
|
|
|
|
// needs that all fields in ORDER BY are also in SELECT
|
|
|
|
// when using DISTINCT on selected fields
|
2023-03-10 17:30:56 +00:00
|
|
|
$query->select($fileid, ...TimelineQuery::TIMELINE_SELECT)
|
2022-09-24 01:54:14 +00:00
|
|
|
->from('memories', 'm')
|
2022-10-19 17:10:36 +00:00
|
|
|
;
|
2022-10-29 01:11:58 +00:00
|
|
|
|
2023-10-03 00:22:05 +00:00
|
|
|
// Add hidden field
|
|
|
|
if ($hidden) {
|
|
|
|
$query->addSelect('cte_f.hidden');
|
|
|
|
}
|
|
|
|
|
2022-10-29 01:11:58 +00:00
|
|
|
// JOIN with mimetypes to get the mimetype
|
|
|
|
$query->join('f', 'mimetypes', 'mimetypes', $query->expr()->eq('f.mimetype', 'mimetypes.id'));
|
2022-10-06 22:01:28 +00:00
|
|
|
|
2023-10-14 23:35:06 +00:00
|
|
|
if ($monthView) {
|
|
|
|
// Convert monthIds to dayIds
|
|
|
|
$query->andWhere($query->expr()->orX(...array_map(fn ($monthId) => $query->expr()->andX(
|
|
|
|
$query->expr()->gte('m.dayid', $query->createNamedParameter($monthId, IQueryBuilder::PARAM_INT)),
|
|
|
|
$query->expr()->lte('m.dayid', $query->createNamedParameter($this->monthEndDayId($monthId), IQueryBuilder::PARAM_INT))
|
|
|
|
), $dayIds)));
|
|
|
|
} else {
|
|
|
|
// Filter by list of dayIds
|
|
|
|
$query->andWhere($query->expr()->in('m.dayid', $query->createNamedParameter($dayIds, IQueryBuilder::PARAM_INT_ARRAY)));
|
|
|
|
}
|
2022-09-24 01:54:14 +00:00
|
|
|
|
|
|
|
// Add favorite field
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->addFavoriteTag($query);
|
2022-09-24 01:54:14 +00:00
|
|
|
|
|
|
|
// Group and sort by date taken
|
|
|
|
$query->orderBy('m.datetaken', 'DESC');
|
2022-10-16 19:18:31 +00:00
|
|
|
$query->addOrderBy('m.fileid', 'DESC'); // tie-breaker
|
2022-09-24 01:54:14 +00:00
|
|
|
|
|
|
|
// Apply all transformations
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->applyAllTransforms($queryTransforms, $query, false);
|
2022-09-11 02:05:04 +00:00
|
|
|
|
2023-04-16 22:46:26 +00:00
|
|
|
// JOIN with filecache for existing files
|
2023-10-03 00:22:05 +00:00
|
|
|
$query = $this->joinFilecache($query, null, $recursive, $archive, $hidden);
|
2023-04-16 22:46:26 +00:00
|
|
|
|
2023-03-25 14:58:03 +00:00
|
|
|
// FETCH all photos in this day
|
|
|
|
$day = $this->executeQueryWithCTEs($query)->fetchAll();
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2023-03-25 14:58:03 +00:00
|
|
|
// Post process the day in-place
|
|
|
|
foreach ($day as &$photo) {
|
2023-10-14 23:35:06 +00:00
|
|
|
$this->processDayPhoto($photo, $monthView);
|
2023-03-25 14:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $day;
|
2022-09-11 02:05:04 +00:00
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
public function executeQueryWithCTEs(IQueryBuilder $query, string $psql = ''): \OCP\DB\IResult
|
2023-03-23 23:58:49 +00:00
|
|
|
{
|
|
|
|
$sql = empty($psql) ? $query->getSQL() : $psql;
|
|
|
|
$params = $query->getParameters();
|
|
|
|
$types = $query->getParameterTypes();
|
|
|
|
|
|
|
|
// Get SQL
|
2023-10-03 00:22:05 +00:00
|
|
|
$CTE_SQL = \array_key_exists('cteFoldersArchive', $params)
|
2023-04-14 05:49:12 +00:00
|
|
|
? self::CTE_FOLDERS_ARCHIVE()
|
2023-10-03 00:22:05 +00:00
|
|
|
: self::CTE_FOLDERS(\array_key_exists('cteIncludeHidden', $params));
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
// Add WITH clause if needed
|
|
|
|
if (false !== strpos($sql, 'cte_folders')) {
|
|
|
|
$sql = $CTE_SQL.' '.$sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->connection->executeQuery($sql, $params, $types);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inner join with oc_filecache.
|
|
|
|
*
|
|
|
|
* @param IQueryBuilder $query Query builder
|
|
|
|
* @param TimelineRoot $root Either the top folder or null for all
|
|
|
|
* @param bool $recursive Whether to get the days recursively
|
|
|
|
* @param bool $archive Whether to get the days only from the archive folder
|
2023-10-03 00:22:05 +00:00
|
|
|
* @param bool $hidden Whether to include hidden files
|
2023-03-23 23:58:49 +00:00
|
|
|
*/
|
|
|
|
public function joinFilecache(
|
|
|
|
IQueryBuilder $query,
|
|
|
|
?TimelineRoot $root = null,
|
|
|
|
bool $recursive = true,
|
2023-10-03 00:22:05 +00:00
|
|
|
bool $archive = false,
|
|
|
|
bool $hidden = false
|
2023-04-02 01:14:37 +00:00
|
|
|
): IQueryBuilder {
|
2023-10-13 22:10:59 +00:00
|
|
|
// Get the timeline root object
|
2023-10-13 22:28:06 +00:00
|
|
|
if (null === $root) {
|
2023-10-13 22:10:59 +00:00
|
|
|
// Cache the root object. This is fast when there are
|
|
|
|
// multiple queries such as days-day preloading BUT that
|
|
|
|
// means that any subsequent requests that don't match the
|
|
|
|
// same root MUST specify a separate root this function
|
|
|
|
if (null === $this->_root) {
|
|
|
|
$this->_root = new TimelineRoot();
|
|
|
|
|
|
|
|
// Populate the root using parameters from the request
|
|
|
|
$fs = \OC::$server->get(FsManager::class);
|
2023-10-13 22:28:06 +00:00
|
|
|
$fs->populateRoot($this->_root, $recursive);
|
2023-10-13 22:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the cached / newly populated root
|
|
|
|
$root = $this->_root;
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
// Join with memories
|
|
|
|
$baseOp = $query->expr()->eq('f.fileid', 'm.fileid');
|
|
|
|
if ($root->isEmpty()) {
|
2023-10-13 22:28:06 +00:00
|
|
|
// This is illegal in most cases except albums,
|
|
|
|
// which don't have a folder associated.
|
|
|
|
if (!$this->_rootEmptyAllowed) {
|
|
|
|
throw new \Exception('No valid root folder found (.nomedia?)');
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
return $query->innerJoin('m', 'filecache', 'f', $baseOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by folder (recursive or otherwise)
|
|
|
|
$pathOp = null;
|
|
|
|
if ($recursive) {
|
|
|
|
// Join with folders CTE
|
2023-10-03 00:22:05 +00:00
|
|
|
$this->addSubfolderJoinParams($query, $root, $archive, $hidden);
|
2023-03-23 23:58:49 +00:00
|
|
|
$query->innerJoin('f', 'cte_folders', 'cte_f', $query->expr()->eq('f.parent', 'cte_f.fileid'));
|
|
|
|
} else {
|
|
|
|
// If getting non-recursively folder only check for parent
|
|
|
|
$pathOp = $query->expr()->eq('f.parent', $query->createNamedParameter($root->getOneId(), IQueryBuilder::PARAM_INT));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->innerJoin('m', 'filecache', 'f', $query->expr()->andX(
|
|
|
|
$baseOp,
|
|
|
|
$pathOp,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
/**
|
|
|
|
* Process the days response.
|
|
|
|
*
|
|
|
|
* @param array $days
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
private function processDays($days): array
|
2022-10-19 17:10:36 +00:00
|
|
|
{
|
|
|
|
foreach ($days as &$row) {
|
2022-10-19 17:15:14 +00:00
|
|
|
$row['dayid'] = (int) $row['dayid'];
|
|
|
|
$row['count'] = (int) $row['count'];
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $days;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the single day response.
|
2023-10-14 23:35:06 +00:00
|
|
|
*
|
|
|
|
* @param array $row The day response
|
|
|
|
* @param bool $monthView Whether the response is in month view
|
2022-10-19 17:10:36 +00:00
|
|
|
*/
|
2023-10-14 23:35:06 +00:00
|
|
|
private function processDayPhoto(array &$row, bool $monthView = false): void
|
2022-10-19 17:10:36 +00:00
|
|
|
{
|
2023-03-25 14:58:03 +00:00
|
|
|
// Convert field types
|
|
|
|
$row['fileid'] = (int) $row['fileid'];
|
|
|
|
$row['isvideo'] = (int) $row['isvideo'];
|
|
|
|
$row['video_duration'] = (int) $row['video_duration'];
|
|
|
|
$row['dayid'] = (int) $row['dayid'];
|
|
|
|
$row['w'] = (int) $row['w'];
|
|
|
|
$row['h'] = (int) $row['h'];
|
2023-04-16 20:47:49 +00:00
|
|
|
|
|
|
|
// Optional fields
|
2023-03-25 14:58:03 +00:00
|
|
|
if (!$row['isvideo']) {
|
|
|
|
unset($row['isvideo'], $row['video_duration']);
|
|
|
|
}
|
|
|
|
if (!$row['liveid']) {
|
|
|
|
unset($row['liveid']);
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-16 20:47:49 +00:00
|
|
|
// Favorite field, may not be present
|
|
|
|
if (\array_key_exists('categoryid', $row) && $row['categoryid']) {
|
|
|
|
$row['isfavorite'] = 1;
|
|
|
|
}
|
|
|
|
unset($row['categoryid']);
|
|
|
|
|
2023-10-03 00:54:56 +00:00
|
|
|
// Get hidden field if present
|
|
|
|
if (\array_key_exists('hidden', $row) && $row['hidden']) {
|
|
|
|
$row['ishidden'] = 1;
|
|
|
|
}
|
|
|
|
unset($row['hidden']);
|
|
|
|
|
2023-03-25 14:58:03 +00:00
|
|
|
// All cluster transformations
|
|
|
|
ClustersBackend\Manager::applyDayPostTransforms($this->request, $row);
|
|
|
|
|
2023-08-21 06:07:07 +00:00
|
|
|
// This field is only required due to the GROUP BY clause
|
|
|
|
unset($row['datetaken']);
|
|
|
|
|
|
|
|
// Calculate the AUID if we can
|
|
|
|
if (\array_key_exists('epoch', $row) && \array_key_exists('size', $row)
|
|
|
|
&& ($epoch = (int) $row['epoch']) && ($size = (int) $row['size'])) {
|
2023-10-04 19:54:24 +00:00
|
|
|
// compute AUID and discard size
|
|
|
|
// epoch is used for ordering, so we keep it
|
2023-08-21 06:07:07 +00:00
|
|
|
$row['auid'] = Exif::getAUID($epoch, $size);
|
2023-10-04 19:54:24 +00:00
|
|
|
unset($row['size']);
|
2023-05-09 04:30:45 +00:00
|
|
|
}
|
2023-10-14 23:35:06 +00:00
|
|
|
|
|
|
|
// Convert dayId to monthId if needed
|
|
|
|
if ($monthView) {
|
|
|
|
$row['dayid'] = $this->dayIdToMonthId($row['dayid']);
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:29:32 +00:00
|
|
|
/**
|
2022-10-26 17:06:45 +00:00
|
|
|
* Get all folders inside a top folder.
|
2022-10-26 16:29:32 +00:00
|
|
|
*/
|
2022-10-27 19:54:51 +00:00
|
|
|
private function addSubfolderJoinParams(
|
2022-10-27 16:19:25 +00:00
|
|
|
IQueryBuilder &$query,
|
2022-11-16 07:45:01 +00:00
|
|
|
TimelineRoot &$root,
|
2023-10-03 00:22:05 +00:00
|
|
|
bool $archive,
|
|
|
|
bool $hidden
|
2023-10-14 08:25:50 +00:00
|
|
|
): void {
|
2022-10-27 16:19:25 +00:00
|
|
|
// Add query parameters
|
2022-11-16 07:45:01 +00:00
|
|
|
$query->setParameter('topFolderIds', $root->getIds(), IQueryBuilder::PARAM_INT_ARRAY);
|
2023-10-03 00:22:05 +00:00
|
|
|
|
|
|
|
if ($archive) {
|
|
|
|
$query->setParameter('cteFoldersArchive', true, IQueryBuilder::PARAM_BOOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($hidden) {
|
|
|
|
$query->setParameter('cteIncludeHidden', true, IQueryBuilder::PARAM_BOOL);
|
|
|
|
}
|
2022-10-26 16:29:32 +00:00
|
|
|
}
|
2023-10-14 23:35:06 +00:00
|
|
|
|
|
|
|
private function monthEndDayId(int $monthId): int
|
|
|
|
{
|
|
|
|
return (int) (strtotime(date('Ymt', $monthId * 86400)) / 86400);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function dayIdToMonthId(int $dayId): int
|
|
|
|
{
|
|
|
|
static $memoize = [];
|
|
|
|
if (\array_key_exists($dayId, $memoize)) {
|
|
|
|
return $memoize[$dayId];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $memoize[$dayId] = strtotime(date('Ym', $dayId * 86400).'01') / 86400;
|
|
|
|
}
|
2022-09-11 02:05:04 +00:00
|
|
|
}
|