commit c68ea0d80c3ed868312948783dc5012869d079cf
parent d405c300310ddeaba9ccb1bdbd6e57b7d2691915
Author: René Wagner <rwa@clttr.info>
Date: Mon, 3 Apr 2023 19:51:40 +0200
fix and extend parameter validation
Diffstat:
M | phosta | | | 128 | ++++++++++++++++++++++++++++++------------------------------------------------- |
1 file changed, 48 insertions(+), 80 deletions(-)
diff --git a/phosta b/phosta
@@ -73,20 +73,24 @@ sub validate
!defined($opt_g) || grep /$opt_g/, @group_params or return 0;
my @select_params = qw(file maker model lensmake lens aperture exposuretime iso focallength focallength35mm);
- if (defined($opt_s))
- {
- foreach (split (/,/, $opt_s))
- {
- grep /$_/, @select_params or return 1;
+ if (defined($opt_s)) {
+ foreach (split (/,/, $opt_s)) {
+ my $val = $_;
+ grep /$val/, @select_params or return 0;
+ }
+ }
+ if (defined($opt_f)) {
+ foreach (split (/,/, $opt_f)) {
+ my @filter = split(/=/, $_);
+ grep /$filter[0]/, @select_params or return 0;
}
}
my @order_params = (@group_params, @select_params, 'count');
- if (defined($opt_o))
- {
- foreach (split (/,/, $opt_o))
- {
- grep /$_/, @order_params or return 0;
+ if (defined($opt_o)) {
+ foreach (split (/,/, $opt_o)) {
+ my $val = $_;
+ grep /$val/, @order_params or return 0;
}
}
(!defined($opt_n) || (looks_like_number($opt_n) && $opt_n > 0)) or return 0;
@@ -121,12 +125,10 @@ sub writeconfig
sub get_aliases
{
my @aliases;
- if ( -f $aliasfile )
- {
+ if ( -f $aliasfile ) {
open(FH, '<', $aliasfile) or die "Could not read file '$aliasfile': $!";
- while(<FH>)
- {
+ while(<FH>) {
chomp $_;
my @line = split /\t{1,}/, $_;
push @aliases, \@line;
@@ -135,8 +137,7 @@ sub get_aliases
close(FH);
}
- if ( $opt_v )
- {
+ if ( $opt_v ) {
say '### Aliases found';
foreach (@aliases) { say join(' -> ', @$_) }
}
@@ -170,8 +171,7 @@ sub populate
my $dbh = DBI->connect($dsn, '', '', { RaiseError => 1, AutoCommit => 0 }) or die $DBI::errstr;
my $errorcount = 0;
- foreach (@lines)
- {
+ foreach (@lines) {
chomp $_;
my @line = split(/#/, $_);
@@ -217,12 +217,10 @@ sub get_timerange
sub get_filters
{
- if (defined($opt_f))
- {
+ if (defined($opt_f)) {
my @returnarray;
my (@filters) = split /\,/, $opt_f;
- foreach (@filters)
- {
+ foreach (@filters) {
my @line = split /(!{0,1}=)/, $_;
push @returnarray, \@line;
}
@@ -237,53 +235,44 @@ sub get_sql
my @grouparray;
my $orderlist = '';
my @wherearray;
- given ($opt_g)
- {
- when ('month')
- {
+ given ($opt_g) {
+ when ('month') {
$fieldlist = "IFNULL(strftime('%Y/%m', datetimeoriginal), '-') as month,";
push @grouparray, "strftime('%Y/%m', datetimeoriginal)";
}
- when ('week')
- {
+ when ('week') {
$fieldlist = "IFNULL(strftime('%Y/%W', datetimeoriginal), '-') as week,";
push @grouparray, "strftime('%Y/%W', datetimeoriginal)";
}
- when ('year')
- {
+ when ('year') {
$fieldlist = "IFNULL(strftime('%Y', datetimeoriginal), '-') as year,";
push @grouparray, "strftime('%Y', datetimeoriginal)";
}
- when ('hour')
- {
+ when ('hour') {
$fieldlist = "IFNULL(strftime('%H', datetimeoriginal), '-') as hour,";
push @grouparray, "strftime('%H', datetimeoriginal)";
}
}
- if (defined($opt_s))
- {
+ if (defined($opt_s)) {
$fieldlist = $fieldlist . $opt_s. ', ';
push @grouparray, $opt_s;
}
- if (defined($opt_o))
- {
+ if (defined($opt_o)) {
my $sorter = $opt_r ? ' ASC' : ' DESC';
my @order = split(/\,/, $opt_o);
$orderlist = ' ORDER BY '. join("$sorter, ", @order) .$sorter;
}
- if (defined($opt_t))
- {
+ if (defined($opt_t)) {
my ($from, $to) = get_timerange();
!defined($from) or push @wherearray, "datetimeoriginal >= '$from'";
!defined($to) or push @wherearray, "datetimeoriginal <= '$to'";
}
- foreach (get_filters())
- {
+ foreach (get_filters()) {
push @wherearray, @{$_}[0] . (@{$_}[1] =~ /!/ ? ' NOT' : '') . " LIKE '%@{$_}[2]%'";
}
@@ -315,12 +304,10 @@ sub query
push @$headers, 'percent';
my $sum = 0;
- foreach (@$rows)
- {
+ foreach (@$rows) {
$sum += @$_[$#$_];
}
- foreach (@$rows)
- {
+ foreach (@$rows) {
my $perc = $sum > 0 ? sprintf("%.1f", (@$_[$#$_] / $sum)* 100). '%' : '';
push @$_, $perc;
}
@@ -328,12 +315,9 @@ sub query
my ($limited_rows, $skippedlines) = limit_results($rows);
print_filterinfo(scalar @$limited_rows, $skippedlines, $sum);
- if ($opt_T eq 'off' || (@$headers == 3 and $opt_T eq 'auto') )
- {
+ if ($opt_T eq 'off' || (@$headers == 3 and $opt_T eq 'auto') ) {
result_to_graph($headers, $limited_rows, $sum);
- }
- else
- {
+ } else {
result_to_table($headers, $limited_rows, $sum);
}
}
@@ -345,28 +329,21 @@ sub limit_results
my $currentlines = 0;
my @limited_rows;
- foreach (@$rows)
- {
- if (!defined($opt_n) || ($currentlines < $opt_n))
- {
- if ($opt_e)
- {
+ foreach (@$rows) {
+ if (!defined($opt_n) || ($currentlines < $opt_n)) {
+ if ($opt_e) {
my $skip = 1;
- for (my $i=0; $i < (scalar @$_ - 2); $i++)
- {
+ for (my $i=0; $i < (scalar @$_ - 2); $i++) {
@$_[$i] eq '-' or $skip = 0;
}
- if ($skip)
- {
+ if ($skip) {
$skippedlines++;
next;
}
}
push @limited_rows, $_;
$currentlines++;
- }
- else
- {
+ } else {
$skippedlines++;
}
}
@@ -376,18 +353,15 @@ sub limit_results
sub print_filterinfo
{
my ($currentlines, $skippedlines, $sum) = @_;
- if (defined($opt_f) || defined($opt_t) || defined($opt_g))
- {
+ if (defined($opt_f) || defined($opt_t) || defined($opt_g)) {
say 'Applied filters:';
- if ($opt_t)
- {
+ if ($opt_t) {
my ($from, $to) = get_timerange();
if (defined($from) && defined($to)) { say ' - images taken between '. color('bold'). $from .color('reset') .' and '. color('bold'). $to . color('reset'); }
if (defined($from) && !defined($to)) { say ' - images taken since '. color('bold'). $from . color('reset'); }
if (defined($to) && !defined($from)) { say ' - images taken till '. color('bold'). $to . color('reset'); }
}
- foreach (get_filters())
- {
+ foreach (get_filters()) {
say ' - ' .color('italic') . @{$_}[0] .color('reset') . (@{$_}[1] =~ /!/ ? ' NOT' : '') .' LIKE '.color('bold') .@{$_}[2]. color('reset');
}
if ($opt_g) {
@@ -399,13 +373,11 @@ sub print_filterinfo
}
say 'A total of '. color('bold'). "$sum images". color('reset') .' matched your criteria.';
- if (defined($opt_o) || defined($opt_r))
- {
+ if (defined($opt_o) || defined($opt_r)) {
say 'Sorted in '.color('bold'). ($opt_r ? 'ascending' : 'descending') .color('reset').' order by '. color('italic') . join(color('reset').', '.color('italic'), split(/\,/, $opt_o)). color('reset').'.';
}
- if (defined($opt_n) || $opt_e)
- {
+ if (defined($opt_n) || $opt_e) {
say 'Showing '. color('bold'). "$currentlines". color('reset'). ' results, skipping '. color('italic') . "$skippedlines results".color('reset') .' due to empty fields.';
}
say '';
@@ -422,18 +394,15 @@ sub result_to_graph
my $countcolumn = @$headers - 2;
my $percentcolumn = @$headers - 1;
- foreach (@$rows)
- {
+ foreach (@$rows) {
# join columns for display before the chart
- if ( @$headers >= 4)
- {
+ if ( @$headers >= 4) {
# TODO: give remaining columns more room if first columns are shorter
# TODO: remove trailing whitespace
my $titlecolumns = @$headers - 2;
my $columnwidth = POSIX::lround($titlewidth / $titlecolumns);
my $title = '';
- for (my $i = 0; $i < $titlecolumns; $i++)
- {
+ for (my $i = 0; $i < $titlecolumns; $i++) {
$title = $title . ((length(@$_[$i]) > $columnwidth) ? substr(@$_[$i], 0, $columnwidth-1).'~ ' : @$_[$i] . ' ');
}
@$_[0] = $title;
@@ -449,8 +418,7 @@ sub result_to_table
my $tb = Text::SimpleTable::AutoWidth->new('max_width' => $opt_W);
$tb->captions($headers);
- foreach (@$rows)
- {
+ foreach (@$rows) {
$tb->row(@$_);
}