<%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 <%flags> inherit => undef <%perl> use JSON; use Data::Dumper; use HTML::Entities; use CommodsWeb; die if $what =~ m/[^a-z]/; my $chk= $m->fetch_comp("check_${what}"); my $mydbh; $dbh ||= ($mydbh= dbw_connect($ocean)); my $sqlstmt= $chk->scall_method("sqlstmt"); my $sth= $dbh->prepare($sqlstmt); my @sqlstmt_qs= $sqlstmt =~ m/\?/g; #die "$sqlstmt @sqlstmt_qs"; my $emsg= ''; my @results; my @specs= $chk->attr('multiple') ? (split m#[/|,]#, $string) : ($string); no warnings qw(exiting); foreach my $each (@specs) { $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g; next if !length $each; my $err= sub { $emsg= $_[0]; last; }; my %m; my $results; foreach my $pat ("$each", "$each\%", "\%$each\%") { $sth->execute(($pat) x @sqlstmt_qs); $results= $sth->fetchall_arrayref(); last if @$results==1; map { $m{ $_->[0] }=1 } @$results; $results= undef; } if (!$results) { if (!%m) { $err->($chk->scall_method("nomatch", spec => $each)); } elsif (keys(%m) > $chk->attr('maxambig')) { $err->($chk->scall_method("manyambig")); } else { $err->($chk->scall_method("ambiguous", spec => $each, couldbe => join(', ', sort keys %m))); } } push @results, $results->[0]; }; $emsg='' if !defined $emsg; my $canontext= join ' | ', map { $_->[0] } @results; if ($format =~ /json/) { $r->content_type($ctype or $format); 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 ];