chiark / gitweb /
Merge branch 'stable-3.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 </%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 my $sqlstmt= $chk->scall_method("sqlstmt");
66 my $sth= $dbh->prepare($sqlstmt);
67 my @sqlstmt_qs= $sqlstmt =~ m/\?/g;
68
69 #die "$sqlstmt @sqlstmt_qs";
70
71 my $emsg= '';
72 my @results;
73
74 my @specs= $chk->attr('multiple') ? (split m#[/|,]#, $string) : ($string);
75
76 no warnings qw(exiting);
77
78 foreach my $each (@specs) {
79         $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
80         next if !length $each;
81         my $err= sub { $emsg= $_[0]; last; };
82         my %m;
83         my $results;
84         foreach my $pat ("$each", "$each\%", "\%$each\%") {
85                 $sth->execute(($pat) x @sqlstmt_qs);
86                 $results= $sth->fetchall_arrayref();
87                 last if @$results==1;
88                 map { $m{ $_->[0] }=1 } @$results;
89                 $results= undef;
90         }
91         if (!$results) {
92                 if (!%m) {
93                         $err->($chk->scall_method("nomatch",
94                                 spec => $each));
95                 } elsif (keys(%m) > $chk->attr('maxambig')) {
96                         $err->($chk->scall_method("manyambig"));
97                 } else {
98                         $err->($chk->scall_method("ambiguous",
99                                 spec => $each,
100                                 couldbe => join(', ', sort keys %m)));
101                 }
102         }
103         push @results, $results->[0];
104 };
105
106 $emsg='' if !defined $emsg;
107 my $canontext= join ' | ', map { $_->[0] } @results;
108
109 if ($format =~ /json/) {
110         $r->content_type($ctype or $format);
111         my $jobj= {
112                 success => 1*!length $emsg,
113                 show => (length $emsg      ? $emsg                       :
114                          length $canontext ? encode_entities($canontext) :
115                                              '&nbsp;'),
116         };
117         print to_json_shim($jobj);
118 }
119 if ($format =~ /dump/) {
120         $r->content_type('text/plain');
121         print Dumper($emsg, $canontext, \@results);
122 }
123
124 $mydbh->rollback() if $mydbh;
125
126 return  $emsg,
127         $canontext,
128         [ @results ];
129
130 </%perl>