chiark / gitweb /
Fix spelling error in intro
[ypp-sc-tools.main.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
64 my $mydbh;
65 $dbh ||= ($mydbh= dbw_connect($ocean));
66
67 my $debugf= !$debug ? sub { } : sub {
68     print escapeHTML("@_")."\n";
69 };
70
71 $debugf->("QTSC STRING '$string'");
72
73 my $emsg= '';
74 my @results;
75 my $canontext;
76
77 $string =~ s/^\s*//;
78 $string =~ s/\s$//;
79 $string =~ s/\s+/ /g;
80
81 if ($chk->method_exists('execute')) {
82         ($canontext, @results)= eval {
83                 $chk->call_method('execute',
84                                 dbh => $dbh, string => $string,
85                                 debugf => $debugf);
86         };
87         if ($@) {
88                 die unless blessed $@ && $@->isa('CommodsWeb::ExpectedError');
89                 $emsg= $@->emsg();
90         }
91 } else {
92         my $sqlstmt= $chk->scall_method("sqlstmt");
93         my $sth= $dbh->prepare($sqlstmt);
94         my @sqlstmt_nqs= $sqlstmt =~ m/\?/g;
95         my $sqlstmt_nqs= @sqlstmt_nqs;
96
97         my @specs= $chk->attr('multiple')
98                 ? (split m#\s*[/|,]\s*#, $string)
99                 : ($string);
100
101         foreach my $each (@specs) {
102                 next unless $each =~ m/\S/;
103                 my ($temsg, @tresults) =
104                     dbw_lookup_string($each,
105                         $sth, $sqlstmt_nqs,
106                         $chk->attr_exists('abbrev_initials'),
107                         $chk->attr('maxambig'),
108                         $chk->scall_method("nomatch", specq => escerrq($each)),
109                         $chk->scall_method("manyambig"),
110                         sub {
111                                 $chk->scall_method("ambiguous",
112                                         specq => escerrq($each),
113                                         couldbe => $_[1])
114                         });
115                 if (defined $temsg) {
116                         $emsg= $temsg;
117                         last;
118                 }
119                 push @results, [ @tresults ];
120         };
121 }
122
123 if (!defined $canontext) {
124         $canontext= join ' | ', map { $_->[0] } @results;
125 }
126
127 $emsg='' if !defined $emsg;
128 @results=() if length $emsg;
129
130 $debugf->("QTSC EMSG='$emsg' RESULTS='@results'");
131
132 if ($format =~ /json/) {
133         $ctype ||= $format;
134         die unless grep { $_ eq $ctype }
135                 qw(application/json text/plain text/xml);
136         $r->content_type($ctype);
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>