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
+</%args>
+
<%flags>
inherit => undef
</%flags>
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) :
' '),
};
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 ];
+
</%perl>