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 parses textual strings giving lists of islands
33 and archipelagoes, ie textual route strings.
45 # typical url for this script:
46 # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/routetextstring?format=json&ocean=Midnight&string=d
54 my $dbh= dbw_connect($ocean);
56 my $sth= $dbh->prepare("SELECT archipelago,islandid,islandname
57 FROM islands WHERE islandname LIKE ?
58 UNION ALL SELECT DISTINCT archipelago,NULL,archipelago
59 FROM islands WHERE archipelago LIKE ?");
61 my (@results, $canontext);
62 my ($output, $output_wrong);
64 if ($format =~ /json/) {
65 $r->content_type($ctype or $format);
66 $output= sub { print to_json({
68 show => length $canontext ? encode_entities($canontext)
71 $output_wrong= sub { print to_json({
76 if ($format =~ /return/) {
77 $output= sub { return { Error => '', Results => \@results }; };
78 $output_wrong= sub { return { Error => $_[0] }; };
80 if ($format =~ /dump/) {
81 $r->content_type('text/plain');
82 $output_wrong= sub { print Dumper(\@_); };
83 $output= sub { print Dumper(\@results, $canontext); };
86 foreach my $each (split m#[/|,]#, $string) {
87 $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g;
88 next if !length $each;
90 my $msg= sprintf $_[0], encode_entities($each);
91 $output_wrong->($msg);
95 foreach my $pat ("$each\%", "\%$each\%") {
96 $sth->execute($pat,$pat);
97 $results= $sth->fetchall_arrayref();
99 map { $m{ $_->[2] }=1 } @$results;
104 return $err->('no island or arch matches "%s"');
106 return $err->(' ');
108 return $err->('ambiguous island or arch "%s",'.
109 ' could be '.join(', ', sort keys %m));
112 push @results, $results->[0];
115 $canontext= join ' | ', map { $_->[2] } @results;