memories/lib/Db/TimelineQueryDays.php

156 lines
5.0 KiB
PHP
Raw Normal View History

2022-09-11 02:05:04 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCP\IDBConnection;
use OCP\DB\QueryBuilder\IQueryBuilder;
2022-09-24 01:54:14 +00:00
use OCP\Files\Folder;
2022-09-11 02:05:04 +00:00
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;
}
2022-09-24 01:54:14 +00:00
/**
* Process the single day response
* @param array $day
*/
private function processDay(&$day) {
foreach($day as &$row) {
// We don't need date taken (see query builder)
unset($row['datetaken']);
2022-09-11 02:05:04 +00:00
2022-09-24 01:54:14 +00:00
// Convert field types
$row["fileid"] = intval($row["fileid"]);
$row["isvideo"] = intval($row["isvideo"]);
if (!$row["isvideo"]) {
unset($row["isvideo"]);
}
if ($row["categoryid"]) {
$row["isfavorite"] = 1;
}
unset($row["categoryid"]);
}
return $day;
}
/** Get the query for oc_filecache join */
private function getFilecacheJoinQuery(IQueryBuilder &$query, Folder &$folder, bool $recursive) {
// Subquery to get storage and path
$subQuery = $query->getConnection()->getQueryBuilder();
2022-09-25 11:30:28 +00:00
$cursor = $subQuery->select('path', 'storage')->from('filecache')->where(
2022-09-24 01:54:14 +00:00
$subQuery->expr()->eq('fileid', $subQuery->createNamedParameter($folder->getId())),
2022-09-25 11:30:28 +00:00
)->executeQuery();
$finfo = $cursor->fetch();
$cursor->closeCursor();
2022-09-24 01:54:14 +00:00
if (empty($finfo)) {
throw new \Exception("Folder not found");
}
$pathQuery = null;
if ($recursive) {
// Filter by path for recursive query
$likePath = $finfo["path"];
if (!empty($likePath)) {
$likePath .= '/';
}
$likePath = $likePath . '%';
$pathQuery = $query->expr()->like('f.path', $query->createNamedParameter($likePath));
} else {
// If getting non-recursively folder only check for parent
$pathQuery = $query->expr()->eq('f.parent', $query->createNamedParameter($folder->getId(), IQueryBuilder::PARAM_INT));
}
return $query->expr()->andX(
$query->expr()->eq('f.fileid', 'm.fileid'),
$query->expr()->in('f.storage', $query->createNamedParameter($finfo["storage"])),
$pathQuery,
);
2022-09-11 02:05:04 +00:00
}
/**
* Get the days response from the database for the timeline
* @param string $userId
*/
public function getDays(
2022-09-24 01:54:14 +00:00
Folder &$folder,
string $uid,
bool $recursive,
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')
->innerJoin('m', 'filecache', 'f', $this->getFilecacheJoinQuery($query, $folder, $recursive));
// Group and sort by dayid
$query->groupBy('m.dayid')
->orderBy('m.dayid', 'DESC');
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-24 01:54:14 +00:00
$transform($query, $uid);
2022-09-12 01:33:38 +00:00
}
2022-09-25 11:30:28 +00:00
$cursor = $query->executeQuery();
$rows = $cursor->fetchAll();
$cursor->closeCursor();
2022-09-11 02:05:04 +00:00
return $this->processDays($rows);
}
2022-09-24 01:54:14 +00:00
/**
* Get the days response from the database for the timeline
* @param string $userId
2022-09-11 02:05:04 +00:00
*/
2022-09-24 01:54:14 +00:00
public function getDay(
Folder &$folder,
string $uid,
int $dayid,
bool $recursive,
array $queryTransforms = []
): array {
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
$query->select($fileid, 'f.etag', 'm.isvideo', 'vco.categoryid', 'm.datetaken')
->from('memories', 'm')
->innerJoin('m', 'filecache', 'f', $this->getFilecacheJoinQuery($query, $folder, $recursive))
->andWhere($query->expr()->eq('m.dayid', $query->createNamedParameter($dayid, IQueryBuilder::PARAM_INT)));
// Add favorite field
$this->addFavoriteTag($query, $uid);
// Group and sort by date taken
$query->orderBy('m.datetaken', 'DESC');
// Apply all transformations
foreach ($queryTransforms as &$transform) {
$transform($query, $uid);
}
2022-09-11 02:05:04 +00:00
2022-09-25 11:30:28 +00:00
$cursor = $query->executeQuery();
$rows = $cursor->fetchAll();
$cursor->closeCursor();
2022-09-24 01:54:14 +00:00
return $this->processDay($rows);
2022-09-11 02:05:04 +00:00
}
}