X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.main.git;a=blobdiff_plain;f=yarrg%2Fweb%2Froutetextstring;h=685b465255325fe566c6ed0f5468b51fa492c711;hp=b84fd8447708e70b4b463ab10fdf2719545cc1d0;hb=4a7daddbd09806e47fc3b6df9cfc42ed5eb84758;hpb=7031695ce7767c196f2ef0f279bf4e3c7931c211 diff --git a/yarrg/web/routetextstring b/yarrg/web/routetextstring index b84fd84..685b465 100644 --- a/yarrg/web/routetextstring +++ b/yarrg/web/routetextstring @@ -29,39 +29,51 @@ if ($format =~ 'json') { $r->content_type($ctype or $format); $output= sub { print to_json({ success => 1, - show => encode_entities($canontext), - })}; + show => length $canontext ? encode_entities($canontext) + : ' ', + })}; $output_wrong= sub { print to_json({ success => 0, show => $_[0], - })}; -} + })}; +} +if ($format =~ 'return') { + $output= sub { return { Error => '', Results => \@results }; }; + $output_wrong= sub { return { Error => $_[0] }; }; +} foreach my $each (split m#[/|,]#, $string) { $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g; next if !length $each; - my $pat= "\%$each\%"; - my $nrows= $sth->execute($pat,$pat); my $err= sub { my $msg= sprintf $_[0], encode_entities($each); $output_wrong->($msg); $m->abort(); }; - my $results= $sth->fetchall_arrayref(); - if (!@$results) { - $err->('no island or arch matches "%s"'); - } elsif (@$results > 3) { - $err->(' '); - } elsif (@$results > 1) { - my @m= map { $_->[2] } @$results; - $err->('ambiguous island or arch "%s", could be '. - join(', ', @m)); + my %m; + my $results; + foreach my $pat ("$each\%", "\%$each\%") { + $sth->execute($pat,$pat); + $results= $sth->fetchall_arrayref(); + last if @$results==1; + map { $m{ $_->[2] }=1 } @$results; + $results= undef; + } + if (!$results) { + if (!%m) { + return $err->('no island or arch matches "%s"'); + } elsif (%m > 5) { + return $err->(' '); + } else { + return $err->('ambiguous island or arch "%s",'. + ' could be '.join(', ', sort keys %m)); + } } push @results, $results->[0]; } -$canontext= join ' | ', map { encode_entities($_->[2]) } @results; +$canontext= join ' | ', map { $_->[2] } @results; -$output->(); +return $output->();