memories/lib/Db/TimelineQuery.php

129 lines
3.8 KiB
PHP
Raw Normal View History

2022-08-20 02:53:21 +00:00
<?php
2022-10-19 17:10:36 +00:00
2022-08-20 02:53:21 +00:00
declare(strict_types=1);
namespace OCA\Memories\Db;
2022-10-27 16:19:25 +00:00
use OCP\DB\QueryBuilder\IQueryBuilder;
2022-08-20 02:53:21 +00:00
use OCP\IDBConnection;
2022-10-19 17:10:36 +00:00
class TimelineQuery
{
2022-10-26 23:20:28 +00:00
use TimelineQueryAlbums;
2022-09-11 02:05:04 +00:00
use TimelineQueryDays;
2022-09-13 07:55:32 +00:00
use TimelineQueryFilters;
2022-11-07 04:48:10 +00:00
use TimelineQueryFolders;
2022-11-22 11:31:31 +00:00
use TimelineQueryLivePhoto;
2023-02-08 21:53:38 +00:00
use TimelineQueryMap;
2022-12-08 21:00:53 +00:00
use TimelineQueryPeopleFaceRecognition;
use TimelineQueryPeopleRecognize;
2023-02-06 01:15:18 +00:00
use TimelineQueryPlaces;
2023-03-10 17:30:56 +00:00
use TimelineQuerySingleItem;
2022-11-07 04:49:33 +00:00
use TimelineQueryTags;
2022-09-11 02:05:04 +00:00
2023-03-10 17:30:56 +00:00
public const TIMELINE_SELECT = [
'm.isvideo', 'm.video_duration', 'm.datetaken', 'm.dayid', 'm.w', 'm.h', 'm.liveid',
'f.etag', 'f.name AS basename', 'mimetypes.mimetype',
2023-03-10 17:30:56 +00:00
];
2022-09-09 07:31:42 +00:00
protected IDBConnection $connection;
2022-08-20 02:53:21 +00:00
2022-10-19 17:10:36 +00:00
public function __construct(IDBConnection $connection)
{
2022-09-09 07:31:42 +00:00
$this->connection = $connection;
}
2022-09-25 13:21:40 +00:00
2022-10-27 19:54:51 +00:00
public static function debugQuery(IQueryBuilder &$query, string $sql = '')
2022-10-27 16:19:25 +00:00
{
// Print the query and exit
$sql = empty($sql) ? $query->getSQL() : $sql;
$sql = str_replace('*PREFIX*', 'oc_', $sql);
2022-10-27 19:58:47 +00:00
$sql = self::replaceQueryParams($query, $sql);
2022-10-27 19:54:51 +00:00
echo "{$sql}";
exit;
}
public static function replaceQueryParams(IQueryBuilder &$query, string $sql)
{
$params = $query->getParameters();
2022-10-27 16:19:25 +00:00
foreach ($params as $key => $value) {
2023-02-05 20:39:46 +00:00
if (\is_array($value)) {
$value = implode(',', $value);
} elseif (\is_bool($value)) {
$value = $value ? '1' : '0';
} elseif (null === $value) {
$value = 'NULL';
}
$value = $query->getConnection()->getDatabasePlatform()->quoteStringLiteral($value);
$sql = str_replace(':'.$key, $value, $sql);
2022-10-27 16:19:25 +00:00
}
2022-10-27 19:54:51 +00:00
return $sql;
2022-10-27 16:19:25 +00:00
}
2022-10-28 21:26:56 +00:00
public function transformExtraFields(IQueryBuilder &$query, string $uid, array &$fields)
{
}
2022-11-07 21:25:52 +00:00
public function getInfoById(int $id, bool $basic): array
2022-10-19 17:10:36 +00:00
{
2022-09-25 13:21:40 +00:00
$qb = $this->connection->getQueryBuilder();
2022-11-07 21:25:52 +00:00
$qb->select('fileid', 'dayid', 'datetaken', 'w', 'h')
2022-09-25 13:21:40 +00:00
->from('memories')
2022-10-19 17:10:36 +00:00
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($id, \PDO::PARAM_INT)))
;
2022-09-25 13:21:40 +00:00
2022-11-07 21:25:52 +00:00
if (!$basic) {
$qb->addSelect('exif');
}
2022-09-25 13:21:40 +00:00
$result = $qb->executeQuery();
$row = $result->fetch();
$result->closeCursor();
2022-09-27 21:36:23 +00:00
$utcTs = 0;
2022-10-19 17:10:36 +00:00
2022-09-27 21:36:23 +00:00
try {
$utcDate = new \DateTime($row['datetaken'], new \DateTimeZone('UTC'));
$utcTs = $utcDate->getTimestamp();
2022-10-19 17:10:36 +00:00
} catch (\Throwable $e) {
}
2022-09-27 21:36:23 +00:00
2022-11-11 06:39:52 +00:00
$exif = [];
if (!$basic && !empty($row['exif'])) {
try {
$exif = json_decode($row['exif'], true);
} catch (\Throwable $e) {
}
}
$gisType = \OCA\Memories\Util::placesGISType();
$address = -1 === $gisType ? 'Geocoding Unconfigured' : null;
if (!$basic && $gisType > 0) {
2023-02-06 03:46:44 +00:00
$qb = $this->connection->getQueryBuilder();
$qb->select('e.name')
->from('memories_places', 'mp')
->innerJoin('mp', 'memories_planet', 'e', $qb->expr()->eq('mp.osm_id', 'e.osm_id'))
->where($qb->expr()->eq('mp.fileid', $qb->createNamedParameter($id, \PDO::PARAM_INT)))
->orderBy('e.admin_level', 'DESC')
;
$places = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
if (\count($places) > 0) {
$address = implode(', ', $places);
}
}
2022-09-25 13:21:40 +00:00
return [
2022-10-19 17:15:14 +00:00
'fileid' => (int) $row['fileid'],
'dayid' => (int) $row['dayid'],
2022-11-07 21:25:52 +00:00
'w' => (int) $row['w'],
'h' => (int) $row['h'],
2023-02-06 03:46:44 +00:00
'datetaken' => $utcTs,
'address' => $address,
2022-11-11 06:39:52 +00:00
'exif' => $exif,
2022-09-25 13:21:40 +00:00
];
}
2022-10-19 17:10:36 +00:00
}