X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.web-live.git;a=blobdiff_plain;f=yarrg%2Fweb%2Fqtextstringcheck;h=b2c101390589b01daf00d858c60f9d205d05ce83;hp=41960188fa6b896b32d9d286e569131e8cfb1c99;hb=98610392fde2add293bee6199f2de1d6f88559d8;hpb=3766fb8d851fe463a5b8b469cf148c28efb1c578 diff --git a/yarrg/web/qtextstringcheck b/yarrg/web/qtextstringcheck old mode 100644 new mode 100755 index 4196018..b2c1013 --- a/yarrg/web/qtextstringcheck +++ b/yarrg/web/qtextstringcheck @@ -32,7 +32,20 @@ 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 +$dbh => undef + + <%flags> inherit => undef @@ -41,33 +54,77 @@ 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 $chk= $m->fetch_comp("check_${what}"); + +my $mydbh; +$dbh ||= ($mydbh= dbw_connect($ocean)); + +my $sqlstmt= $chk->scall_method("sqlstmt"); +my $sth= $dbh->prepare($sqlstmt); +my @sqlstmt_qs= $sqlstmt =~ m/\?/g; + +#die "$sqlstmt @sqlstmt_qs"; + +my $emsg= ''; +my @results; + +my @specs= $chk->attr('multiple') ? (split m#[/|,]#, $string) : ($string); + +no warnings qw(exiting); + +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->($chk->scall_method("nomatch", + spec => $each)); + } elsif (keys(%m) > $chk->attr('maxambig')) { + $err->($chk->scall_method("manyambig")); + } else { + $err->($chk->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) : ' '), }; print to_json_shim($jobj); } -if ($format =~ /return/) { - return { - Error => $emsg, - Results => length $emsg ? $results : undef - }; -} if ($format =~ /dump/) { $r->content_type('text/plain'); - print Dumper($emsg, $canontext, $results); + print Dumper($emsg, $canontext, \@results); } +$mydbh->rollback() if $mydbh; + +return $emsg, + $canontext, + [ @results ]; +