chiark / gitweb /
Merge branch 'master' into stable-5.x
[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
64 my $mydbh;
65 $dbh ||= ($mydbh= dbw_connect($ocean));
66
67 my $debugf= !$debug ? sub { } : sub {
68     print "@_\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", spec => $each),
109                         $chk->scall_method("manyambig"),
110                         sub {
111                                 $chk->scall_method("ambiguous",
112                                         spec => $each, couldbe => $_[1])
113                         });
114                 if (defined $temsg) {
115                         $emsg= $temsg;
116                         last;
117                 }
118                 push @results, [ @tresults ];
119         };
120 }
121
122 if (!defined $canontext) {
123         $canontext= join ' | ', map { $_->[0] } @results;
124 }
125
126 $emsg='' if !defined $emsg;
127 @results=() if length $emsg;
128
129 $debugf->("QTSC EMSG='$emsg' RESULTS='@results'");
130
131 if ($format =~ /json/) {
132         $ctype ||= $format;
133         die unless grep { $_ eq $ctype }
134                 qw(application/json text/plain text/xml);
135         $r->content_type($ctype);
136         my $jobj= {
137                 success => 1*!length $emsg,
138                 show => (length $emsg      ? $emsg                       :
139                          length $canontext ? encode_entities($canontext) :
140                                              '&nbsp;'),
141         };
142         print to_json_shim($jobj);
143 }
144 if ($format =~ /dump/) {
145         $r->content_type('text/plain');
146         print Dumper($emsg, $canontext, \@results);
147 }
148
149 $mydbh->rollback() if $mydbh;
150
151 return  $emsg,
152         $canontext,
153         @results;
154
155 </%perl>