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 TimelineQueryDay {
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the single day response
|
|
|
|
* @param array $day
|
|
|
|
*/
|
|
|
|
private function processDay(&$day) {
|
|
|
|
foreach($day as &$row) {
|
2022-09-14 22:47:26 +00:00
|
|
|
// We don't need date taken (see query builder)
|
|
|
|
unset($row['date_taken']);
|
|
|
|
|
|
|
|
// Convert field types
|
2022-09-11 02:05:04 +00:00
|
|
|
$row["fileid"] = intval($row["fileid"]);
|
|
|
|
$row["isvideo"] = intval($row["isvideo"]);
|
|
|
|
if (!$row["isvideo"]) {
|
|
|
|
unset($row["isvideo"]);
|
|
|
|
}
|
2022-09-13 18:25:24 +00:00
|
|
|
if ($row["categoryid"]) {
|
2022-09-12 03:52:07 +00:00
|
|
|
$row["isfavorite"] = 1;
|
2022-09-12 03:46:31 +00:00
|
|
|
}
|
2022-09-13 18:25:24 +00:00
|
|
|
unset($row["categoryid"]);
|
2022-09-11 02:05:04 +00:00
|
|
|
}
|
|
|
|
return $day;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the base query builder for day */
|
2022-09-12 01:03:40 +00:00
|
|
|
private function makeQueryDay(
|
2022-09-11 02:05:04 +00:00
|
|
|
IQueryBuilder &$query,
|
2022-09-12 01:03:40 +00:00
|
|
|
int $dayid,
|
2022-09-12 03:46:31 +00:00
|
|
|
string $user,
|
2022-09-12 01:03:40 +00:00
|
|
|
$whereFilecache
|
2022-09-11 02:05:04 +00:00
|
|
|
) {
|
|
|
|
// Get all entries also present in filecache
|
2022-09-14 22:36:06 +00:00
|
|
|
$fileid = $query->createFunction('DISTINCT m.fileid');
|
2022-09-14 22:47:26 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
$query->select($fileid, 'f.etag', 'm.isvideo', 'vco.categoryid', 'm.datetaken')
|
2022-09-11 02:05:04 +00:00
|
|
|
->from('memories', 'm')
|
|
|
|
->innerJoin('m', 'filecache', 'f',
|
|
|
|
$query->expr()->andX(
|
|
|
|
$query->expr()->eq('f.fileid', 'm.fileid'),
|
|
|
|
$whereFilecache
|
|
|
|
))
|
2022-09-12 01:03:40 +00:00
|
|
|
->andWhere($query->expr()->eq('m.dayid', $query->createNamedParameter($dayid, IQueryBuilder::PARAM_INT)));
|
2022-09-11 02:05:04 +00:00
|
|
|
|
2022-09-12 03:46:31 +00:00
|
|
|
// Add favorite field
|
|
|
|
$this->addFavoriteTag($query, $user);
|
|
|
|
|
2022-09-11 02:05:04 +00:00
|
|
|
// Group and sort by date taken
|
|
|
|
$query->orderBy('m.datetaken', 'DESC');
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a day response from the database for the timeline
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param string $userId
|
|
|
|
* @param int $dayId
|
|
|
|
*/
|
|
|
|
public function getDay(
|
|
|
|
IConfig &$config,
|
|
|
|
string $user,
|
2022-09-12 01:33:38 +00:00
|
|
|
int $dayId,
|
|
|
|
array $queryTransforms = []
|
2022-09-12 01:06:16 +00:00
|
|
|
): array {
|
2022-09-12 01:03:40 +00:00
|
|
|
// Filter by path starting with timeline path
|
2022-09-13 17:39:38 +00:00
|
|
|
$configPath = Exif::getPhotosPath($config, $user);
|
|
|
|
$likeHome = Exif::removeExtraSlash("files/" . $configPath . "%");
|
|
|
|
$likeExt = Exif::removeLeadingSlash(Exif::removeExtraSlash($configPath . "%"));
|
|
|
|
|
2022-09-11 02:05:04 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2022-09-13 17:39:38 +00:00
|
|
|
$this->makeQueryDay($query, $dayId, $user, $query->expr()->orX(
|
|
|
|
$query->expr()->like('f.path', $query->createNamedParameter($likeHome)),
|
|
|
|
$query->expr()->like('f.path', $query->createNamedParameter($likeExt)),
|
2022-09-12 01:03:40 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// Filter by UID
|
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->processDay($rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a day response from the database for one folder
|
|
|
|
* @param int $folderId
|
|
|
|
* @param int $dayId
|
|
|
|
*/
|
|
|
|
public function getDayFolder(
|
2022-09-12 03:46:31 +00:00
|
|
|
string $user,
|
2022-09-11 02:05:04 +00:00
|
|
|
int $folderId,
|
2022-09-12 01:06:16 +00:00
|
|
|
int $dayId
|
|
|
|
): array {
|
2022-09-11 02:05:04 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2022-09-12 03:46:31 +00:00
|
|
|
$this->makeQueryDay($query, $dayId, $user, $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->processDay($rows);
|
|
|
|
}
|
|
|
|
}
|