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
+
</%doc>
+
+<%args>
+$ocean
+$format
+$ctype => undef
+$string
+$what
+$dbh => undef
+</%args>
+
<%flags>
inherit => undef
</%flags>
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;
+ 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];
+};
+
+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) :
' '),
};
}
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 ];
+
</%perl>