/[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 171 by dpavlin, Mon Jun 16 13:16:57 2008 UTC revision 213 by dpavlin, Fri Jun 20 20:44:18 2008 UTC
# Line 4  use strict; Line 4  use strict;
4  use warnings;  use warnings;
5    
6  use base qw(Jifty::Object Class::Accessor::Fast);  use base qw(Jifty::Object Class::Accessor::Fast);
7  __PACKAGE__->mk_accessors( qw(site uid) );  __PACKAGE__->mk_accessors( qw(instance uid) );
8    
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    
18  our $debug = 0;  our $debug = 0;
19    
# Line 20  Strix Line 25  Strix
25    
26  =head2 new  =head2 new
27    
28    my $strix = Strix->new({ site => 'os-test0604-zg' });    my $strix = Strix->new({ instance => 'os-test0604-zg' });
29    
30  =head2 dbh  =head2 dbh
31    
32    my $dbh = Strix->dbh( $site_name );    my $dbh = Strix->dbh( $strix_instance );
33    
34    my $dbh = $strix->dbh;    my $dbh = $strix->dbh;
35    
36  =cut  =cut
37    
38  our $site_dbh;  our $instance_dbh;
39    our @instances_active;
40    
41  sub dbh {  sub dbh {
42          my $self = shift;          my $self = shift;
43    
44          my $site = shift || $self->site || confess "no site";          my $instance = shift || ref($self) && $self->instance || confess "no instance";
45    
46          return $site_dbh->{$site} if $site_dbh->{$site};          return $instance_dbh->{$instance} if $instance_dbh->{$instance};
47    
48          my $config = Jifty->config->app('strix') or die "need strix config";          my $config = Jifty->config->app('strix') or die "need strix config";
49          my $database = $config->{database} or die "no strix.database in config";          my $database = $config->{database} or die "no strix.database in config";
# Line 45  sub dbh { Line 51  sub dbh {
51          Jifty->log->debug("using config ", dump( $database ));          Jifty->log->debug("using config ", dump( $database ));
52    
53          my $dsn =          my $dsn =
54                  'DBI:Pg:dbname=' . $site .                  'DBI:Pg:dbname=' . $instance .
55                  ';host=' . $database->{host} .                  ';host=' . $database->{host} .
56                  ';port=' . $database->{port};                  ';port=' . $database->{port};
57    
58          Jifty->log->info("Connect to site $site 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\n";
61    
62          my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr;          # 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          $site_dbh->{$site} = $dbh;          $instance_dbh->{$instance} = $dbh;
67            push @instances_active, $instance;
68    
69          warn "## site_dbh = ",dump( $site_dbh );          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;
76    
77          return $dbh;          return $dbh;
78  }  }
# Line 116  sub layout { Line 133  sub layout {
133          });          });
134          $sth->execute( $category->{template_id} );          $sth->execute( $category->{template_id} );
135    
136            sub module_args {
137                    my $row = shift;
138                    return undef unless $row->{module_args};
139                    my $args;
140                    foreach my $a ( split(/\&/, $row->{module_args} ) ) {
141                            $args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/;
142                    }
143                    return $args;
144            }
145    
146          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
147                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
148                  push @{ $page->{free} }, { $row->{name} => $row->{module_args} };                  push @{ $page->{free} }, { $row->{name} => module_args( $row ) };
149          }          }
150    
151          warn "### pre layout...\n" if $debug;          warn "### pre layout...\n" if $debug;
# Line 138  sub layout { Line 165  sub layout {
165    
166          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
167                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
168                  push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => $row->{module_args} };                  push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) };
169          }          }
170    
171          warn "### post layout...\n" if $debug;          warn "### post layout...\n" if $debug;
# Line 153  sub layout { Line 180  sub layout {
180    
181          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
182                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
183                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => $row->{module_args} };                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
184          }          }
185    
186          return $page;          return $page;
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;  
   
         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;  
200    
201          my $sth = $self->dbh->prepare( $query );          my $sth = $self->dbh->prepare(
202          if ( $site_id ) {          "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr"
203                  $sth->execute( $site_id );          );
204          } else {          $sth->execute;
                 $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
# Line 205  sub site_navigation { Line 224  sub 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 );
# Line 217  sub site_navigation { Line 242  sub site_navigation {
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/ ) {
# Line 225  sub site_navigation { Line 253  sub site_navigation {
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    
264                  warn "## category depth = $depth pos = ",dump( @pos );                  warn "## category depth = $depth pos = ",dump( @pos ) if $debug;
265    
266                  my $code = '$navigation';                  my $code = '$navigation';
267                  map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;                  map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;
268                  $code =~ s/->{children}$//;                  $code =~ s/->{children}$//;
269                  warn "## category code: $code\n";                  warn "## category code: $code\n" if $debug;
270                  eval $code . '= $node';                  eval $code . '= $node';
271                  if ( $@ ) {                  if ( $@ ) {
272                          warn "SKIPPED CATEGORY: $@ ",dump( $kat );                          warn "SKIPPED CATEGORY: $@ ",dump( $kat );
# Line 267  sub site_navigation { Line 300  sub site_navigation {
300                                                                    
301                                  my $ms_depth = $ms->{depth} + $depth;                                  my $ms_depth = $ms->{depth} + $depth;
302                                  my $p = $pos[ $ms_depth - 1 ]++;                                  my $p = $pos[ $ms_depth - 1 ]++;
303                                  warn "## multistatic depth = $ms_depth pos = ",dump( @pos );                                  warn "## multistatic depth = $ms_depth pos = ",dump( @pos ) if $debug;
304    
305                                  my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';                                  my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';
306                                  warn "## multistatic code: $ms_code\n";                                  warn "## multistatic code: $ms_code\n" if $debug;
307                                  eval $ms_code;                                  eval $ms_code;
308                                  if ( $@ ) {                                  if ( $@ ) {
309                                          warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );                                          warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );
# Line 281  sub site_navigation { Line 314  sub site_navigation {
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;

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

  ViewVC Help
Powered by ViewVC 1.1.26