11 |
use Carp qw/confess/; |
use Carp qw/confess/; |
12 |
use Jifty; |
use Jifty; |
13 |
|
|
14 |
|
use File::Slurp; |
15 |
|
use JSON::XS; |
16 |
|
use Carp qw/confess/; |
17 |
|
|
18 |
our $debug = 0; |
our $debug = 0; |
19 |
|
|
20 |
=head1 NAME |
=head1 NAME |
36 |
=cut |
=cut |
37 |
|
|
38 |
our $instance_dbh; |
our $instance_dbh; |
39 |
|
our @instances_active; |
40 |
|
|
41 |
sub dbh { |
sub dbh { |
42 |
my $self = shift; |
my $self = shift; |
57 |
|
|
58 |
Jifty->log->info("Connect to instance $instance with dsn $dsn"); |
Jifty->log->info("Connect to instance $instance with dsn $dsn"); |
59 |
|
|
60 |
my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr; |
my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die "$DBI::errstr\n"; |
61 |
|
|
62 |
|
# force database to send us back UTF-8 no metter what it's encoding |
63 |
|
$dbh->do("set client_encoding='utf-8'"); |
64 |
|
$dbh->{pg_enable_utf8} = 1; |
65 |
|
|
66 |
$instance_dbh->{$instance} = $dbh; |
$instance_dbh->{$instance} = $dbh; |
67 |
|
push @instances_active, $instance; |
68 |
|
|
69 |
|
if ( $#instances_active > 5 ) { |
70 |
|
my $i = shift @instances_active; |
71 |
|
warn "## remove connection to instance $instance\n"; |
72 |
|
delete( $instance_dbh->{$i} ); |
73 |
|
} |
74 |
|
|
75 |
warn "## instance_dbh = ",dump( $instance_dbh ) if $debug; |
warn "## instance_dbh = ",dump( $instance_dbh ) if $debug; |
76 |
|
|
187 |
|
|
188 |
} |
} |
189 |
|
|
190 |
=head2 sitemap |
=head2 sites |
191 |
|
|
192 |
my $sitemap = $strix->sitemap( $site_id, $uid ); |
my @sites = $strix->sites; |
193 |
|
|
194 |
=cut |
=cut |
195 |
|
|
196 |
sub sitemap { |
sub sites { |
197 |
my $self = shift; |
my $self = shift; |
198 |
|
|
199 |
my ( $site_id, $uid ) = @_; |
my @sites; |
|
my $sitemap; |
|
200 |
|
|
201 |
my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?"; |
my $sth = $self->dbh->prepare( |
202 |
$query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id; |
"SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" |
203 |
|
); |
204 |
my $sth = $self->dbh->prepare( $query ); |
$sth->execute; |
|
if ( $site_id ) { |
|
|
$sth->execute( $site_id ); |
|
|
} else { |
|
|
$sth->execute; |
|
|
} |
|
205 |
|
|
206 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
207 |
warn dump( $row ) if $debug; |
push @sites, $row; |
|
|
|
|
$sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid ); |
|
208 |
} |
} |
209 |
|
|
210 |
return $sitemap; |
return @sites; |
211 |
} |
} |
212 |
|
|
213 |
=head2 site_navigation |
=head2 site_navigation |
224 |
$uid ||= 1; # anonymous |
$uid ||= 1; # anonymous |
225 |
# $uid ||= 2; # admin |
# $uid ||= 2; # admin |
226 |
|
|
227 |
|
my $cache_format = 'site-%d-navigation-for-uid-%d.js'; |
228 |
|
if ( my $data = $self->read_cache( $cache_format, $site_id, $uid ) ) { |
229 |
|
return $data; |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
my $sth = $self->dbh->prepare( |
my $sth = $self->dbh->prepare( |
234 |
"SELECT kategorija.*, ((length(prikaz)+length(coalesce(ordstr,'')))/3)-1 as depth FROM kategorija JOIN navigacija ON (kategorija.id = kategorija_id), site WHERE site_id = ? AND site.id = site_id AND userCanDoOnObject(?, 1, 'kats', kategorija.id) ORDER BY prikaz"); |
"SELECT kategorija.*, ((length(prikaz)+length(coalesce(ordstr,'')))/3)-1 as depth FROM kategorija JOIN navigacija ON (kategorija.id = kategorija_id), site WHERE site_id = ? AND site.id = site_id AND userCanDoOnObject(?, 1, 'kats', kategorija.id) ORDER BY prikaz"); |
235 |
$sth->execute( $site_id, $uid ); |
$sth->execute( $site_id, $uid ); |
242 |
|
|
243 |
while (my $kat = $sth->fetchrow_hashref() ) { |
while (my $kat = $sth->fetchrow_hashref() ) { |
244 |
warn "# kat = ",dump( $kat ) if $debug; |
warn "# kat = ",dump( $kat ) if $debug; |
245 |
die "no depth" unless $kat->{depth}; |
if ( ! $kat->{depth} ) { |
246 |
|
Jifty->log->error("depth increased to 1 in ",dump( $kat )); |
247 |
|
$kat->{depth} = 1; |
248 |
|
} |
249 |
|
|
250 |
my $node = { type => 'category' }; |
my $node = { type => 'category' }; |
251 |
foreach my $c ( qw/naziv url/ ) { |
foreach my $c ( qw/naziv url/ ) { |
253 |
} |
} |
254 |
|
|
255 |
my $depth = $kat->{depth}; |
my $depth = $kat->{depth}; |
256 |
|
if ( ! defined $pos[ $depth - 2 ] ) { |
257 |
|
warn "FIXING CATEGORY: ",dump( $kat ); |
258 |
|
$node->{class} = "error"; |
259 |
|
$depth--; |
260 |
|
} |
261 |
@pos = splice( @pos, 0, $depth ); |
@pos = splice( @pos, 0, $depth ); |
262 |
$pos[ $depth - 1 ]++; |
$pos[ $depth - 1 ]++; |
263 |
|
|
314 |
|
|
315 |
} |
} |
316 |
|
|
317 |
|
$self->write_cache( $navigation, $cache_format, $site_id, $uid ); |
318 |
|
|
319 |
return $navigation; |
return $navigation; |
320 |
|
|
321 |
} |
} |
322 |
|
|
323 |
|
=head2 cache_path |
324 |
|
|
325 |
|
my $path = $strix->cache_path( 'format-%d', $var, ... ); |
326 |
|
|
327 |
|
=cut |
328 |
|
|
329 |
|
sub cache_path { |
330 |
|
my $self = shift; |
331 |
|
|
332 |
|
warn "# cache_path",dump( @_ ); |
333 |
|
|
334 |
|
my $path = Jifty::Util->absolute_path( 'var/strix' ); |
335 |
|
|
336 |
|
if ( ! -e $path ) { |
337 |
|
mkdir $path || die "can't create $path: $!"; |
338 |
|
} |
339 |
|
|
340 |
|
$path .= '/' . sprintf( shift, @_ ); # XXX shift is important here! |
341 |
|
return $path; |
342 |
|
} |
343 |
|
|
344 |
|
=head2 write_cache |
345 |
|
|
346 |
|
write_cache( $data, 'format-%d', $var, ... ); |
347 |
|
|
348 |
|
=cut |
349 |
|
|
350 |
|
sub write_cache { |
351 |
|
my $self = shift; |
352 |
|
my $data = shift || confess "no data?"; |
353 |
|
my $path = $self->cache_path( @_ ); |
354 |
|
write_file( $path, encode_json( $data )) || die "can't save into $path: $!"; |
355 |
|
} |
356 |
|
|
357 |
|
=head2 read_cache |
358 |
|
|
359 |
|
my $data = read_cache( 'format-%d', $var ... ); |
360 |
|
|
361 |
|
=cut |
362 |
|
|
363 |
|
sub read_cache { |
364 |
|
my $self = shift; |
365 |
|
my $path = $self->cache_path( @_ ); |
366 |
|
return unless -e $path; |
367 |
|
warn "# read_cache( $path )"; |
368 |
|
return decode_json( read_file( $path ) ) || die "can't read $path: $!"; |
369 |
|
} |
370 |
|
|
371 |
1; |
1; |