/[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 174 by dpavlin, Mon Jun 16 16:48:16 2008 UTC revision 216 by dpavlin, Fri Jun 20 21:49:16 2008 UTC
# Line 9  __PACKAGE__->mk_accessors( qw(instance u Line 9  __PACKAGE__->mk_accessors( qw(instance u
9  use DBI;  use DBI;
10  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
11  use Carp qw/confess/;  use Carp qw/confess/;
12    use Jifty;
13    
14    use File::Slurp;
15    use JSON::XS;
16    use Carp qw/confess/;
17    use URI::Escape;
18    
19  our $debug = 0;  our $debug = 0;
20    
# Line 31  Strix Line 37  Strix
37  =cut  =cut
38    
39  our $instance_dbh;  our $instance_dbh;
40    our @instances_active;
41    
42  sub dbh {  sub dbh {
43          my $self = shift;          my $self = shift;
44    
45          my $instance = shift || $self->instance || confess "no instance";          my $instance = shift || ref($self) && $self->instance || confess "no instance";
46    
47          return $instance_dbh->{$instance} if $instance_dbh->{$instance};          return $instance_dbh->{$instance} if $instance_dbh->{$instance};
48    
# Line 51  sub dbh { Line 58  sub dbh {
58    
59          Jifty->log->info("Connect to instance $instance with dsn $dsn");          Jifty->log->info("Connect to instance $instance with dsn $dsn");
60    
61          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";
62    
63            # force database to send us back UTF-8 no metter what it's encoding
64            $dbh->do("set client_encoding='utf-8'");
65            $dbh->{pg_enable_utf8} = 1;
66    
67          $instance_dbh->{$instance} = $dbh;          $instance_dbh->{$instance} = $dbh;
68            push @instances_active, $instance;
69    
70            if ( $#instances_active > 5 ) {
71                    my $i = shift @instances_active;
72                    warn "## remove connection to instance $instance\n";
73                    delete( $instance_dbh->{$i} );
74            }
75    
76          warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;          warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;
77    
# Line 71  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 92  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 166  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  }  }
199    
200  =head2 sitemap  =head2 sites
201    
202          my $sitemap = $strix->sitemap( $site_id, $uid );          my @sites = $strix->sites;
203    
204  =cut  =cut
205    
206  sub sitemap {  sub sites {
207          my $self = shift;          my $self = shift;
208    
209          my ( $site_id, $uid ) = @_;          my @sites = $self->read_cache;
210          my $sitemap;          return @sites if @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;  
211    
212          my $sth = $self->dbh->prepare( $query );          my $sth = $self->dbh->prepare(
213          if ( $site_id ) {          "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr"
214                  $sth->execute( $site_id );          );
215          } else {          $sth->execute;
                 $sth->execute;  
         }  
216    
217          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
218                  warn dump( $row ) if $debug;                  push @sites, $row;
   
                 $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid );  
219          }          }
220    
221          return $sitemap;          $self->write_cache( \@sites );
222    
223            return @sites;
224  }  }
225    
226  =head2 site_navigation  =head2 site_navigation
# Line 215  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 $data = $self->read_cache( $site_id, $uid );
241            return $data if $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");
245          $sth->execute( $site_id, $uid );          $sth->execute( $site_id, $uid );
# Line 227  sub site_navigation { Line 252  sub site_navigation {
252    
253          while (my $kat = $sth->fetchrow_hashref() ) {          while (my $kat = $sth->fetchrow_hashref() ) {
254                  warn "# kat = ",dump( $kat ) if $debug;                  warn "# kat = ",dump( $kat ) if $debug;
255                  die "no depth" unless $kat->{depth};                  if ( ! $kat->{depth} ) {
256                            Jifty->log->error("depth increased to 1 in ",dump( $kat ));
257                            $kat->{depth} = 1;
258                    }
259    
260                  my $node = { type => 'category' };                  my $node = { type => 'category' };
261                  foreach my $c ( qw/naziv url/ ) {                  foreach my $c ( qw/naziv url/ ) {
# Line 235  sub site_navigation { Line 263  sub site_navigation {
263                  }                  }
264    
265                  my $depth = $kat->{depth};                  my $depth = $kat->{depth};
266                    if ( ! defined $pos[ $depth - 2 ] ) {
267                            warn "FIXING CATEGORY: ",dump( $kat );
268                            $node->{class} = "error";
269                            $depth--;
270                    }
271                  @pos = splice( @pos, 0, $depth );                  @pos = splice( @pos, 0, $depth );
272                  $pos[ $depth - 1 ]++;                  $pos[ $depth - 1 ]++;
273    
# Line 291  sub site_navigation { Line 324  sub site_navigation {
324    
325          }          }
326    
327            $self->write_cache( $navigation, $site_id, $uid );
328    
329          return $navigation;          return $navigation;
330    
331  }  }
332    
333    =head2 cache_path
334    
335    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
342    
343    sub cache_path {
344            my $self = shift;
345    
346            #warn "# cache_path",dump( @_ );
347    
348            my $path = Jifty::Util->absolute_path( 'var/strix' );
349    
350            if ( ! -e $path ) {
351                    mkdir $path || die "can't create $path: $!";
352            }
353    
354            #warn "## caller = ",dump( (caller(2))[3] );
355            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
364    
365      write_cache( $data, $key_var, ... );
366    
367    =cut
368    
369    sub write_cache {
370            my $self = shift;
371            my $data = shift || confess "no data?";
372            my $path = $self->cache_path( @_ );
373            write_file( $path, encode_json( $data )) || die "can't save into $path: $!";
374    }
375    
376    =head2 read_cache
377    
378            my $data = read_cache( 'format-%d', $var ... );
379    
380    =cut
381    
382    sub read_cache {
383            my $self = shift;
384            my $path = $self->cache_path( @_ );
385            return unless -e $path;
386            #warn "# read_cache( $path )";
387            return decode_json( read_file( $path ) ) || die "can't read $path: $!";
388    }
389    
390  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26