/[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 219 by dpavlin, Sun Jun 22 14:41:23 2008 UTC
# Line 3  package Strix; Line 3  package Strix;
3  use strict;  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 A3C::Cache);
7  __PACKAGE__->mk_accessors( qw(instance 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 Carp qw/confess/;
15    use URI::Escape;
16    
17  our $debug = 0;  our $debug = 0;
18    
# Line 31  Strix Line 35  Strix
35  =cut  =cut
36    
37  our $instance_dbh;  our $instance_dbh;
38    our @instances_active;
39    
40  sub dbh {  sub dbh {
41          my $self = shift;          my $self = shift;
42    
43          my $instance = shift || $self->instance || confess "no instance";          my $instance = shift || ref($self) && $self->instance || confess "no instance";
44    
45          return $instance_dbh->{$instance} if $instance_dbh->{$instance};          return $instance_dbh->{$instance} if $instance_dbh->{$instance};
46    
# Line 51  sub dbh { Line 56  sub dbh {
56    
57          Jifty->log->info("Connect to instance $instance with dsn $dsn");          Jifty->log->info("Connect to instance $instance with dsn $dsn");
58    
59          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";
60    
61            # force database to send us back UTF-8 no metter what it's encoding
62            $dbh->do("set client_encoding='utf-8'");
63            $dbh->{pg_enable_utf8} = 1;
64    
65          $instance_dbh->{$instance} = $dbh;          $instance_dbh->{$instance} = $dbh;
66            push @instances_active, $instance;
67    
68            if ( $#instances_active > 5 ) {
69                    my $i = shift @instances_active;
70                    warn "## remove connection to instance $instance\n";
71                    delete( $instance_dbh->{$i} );
72            }
73    
74          warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;          warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;
75    
# Line 71  sub category { Line 87  sub category {
87    
88          my $url = shift || confess "no url";          my $url = shift || confess "no url";
89    
90            my $data = $self->read_cache( uri_escape($url) );
91            return $data if $data;
92    
93          # sysinc/profiles.php          # sysinc/profiles.php
94          my $sth = $self->dbh->prepare(qq{          my $sth = $self->dbh->prepare(qq{
95          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
96          });          });
97          $sth->execute($url);          $sth->execute($url);
98    
99          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";
100            $self->write_cache( $category, uri_escape($url) );
101          return $category;          return $category;
102  }  }
103    
# Line 92  sub layout { Line 112  sub layout {
112    
113          my $url = shift || confess "no url";          my $url = shift || confess "no url";
114    
115            my $data = $self->read_cache( uri_escape($url) );
116            return $data if $data;
117    
118          my $dbh = $self->dbh;          my $dbh = $self->dbh;
119          my $category = $self->category( $url );          my $category = $self->category( $url );
120    
# Line 166  sub layout { Line 189  sub layout {
189                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
190          }          }
191    
192            $self->write_cache( $page, uri_escape($url) );
193    
194          return $page;          return $page;
195    
196  }  }
197    
198  =head2 sitemap  =head2 sites
199    
200          my $sitemap = $strix->sitemap( $site_id, $uid );          my @sites = $strix->sites;
201    
202  =cut  =cut
203    
204  sub sitemap {  sub sites {
205          my $self = shift;          my $self = shift;
206    
207          my ( $site_id, $uid ) = @_;          my @sites = $self->read_cache;
208          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;  
209    
210          my $sth = $self->dbh->prepare( $query );          my $sth = $self->dbh->prepare(
211          if ( $site_id ) {          "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr"
212                  $sth->execute( $site_id );          );
213          } else {          $sth->execute;
                 $sth->execute;  
         }  
214    
215          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
216                  warn dump( $row ) if $debug;                  push @sites, $row;
   
                 $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid );  
217          }          }
218    
219          return $sitemap;          $self->write_cache( \@sites );
220    
221            return @sites;
222  }  }
223    
224  =head2 site_navigation  =head2 site_navigation
# Line 215  sub site_navigation { Line 235  sub site_navigation {
235          $uid ||= 1; # anonymous          $uid ||= 1; # anonymous
236  #       $uid ||= 2; # admin  #       $uid ||= 2; # admin
237    
238            my $data = $self->read_cache( $site_id, $uid );
239            return $data if $data;
240    
241          my $sth = $self->dbh->prepare(          my $sth = $self->dbh->prepare(
242          "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");
243          $sth->execute( $site_id, $uid );          $sth->execute( $site_id, $uid );
# Line 227  sub site_navigation { Line 250  sub site_navigation {
250    
251          while (my $kat = $sth->fetchrow_hashref() ) {          while (my $kat = $sth->fetchrow_hashref() ) {
252                  warn "# kat = ",dump( $kat ) if $debug;                  warn "# kat = ",dump( $kat ) if $debug;
253                  die "no depth" unless $kat->{depth};                  if ( ! $kat->{depth} ) {
254                            Jifty->log->error("depth increased to 1 in ",dump( $kat ));
255                            $kat->{depth} = 1;
256                    }
257    
258                  my $node = { type => 'category' };                  my $node = { type => 'category' };
259                  foreach my $c ( qw/naziv url/ ) {                  foreach my $c ( qw/naziv url/ ) {
# Line 235  sub site_navigation { Line 261  sub site_navigation {
261                  }                  }
262    
263                  my $depth = $kat->{depth};                  my $depth = $kat->{depth};
264                    if ( ! defined $pos[ $depth - 2 ] ) {
265                            warn "FIXING CATEGORY: ",dump( $kat );
266                            $node->{class} = "error";
267                            $depth--;
268                    }
269                  @pos = splice( @pos, 0, $depth );                  @pos = splice( @pos, 0, $depth );
270                  $pos[ $depth - 1 ]++;                  $pos[ $depth - 1 ]++;
271    
# Line 291  sub site_navigation { Line 322  sub site_navigation {
322    
323          }          }
324    
325            $self->write_cache( $navigation, $site_id, $uid );
326    
327          return $navigation;          return $navigation;
328    
329  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26