chiark / gitweb /
0f3f43df8fd236975a57ef5310cccdbf4f14b25f
[ypp-sc-tools.db-test.git] / yarrg / CommodsWeb.pm
1 # This is part of the YARRG website.  YARRG is a tool and website
2 # for assisting players of Yohoho Puzzle Pirates.
3 #
4 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 # Copyright (C) 2009 Clare Boothby
6 #
7 #  YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
8 #  The YARRG website is covered by the GNU Affero GPL v3 or later, which
9 #   basically means that every installation of the website will let you
10 #   download the source.
11 #
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU Affero General Public License as
14 # published by the Free Software Foundation, either version 3 of the
15 # License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU Affero General Public License for more details.
21 #
22 # You should have received a copy of the GNU Affero General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 #
25 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
26 # are used without permission.  This program is not endorsed or
27 # sponsored by Three Rings.
28
29
30 # This Perl module is used by the Mason scripts in yarrg/web/.
31 # We look for a symlink DATA to the actual data to use, so that
32 # the data uploader and website displayer can use different code.
33
34 package CommodsWeb;
35
36 use strict;
37 use warnings;
38
39 use DBI;
40 use POSIX;
41 use JSON;
42
43 use Commods;
44 use CommodsDatabase;
45
46 BEGIN {
47     use Exporter ();
48     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
49     $VERSION     = 1.00;
50     @ISA         = qw(Exporter);
51     @EXPORT      = qw(&dbw_connect &dbw_filename &ocean_list &sourcebasedir
52                       &to_json_shim &to_json_protecttags
53                       &set_ctype_utf8 &webdatadir
54                       &expected_error &dbw_lookup_string
55                       &prettyprint_age &meta_prettyprint_age);
56     %EXPORT_TAGS = ( );
57
58     @EXPORT_OK   = qw();
59 }
60
61 sub dotperllibdir () {
62     my $dir;
63     
64     for my $dir (@INC) {
65         if ($dir =~ m/\.perl-lib$/) {
66             return $dir;
67         }
68     }
69     die "no appropriate dotperllib dir in @INC";
70 }
71
72 sub sourcebasedir () {
73     return dotperllibdir().'/..';
74 }
75
76 sub some_datadir ($) {
77     my ($what) = @_;
78     my $edir= $ENV{"YARRG_${what}_DIR"};
79     return $edir if defined $edir;
80     my $dir= dotperllibdir();
81     my $dirwhat= "$dir/$what";
82     if (stat $dirwhat) {
83         return $dirwhat;
84     } elsif ($!==&ENOENT) {
85         return "$dir";
86     } else {
87         die "stat $dirwhat $!";
88     }
89     return '.';
90 }
91
92 sub webdatadir () { return some_datadir('WEBDATA'); }
93 sub datadir () { return some_datadir('DATA'); }
94
95 my @ocean_list;
96
97 sub ocean_list () {
98     my $datadir= datadir();
99     if (!@ocean_list) {
100         my $fn= "$datadir/source-info.txt";
101         my $f= new IO::File $fn or die "$fn $!";
102         my @r;
103         while (<$f>) {
104             next unless m/^ocean\s+(\S.*\S)\s*$/;
105             push @r, $1;
106         }
107         $f->error and die $!;
108         close $fn;
109         @ocean_list= @r;
110     }
111     return @ocean_list;
112 }
113
114 sub dbw_filename ($) {
115     my ($ocean) = @_;
116     die "unknown ocean $ocean ?"
117         unless grep { $_ eq $ocean } ocean_list();
118     return dbr_filename(datadir(), $ocean);
119 }
120
121 sub dbw_connect ($) {
122     my ($ocean) = @_;
123     my $fn= dbw_filename($ocean);
124     return db_connect_core($fn);
125 }
126
127 sub to_json_shim ($) {
128     my ($obj) = @_;
129     # In JSON.pm 2.x, jsonToObj prints a warning to stderr which
130     # our callers don't like at all.
131     if ($JSON::VERSION >= 2.0) {
132         return to_json($obj);
133     } else {
134         return objToJson($obj);
135     }
136 }
137
138 sub to_json_protecttags ($) {
139     my ($v) = @_;
140     my $j= to_json_shim($v);
141     $j =~ s,/,\\/,g;
142     return $j;
143 }
144
145 sub meta_prettyprint_age ($$$) {
146     my ($age,$floor,$plus) = @_;
147     return <<END;
148         $age < 60 ?             'less than a minute'                    :
149         $age < 60*2 ?           '1 minute'                              :
150         $age < 3600*2 ?         $floor ($age/60) $plus' minutes'        :
151         $age < 86400*2 ?        $floor ($age/3600) $plus ' hours'       :
152                                 $floor ($age/86400) $plus ' days';
153 END
154 };
155
156 BEGIN { eval '
157   sub prettyprint_age ($) {
158                 my ($age) = @_;
159                 '.meta_prettyprint_age('$age','floor','.').'
160   };
161   1;
162 ' or die "$@";
163 }
164
165
166 sub dbw_lookup_string ($$$$$$$$) { # => ( $emsg, @dbresults )
167     my ($each,
168         $sth, $stmt_nqs, $abbrev_initials, $maxambig,
169         $em_nomatch, $em_manyambig, $emf_ambiguous) = @_;
170     
171     $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
172     my %m;
173     my $results;
174     my @pats= ("$each", "$each \%", "$each\%", "\%$each\%");
175     if ($abbrev_initials) {
176         push @pats, join ' ', map { "$_%" } split //, $each;
177     }
178     foreach my $pat (@pats) {
179         $sth->execute(($pat) x $stmt_nqs);
180         $results= $sth->fetchall_arrayref();
181         last if @$results==1;
182         $m{ $_->[0] }=1 for @$results;
183         $results= undef;
184     }
185     if (!$results) {
186         if (!%m) {
187             return $em_nomatch;
188         } elsif (keys(%m) > $maxambig) {
189             return $em_manyambig;
190         } else {
191             return $emf_ambiguous->($each, join(', ', sort keys %m));
192         }
193     }
194     return (undef, @{ $results->[0] });
195 }
196
197 sub expected_error ($) {
198     my $r= { Emsg => $_[0] };
199     bless $r, 'CommodsWeb::ExpectedError';
200     die $r;
201 }
202
203 package CommodsWeb::ExpectedError;
204
205 sub emsg ($) {
206     my ($self) = @_;
207     return $self->{Emsg};
208 }
209
210 1;