chiark / gitweb /
Minimum trade value feature
[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 $debug => 0
48 </%args>
49
50 <%flags>
51 inherit => undef
52 </%flags>
53 <%perl>
54
55 use JSON;
56 use Data::Dumper;
57 use HTML::Entities;
58 use CommodsWeb;
59 use Scalar::Util qw(blessed);
60
61 die if $what =~ m/[^a-z]/;
62 my $chk= $m->fetch_comp("check_${what}");
63 die "check_$what" unless $chk;
64
65 my $mydbh;
66 $dbh ||= ($mydbh= dbw_connect($ocean));
67
68 my $debugf= !$debug ? sub { } : sub {
69     print escapeHTML("@_")."\n";
70 };
71
72 $debugf->("QTSC STRING '$string'");
73
74 my $emsg= '';
75 my @results;
76 my $canontext;
77
78 $string =~ s/^\s*//;
79 $string =~ s/\s$//;
80 $string =~ s/\s+/ /g;
81
82 if ($chk->method_exists('execute')) {
83         ($canontext, @results)= eval {
84                 $chk->call_method('execute',
85                                 dbh => $dbh, string => $string,
86                                 debugf => $debugf);
87         };
88         if ($@) {
89                 die unless blessed $@ && $@->isa('CommodsWeb::ExpectedError');
90                 $emsg= $@->emsg();
91         }
92 } else {
93         my $sqlstmt= $chk->scall_method("sqlstmt");
94         my $sth= $dbh->prepare($sqlstmt);
95         my @sqlstmt_nqs= $sqlstmt =~ m/\?/g;
96         my $sqlstmt_nqs= @sqlstmt_nqs;
97
98         my @specs= $chk->attr('multiple')
99                 ? (split m#\s*[/|,]\s*#, $string)
100                 : ($string);
101
102         foreach my $each (@specs) {
103                 next unless $each =~ m/\S/;
104                 my ($temsg, @tresults) =
105                     dbw_lookup_string($each,
106                         $sth, $sqlstmt_nqs,
107                         $chk->attr_exists('abbrev_initials'),
108                         $chk->attr('maxambig'),
109                         $chk->scall_method("nomatch", specq => escerrq($each)),
110                         $chk->scall_method("manyambig"),
111                         sub {
112                                 $chk->scall_method("ambiguous",
113                                         specq => escerrq($each),
114                                         couldbe => $_[1])
115                         });
116                 if (defined $temsg) {
117                         $emsg= $temsg;
118                         last;
119                 }
120                 push @results, [ @tresults ];
121         };
122 }
123
124 if (!defined $canontext) {
125         $canontext= join ' | ', map { $_->[0] } @results;
126 }
127
128 $emsg='' if !defined $emsg;
129 @results=() if length $emsg;
130
131 $debugf->("QTSC EMSG='$emsg' RESULTS='@results'");
132
133 if ($format =~ /json/) {
134         $ctype ||= $format;
135         die unless grep { $_ eq $ctype }
136                 qw(application/json text/plain text/xml);
137         $r->content_type($ctype);
138         my $jobj= {
139                 success => 1*!length $emsg,
140                 show => (length $emsg      ? $emsg                       :
141                          length $canontext ? encode_entities($canontext) :
142                                              '&nbsp;'),
143         };
144         print to_json_shim($jobj);
145 }
146 if ($format =~ /dump/) {
147         $r->content_type('text/plain');
148         print Dumper($emsg, $canontext, \@results);
149 }
150
151 $mydbh->rollback() if $mydbh;
152
153 return  $emsg,
154         $canontext,
155         @results;
156
157 </%perl>