chiark / gitweb /
Merge ../ypp-sc-tools.pctb-dict-test
[ypp-sc-tools.db-live.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         foreach my $pat ("$each", "$each\%", "\%$each\%") {
98                 $sth->execute(($pat) x @sqlstmt_qs);
99                 $results= $sth->fetchall_arrayref();
100                 last if @$results==1;
101                 map { $m{ $_->[0] }=1 } @$results;
102                 $results= undef;
103         }
104         if (!$results) {
105                 if (!%m) {
106                         $err->($chk->scall_method("nomatch",
107                                 spec => $each));
108                 } elsif (keys(%m) > $chk->attr('maxambig')) {
109                         $err->($chk->scall_method("manyambig"));
110                 } else {
111                         $err->($chk->scall_method("ambiguous",
112                                 spec => $each,
113                                 couldbe => join(', ', sort keys %m)));
114                 }
115         }
116         push @results, $results->[0];
117 };
118
119 if (!defined $canontext) {
120         $canontext= join ' | ', map { $_->[0] } @results;
121 }
122 if ($chk->method_exists('postquery')) {
123         $chk->call_method('postquery', h => $hooks);
124 }
125
126 $emsg='' if !defined $emsg;
127 @results=() if length $emsg;
128
129 #print STDERR "qtsc emsg=\`$emsg' results=\`@results'\n";
130
131 if ($format =~ /json/) {
132         $r->content_type($ctype or $format);
133         my $jobj= {
134                 success => 1*!length $emsg,
135                 show => (length $emsg      ? $emsg                       :
136                          length $canontext ? encode_entities($canontext) :
137                                              '&nbsp;'),
138         };
139         print to_json_shim($jobj);
140 }
141 if ($format =~ /dump/) {
142         $r->content_type('text/plain');
143         print Dumper($emsg, $canontext, \@results);
144 }
145
146 $mydbh->rollback() if $mydbh;
147
148 return  $emsg,
149         $canontext,
150         [ @results ];
151
152 </%perl>