/[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 173 by dpavlin, Mon Jun 16 14:43:07 2008 UTC revision 193 by dpavlin, Tue Jun 17 21:27:31 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    our @instances_active;
36    
37  sub dbh {  sub dbh {
38          my $self = shift;          my $self = shift;
39    
40          my $site = shift || $self->site || confess "no site";          my $instance = shift || ref($self) && $self->instance || confess "no instance";
41    
42          return $site_dbh->{$site} if $site_dbh->{$site};          return $instance_dbh->{$instance} if $instance_dbh->{$instance};
43    
44          my $config = Jifty->config->app('strix') or die "need strix config";          my $config = Jifty->config->app('strix') or die "need strix config";
45          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 47  sub dbh {
47          Jifty->log->debug("using config ", dump( $database ));          Jifty->log->debug("using config ", dump( $database ));
48    
49          my $dsn =          my $dsn =
50                  'DBI:Pg:dbname=' . $site .                  'DBI:Pg:dbname=' . $instance .
51                  ';host=' . $database->{host} .                  ';host=' . $database->{host} .
52                  ';port=' . $database->{port};                  ';port=' . $database->{port};
53    
54          Jifty->log->info("Connect to site $site with dsn $dsn");          Jifty->log->info("Connect to instance $instance with dsn $dsn");
55    
56          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";
57    
58          $site_dbh->{$site} = $dbh;          # force database to send us back UTF-8 no metter what it's encoding
59            $dbh->do("set client_encoding='utf-8'");
60            $dbh->{pg_enable_utf8} = 1;
61    
62          warn "## site_dbh = ",dump( $site_dbh ) if $debug;          $instance_dbh->{$instance} = $dbh;
63            push @instances_active, $instance;
64    
65            if ( $#instances_active > 5 ) {
66                    my $i = shift @instances_active;
67                    warn "## remove connection to instance $instance\n";
68                    delete( $instance_dbh->{$i} );
69            }
70    
71            warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;
72    
73          return $dbh;          return $dbh;
74  }  }
# Line 170  sub layout { Line 183  sub layout {
183    
184  }  }
185    
186  =head2 sitemap  =head2 sites
187    
188          my $sitemap = $strix->sitemap( $site_id, $uid );          my @sites = $strix->sites;
189    
190  =cut  =cut
191    
192  sub sitemap {  sub sites {
193          my $self = shift;          my $self = shift;
194    
195          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;  
196    
197          my $sth = $self->dbh->prepare( $query );          my $sth = $self->dbh->prepare(
198          if ( $site_id ) {          "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr"
199                  $sth->execute( $site_id );          );
200          } else {          $sth->execute;
                 $sth->execute;  
         }  
201    
202          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
203                  warn dump( $row ) if $debug;                  push @sites, $row;
   
                 $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid );  
204          }          }
205    
206          return $sitemap;          return @sites;
207  }  }
208    
209  =head2 site_navigation  =head2 site_navigation
# Line 227  sub site_navigation { Line 232  sub site_navigation {
232    
233          while (my $kat = $sth->fetchrow_hashref() ) {          while (my $kat = $sth->fetchrow_hashref() ) {
234                  warn "# kat = ",dump( $kat ) if $debug;                  warn "# kat = ",dump( $kat ) if $debug;
235                  die "no depth" unless $kat->{depth};                  if ( ! $kat->{depth} ) {
236                            Jifty->log->error("depth increased to 1 in ",dump( $kat ));
237                            $kat->{depth} = 1;
238                    }
239    
240                  my $node = { type => 'category' };                  my $node = { type => 'category' };
241                  foreach my $c ( qw/naziv url/ ) {                  foreach my $c ( qw/naziv url/ ) {
# Line 235  sub site_navigation { Line 243  sub site_navigation {
243                  }                  }
244    
245                  my $depth = $kat->{depth};                  my $depth = $kat->{depth};
246                    if ( ! defined $pos[ $depth - 2 ] ) {
247                            warn "FIXING CATEGORY: ",dump( $kat );
248                            $node->{class} = "error";
249                            $depth--;
250                    }
251                  @pos = splice( @pos, 0, $depth );                  @pos = splice( @pos, 0, $depth );
252                  $pos[ $depth - 1 ]++;                  $pos[ $depth - 1 ]++;
253    

Legend:
Removed from v.173  
changed lines
  Added in v.193

  ViewVC Help
Powered by ViewVC 1.1.26