X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-live.git;a=blobdiff_plain;f=yarrg%2Fweb%2Fqtextstringcheck;h=686a506aa4c348bfefbc2b0f73fd2e160785937a;hp=40184558d0662fb073645c291fc46af95bf12ab3;hb=555b3391b3cd9967a29b219fff242b583137d2b8;hpb=dfae6286760f90eec22d236d57014f68149161ad diff --git a/yarrg/web/qtextstringcheck b/yarrg/web/qtextstringcheck old mode 100644 new mode 100755 index 4018455..686a506 --- 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,19 +54,89 @@ inherit => undef use JSON; use Data::Dumper; use HTML::Entities; +use CommodsWeb; + +die if $what =~ m/[^a-z]/; +my $chk= $m->fetch_comp("check_${what}"); + +my $mydbh; +$dbh ||= ($mydbh= dbw_connect($ocean)); + +#print STDERR "qtsc string=\`$string'\n"; + +my ($sth, @sqlstmt_qs); +if ($chk->method_exists('sqlstmt')) { + my $sqlstmt= $chk->scall_method("sqlstmt"); + $sth= $dbh->prepare($sqlstmt); + @sqlstmt_qs= $sqlstmt =~ m/\?/g; +} -my ($emsg, $canontext, $results)= $m->call_next(); +my $emsg= ''; +my @results; +my @specs; +my $canontext; +my $hooks = { Emsg => \$emsg, String => \$string, + Results => \@results, Specs => \@specs, + Canon => \$canontext + }; + +if ($chk->method_exists('preparse')) { + $chk->call_method('preparse', h => $hooks); +} else { + @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; + my @pats= ("$each", "$each\%", "\%$each\%"); + if ($chk->attr_exists('abbrev_initials')) { + push @pats, join ' ', map { "$_%" } split //, $each; + } + foreach my $pat (@pats) { + $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]; +}; + +if (!defined $canontext) { + $canontext= join ' | ', map { $_->[0] } @results; +} +if ($chk->method_exists('postquery')) { + $chk->call_method('postquery', h => $hooks); +} $emsg='' if !defined $emsg; +@results=() if length $emsg; -my $format= $ARGS{'format'}; -my $ctype= $ARGS{'ctype'}; +#print STDERR "qtsc emsg=\`$emsg' results=\`@results'\n"; 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 +144,13 @@ if ($format =~ /json/) { } 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 ]; +