memories/lib/Db/TimelineQueryPlaces.php

81 lines
2.6 KiB
PHP
Raw Normal View History

2023-02-05 21:43:25 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
trait TimelineQueryPlaces
{
protected IDBConnection $connection;
public function transformPlaceFilter(IQueryBuilder &$query, string $userId, int $locationId)
{
2023-02-06 01:15:18 +00:00
$query->innerJoin('m', 'memories_places', 'mp', $query->expr()->andX(
$query->expr()->eq('mp.fileid', 'm.fileid'),
$query->expr()->eq('mp.osm_id', $query->createNamedParameter($locationId)),
2023-02-05 21:43:25 +00:00
));
}
public function getPlaces()
2023-02-05 21:43:25 +00:00
{
$query = $this->connection->getQueryBuilder();
// SELECT location name and count of photos
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
2023-02-06 01:15:18 +00:00
$query->select('e.osm_id', 'e.name', $count)->from('memories_planet', 'e');
2023-02-05 21:43:25 +00:00
// WHERE there are items with this osm_id
2023-02-06 01:15:18 +00:00
$query->innerJoin('e', 'memories_places', 'mp', $query->expr()->eq('mp.osm_id', 'e.osm_id'));
2023-02-05 21:43:25 +00:00
// WHERE these items are memories indexed photos
2023-02-06 01:15:18 +00:00
$query->innerJoin('mp', 'memories', 'm', $query->expr()->eq('m.fileid', 'mp.fileid'));
2023-02-05 21:43:25 +00:00
// WHERE these photos are in the user's requested folder recursively
$query = $this->joinFilecache($query);
2023-02-05 21:43:25 +00:00
// GROUP and ORDER by tag name
2023-02-06 06:23:44 +00:00
$query->groupBy('e.osm_id', 'e.name');
2023-02-06 01:15:18 +00:00
$query->orderBy($query->createFunction('LOWER(e.name)'), 'ASC');
$query->addOrderBy('e.osm_id'); // tie-breaker
2023-02-05 21:43:25 +00:00
// FETCH all tags
2023-02-06 01:15:18 +00:00
$cursor = $this->executeQueryWithCTEs($query);
2023-02-05 21:43:25 +00:00
$places = $cursor->fetchAll();
// Post process
foreach ($places as &$row) {
$row['osm_id'] = (int) $row['osm_id'];
$row['count'] = (int) $row['count'];
}
return $places;
}
public function getPlacePhotos(int $id, ?int $limit): array
2023-02-05 21:43:25 +00:00
{
$query = $this->connection->getQueryBuilder();
// SELECT all photos with this tag
2023-02-06 01:15:18 +00:00
$query->select('f.fileid', 'f.etag')->from('memories_places', 'mp')
->where($query->expr()->eq('mp.osm_id', $query->createNamedParameter($id)))
2023-02-05 21:43:25 +00:00
;
// WHERE these items are memories indexed photos
2023-02-06 01:15:18 +00:00
$query->innerJoin('mp', 'memories', 'm', $query->expr()->eq('m.fileid', 'mp.fileid'));
2023-02-05 21:43:25 +00:00
// WHERE these photos are in the user's requested folder recursively
$query = $this->joinFilecache($query);
2023-02-05 21:43:25 +00:00
// MAX number of photos
if (null !== $limit) {
$query->setMaxResults($limit);
}
2023-02-05 21:43:25 +00:00
// FETCH tag photos
return $this->executeQueryWithCTEs($query)->fetchAll();
2023-02-05 21:43:25 +00:00
}
}