2023-02-05 21:43:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
namespace OCA\Memories\ClustersBackend;
|
2023-02-05 21:43:25 +00:00
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
use OCA\Memories\Db\TimelineQuery;
|
2023-03-23 20:49:26 +00:00
|
|
|
use OCA\Memories\Util;
|
2023-03-23 23:58:49 +00:00
|
|
|
use OCP\IRequest;
|
2023-03-23 18:08:36 +00:00
|
|
|
|
|
|
|
class PlacesBackend extends Backend
|
2023-02-05 21:43:25 +00:00
|
|
|
{
|
2023-10-15 01:51:17 +00:00
|
|
|
public function __construct(
|
|
|
|
protected TimelineQuery $tq,
|
|
|
|
protected IRequest $request,
|
|
|
|
) {}
|
2023-03-23 18:08:36 +00:00
|
|
|
|
2023-03-24 11:17:54 +00:00
|
|
|
public static function appName(): string
|
2023-02-05 21:43:25 +00:00
|
|
|
{
|
2023-03-22 19:54:03 +00:00
|
|
|
return 'Places';
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 11:17:54 +00:00
|
|
|
public static function clusterType(): string
|
|
|
|
{
|
|
|
|
return 'places';
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function isEnabled(): bool
|
2023-02-05 21:43:25 +00:00
|
|
|
{
|
2023-03-23 20:49:26 +00:00
|
|
|
return Util::placesGISType() > 0;
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
2023-02-05 21:43:25 +00:00
|
|
|
|
2023-03-24 00:19:05 +00:00
|
|
|
public function transformDayQuery(&$query, bool $aggregate): void
|
2023-03-23 23:58:49 +00:00
|
|
|
{
|
|
|
|
$locationId = (int) $this->request->getParam('places');
|
|
|
|
|
|
|
|
$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-08-04 02:56:32 +00:00
|
|
|
public function getClustersInternal(int $fileid = 0): array
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-08-04 01:56:51 +00:00
|
|
|
if ($fileid) {
|
|
|
|
throw new \Exception('PlacesBackend: fileid filter not implemented');
|
|
|
|
}
|
|
|
|
|
2023-05-30 06:12:49 +00:00
|
|
|
$inside = (int) $this->request->getParam('inside', 0);
|
|
|
|
$marked = (int) $this->request->getParam('mark', 1);
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
$query = $this->tq->getBuilder();
|
|
|
|
|
|
|
|
// SELECT location name and count of photos
|
|
|
|
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
|
2023-03-29 19:14:14 +00:00
|
|
|
$query->select('e.osm_id', $count)->from('memories_planet', 'e');
|
2023-03-23 23:58:49 +00:00
|
|
|
|
2023-03-28 00:50:10 +00:00
|
|
|
// WHERE these are not special clusters (e.g. timezone)
|
2023-04-25 05:07:58 +00:00
|
|
|
$query->where($query->expr()->gt('e.admin_level', $query->expr()->literal(0, \PDO::PARAM_INT)));
|
2023-03-28 00:50:10 +00:00
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
// WHERE there are items with this osm_id
|
2023-05-30 06:12:49 +00:00
|
|
|
$mpJoinOn = [$query->expr()->eq('mp.osm_id', 'e.osm_id')];
|
|
|
|
|
|
|
|
// AND these items are inside the requested place
|
|
|
|
if ($inside > 0) {
|
|
|
|
$sub = $this->tq->getBuilder();
|
|
|
|
$sub->select($query->expr()->literal(1))->from('memories_places', 'mp_sq')
|
|
|
|
->where($sub->expr()->eq('mp_sq.osm_id', $query->createNamedParameter($inside, \PDO::PARAM_INT)))
|
|
|
|
->andWhere($sub->expr()->eq('mp_sq.fileid', 'mp.fileid'))
|
|
|
|
;
|
|
|
|
$mpJoinOn[] = $query->createFunction("EXISTS ({$sub->getSQL()})");
|
|
|
|
|
|
|
|
// Add WHERE clauses to main query to filter out admin_levels
|
|
|
|
$sub = $this->tq->getBuilder();
|
|
|
|
$sub->select('e_sq.admin_level')
|
|
|
|
->from('memories_planet', 'e_sq')
|
|
|
|
->where($sub->expr()->eq('e_sq.osm_id', $query->createNamedParameter($inside, \PDO::PARAM_INT)))
|
|
|
|
;
|
|
|
|
$adminSql = "({$sub->getSQL()})";
|
|
|
|
$query->andWhere($query->expr()->gt('e.admin_level', $query->createFunction($adminSql)))
|
|
|
|
->andWhere($query->expr()->lte('e.admin_level', $query->createFunction("{$adminSql} + 3")))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else if we are looking for countries
|
2023-05-30 06:23:40 +00:00
|
|
|
elseif (-1 === $inside) {
|
2023-08-03 02:33:15 +00:00
|
|
|
$query->andWhere($query->expr()->eq('e.admin_level', $query->expr()->literal(2, \PDO::PARAM_INT)));
|
2023-05-30 06:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AND these items are marked (only if not inside)
|
|
|
|
elseif ($marked > 0) {
|
|
|
|
$mpJoinOn[] = $query->expr()->eq('mp.mark', $query->expr()->literal(1, \PDO::PARAM_INT));
|
|
|
|
}
|
|
|
|
|
|
|
|
// JOIN on memories_places
|
|
|
|
$query->innerJoin('e', 'memories_places', 'mp', $query->expr()->andX(...$mpJoinOn));
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
// WHERE these items are memories indexed photos
|
|
|
|
$query->innerJoin('mp', 'memories', 'm', $query->expr()->eq('m.fileid', 'mp.fileid'));
|
|
|
|
|
|
|
|
// WHERE these photos are in the user's requested folder recursively
|
|
|
|
$query = $this->tq->joinFilecache($query);
|
|
|
|
|
|
|
|
// GROUP and ORDER by tag name
|
2023-03-29 19:14:14 +00:00
|
|
|
$query->groupBy('e.osm_id');
|
|
|
|
|
|
|
|
// We use this as the subquery for the main query, where we also re-join with
|
|
|
|
// oc_memories_planet to the the names from the IDS
|
|
|
|
// If we just AGGREGATE+GROUP with the name in one query, then it can't use indexes
|
|
|
|
$sub = $query;
|
|
|
|
|
|
|
|
// Create new query and copy over parameters (and types)
|
|
|
|
$query = $this->tq->getBuilder();
|
|
|
|
$query->setParameters($sub->getParameters(), $sub->getParameterTypes());
|
|
|
|
|
|
|
|
// Create the subquery function for selecting from it
|
|
|
|
$sqf = $query->createFunction("({$sub->getSQL()})");
|
|
|
|
|
|
|
|
// SELECT osm_id
|
|
|
|
$query->select('sub.osm_id', 'sub.count', 'e.name', 'e.other_names')->from($sqf, 'sub');
|
|
|
|
|
|
|
|
// INNER JOIN back on the planet table to get the names
|
|
|
|
$query->innerJoin('sub', 'memories_planet', 'e', $query->expr()->eq('e.osm_id', 'sub.osm_id'));
|
|
|
|
|
2023-05-30 06:12:49 +00:00
|
|
|
// WHERE at least 3 photos if want marked clusters
|
|
|
|
if ($marked) {
|
|
|
|
$query->andWhere($query->expr()->gte('sub.count', $query->expr()->literal(3, \PDO::PARAM_INT)));
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:14:14 +00:00
|
|
|
// ORDER BY name and osm_id
|
2023-05-30 06:12:49 +00:00
|
|
|
$query->orderBy($query->createFunction('sub.count'), 'DESC');
|
|
|
|
$query->addOrderBy('e.name');
|
2023-03-23 23:58:49 +00:00
|
|
|
$query->addOrderBy('e.osm_id'); // tie-breaker
|
|
|
|
|
|
|
|
// FETCH all tags
|
2023-03-24 01:04:20 +00:00
|
|
|
$places = $this->tq->executeQueryWithCTEs($query)->fetchAll();
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
// Post process
|
2023-03-28 01:53:01 +00:00
|
|
|
$lang = Util::getUserLang();
|
2023-03-23 23:58:49 +00:00
|
|
|
foreach ($places as &$row) {
|
|
|
|
$row['osm_id'] = (int) $row['osm_id'];
|
2023-10-01 11:07:05 +00:00
|
|
|
$row['count'] = (int) $row['count'];
|
2023-03-28 01:53:01 +00:00
|
|
|
self::choosePlaceLang($row, $lang);
|
2023-03-23 23:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $places;
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
2023-02-05 21:43:25 +00:00
|
|
|
|
2023-03-24 11:17:54 +00:00
|
|
|
public static function getClusterId(array $cluster)
|
|
|
|
{
|
|
|
|
return $cluster['osm_id'];
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function getPhotos(string $name, ?int $limit = null): array
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-23 23:58:49 +00:00
|
|
|
$query = $this->tq->getBuilder();
|
|
|
|
|
|
|
|
// SELECT all photos with this tag
|
|
|
|
$query->select('f.fileid', 'f.etag')->from('memories_places', 'mp')
|
|
|
|
->where($query->expr()->eq('mp.osm_id', $query->createNamedParameter((int) $name)))
|
|
|
|
;
|
|
|
|
|
|
|
|
// WHERE these items are memories indexed photos
|
|
|
|
$query->innerJoin('mp', 'memories', 'm', $query->expr()->eq('m.fileid', 'mp.fileid'));
|
|
|
|
|
|
|
|
// WHERE these photos are in the user's requested folder recursively
|
|
|
|
$query = $this->tq->joinFilecache($query);
|
|
|
|
|
|
|
|
// MAX number of photos
|
|
|
|
if (null !== $limit) {
|
|
|
|
$query->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FETCH tag photos
|
|
|
|
return $this->tq->executeQueryWithCTEs($query)->fetchAll() ?: [];
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
2023-03-28 01:53:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Choose the best name for the place.
|
|
|
|
*/
|
|
|
|
public static function choosePlaceLang(array &$place, string $lang): array
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$otherNames = json_decode($place['other_names'], true);
|
|
|
|
if (isset($otherNames[$lang])) {
|
|
|
|
$place['name'] = $otherNames[$lang];
|
|
|
|
}
|
2023-03-29 19:14:14 +00:00
|
|
|
} catch (\Error $e) {
|
|
|
|
// Ignore
|
2023-03-28 01:53:01 +00:00
|
|
|
} finally {
|
|
|
|
unset($place['other_names']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $place;
|
|
|
|
}
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|