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-03-23 23:58:49 +00:00
|
|
|
protected TimelineQuery $tq;
|
|
|
|
protected IRequest $request;
|
2023-03-23 18:08:36 +00:00
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
public function __construct(TimelineQuery $tq, IRequest $request)
|
|
|
|
{
|
|
|
|
$this->tq = $tq;
|
|
|
|
$this->request = $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-03-23 18:08:36 +00:00
|
|
|
public function getClusters(): array
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
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');
|
|
|
|
$query->select('e.osm_id', 'e.name', $count)->from('memories_planet', 'e');
|
|
|
|
|
|
|
|
// WHERE there are items with this osm_id
|
|
|
|
$query->innerJoin('e', 'memories_places', 'mp', $query->expr()->eq('mp.osm_id', 'e.osm_id'));
|
|
|
|
|
|
|
|
// 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
|
|
|
|
$query->groupBy('e.osm_id', 'e.name');
|
|
|
|
$query->orderBy($query->createFunction('LOWER(e.name)'), 'ASC');
|
|
|
|
$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
|
|
|
|
foreach ($places as &$row) {
|
|
|
|
$row['osm_id'] = (int) $row['osm_id'];
|
|
|
|
$row['count'] = (int) $row['count'];
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|