2022-12-07 18:42:48 +00:00
|
|
|
package Travelynx::Model::Stations;
|
|
|
|
|
2023-07-03 15:59:25 +00:00
|
|
|
# Copyright (C) 2022 Birte Kristina Friesel
|
2022-12-07 18:42:48 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use 5.020;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my ( $class, %opt ) = @_;
|
|
|
|
|
|
|
|
return bless( \%opt, $class );
|
|
|
|
}
|
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
sub get_backend_id {
|
2023-08-13 10:51:15 +00:00
|
|
|
my ( $self, %opt ) = @_;
|
2024-07-26 16:55:58 +00:00
|
|
|
|
|
|
|
if ( $opt{iris} ) {
|
|
|
|
|
|
|
|
# special case
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( $opt{hafas} and $self->{backend_id}{hafas}{ $opt{hafas} } ) {
|
|
|
|
return $self->{backend_id}{hafas}{ $opt{hafas} };
|
|
|
|
}
|
|
|
|
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $backend_id = 0;
|
|
|
|
|
|
|
|
if ( $opt{hafas} ) {
|
|
|
|
$backend_id = $db->select(
|
|
|
|
'backends',
|
|
|
|
['id'],
|
|
|
|
{
|
|
|
|
hafas => 1,
|
|
|
|
name => $opt{hafas}
|
|
|
|
}
|
|
|
|
)->hash->{id};
|
|
|
|
$self->{backend_id}{hafas}{ $opt{hafas} } = $backend_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $backend_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_hafas_name {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
if ( exists $self->{hafas_name}{ $opt{backend_id} } ) {
|
|
|
|
return $self->{hafas_name}{ $opt{backend_id} };
|
|
|
|
}
|
|
|
|
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $hafas_name;
|
|
|
|
my $ret = $db->select(
|
|
|
|
'backends',
|
|
|
|
['name'],
|
|
|
|
{
|
|
|
|
hafas => 1,
|
|
|
|
id => $opt{backend_id},
|
2023-08-13 10:51:15 +00:00
|
|
|
}
|
2024-07-26 16:55:58 +00:00
|
|
|
)->hash;
|
|
|
|
|
|
|
|
if ($ret) {
|
|
|
|
$hafas_name = $ret->{name};
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{hafas_name}{ $opt{backend_id} } = $hafas_name;
|
|
|
|
|
|
|
|
return $hafas_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_backends {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
|
|
|
|
my $res = $opt{db}->select( 'backends', [ 'id', 'name', 'iris', 'hafas' ] );
|
|
|
|
my @ret;
|
|
|
|
|
|
|
|
while ( my $row = $res->hash ) {
|
|
|
|
push(
|
|
|
|
@ret,
|
|
|
|
{
|
|
|
|
id => $row->{id},
|
|
|
|
name => $row->{name},
|
|
|
|
iris => $row->{iris},
|
|
|
|
hafas => $row->{hafas},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_or_update {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $stop = $opt{stop};
|
|
|
|
my $loc = $stop->loc;
|
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
|
|
|
|
|
|
|
if (
|
|
|
|
my $s = $self->get_by_eva(
|
|
|
|
$loc->eva,
|
|
|
|
db => $opt{db},
|
|
|
|
backend_id => $opt{backend_id}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$opt{db}->update(
|
2023-08-13 10:51:15 +00:00
|
|
|
'stations',
|
|
|
|
{
|
2023-12-27 09:59:35 +00:00
|
|
|
name => $loc->name,
|
|
|
|
lat => $loc->lat,
|
|
|
|
lon => $loc->lon,
|
2023-08-13 10:51:15 +00:00
|
|
|
archived => 0
|
|
|
|
},
|
2024-07-26 16:55:58 +00:00
|
|
|
{
|
|
|
|
eva => $loc->eva,
|
|
|
|
source => $opt{backend_id}
|
|
|
|
}
|
2023-08-13 10:51:15 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db}->insert(
|
2023-08-13 10:51:15 +00:00
|
|
|
'stations',
|
|
|
|
{
|
2023-12-27 09:59:35 +00:00
|
|
|
eva => $loc->eva,
|
|
|
|
name => $loc->name,
|
|
|
|
lat => $loc->lat,
|
|
|
|
lon => $loc->lon,
|
2024-07-26 16:55:58 +00:00
|
|
|
source => $opt{backend_id},
|
2023-08-13 10:51:15 +00:00
|
|
|
archived => 0
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-01 05:11:58 +00:00
|
|
|
sub add_meta {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $eva = $opt{eva};
|
|
|
|
my @meta = @{ $opt{meta} };
|
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
|
|
|
|
2023-10-01 05:11:58 +00:00
|
|
|
for my $meta (@meta) {
|
|
|
|
if ( $meta != $eva ) {
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db}->insert(
|
2023-10-01 05:11:58 +00:00
|
|
|
'related_stations',
|
|
|
|
{
|
2024-07-26 16:55:58 +00:00
|
|
|
eva => $eva,
|
|
|
|
meta => $meta,
|
|
|
|
backend_id => $opt{backend_id},
|
2023-10-01 05:11:58 +00:00
|
|
|
},
|
|
|
|
{ on_conflict => undef }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 18:09:16 +00:00
|
|
|
sub get_db_iterator {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2024-08-07 10:13:15 +00:00
|
|
|
return $self->{pg}->db->select( 'stations_str', '*' );
|
2024-02-09 18:09:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 05:11:58 +00:00
|
|
|
sub get_meta {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $eva = $opt{eva};
|
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{backend_id} //= $self->get_backend_id( %opt, db => $db );
|
|
|
|
|
|
|
|
my $res = $db->select(
|
|
|
|
'related_stations',
|
|
|
|
['meta'],
|
|
|
|
{
|
|
|
|
eva => $eva,
|
|
|
|
backend_id => $opt{backend_id}
|
|
|
|
}
|
|
|
|
);
|
2023-10-01 05:11:58 +00:00
|
|
|
my @ret;
|
|
|
|
|
|
|
|
while ( my $row = $res->hash ) {
|
|
|
|
push( @ret, $row->{meta} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
2023-08-26 07:17:28 +00:00
|
|
|
sub get_for_autocomplete {
|
2024-07-26 16:55:58 +00:00
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
2023-08-26 07:17:28 +00:00
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
my $res = $self->{pg}
|
|
|
|
->db->select( 'stations', ['name'], { source => $opt{backend_id} } );
|
2023-08-26 07:17:28 +00:00
|
|
|
my %ret;
|
|
|
|
|
|
|
|
while ( my $row = $res->hash ) {
|
|
|
|
$ret{ $row->{name} } = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return \%ret;
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:42:48 +00:00
|
|
|
# Fast
|
|
|
|
sub get_by_eva {
|
|
|
|
my ( $self, $eva, %opt ) = @_;
|
|
|
|
|
|
|
|
if ( not $eva ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
2022-12-07 18:42:48 +00:00
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
return $opt{db}->select(
|
|
|
|
'stations',
|
|
|
|
'*',
|
|
|
|
{
|
|
|
|
eva => $eva,
|
|
|
|
source => $opt{backend_id}
|
|
|
|
}
|
|
|
|
)->hash;
|
2022-12-07 18:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Fast
|
|
|
|
sub get_by_evas {
|
2024-07-26 16:55:58 +00:00
|
|
|
my ( $self, %opt ) = @_;
|
2022-12-07 18:42:48 +00:00
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
|
|
|
|
|
|
|
my @ret = $self->{pg}->db->select(
|
|
|
|
'stations',
|
|
|
|
'*',
|
|
|
|
{
|
|
|
|
eva => { '=', $opt{evas} },
|
|
|
|
source => $opt{backend_id}
|
|
|
|
}
|
|
|
|
)->hashes->each;
|
2022-12-07 18:42:48 +00:00
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Slow
|
|
|
|
sub get_by_name {
|
|
|
|
my ( $self, $name, %opt ) = @_;
|
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
2022-12-07 18:42:48 +00:00
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
return $opt{db}->select(
|
|
|
|
'stations',
|
|
|
|
'*',
|
|
|
|
{
|
|
|
|
name => $name,
|
|
|
|
source => $opt{backend_id}
|
|
|
|
},
|
|
|
|
{ limit => 1 }
|
|
|
|
)->hash;
|
2022-12-07 18:42:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 17:22:09 +00:00
|
|
|
# Slow
|
|
|
|
sub get_by_names {
|
|
|
|
my ( $self, @names ) = @_;
|
|
|
|
|
|
|
|
my @ret
|
|
|
|
= $self->{pg}->db->select( 'stations', '*', { name => { '=', \@names } } )
|
|
|
|
->hashes->each;
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:42:48 +00:00
|
|
|
# Slow
|
|
|
|
sub get_by_ds100 {
|
|
|
|
my ( $self, $ds100, %opt ) = @_;
|
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
2022-12-07 18:42:48 +00:00
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
return $opt{db}->select(
|
|
|
|
'stations',
|
|
|
|
'*',
|
|
|
|
{
|
|
|
|
ds100 => $ds100,
|
|
|
|
source => $opt{backend_id}
|
|
|
|
},
|
|
|
|
{ limit => 1 }
|
|
|
|
)->hash;
|
2022-12-07 18:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Can be slow
|
|
|
|
sub search {
|
|
|
|
my ( $self, $identifier, %opt ) = @_;
|
|
|
|
|
2024-07-26 16:55:58 +00:00
|
|
|
$opt{db} //= $self->{pg}->db;
|
|
|
|
$opt{backend_id} //= $self->get_backend_id(%opt);
|
|
|
|
|
2022-12-07 18:42:48 +00:00
|
|
|
if ( $identifier =~ m{ ^ \d+ $ }x ) {
|
|
|
|
return $self->get_by_eva( $identifier, %opt )
|
|
|
|
// $self->get_by_ds100( $identifier, %opt )
|
|
|
|
// $self->get_by_name( $identifier, %opt );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->get_by_ds100( $identifier, %opt )
|
|
|
|
// $self->get_by_name( $identifier, %opt );
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:22:09 +00:00
|
|
|
# Slow
|
|
|
|
sub grep_unknown {
|
|
|
|
my ( $self, @stations ) = @_;
|
|
|
|
|
|
|
|
my %station = map { $_->{name} => 1 } $self->get_by_names(@stations);
|
|
|
|
my @unknown_stations = grep { not $station{$_} } @stations;
|
|
|
|
|
|
|
|
return @unknown_stations;
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:42:48 +00:00
|
|
|
1;
|