places: support user lang

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/563/head
Varun Patil 2023-03-27 18:53:01 -07:00
parent 3b24a62ba0
commit 2453aad507
5 changed files with 118 additions and 4 deletions

View File

@ -69,7 +69,7 @@ class PlacesBackend extends Backend
// 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');
$query->select('e.osm_id', 'e.name', 'e.other_names', $count)->from('memories_planet', 'e');
// WHERE these are not special clusters (e.g. timezone)
$query->where($query->expr()->gt('e.admin_level', $query->createNamedParameter(0)));
@ -84,7 +84,7 @@ class PlacesBackend extends Backend
$query = $this->tq->joinFilecache($query);
// GROUP and ORDER by tag name
$query->groupBy('e.osm_id', 'e.name');
$query->groupBy('e.osm_id', 'e.name', 'e.other_names');
$query->orderBy($query->createFunction('LOWER(e.name)'), 'ASC');
$query->addOrderBy('e.osm_id'); // tie-breaker
@ -92,9 +92,11 @@ class PlacesBackend extends Backend
$places = $this->tq->executeQueryWithCTEs($query)->fetchAll();
// Post process
$lang = Util::getUserLang();
foreach ($places as &$row) {
$row['osm_id'] = (int) $row['osm_id'];
$row['count'] = (int) $row['count'];
self::choosePlaceLang($row, $lang);
}
return $places;
@ -128,4 +130,21 @@ class PlacesBackend extends Backend
// FETCH tag photos
return $this->tq->executeQueryWithCTEs($query)->fetchAll() ?: [];
}
/**
* 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];
}
} finally {
unset($place['other_names']);
}
return $place;
}
}

View File

@ -144,6 +144,7 @@ class PlacesSetup extends Command
'osm_id' => $query->createParameter('osm_id'),
'admin_level' => $query->createParameter('admin_level'),
'name' => $query->createParameter('name'),
'other_names' => $query->createParameter('other_names'),
])
;
$insertPlace = $this->connection->prepare($query->getSQL());
@ -200,6 +201,7 @@ class PlacesSetup extends Command
$adminLevel = $data['admin_level'];
$name = $data['name'];
$boundaries = $data['geometry'];
$otherNames = json_encode($data['other_names'] ?? []);
// Skip some places
if ($adminLevel > -2 && ($adminLevel <= 1 || $adminLevel >= 10)) {
@ -213,6 +215,7 @@ class PlacesSetup extends Command
$insertPlace->bindValue('osm_id', $osmId);
$insertPlace->bindValue('admin_level', $adminLevel);
$insertPlace->bindValue('name', $name);
$insertPlace->bindValue('other_names', $otherNames);
$insertPlace->execute();
// Insert polygons into database

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\ClustersBackend\PlacesBackend;
use OCA\Memories\Util;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@ -56,6 +58,7 @@ trait TimelineQuerySingleItem
} catch (\Throwable $e) {
}
// Get exif if needed
$exif = [];
if (!$basic && !empty($row['exif'])) {
try {
@ -64,19 +67,23 @@ trait TimelineQuerySingleItem
}
}
// Get address from places
$gisType = \OCA\Memories\Util::placesGISType();
$address = -1 === $gisType ? 'Geocoding Unconfigured' : null;
if (!$basic && $gisType > 0) {
$qb = $this->connection->getQueryBuilder();
$qb->select('e.name')
$qb->select('e.name', 'e.other_names')
->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)))
->andWhere($qb->expr()->gt('e.admin_level', $qb->createNamedParameter(0)))
->orderBy('e.admin_level', 'DESC')
;
$places = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
$places = $qb->executeQuery()->fetchAll();
$lang = Util::getUserLang();
if (\count($places) > 0) {
$places = array_map(fn ($p) => PlacesBackend::choosePlaceLang($p, $lang)['name'], $places);
$address = implode(', ', $places);
}
}

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Your name <your@email.com>
* @author Your name <your@email.com>
* @license GNU AGPL version 3 or any later version
*
* 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/>.
*/
namespace OCA\Memories\Migration;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version401300Date20230328012131 extends SimpleMigrationStep
{
/**
* @param \Closure(): ISchemaWrapper $schemaClosure
*/
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void
{
}
/**
* @param \Closure(): ISchemaWrapper $schemaClosure
*/
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options): ?ISchemaWrapper
{
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
// Add lat lon to memories
$table = $schema->getTable('memories_planet');
$table->addColumn('other_names', 'text', [
'notnull' => false,
]);
return $schema;
}
/**
* @param \Closure(): ISchemaWrapper $schemaClosure
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void
{
}
}

View File

@ -75,4 +75,24 @@ trait UtilController
return \OC::$server->get(\OCP\Files\IRootFolder::class)->getUserFolder($uid);
}
/**
* Get the language code for the current user.
*/
public static function getUserLang(): string
{
// Default language
$config = \OC::$server->get(\OCP\IConfig::class);
$default = $config->getSystemValue('default_language', 'en');
// Get UID of the user
try {
$uid = self::getUID();
} catch (\Exception $e) {
return 'en';
}
// Get language of the user
return $config->getUserValue($uid, 'core', 'lang', $default);
}
}