chiark / gitweb /
Invokes "routetrade" component to get answers
[ypp-sc-tools.main.git] / yarrg / web / routetextstring
index b84fd8447708e70b4b463ab10fdf2719545cc1d0..685b465255325fe566c6ed0f5468b51fa492c711 100644 (file)
@@ -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->();
 
 </%perl>