chiark / gitweb /
Reverse colours of all stripey tables to have dark (different) first
[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 $hooks = {   Emsg => \$emsg,         String => \$string,
78                 Results => \@results,   Specs => \@specs,
79             };
80
81 if ($chk->method_exists('preparse')) {
82         $chk->call_method('preparse', h => $hooks);
83 } else {
84         @specs= $chk->attr('multiple') ? (split m#[/|,]#, $string) : ($string);
85 }
86
87 no warnings qw(exiting);
88
89 foreach my $each (@specs) {
90         $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
91         next if !length $each;
92         my $err= sub { $emsg= $_[0]; last; };
93         my %m;
94         my $results;
95         foreach my $pat ("$each", "$each\%", "\%$each\%") {
96                 $sth->execute(($pat) x @sqlstmt_qs);
97                 $results= $sth->fetchall_arrayref();
98                 last if @$results==1;
99                 map { $m{ $_->[0] }=1 } @$results;
100                 $results= undef;
101         }
102         if (!$results) {
103                 if (!%m) {
104                         $err->($chk->scall_method("nomatch",
105                                 spec => $each));
106                 } elsif (keys(%m) > $chk->attr('maxambig')) {
107                         $err->($chk->scall_method("manyambig"));
108                 } else {
109                         $err->($chk->scall_method("ambiguous",
110                                 spec => $each,
111                                 couldbe => join(', ', sort keys %m)));
112                 }
113         }
114         push @results, $results->[0];
115 };
116
117 my $canontext= join ' | ', map { $_->[0] } @results;
118 if ($chk->method_exists('postquery')) {
119         $hooks->{Canon}= \$canontext;
120         $chk->call_method('postquery', h => $hooks);
121 }
122
123 $emsg='' if !defined $emsg;
124 @results=() if length $emsg;
125
126 #print STDERR "qtsc emsg=\`$emsg' results=\`@results'\n";
127
128 if ($format =~ /json/) {
129         $r->content_type($ctype or $format);
130         my $jobj= {
131                 success => 1*!length $emsg,
132                 show => (length $emsg      ? $emsg                       :
133                          length $canontext ? encode_entities($canontext) :
134                                              '&nbsp;'),
135         };
136         print to_json_shim($jobj);
137 }
138 if ($format =~ /dump/) {
139         $r->content_type('text/plain');
140         print Dumper($emsg, $canontext, \@results);
141 }
142
143 $mydbh->rollback() if $mydbh;
144
145 return  $emsg,
146         $canontext,
147         [ @results ];
148
149 </%perl>