chiark / gitweb /
Slightly improved formatting
[ypp-sc-tools.db-test.git] / yarrg / web / routetextstring
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 parses textual strings giving lists of islands
33  and archipelagoes, ie textual route strings.
34
35
36 </%doc>
37
38 <%flags>
39 inherit => undef
40 </%flags>
41
42 <%args>
43 $ocean
44 $format
45 $ctype => undef
46 $string
47 </%args>
48
49 <%perl>
50
51 # typical url for this script:
52 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/routetextstring?format=json&ocean=Midnight&string=d
53
54
55 use CommodsWeb;
56 use HTML::Entities;
57 use JSON;
58 use Data::Dumper;
59
60 my $dbh= dbw_connect($ocean);
61
62 my $sth= $dbh->prepare("SELECT archipelago,islandid,islandname
63                                 FROM islands WHERE islandname LIKE ?
64         UNION ALL       SELECT DISTINCT archipelago,NULL,archipelago
65                                 FROM islands WHERE archipelago LIKE ?");
66
67 my (@results, $canontext);
68 my ($output, $output_wrong);
69
70 if ($format =~ /json/) {
71         $r->content_type($ctype or $format);
72         $output= sub { print to_json_shim({
73                 success => 1,
74                 show => length $canontext ? encode_entities($canontext)
75                         : '&nbsp;',
76                 })};
77         $output_wrong= sub { print to_json_shim({
78                 success => 0,
79                 show => $_[0],
80                 })};
81 }
82 if ($format =~ /return/) {
83         $output= sub { return { Error => '', Results => \@results }; };
84         $output_wrong= sub { return { Error => $_[0] }; };
85 }
86 if ($format =~ /dump/) {
87         $r->content_type('text/plain');
88         $output_wrong= sub { print Dumper(\@_); };
89         $output= sub { print Dumper(\@results, $canontext); };
90 }
91
92 foreach my $each (split m#[/|,]#, $string) {
93         $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
94         next if !length $each;
95         my $err= sub {
96                 my $msg= sprintf $_[0], encode_entities($each);
97                 $output_wrong->($msg);
98         };
99         my %m;
100         my $results;
101         foreach my $pat ("$each\%", "\%$each\%") {
102                 $sth->execute($pat,$pat);
103                 $results= $sth->fetchall_arrayref();
104                 last if @$results==1;
105                 map { $m{ $_->[2] }=1 } @$results;
106                 $results= undef;
107         }
108         if (!$results) {
109                 if (!%m) {
110                         return $err->('no island or arch matches "%s"');
111                 } elsif (keys(%m) > 5) {
112                         return $err->('&nbsp;');
113                 } else {
114                         return $err->('ambiguous island or arch "%s",'.
115                                 ' could be '.join(', ', sort keys %m));
116                 }
117         }
118         push @results, $results->[0];
119 }
120
121 $canontext= join ' | ', map { $_->[2] } @results;
122 return $output->();
123
124 </%perl>