/[A3C]/lib/Strix.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/Strix.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 213 by dpavlin, Fri Jun 20 20:44:18 2008 UTC revision 216 by dpavlin, Fri Jun 20 21:49:16 2008 UTC
# Line 14  use Jifty; Line 14  use Jifty;
14  use File::Slurp;  use File::Slurp;
15  use JSON::XS;  use JSON::XS;
16  use Carp qw/confess/;  use Carp qw/confess/;
17    use URI::Escape;
18    
19  our $debug = 0;  our $debug = 0;
20    
# Line 88  sub category { Line 89  sub category {
89    
90          my $url = shift || confess "no url";          my $url = shift || confess "no url";
91    
92            my $data = $self->read_cache( uri_escape($url) );
93            return $data if $data;
94    
95          # sysinc/profiles.php          # sysinc/profiles.php
96          my $sth = $self->dbh->prepare(qq{          my $sth = $self->dbh->prepare(qq{
97          SELECT kategorija.*, lang.langid, lang.locale, template.tfilename, template.tflags, site.naziv as sitename, site.admin_mail, site.address, site.root as site_root, getPathFromNav(kategorija.id) as path, site.ordstr as site_ordstr FROM kategorija, template, site, lang WHERE kategorija.url = ? AND kategorija.template_id = template.id AND kategorija.site_id = site.id AND lang.id = kategorija.lang          SELECT kategorija.*, lang.langid, lang.locale, template.tfilename, template.tflags, site.naziv as sitename, site.admin_mail, site.address, site.root as site_root, getPathFromNav(kategorija.id) as path, site.ordstr as site_ordstr FROM kategorija, template, site, lang WHERE kategorija.url = ? AND kategorija.template_id = template.id AND kategorija.site_id = site.id AND lang.id = kategorija.lang
98          });          });
99          $sth->execute($url);          $sth->execute($url);
100    
101          my $category = $sth->fetchrow_hashref() or die "can't fetch category $url";          my $category = $sth->fetchrow_hashref() or die "can't fetch category $url from instance ",$self->instance,"\n";
102            $self->write_cache( $category, uri_escape($url) );
103          return $category;          return $category;
104  }  }
105    
# Line 109  sub layout { Line 114  sub layout {
114    
115          my $url = shift || confess "no url";          my $url = shift || confess "no url";
116    
117            my $data = $self->read_cache( uri_escape($url) );
118            return $data if $data;
119    
120          my $dbh = $self->dbh;          my $dbh = $self->dbh;
121          my $category = $self->category( $url );          my $category = $self->category( $url );
122    
# Line 183  sub layout { Line 191  sub layout {
191                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
192          }          }
193    
194            $self->write_cache( $page, uri_escape($url) );
195    
196          return $page;          return $page;
197    
198  }  }
# Line 196  sub layout { Line 206  sub layout {
206  sub sites {  sub sites {
207          my $self = shift;          my $self = shift;
208    
209          my @sites;          my @sites = $self->read_cache;
210            return @sites if @sites;
211    
212          my $sth = $self->dbh->prepare(          my $sth = $self->dbh->prepare(
213          "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr"          "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr"
# Line 207  sub sites { Line 218  sub sites {
218                  push @sites, $row;                  push @sites, $row;
219          }          }
220    
221            $self->write_cache( \@sites );
222    
223          return @sites;          return @sites;
224  }  }
225    
# Line 224  sub site_navigation { Line 237  sub site_navigation {
237          $uid ||= 1; # anonymous          $uid ||= 1; # anonymous
238  #       $uid ||= 2; # admin  #       $uid ||= 2; # admin
239    
240          my $cache_format = 'site-%d-navigation-for-uid-%d.js';          my $data = $self->read_cache( $site_id, $uid );
241          if ( my $data = $self->read_cache( $cache_format, $site_id, $uid ) ) {          return $data if $data;
                 return $data;  
         }  
   
242    
243          my $sth = $self->dbh->prepare(          my $sth = $self->dbh->prepare(
244          "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");
# Line 314  sub site_navigation { Line 324  sub site_navigation {
324    
325          }          }
326    
327          $self->write_cache( $navigation, $cache_format, $site_id, $uid );          $self->write_cache( $navigation, $site_id, $uid );
328    
329          return $navigation;          return $navigation;
330    
# Line 322  sub site_navigation { Line 332  sub site_navigation {
332    
333  =head2 cache_path  =head2 cache_path
334    
335    my $path = $strix->cache_path( 'format-%d', $var, ... );  Generate unique name for specified values
336    
337      my $path = $strix->cache_path( $var, ... );
338    
339    Variables have to be path-safe (e.g. use C<uri_encode> if needed)
340    
341  =cut  =cut
342    
343  sub cache_path {  sub cache_path {
344          my $self = shift;          my $self = shift;
345    
346          warn "# cache_path",dump( @_ );          #warn "# cache_path",dump( @_ );
347    
348          my $path = Jifty::Util->absolute_path( 'var/strix' );          my $path = Jifty::Util->absolute_path( 'var/strix' );
349    
# Line 337  sub cache_path { Line 351  sub cache_path {
351                  mkdir $path || die "can't create $path: $!";                  mkdir $path || die "can't create $path: $!";
352          }          }
353    
354          $path .= '/' . sprintf( shift, @_ );    # XXX shift is important here!          #warn "## caller = ",dump( (caller(2))[3] );
355          return $path;          my $uid = (caller(2))[3];
356            $uid =~ s/^[^:]+:://;
357            $uid .= '-' . join('-', @_) if @_;
358            $uid .= '.js';
359    
360            return $path . '/' . $self->instance . '-' . $uid;
361  }  }
362    
363  =head2 write_cache  =head2 write_cache
364    
365    write_cache( $data, 'format-%d', $var, ... );    write_cache( $data, $key_var, ... );
366    
367  =cut  =cut
368    
# Line 364  sub read_cache { Line 383  sub read_cache {
383          my $self = shift;          my $self = shift;
384          my $path = $self->cache_path( @_ );          my $path = $self->cache_path( @_ );
385          return unless -e $path;          return unless -e $path;
386          warn "# read_cache( $path )";          #warn "# read_cache( $path )";
387          return decode_json( read_file( $path ) ) || die "can't read $path: $!";          return decode_json( read_file( $path ) ) || die "can't read $path: $!";
388  }  }
389    

Legend:
Removed from v.213  
changed lines
  Added in v.216

  ViewVC Help
Powered by ViewVC 1.1.26