X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Fweb%2Fqtextstringcheck;h=5ef8971f644b63991af5d6eb5379be7fec1da596;hb=9d01242d0991d15f7ea84454264c868e1c4ed8ad;hp=41960188fa6b896b32d9d286e569131e8cfb1c99;hpb=3766fb8d851fe463a5b8b469cf148c28efb1c578;p=ypp-sc-tools.main.git diff --git a/yarrg/web/qtextstringcheck b/yarrg/web/qtextstringcheck old mode 100644 new mode 100755 index 4196018..5ef8971 --- 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,33 +53,76 @@ 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 $dbh= 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); } +$dbh->rollback(); + +return $emsg, + $canontext, + [ @results ]; +