X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=blobdiff_plain;f=yarrg%2Fweb%2Fqtextstringcheck;fp=yarrg%2Fweb%2Fqtextstringcheck;h=337ed31f2deb156e831b491f1d6883abda40900a;hp=40184558d0662fb073645c291fc46af95bf12ab3;hb=b6d8c4a781c0bedf79a4b13af5afe9ad47de97ed;hpb=c5b3ac45babf8012719dfb7eafd0cfa9d9eddff0 diff --git a/yarrg/web/qtextstringcheck b/yarrg/web/qtextstringcheck old mode 100644 new mode 100755 index 4018455..337ed31 --- a/yarrg/web/qtextstringcheck +++ b/yarrg/web/qtextstringcheck @@ -32,7 +32,19 @@ This Mason component handles the generic output format options for text string parsers/checkers like check_routestring. +# typical url for this script: +# http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/qtextstring?what=routestring?format=json&ocean=Midnight&string=d + + +<%args> +$ocean +$format +$ctype => undef +$string +$what + + <%flags> inherit => undef @@ -41,19 +53,60 @@ inherit => undef use JSON; use Data::Dumper; use HTML::Entities; - -my ($emsg, $canontext, $results)= $m->call_next(); +use CommodsWeb; + +die if $what =~ m/[^a-z]/; +my $specifics= "check_${what}"; +my $specific= $m->fetch_comp($specifics); + +my $dbh= dbw_connect($ocean); +my $sqlstmt= $specific->scall_method("sqlstmt"); +my $sth= $dbh->prepare($sqlstmt); +my @sqlstmt_qs= $sqlstmt =~ m/\?/g; + +#die "$sqlstmt @sqlstmt_qs"; + +my $emsg= ''; +my @results; + +my @specs= $specific->attr('multiple') ? (split m#[/|,]#, $string) : ($string); + +foreach my $each (@specs) { + $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g; + next if !length $each; + my $err= sub { $emsg= $_[0]; last; }; + my %m; + my $results; + foreach my $pat ("$each", "$each\%", "\%$each\%") { + $sth->execute(($pat) x @sqlstmt_qs); + $results= $sth->fetchall_arrayref(); + last if @$results==1; + map { $m{ $_->[0] }=1 } @$results; + $results= undef; + } + if (!$results) { + if (!%m) { + $err->($specific->scall_method("nomatch", + spec => $each)); + } elsif (keys(%m) > 5) { + $err->(' '); + } else { + $err->($specific->scall_method("ambiguous", + spec => $each, + couldbe => join(', ', sort keys %m))); + } + } + push @results, $results->[0]; +}; $emsg='' if !defined $emsg; - -my $format= $ARGS{'format'}; -my $ctype= $ARGS{'ctype'}; +my $canontext= join ' | ', map { $_->[0] } @results; if ($format =~ /json/) { $r->content_type($ctype or $format); my $jobj= { success => 1*!length $emsg, - show => (length $emsg ? encode_entities($emsg) : + show => (length $emsg ? $emsg : length $canontext ? encode_entities($canontext) : ' '), }; @@ -61,7 +114,13 @@ if ($format =~ /json/) { } if ($format =~ /dump/) { $r->content_type('text/plain'); - print Dumper($emsg, $canontext, $results); + print Dumper($emsg, $canontext, \@results); } +$dbh->rollback(); + +return $emsg, + $canontext, + [ @results ]; +