chiark / gitweb /
686a506aa4c348bfefbc2b0f73fd2e160785937a
[ypp-sc-tools.db-test.git] / yarrg / web / qtextstringcheck
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
9   YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10   The YARRG website is covered by the GNU Affero GPL v3 or later, which
11    basically means that every installation of the website will let you
12    download the source.
13
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Affero General Public License as
16  published by the Free Software Foundation, either version 3 of the
17  License, or (at your option) any later version.
18
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU Affero General Public License for more details.
23
24  You should have received a copy of the GNU Affero General Public License
25  along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28  are used without permission.  This program is not endorsed or
29  sponsored by Three Rings.
30
31
32  This Mason component handles the generic output format options for
33  text string parsers/checkers like check_routestring.
34
35 # typical url for this script:
36 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/qtextstring?what=routestring?format=json&ocean=Midnight&string=d
37
38 </%doc>
39
40 <%args>
41 $ocean
42 $format
43 $ctype => undef
44 $string
45 $what
46 $dbh => undef
47 </%args>
48
49 <%flags>
50 inherit => undef
51 </%flags>
52 <%perl>
53
54 use JSON;
55 use Data::Dumper;
56 use HTML::Entities;
57 use CommodsWeb;
58
59 die if $what =~ m/[^a-z]/;
60 my $chk= $m->fetch_comp("check_${what}");
61
62 my $mydbh;
63 $dbh ||= ($mydbh= dbw_connect($ocean));
64
65 #print STDERR "qtsc string=\`$string'\n";
66
67 my ($sth, @sqlstmt_qs);
68 if ($chk->method_exists('sqlstmt')) {
69         my $sqlstmt= $chk->scall_method("sqlstmt");
70         $sth= $dbh->prepare($sqlstmt);
71         @sqlstmt_qs= $sqlstmt =~ m/\?/g;
72 }
73
74 my $emsg= '';
75 my @results;
76 my @specs;
77 my $canontext;
78 my $hooks = {   Emsg => \$emsg,         String => \$string,
79                 Results => \@results,   Specs => \@specs,
80                 Canon => \$canontext
81             };
82
83 if ($chk->method_exists('preparse')) {
84         $chk->call_method('preparse', h => $hooks);
85 } else {
86         @specs= $chk->attr('multiple') ? (split m#[/|,]#, $string) : ($string);
87 }
88
89 no warnings qw(exiting);
90
91 foreach my $each (@specs) {
92         $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
93         next if !length $each;
94         my $err= sub { $emsg= $_[0]; last; };
95         my %m;
96         my $results;
97         my @pats= ("$each", "$each\%", "\%$each\%");
98         if ($chk->attr_exists('abbrev_initials')) {
99                 push @pats, join ' ', map { "$_%" } split //, $each;
100         }
101         foreach my $pat (@pats) {
102                 $sth->execute(($pat) x @sqlstmt_qs);
103                 $results= $sth->fetchall_arrayref();
104                 last if @$results==1;
105                 map { $m{ $_->[0] }=1 } @$results;
106                 $results= undef;
107         }
108         if (!$results) {
109                 if (!%m) {
110                         $err->($chk->scall_method("nomatch",
111                                 spec => $each));
112                 } elsif (keys(%m) > $chk->attr('maxambig')) {
113                         $err->($chk->scall_method("manyambig"));
114                 } else {
115                         $err->($chk->scall_method("ambiguous",
116                                 spec => $each,
117                                 couldbe => join(', ', sort keys %m)));
118                 }
119         }
120         push @results, $results->[0];
121 };
122
123 if (!defined $canontext) {
124         $canontext= join ' | ', map { $_->[0] } @results;
125 }
126 if ($chk->method_exists('postquery')) {
127         $chk->call_method('postquery', h => $hooks);
128 }
129
130 $emsg='' if !defined $emsg;
131 @results=() if length $emsg;
132
133 #print STDERR "qtsc emsg=\`$emsg' results=\`@results'\n";
134
135 if ($format =~ /json/) {
136         $r->content_type($ctype or $format);
137         my $jobj= {
138                 success => 1*!length $emsg,
139                 show => (length $emsg      ? $emsg                       :
140                          length $canontext ? encode_entities($canontext) :
141                                              '&nbsp;'),
142         };
143         print to_json_shim($jobj);
144 }
145 if ($format =~ /dump/) {
146         $r->content_type('text/plain');
147         print Dumper($emsg, $canontext, \@results);
148 }
149
150 $mydbh->rollback() if $mydbh;
151
152 return  $emsg,
153         $canontext,
154         [ @results ];
155
156 </%perl>