<%doc> This is part of the YARRG website. YARRG is a tool and website for assisting players of Yohoho Puzzle Pirates. Copyright (C) 2009 Ian Jackson Copyright (C) 2009 Clare Boothby YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later). The YARRG website is covered by the GNU Affero GPL v3 or later, which basically means that every installation of the website will let you download the source. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . Yohoho and Puzzle Pirates are probably trademarks of Three Rings and are used without permission. This program is not endorsed or sponsored by Three Rings. This Mason component handles the generic output format options for text string parsers/checkers like check_routestring. # typical url for this script: # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/qtextstring?what=routestring?format=json&ocean=Midnight&string=d <%args> $ocean $format $ctype => undef $string $what $dbh => undef $debug => 0 <%flags> inherit => undef <%perl> use JSON; use Data::Dumper; use HTML::Entities; use CommodsWeb; use Scalar::Util qw(blessed); die if $what =~ m/[^a-z]/; my $chk= $m->fetch_comp("check_${what}"); my $mydbh; $dbh ||= ($mydbh= dbw_connect($ocean)); my $debugf= !$debug ? sub { } : sub { print "@_\n"; }; $debugf->("QTSC STRING '$string'"); my $emsg= ''; my @results; my $canontext; $string =~ s/^\s*//; $string =~ s/\s$//; $string =~ s/\s+/ /g; if ($chk->method_exists('execute')) { ($canontext, @results)= eval { $chk->call_method('execute', dbh => $dbh, string => $string, debugf => $debugf); }; if ($@) { die unless blessed $@ && $@->isa('CommodsWeb::ExpectedError'); $emsg= $@->emsg(); } } else { my $sqlstmt= $chk->scall_method("sqlstmt"); my $sth= $dbh->prepare($sqlstmt); my @sqlstmt_nqs= $sqlstmt =~ m/\?/g; my $sqlstmt_nqs= @sqlstmt_nqs; my @specs= $chk->attr('multiple') ? (split m#\s*[/|,]\s*#, $string) : ($string); foreach my $each (@specs) { next unless $each =~ m/\S/; my ($temsg, @tresults) = dbw_lookup_string($each, $sth, $sqlstmt_nqs, $chk->attr_exists('abbrev_initials'), $chk->attr('maxambig'), $chk->scall_method("nomatch", spec => $each), $chk->scall_method("manyambig"), sub { $chk->scall_method("ambiguous", spec => $each, couldbe => $_[1]) }); if (defined $temsg) { $emsg= $temsg; last; } push @results, [ @tresults ]; }; } if (!defined $canontext) { $canontext= join ' | ', map { $_->[0] } @results; } $emsg='' if !defined $emsg; @results=() if length $emsg; $debugf->("QTSC EMSG='$emsg' RESULTS='@results'"); if ($format =~ /json/) { $ctype ||= $format; die unless grep { $_ eq $ctype } qw(application/json text/plain text/xml); $r->content_type($ctype); my $jobj= { success => 1*!length $emsg, show => (length $emsg ? $emsg : length $canontext ? encode_entities($canontext) : ' '), }; print to_json_shim($jobj); } if ($format =~ /dump/) { $r->content_type('text/plain'); print Dumper($emsg, $canontext, \@results); } $mydbh->rollback() if $mydbh; return $emsg, $canontext, @results;