/[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 167 by dpavlin, Sun Jun 15 23:19:02 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/;  use base qw(Jifty::Object Class::Accessor::Fast A3C::Cache);
7    __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/;
12    use Jifty;
13    
14    use Carp qw/confess/;
15    use URI::Escape;
16    
17    our $debug = 0;
18    
19  =head1 NAME  =head1 NAME
20    
# Line 14  Strix Line 22  Strix
22    
23  =head1 METHODS  =head1 METHODS
24    
25    =head2 new
26    
27      my $strix = Strix->new({ instance => 'os-test0604-zg' });
28    
29  =head2 dbh  =head2 dbh
30    
31          my $dbh = Strix->dbh( $site_name );    my $dbh = Strix->dbh( $strix_instance );
32    
33      my $dbh = $strix->dbh;
34    
35  =cut  =cut
36    
37    our $instance_dbh;
38    our @instances_active;
39    
40  sub dbh {  sub dbh {
41          my $self = shift;          my $self = shift;
42    
43          my $site = shift or die "no site?";          my $instance = shift || ref($self) && $self->instance || confess "no instance";
44    
45            return $instance_dbh->{$instance} if $instance_dbh->{$instance};
46    
47          my $config = Jifty->config->app('strix') or die "need strix config";          my $config = Jifty->config->app('strix') or die "need strix config";
48          my $database = $config->{database} or die "no strix.database in config";          my $database = $config->{database} or die "no strix.database in config";
# Line 31  sub dbh { Line 50  sub dbh {
50          Jifty->log->debug("using config ", dump( $database ));          Jifty->log->debug("using config ", dump( $database ));
51    
52          my $dsn =          my $dsn =
53                  'DBI:Pg:dbname=' . $site .                  'DBI:Pg:dbname=' . $instance .
54                  ';host=' . $database->{host} .                  ';host=' . $database->{host} .
55                  ';port=' . $database->{port};                  ';port=' . $database->{port};
56    
57          Jifty->log->info("Connect to site $site 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\n";
60    
61          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
62            $dbh->do("set client_encoding='utf-8'");
63            $dbh->{pg_enable_utf8} = 1;
64    
65            $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;
75    
76          return $dbh;          return $dbh;
77  }  }
78    
79    =head2 category
80    
81            my $category = Strix->category( $url );
82    
83    =cut
84    
85    sub category {
86            my $self = shift;
87    
88            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
94            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
96            });
97            $sth->execute($url);
98    
99            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;
102    }
103    
104    =head2 layout
105    
106            my $layout = $strix->layout( $url );
107    
108    =cut
109    
110    sub layout {
111            my $self = shift;
112    
113            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;
119            my $category = $self->category( $url );
120    
121            my $sth = $dbh->prepare(qq{
122            SELECT template.tfilename, template.tflags FROM template WHERE id = ?
123            });
124            $sth->execute( $category->{template_id} );
125    
126            my $template = $sth->fetchrow_hashref() or die "can't fetch template";
127    
128            warn "template = ",dump( $template ) if $debug;
129    
130            my $page;
131            warn "### free layout...\n" if $debug;
132    
133            # index.php
134            $sth = $dbh->prepare(qq{
135            SELECT layout_id, module_id, pozicija, module_args, name, fname, notitle, class_name
136            FROM pre_layout, modules
137            WHERE id=module_id AND ? = template_id AND redoslijed >= 0
138            ORDER BY redoslijed DESC
139            });
140            $sth->execute( $category->{template_id} );
141    
142            sub module_args {
143                    my $row = shift;
144                    return undef unless $row->{module_args};
145                    my $args;
146                    foreach my $a ( split(/\&/, $row->{module_args} ) ) {
147                            $args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/;
148                    }
149                    return $args;
150            }
151    
152            while (my $row = $sth->fetchrow_hashref() ) {
153                    warn dump( $row ) if $debug;
154                    push @{ $page->{free} }, { $row->{name} => module_args( $row ) };
155            }
156    
157            warn "### pre layout...\n" if $debug;
158    
159            $sth = $dbh->prepare(qq{
160            SELECT
161                    l.id as layout_id, l.user_id, l.kategorija_id, l.module_id, l.pozicija, l.redoslijed, l.module_args, l.state, l.notitle,
162                    m.name, m.fname, m.hidden, m.nocache, m.pos, m.class_name,
163                    acl_module.acl_register_id
164            FROM layout as l, modules as m LEFT JOIN acl_module ON (acl_module.modules_id = m.id)
165            WHERE l.user_id=?
166            AND l.kategorija_id=?
167            AND m.id=l.module_id
168            ORDER BY pozicija,redoslijed DESC
169            });
170            $sth->execute( 1, $category->{id} );
171    
172            while (my $row = $sth->fetchrow_hashref() ) {
173                    warn dump( $row ) if $debug;
174                    push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) };
175            }
176    
177            warn "### post layout...\n" if $debug;
178    
179            $sth = $dbh->prepare(qq{
180            SELECT layout_id, module_id, pozicija, module_args, name, notitle
181            FROM pre_layout, modules
182            WHERE id=module_id AND ? = template_id AND redoslijed < 0
183            ORDER BY redoslijed DESC
184            });
185            $sth->execute( $category->{template_id} );
186    
187            while (my $row = $sth->fetchrow_hashref() ) {
188                    warn dump( $row ) if $debug;
189                    push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
190            }
191    
192            $self->write_cache( $page, uri_escape($url) );
193    
194            return $page;
195    
196    }
197    
198    =head2 sites
199    
200            my @sites = $strix->sites;
201    
202    =cut
203    
204    sub sites {
205            my $self = shift;
206    
207            my @sites = $self->read_cache;
208            return @sites if @sites;
209    
210            my $sth = $self->dbh->prepare(
211            "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr"
212            );
213            $sth->execute;
214    
215            while (my $row = $sth->fetchrow_hashref() ) {
216                    push @sites, $row;
217            }
218    
219            $self->write_cache( \@sites );
220    
221            return @sites;
222    }
223    
224    =head2 site_navigation
225    
226      my $navigation = $strix->site_navigation( $site_id, $uid );
227    
228    =cut
229    
230    sub site_navigation {
231            my $self = shift;
232    
233            my ( $site_id, $uid ) = @_;
234    
235            $uid ||= 1; # anonymous
236    #       $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(
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");
243            $sth->execute( $site_id, $uid );
244    
245            Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid");
246    
247            my $navigation = [];
248    
249            my @pos = ( 0 );
250    
251            while (my $kat = $sth->fetchrow_hashref() ) {
252                    warn "# kat = ",dump( $kat ) if $debug;
253                    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' };
259                    foreach my $c ( qw/naziv url/ ) {
260                            $node->{$c} = $kat->{$c};
261                    }
262    
263                    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 );
270                    $pos[ $depth - 1 ]++;
271    
272                    warn "## category depth = $depth pos = ",dump( @pos ) if $debug;
273    
274                    my $code = '$navigation';
275                    map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;
276                    $code =~ s/->{children}$//;
277                    warn "## category code: $code\n" if $debug;
278                    eval $code . '= $node';
279                    if ( $@ ) {
280                            warn "SKIPPED CATEGORY: $@ ",dump( $kat );
281                            next;
282                    }
283    
284                    my $sth_ms = $self->dbh->prepare(
285                    "SELECT
286                            multistatic.id, multistatic.title, multistatic.kname,
287                            multistatic_navigation.kategorija_id, multistatic_navigation.prikaz,
288                            (LENGTH(multistatic_navigation.prikaz)/3) AS depth
289                    FROM multistatic, multistatic_navigation
290                    WHERE multistatic.id = multistatic_navigation.multistatic_id
291                            AND multistatic_navigation.prikaz != ''
292                            AND multistatic_navigation.kategorija_id = ?
293                            AND multistatic.title != ''
294                    ORDER BY multistatic_navigation.prikaz");
295                    $sth_ms->execute( $kat->{id} );
296    
297                    if ( my $rows = $sth_ms->rows ) {
298                            Jifty->log->debug("$site_id has $rows multistatic pages");
299    
300                            while (my $ms = $sth_ms->fetchrow_hashref() ) {
301                                    warn "# ms = ",dump( $ms ) if $debug;
302    
303                                    my $node = {
304                                            naziv => $ms->{title},
305                                            url => $kat->{url} . '?ms_nav=' . $ms->{prikaz},
306                                            type => 'multistatic',
307                                    };
308                                    
309                                    my $ms_depth = $ms->{depth} + $depth;
310                                    my $p = $pos[ $ms_depth - 1 ]++;
311                                    warn "## multistatic depth = $ms_depth pos = ",dump( @pos ) if $debug;
312    
313                                    my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';
314                                    warn "## multistatic code: $ms_code\n" if $debug;
315                                    eval $ms_code;
316                                    if ( $@ ) {
317                                            warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );
318                                            next;
319                                    }
320                            }
321                    }
322    
323            }
324    
325            $self->write_cache( $navigation, $site_id, $uid );
326    
327            return $navigation;
328    
329    }
330    
331  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26