From 007700c6236644463a0e78724b8260f14be4bedd Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 11 Aug 2009 19:37:38 +0100 Subject: [PATCH 1/1] AJAX route validator for text strings --- yarrg/CommodsWeb.pm | 4 ++- yarrg/web/pirate-route | 35 ++++++++++++++++------ yarrg/web/routetextstring | 63 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 yarrg/web/routetextstring diff --git a/yarrg/CommodsWeb.pm b/yarrg/CommodsWeb.pm index 96dde35..377d97b 100644 --- a/yarrg/CommodsWeb.pm +++ b/yarrg/CommodsWeb.pm @@ -36,6 +36,7 @@ use Commods; use CommodsDatabase; our $self_url; +our $base_url; BEGIN { use Exporter (); @@ -43,7 +44,7 @@ BEGIN { $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw($dbh &db_setocean &db_connect &db_doall - $self_url); + $self_url $base_url); %EXPORT_TAGS = ( ); @EXPORT_OK = qw(); @@ -57,5 +58,6 @@ for my $dir (@INC) { } $self_url= 'http://'.$ENV{'SERVER_NAME'}.$ENV{'SCRIPT_NAME'}.$ENV{'PATH_INFO'}; +$base_url= $self_url; $base_url =~ s,/[^/]+,,; 1; diff --git a/yarrg/web/pirate-route b/yarrg/web/pirate-route index 42de303..f58d47c 100644 --- a/yarrg/web/pirate-route +++ b/yarrg/web/pirate-route @@ -66,7 +66,7 @@ foreach my $var (@vars) { print '

'; } -db_setocean($a{'ocean'}); +db_setocean($a{Ocean}); db_connect(); @@ -76,9 +76,12 @@ db_connect(); % if (!$a{Dropdowns}) { Enter route (islands, or archipelagoes, separated by commas; - abbreviations are OK):
+ abbreviations are OK):

-


+

% } else { @@ -132,9 +148,9 @@ while ($row=$sth->fetchrow_arrayref) { <% $islandlistdata %> % } -% } +% } @@ -142,5 +158,6 @@ while ($row=$sth->fetchrow_arrayref) { <%init> use CommodsWeb; use HTML::Entities; +use URI::Escape; diff --git a/yarrg/web/routetextstring b/yarrg/web/routetextstring new file mode 100644 index 0000000..0e4ae6e --- /dev/null +++ b/yarrg/web/routetextstring @@ -0,0 +1,63 @@ +<%args> +$ocean +$format +$ctype => undef +$string + +<%perl> + +use CommodsWeb; +use HTML::Entities; +use JSON; + +db_setocean($ocean); +db_connect(); + +my $sth= $dbh->prepare("SELECT archipelago,islandid,islandname + FROM islands WHERE islandname LIKE ? + UNION ALL SELECT DISTINCT archipelago,NULL,archipelago + FROM islands WHERE archipelago LIKE ?"); + +my (@results, $canontext); +my ($output, $output_wrong); + +if ($format =~ 'json') { + $r->content_type($ctype or $format); + $output= sub { print to_json({ + success => 1, + show => encode_entities($canontext), + })}; + $output_wrong= sub { print to_json({ + success => 0, + show => encode_entities($_[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)); + } + push @results, $results->[0]; +} + +$canontext= join ' | ', map { encode_entities($_->[2]) } @results; + +$output->(); + + -- 2.30.2