--- lib/Strix.pm 2008/06/16 21:29:50 182 +++ lib/Strix.pm 2008/06/20 20:44:18 213 @@ -11,6 +11,10 @@ use Carp qw/confess/; use Jifty; +use File::Slurp; +use JSON::XS; +use Carp qw/confess/; + our $debug = 0; =head1 NAME @@ -32,6 +36,7 @@ =cut our $instance_dbh; +our @instances_active; sub dbh { my $self = shift; @@ -52,9 +57,20 @@ Jifty->log->info("Connect to instance $instance with dsn $dsn"); - 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"; + + # force database to send us back UTF-8 no metter what it's encoding + $dbh->do("set client_encoding='utf-8'"); + $dbh->{pg_enable_utf8} = 1; $instance_dbh->{$instance} = $dbh; + push @instances_active, $instance; + + if ( $#instances_active > 5 ) { + my $i = shift @instances_active; + warn "## remove connection to instance $instance\n"; + delete( $instance_dbh->{$i} ); + } warn "## instance_dbh = ",dump( $instance_dbh ) if $debug; @@ -171,35 +187,27 @@ } -=head2 sitemap +=head2 sites - my $sitemap = $strix->sitemap( $site_id, $uid ); + my @sites = $strix->sites; =cut -sub sitemap { +sub sites { my $self = shift; - my ( $site_id, $uid ) = @_; - my $sitemap; + my @sites; - my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?"; - $query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id; - - my $sth = $self->dbh->prepare( $query ); - if ( $site_id ) { - $sth->execute( $site_id ); - } else { - $sth->execute; - } + my $sth = $self->dbh->prepare( + "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" + ); + $sth->execute; while (my $row = $sth->fetchrow_hashref() ) { - warn dump( $row ) if $debug; - - $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid ); + push @sites, $row; } - return $sitemap; + return @sites; } =head2 site_navigation @@ -216,6 +224,12 @@ $uid ||= 1; # anonymous # $uid ||= 2; # admin + my $cache_format = 'site-%d-navigation-for-uid-%d.js'; + if ( my $data = $self->read_cache( $cache_format, $site_id, $uid ) ) { + return $data; + } + + my $sth = $self->dbh->prepare( "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"); $sth->execute( $site_id, $uid ); @@ -228,7 +242,10 @@ while (my $kat = $sth->fetchrow_hashref() ) { warn "# kat = ",dump( $kat ) if $debug; - die "no depth" unless $kat->{depth}; + if ( ! $kat->{depth} ) { + Jifty->log->error("depth increased to 1 in ",dump( $kat )); + $kat->{depth} = 1; + } my $node = { type => 'category' }; foreach my $c ( qw/naziv url/ ) { @@ -297,8 +314,58 @@ } + $self->write_cache( $navigation, $cache_format, $site_id, $uid ); + return $navigation; } +=head2 cache_path + + my $path = $strix->cache_path( 'format-%d', $var, ... ); + +=cut + +sub cache_path { + my $self = shift; + + warn "# cache_path",dump( @_ ); + + my $path = Jifty::Util->absolute_path( 'var/strix' ); + + if ( ! -e $path ) { + mkdir $path || die "can't create $path: $!"; + } + + $path .= '/' . sprintf( shift, @_ ); # XXX shift is important here! + return $path; +} + +=head2 write_cache + + write_cache( $data, 'format-%d', $var, ... ); + +=cut + +sub write_cache { + my $self = shift; + my $data = shift || confess "no data?"; + my $path = $self->cache_path( @_ ); + write_file( $path, encode_json( $data )) || die "can't save into $path: $!"; +} + +=head2 read_cache + + my $data = read_cache( 'format-%d', $var ... ); + +=cut + +sub read_cache { + my $self = shift; + my $path = $self->cache_path( @_ ); + return unless -e $path; + warn "# read_cache( $path )"; + return decode_json( read_file( $path ) ) || die "can't read $path: $!"; +} + 1;