chiark / gitweb /
Fix nonunique island/arch name reporting
[ypp-sc-tools.main.git] / yarrg / web / routetextstring
index b84fd8447708e70b4b463ab10fdf2719545cc1d0..4cec5f9d337b1f994f694857ec528887a5c2828a 100644 (file)
@@ -29,7 +29,8 @@ 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,
@@ -40,27 +41,34 @@ if ($format =~ 'json') {
 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) {
+                       $err->('no island or arch matches "%s"');
+               } elsif (%m > 5) {
+                       $err->(' ');
+               } else {
+                       $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->();