chiark / gitweb /
Merge branch 'anaplian' into stable-3.x
[ypp-sc-tools.db-live.git] / yarrg / web / qtextstringcheck
index 337ed31f2deb156e831b491f1d6883abda40900a..a489d8e1232940e6c603c44e05f43eed3add47b9 100755 (executable)
@@ -43,6 +43,7 @@ $format
 $ctype => undef
 $string
 $what
 $ctype => undef
 $string
 $what
+$dbh => undef
 </%args>
 
 <%flags>
 </%args>
 
 <%flags>
@@ -56,20 +57,36 @@ use HTML::Entities;
 use CommodsWeb;
 
 die if $what =~ m/[^a-z]/;
 use CommodsWeb;
 
 die if $what =~ m/[^a-z]/;
-my $specifics= "check_${what}";
-my $specific= $m->fetch_comp($specifics);
+my $chk= $m->fetch_comp("check_${what}");
 
 
-my $dbh= dbw_connect($ocean);
-my $sqlstmt= $specific->scall_method("sqlstmt");
-my $sth= $dbh->prepare($sqlstmt);
-my @sqlstmt_qs= $sqlstmt =~ m/\?/g;
+my $mydbh;
+$dbh ||= ($mydbh= dbw_connect($ocean));
 
 
-#die "$sqlstmt @sqlstmt_qs";
+#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= '';
 my @results;
 
 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);
+}
 
 
-my @specs= $specific->attr('multiple') ? (split m#[/|,]#, $string) : ($string);
+no warnings qw(exiting);
 
 foreach my $each (@specs) {
        $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
 
 foreach my $each (@specs) {
        $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
@@ -86,12 +103,12 @@ foreach my $each (@specs) {
        }
        if (!$results) {
                if (!%m) {
        }
        if (!$results) {
                if (!%m) {
-                       $err->($specific->scall_method("nomatch",
+                       $err->($chk->scall_method("nomatch",
                                spec => $each));
                                spec => $each));
-               } elsif (keys(%m) > 5) {
-                       $err->('&nbsp;');
+               } elsif (keys(%m) > $chk->attr('maxambig')) {
+                       $err->($chk->scall_method("manyambig"));
                } else {
                } else {
-                       $err->($specific->scall_method("ambiguous",
+                       $err->($chk->scall_method("ambiguous",
                                spec => $each,
                                couldbe => join(', ', sort keys %m)));
                }
                                spec => $each,
                                couldbe => join(', ', sort keys %m)));
                }
@@ -99,8 +116,17 @@ foreach my $each (@specs) {
        push @results, $results->[0];
 };
 
        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;
 $emsg='' if !defined $emsg;
-my $canontext= join ' | ', map { $_->[0] } @results;
+@results=() if length $emsg;
+
+#print STDERR "qtsc emsg=\`$emsg' results=\`@results'\n";
 
 if ($format =~ /json/) {
        $r->content_type($ctype or $format);
 
 if ($format =~ /json/) {
        $r->content_type($ctype or $format);
@@ -117,7 +143,7 @@ if ($format =~ /dump/) {
        print Dumper($emsg, $canontext, \@results);
 }
 
        print Dumper($emsg, $canontext, \@results);
 }
 
-$dbh->rollback();
+$mydbh->rollback() if $mydbh;
 
 return  $emsg,
        $canontext,
 
 return  $emsg,
        $canontext,