3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
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
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.
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.
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/>.
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.
32 This Mason component handles the generic output format options for
33 text string parsers/checkers like check_routestring.
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
59 use Scalar::Util qw(blessed);
61 die if $what =~ m/[^a-z]/;
62 my $chk= $m->fetch_comp("check_${what}");
65 $dbh ||= ($mydbh= dbw_connect($ocean));
67 my $debugf= !$debug ? sub { } : sub {
71 $debugf->("QTSC STRING '$string'");
81 if ($chk->method_exists('execute')) {
82 ($canontext, @results)= eval {
83 $chk->call_method('execute',
84 dbh => $dbh, string => $string,
88 die unless blessed $@ && $@->isa('CommodsWeb::ExpectedError');
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;
97 my @specs= $chk->attr('multiple')
98 ? (split m#\s*[/|,]\s*#, $string)
101 foreach my $each (@specs) {
102 next unless $each =~ m/\S/;
103 my ($temsg, @tresults) =
104 dbw_lookup_string($each,
106 $chk->attr_exists('abbrev_initials'),
107 $chk->attr('maxambig'),
108 $chk->scall_method("nomatch", spec => $each),
109 $chk->scall_method("manyambig"),
111 $chk->scall_method("ambiguous",
112 spec => $each, couldbe => $_[1])
114 if (defined $temsg) {
118 push @results, [ @tresults ];
122 if (!defined $canontext) {
123 $canontext= join ' | ', map { $_->[0] } @results;
126 $emsg='' if !defined $emsg;
127 @results=() if length $emsg;
129 $debugf->("QTSC EMSG='$emsg' RESULTS='@results'");
131 if ($format =~ /json/) {
133 die unless grep { $_ eq $ctype }
134 qw(application/json text/plain text/xml);
135 $r->content_type($ctype);
137 success => 1*!length $emsg,
138 show => (length $emsg ? $emsg :
139 length $canontext ? encode_entities($canontext) :
142 print to_json_shim($jobj);
144 if ($format =~ /dump/) {
145 $r->content_type('text/plain');
146 print Dumper($emsg, $canontext, \@results);
149 $mydbh->rollback() if $mydbh;