/[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

Annotation of /lib/Strix.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 173 - (hide annotations)
Mon Jun 16 14:43:07 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 7442 byte(s)
- remove debugging output without $debug set
- decode module_args
1 dpavlin 167 package Strix;
2    
3     use strict;
4     use warnings;
5    
6 dpavlin 168 use base qw(Jifty::Object Class::Accessor::Fast);
7 dpavlin 169 __PACKAGE__->mk_accessors( qw(site uid) );
8 dpavlin 167
9     use DBI;
10     use Data::Dump qw/dump/;
11 dpavlin 168 use Carp qw/confess/;
12 dpavlin 167
13 dpavlin 168 our $debug = 0;
14    
15 dpavlin 167 =head1 NAME
16    
17     Strix
18    
19     =head1 METHODS
20    
21 dpavlin 168 =head2 new
22    
23     my $strix = Strix->new({ site => 'os-test0604-zg' });
24    
25 dpavlin 167 =head2 dbh
26    
27 dpavlin 168 my $dbh = Strix->dbh( $site_name );
28 dpavlin 167
29 dpavlin 168 my $dbh = $strix->dbh;
30    
31 dpavlin 167 =cut
32    
33 dpavlin 171 our $site_dbh;
34    
35 dpavlin 167 sub dbh {
36     my $self = shift;
37    
38 dpavlin 168 my $site = shift || $self->site || confess "no site";
39 dpavlin 167
40 dpavlin 171 return $site_dbh->{$site} if $site_dbh->{$site};
41    
42 dpavlin 167 my $config = Jifty->config->app('strix') or die "need strix config";
43     my $database = $config->{database} or die "no strix.database in config";
44    
45     Jifty->log->debug("using config ", dump( $database ));
46    
47     my $dsn =
48     'DBI:Pg:dbname=' . $site .
49     ';host=' . $database->{host} .
50     ';port=' . $database->{port};
51    
52     Jifty->log->info("Connect to site $site with dsn $dsn");
53    
54     my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr;
55    
56 dpavlin 171 $site_dbh->{$site} = $dbh;
57    
58 dpavlin 173 warn "## site_dbh = ",dump( $site_dbh ) if $debug;
59 dpavlin 171
60 dpavlin 167 return $dbh;
61     }
62    
63 dpavlin 168 =head2 category
64    
65     my $category = Strix->category( $url );
66    
67     =cut
68    
69     sub category {
70     my $self = shift;
71    
72     my $url = shift || confess "no url";
73    
74     # sysinc/profiles.php
75     my $sth = $self->dbh->prepare(qq{
76     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
77     });
78     $sth->execute($url);
79    
80     my $category = $sth->fetchrow_hashref() or die "can't fetch category $url";
81     return $category;
82     }
83    
84     =head2 layout
85    
86     my $layout = $strix->layout( $url );
87    
88     =cut
89    
90     sub layout {
91     my $self = shift;
92    
93     my $url = shift || confess "no url";
94    
95     my $dbh = $self->dbh;
96     my $category = $self->category( $url );
97    
98     my $sth = $dbh->prepare(qq{
99     SELECT template.tfilename, template.tflags FROM template WHERE id = ?
100     });
101     $sth->execute( $category->{template_id} );
102    
103     my $template = $sth->fetchrow_hashref() or die "can't fetch template";
104    
105     warn "template = ",dump( $template ) if $debug;
106    
107     my $page;
108     warn "### free layout...\n" if $debug;
109    
110     # index.php
111     $sth = $dbh->prepare(qq{
112     SELECT layout_id, module_id, pozicija, module_args, name, fname, notitle, class_name
113     FROM pre_layout, modules
114     WHERE id=module_id AND ? = template_id AND redoslijed >= 0
115     ORDER BY redoslijed DESC
116     });
117     $sth->execute( $category->{template_id} );
118    
119 dpavlin 173 sub module_args {
120     my $row = shift;
121     return undef unless $row->{module_args};
122     my $args;
123     foreach my $a ( split(/\&/, $row->{module_args} ) ) {
124     $args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/;
125     }
126     return $args;
127     }
128    
129 dpavlin 168 while (my $row = $sth->fetchrow_hashref() ) {
130     warn dump( $row ) if $debug;
131 dpavlin 173 push @{ $page->{free} }, { $row->{name} => module_args( $row ) };
132 dpavlin 168 }
133    
134     warn "### pre layout...\n" if $debug;
135    
136     $sth = $dbh->prepare(qq{
137     SELECT
138     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,
139     m.name, m.fname, m.hidden, m.nocache, m.pos, m.class_name,
140     acl_module.acl_register_id
141     FROM layout as l, modules as m LEFT JOIN acl_module ON (acl_module.modules_id = m.id)
142     WHERE l.user_id=?
143     AND l.kategorija_id=?
144     AND m.id=l.module_id
145     ORDER BY pozicija,redoslijed DESC
146     });
147     $sth->execute( 1, $category->{id} );
148    
149     while (my $row = $sth->fetchrow_hashref() ) {
150     warn dump( $row ) if $debug;
151 dpavlin 173 push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) };
152 dpavlin 168 }
153    
154     warn "### post layout...\n" if $debug;
155    
156     $sth = $dbh->prepare(qq{
157     SELECT layout_id, module_id, pozicija, module_args, name, notitle
158     FROM pre_layout, modules
159     WHERE id=module_id AND ? = template_id AND redoslijed < 0
160     ORDER BY redoslijed DESC
161     });
162     $sth->execute( $category->{template_id} );
163    
164     while (my $row = $sth->fetchrow_hashref() ) {
165     warn dump( $row ) if $debug;
166 dpavlin 173 push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
167 dpavlin 168 }
168    
169     return $page;
170    
171     }
172    
173 dpavlin 169 =head2 sitemap
174    
175     my $sitemap = $strix->sitemap( $site_id, $uid );
176    
177     =cut
178    
179     sub sitemap {
180     my $self = shift;
181    
182     my ( $site_id, $uid ) = @_;
183     my $sitemap;
184    
185     my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?";
186     $query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id;
187    
188     my $sth = $self->dbh->prepare( $query );
189     if ( $site_id ) {
190     $sth->execute( $site_id );
191     } else {
192     $sth->execute;
193     }
194    
195     while (my $row = $sth->fetchrow_hashref() ) {
196     warn dump( $row ) if $debug;
197    
198     $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid );
199     }
200    
201     return $sitemap;
202     }
203    
204     =head2 site_navigation
205    
206     my $navigation = $strix->site_navigation( $site_id, $uid );
207    
208     =cut
209    
210     sub site_navigation {
211     my $self = shift;
212    
213     my ( $site_id, $uid ) = @_;
214    
215     $uid ||= 1; # anonymous
216     # $uid ||= 2; # admin
217    
218     my $sth = $self->dbh->prepare(
219     "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");
220     $sth->execute( $site_id, $uid );
221    
222     Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid");
223    
224     my $navigation = [];
225    
226     my @pos = ( 0 );
227    
228     while (my $kat = $sth->fetchrow_hashref() ) {
229     warn "# kat = ",dump( $kat ) if $debug;
230     die "no depth" unless $kat->{depth};
231    
232     my $node = { type => 'category' };
233     foreach my $c ( qw/naziv url/ ) {
234     $node->{$c} = $kat->{$c};
235     }
236    
237     my $depth = $kat->{depth};
238     @pos = splice( @pos, 0, $depth );
239     $pos[ $depth - 1 ]++;
240    
241 dpavlin 173 warn "## category depth = $depth pos = ",dump( @pos ) if $debug;
242 dpavlin 169
243     my $code = '$navigation';
244     map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;
245     $code =~ s/->{children}$//;
246 dpavlin 173 warn "## category code: $code\n" if $debug;
247 dpavlin 169 eval $code . '= $node';
248     if ( $@ ) {
249     warn "SKIPPED CATEGORY: $@ ",dump( $kat );
250     next;
251     }
252    
253     my $sth_ms = $self->dbh->prepare(
254     "SELECT
255     multistatic.id, multistatic.title, multistatic.kname,
256     multistatic_navigation.kategorija_id, multistatic_navigation.prikaz,
257     (LENGTH(multistatic_navigation.prikaz)/3) AS depth
258     FROM multistatic, multistatic_navigation
259     WHERE multistatic.id = multistatic_navigation.multistatic_id
260     AND multistatic_navigation.prikaz != ''
261     AND multistatic_navigation.kategorija_id = ?
262     AND multistatic.title != ''
263     ORDER BY multistatic_navigation.prikaz");
264     $sth_ms->execute( $kat->{id} );
265    
266     if ( my $rows = $sth_ms->rows ) {
267     Jifty->log->debug("$site_id has $rows multistatic pages");
268    
269     while (my $ms = $sth_ms->fetchrow_hashref() ) {
270     warn "# ms = ",dump( $ms ) if $debug;
271    
272     my $node = {
273     naziv => $ms->{title},
274     url => $kat->{url} . '?ms_nav=' . $ms->{prikaz},
275     type => 'multistatic',
276     };
277    
278     my $ms_depth = $ms->{depth} + $depth;
279     my $p = $pos[ $ms_depth - 1 ]++;
280 dpavlin 173 warn "## multistatic depth = $ms_depth pos = ",dump( @pos ) if $debug;
281 dpavlin 169
282     my $ms_code = $code . '->{children}->[ ' . $p . '] = $node';
283 dpavlin 173 warn "## multistatic code: $ms_code\n" if $debug;
284 dpavlin 169 eval $ms_code;
285     if ( $@ ) {
286     warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );
287     next;
288     }
289     }
290     }
291    
292     }
293    
294     return $navigation;
295    
296     }
297    
298 dpavlin 167 1;

  ViewVC Help
Powered by ViewVC 1.1.26