9 # typical url for this script:
10 # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/routetextstring?format=json&ocean=Midnight&string=d
20 my $sth= $dbh->prepare("SELECT archipelago,islandid,islandname
21 FROM islands WHERE islandname LIKE ?
22 UNION ALL SELECT DISTINCT archipelago,NULL,archipelago
23 FROM islands WHERE archipelago LIKE ?");
25 my (@results, $canontext);
26 my ($output, $output_wrong);
28 if ($format =~ 'json') {
29 $r->content_type($ctype or $format);
30 $output= sub { print to_json({
32 show => encode_entities($canontext),
34 $output_wrong= sub { print to_json({
40 foreach my $each (split m#[/|,]#, $string) {
41 $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g;
42 next if !length $each;
44 my $nrows= $sth->execute($pat,$pat);
46 my $msg= sprintf $_[0], encode_entities($each);
47 $output_wrong->($msg);
50 my $results= $sth->fetchall_arrayref();
52 $err->('no island or arch matches "%s"');
53 } elsif (@$results > 3) {
55 } elsif (@$results > 1) {
56 my @m= map { $_->[2] } @$results;
57 $err->('ambiguous island or arch "%s", could be '.
60 push @results, $results->[0];
63 $canontext= join ' | ', map { $_->[2] } @results;