chiark / gitweb /
Revamp qtextstring arrangements
[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 </%args>
47
48 <%flags>
49 inherit => undef
50 </%flags>
51 <%perl>
52
53 use JSON;
54 use Data::Dumper;
55 use HTML::Entities;
56 use CommodsWeb;
57
58 die if $what =~ m/[^a-z]/;
59 my $specifics= "check_${what}";
60 my $specific= $m->fetch_comp($specifics);
61
62 my $dbh= dbw_connect($ocean);
63 my $sqlstmt= $specific->scall_method("sqlstmt");
64 my $sth= $dbh->prepare($sqlstmt);
65 my @sqlstmt_qs= $sqlstmt =~ m/\?/g;
66
67 #die "$sqlstmt @sqlstmt_qs";
68
69 my $emsg= '';
70 my @results;
71
72 my @specs= $specific->attr('multiple') ? (split m#[/|,]#, $string) : ($string);
73
74 foreach my $each (@specs) {
75         $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
76         next if !length $each;
77         my $err= sub { $emsg= $_[0]; last; };
78         my %m;
79         my $results;
80         foreach my $pat ("$each", "$each\%", "\%$each\%") {
81                 $sth->execute(($pat) x @sqlstmt_qs);
82                 $results= $sth->fetchall_arrayref();
83                 last if @$results==1;
84                 map { $m{ $_->[0] }=1 } @$results;
85                 $results= undef;
86         }
87         if (!$results) {
88                 if (!%m) {
89                         $err->($specific->scall_method("nomatch",
90                                 spec => $each));
91                 } elsif (keys(%m) > 5) {
92                         $err->('&nbsp;');
93                 } else {
94                         $err->($specific->scall_method("ambiguous",
95                                 spec => $each,
96                                 couldbe => join(', ', sort keys %m)));
97                 }
98         }
99         push @results, $results->[0];
100 };
101
102 $emsg='' if !defined $emsg;
103 my $canontext= join ' | ', map { $_->[0] } @results;
104
105 if ($format =~ /json/) {
106         $r->content_type($ctype or $format);
107         my $jobj= {
108                 success => 1*!length $emsg,
109                 show => (length $emsg      ? $emsg                       :
110                          length $canontext ? encode_entities($canontext) :
111                                              '&nbsp;'),
112         };
113         print to_json_shim($jobj);
114 }
115 if ($format =~ /dump/) {
116         $r->content_type('text/plain');
117         print Dumper($emsg, $canontext, \@results);
118 }
119
120 $dbh->rollback();
121
122 return  $emsg,
123         $canontext,
124         [ @results ];
125
126 </%perl>