/[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 188 by dpavlin, Tue Jun 17 10:11:46 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  our $debug = 0;  our $debug = 0;
15    
# Line 20  Strix Line 21  Strix
21    
22  =head2 new  =head2 new
23    
24    my $strix = Strix->new({ site => 'os-test0604-zg' });    my $strix = Strix->new({ instance => 'os-test0604-zg' });
25    
26  =head2 dbh  =head2 dbh
27    
28    my $dbh = Strix->dbh( $site_name );    my $dbh = Strix->dbh( $strix_instance );
29    
30    my $dbh = $strix->dbh;    my $dbh = $strix->dbh;
31    
32  =cut  =cut
33    
34  our $site_dbh;  our $instance_dbh;
35    
36  sub dbh {  sub dbh {
37          my $self = shift;          my $self = shift;
38    
39          my $site = shift || $self->site || confess "no site";          my $instance = shift || ref($self) && $self->instance || confess "no instance";
40    
41          return $site_dbh->{$site} if $site_dbh->{$site};          return $instance_dbh->{$instance} if $instance_dbh->{$instance};
42    
43          my $config = Jifty->config->app('strix') or die "need strix config";          my $config = Jifty->config->app('strix') or die "need strix config";
44          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 46  sub dbh {
46          Jifty->log->debug("using config ", dump( $database ));          Jifty->log->debug("using config ", dump( $database ));
47    
48          my $dsn =          my $dsn =
49                  'DBI:Pg:dbname=' . $site .                  'DBI:Pg:dbname=' . $instance .
50                  ';host=' . $database->{host} .                  ';host=' . $database->{host} .
51                  ';port=' . $database->{port};                  ';port=' . $database->{port};
52    
53          Jifty->log->info("Connect to site $site with dsn $dsn");          Jifty->log->info("Connect to instance $instance with dsn $dsn");
54    
55          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";
56    
57          $site_dbh->{$site} = $dbh;          # force database to send us back UTF-8 no metter what it's encoding
58            $dbh->do("set client_encoding='utf-8'");
59            $dbh->{pg_enable_utf8} = 1;
60    
61          warn "## site_dbh = ",dump( $site_dbh );          $instance_dbh->{$instance} = $dbh;
62    
63            warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;
64    
65          return $dbh;          return $dbh;
66  }  }
# Line 116  sub layout { Line 121  sub layout {
121          });          });
122          $sth->execute( $category->{template_id} );          $sth->execute( $category->{template_id} );
123    
124            sub module_args {
125                    my $row = shift;
126                    return undef unless $row->{module_args};
127                    my $args;
128                    foreach my $a ( split(/\&/, $row->{module_args} ) ) {
129                            $args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/;
130                    }
131                    return $args;
132            }
133    
134          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
135                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
136                  push @{ $page->{free} }, { $row->{name} => $row->{module_args} };                  push @{ $page->{free} }, { $row->{name} => module_args( $row ) };
137          }          }
138    
139          warn "### pre layout...\n" if $debug;          warn "### pre layout...\n" if $debug;
# Line 138  sub layout { Line 153  sub layout {
153    
154          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
155                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
156                  push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => $row->{module_args} };                  push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) };
157          }          }
158    
159          warn "### post layout...\n" if $debug;          warn "### post layout...\n" if $debug;
# Line 153  sub layout { Line 168  sub layout {
168    
169          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
170                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
171                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => $row->{module_args} };                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
172          }          }
173    
174          return $page;          return $page;
# Line 225  sub site_navigation { Line 240  sub site_navigation {
240                  }                  }
241    
242                  my $depth = $kat->{depth};                  my $depth = $kat->{depth};
243                    if ( ! defined $pos[ $depth - 2 ] ) {
244                            warn "FIXING CATEGORY: ",dump( $kat );
245                            $node->{class} = "error";
246                            $depth--;
247                    }
248                  @pos = splice( @pos, 0, $depth );                  @pos = splice( @pos, 0, $depth );
249                  $pos[ $depth - 1 ]++;                  $pos[ $depth - 1 ]++;
250    
251                  warn "## category depth = $depth pos = ",dump( @pos );                  warn "## category depth = $depth pos = ",dump( @pos ) if $debug;
252    
253                  my $code = '$navigation';                  my $code = '$navigation';
254                  map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;                  map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;
255                  $code =~ s/->{children}$//;                  $code =~ s/->{children}$//;
256                  warn "## category code: $code\n";                  warn "## category code: $code\n" if $debug;
257                  eval $code . '= $node';                  eval $code . '= $node';
258                  if ( $@ ) {                  if ( $@ ) {
259                          warn "SKIPPED CATEGORY: $@ ",dump( $kat );                          warn "SKIPPED CATEGORY: $@ ",dump( $kat );
# Line 267  sub site_navigation { Line 287  sub site_navigation {
287                                                                    
288                                  my $ms_depth = $ms->{depth} + $depth;                                  my $ms_depth = $ms->{depth} + $depth;
289                                  my $p = $pos[ $ms_depth - 1 ]++;                                  my $p = $pos[ $ms_depth - 1 ]++;
290                                  warn "## multistatic depth = $ms_depth pos = ",dump( @pos );                                  warn "## multistatic depth = $ms_depth pos = ",dump( @pos ) if $debug;
291    
292                                  my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';                                  my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';
293                                  warn "## multistatic code: $ms_code\n";                                  warn "## multistatic code: $ms_code\n" if $debug;
294                                  eval $ms_code;                                  eval $ms_code;
295                                  if ( $@ ) {                                  if ( $@ ) {
296                                          warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );                                          warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );

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

  ViewVC Help
Powered by ViewVC 1.1.26