1 # This is part of the YARRG website. YARRG is a tool and website
2 # for assisting players of Yohoho Puzzle Pirates.
4 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 # Copyright (C) 2009 Clare Boothby
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.
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.
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.
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/>.
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.
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.
48 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
51 @EXPORT = qw(&dbw_connect &ocean_list &sourcebasedir
52 &to_json_shim &to_json_protecttags
54 &expected_error &dbw_lookup_string
55 &prettyprint_age &meta_prettyprint_age);
61 sub dotperllibdir () {
65 if ($dir =~ m/\.perl-lib$/) {
69 die "no appropriate dotperllib dir in @INC";
72 sub sourcebasedir () {
73 return dotperllibdir().'/..';
77 my $edir= $ENV{'YARRG_DATA_DIR'};
78 return $edir if defined $edir;
79 my $dir= dotperllibdir();
80 if (stat "$dir/DATA") {
82 } elsif ($!==&ENOENT) {
85 die "stat $dir/DATA $!";
93 my $datadir= datadir();
95 my $fn= "$datadir/source-info.txt";
96 my $f= new IO::File $fn or die "$fn $!";
99 next unless m/^ocean\s+(\S.*\S)\s*$/;
102 $f->error and die $!;
109 sub dbw_connect ($) {
111 die "unknown ocean $ocean ?"
112 unless grep { $_ eq $ocean } ocean_list();
113 return dbr_connect(datadir(), $ocean);
116 sub to_json_shim ($) {
118 # In JSON.pm 2.x, jsonToObj prints a warning to stderr which
119 # our callers don't like at all.
120 if ($JSON::VERSION >= 2.0) {
121 return to_json($obj);
123 return objToJson($obj);
127 sub to_json_protecttags ($) {
129 my $j= to_json_shim($v);
134 sub meta_prettyprint_age ($$$) {
135 my ($age,$floor,$plus) = @_;
137 $age < 60 ? 'less than a minute' :
138 $age < 60*2 ? '1 minute' :
139 $age < 3600*2 ? $floor ($age/60) $plus' minutes' :
140 $age < 86400*2 ? $floor ($age/3600) $plus ' hours' :
141 $floor ($age/86400) $plus ' days';
146 sub prettyprint_age ($) {
148 '.meta_prettyprint_age('$age','floor','.').'
155 sub dbw_lookup_string ($$$$$$$$) { # => ( $emsg, @dbresults )
157 $sth, $stmt_nqs, $abbrev_initials, $maxambig,
158 $em_nomatch, $em_manyambig, $emf_ambiguous) = @_;
160 $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g;
163 my @pats= ("$each", "$each \%", "$each\%", "\%$each\%");
164 if ($abbrev_initials) {
165 push @pats, join ' ', map { "$_%" } split //, $each;
167 foreach my $pat (@pats) {
168 $sth->execute(($pat) x $stmt_nqs);
169 $results= $sth->fetchall_arrayref();
170 last if @$results==1;
171 $m{ $_->[0] }=1 for @$results;
177 } elsif (keys(%m) > $maxambig) {
178 return $em_manyambig;
180 return $emf_ambiguous->($each, join(', ', sort keys %m));
183 return (undef, @{ $results->[0] });
186 sub expected_error ($) {
187 my $r= { Emsg => $_[0] };
188 bless $r, 'CommodsWeb::ExpectedError';
192 package CommodsWeb::ExpectedError;
196 return $self->{Emsg};