1 # Copyright (C) 2002 Stanislav Sinyagin
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 # $Id: Renderer.pm,v 1.1 2010-12-27 00:03:39 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 package Torrus::Renderer;
23 use Digest::MD5 qw(md5_hex);
26 use Torrus::ConfigTree;
27 use Torrus::TimeStamp;
30 use Torrus::SiteConfig;
32 use Torrus::Renderer::HTML;
33 use Torrus::Renderer::RRDtool;
35 # Inherit methods from these modules
36 use base qw(Torrus::Renderer::HTML
37 Torrus::Renderer::RRDtool
38 Torrus::Renderer::Frontpage
39 Torrus::Renderer::AdmInfo);
47 if( not defined $Torrus::Global::cacheDir )
49 Error('$Torrus::Global::cacheDir must be defined');
52 elsif( not -d $Torrus::Global::cacheDir )
54 Error("No such directory: $Torrus::Global::cacheDir");
58 $self->{'db'} = new Torrus::DB('render_cache', -WriteAccess => 1);
59 if( not defined( $self->{'db'} ) )
70 # Returns the absolute filename and MIME type:
72 # my($fname, $mimetype) = $renderer->render($config_tree, $token, $view);
78 my $config_tree = shift;
83 # If no options given, preserve the existing ones
86 $self->{'options'} = \%new_options;
89 $self->checkAndClearCache( $config_tree );
91 my($t_render, $t_expires, $filename, $mime_type);
93 my $tree = $config_tree->treeName();
95 if( not $config_tree->isTset($token) )
97 if( my $alias = $config_tree->isAlias($token) )
101 if( not defined( $config_tree->path($token) ) )
103 Error("No such token: $token");
108 $view = $config_tree->getDefaultView($token) unless defined $view;
111 if( $self->{'options'}->{'uid'} )
113 $uid = $self->{'options'}->{'uid'};
116 my $cachekey = $self->cacheKey( $uid . ':' . $tree . ':' .
117 $token . ':' . $view );
119 ($t_render, $t_expires, $filename, $mime_type) =
120 $self->getCache( $cachekey );
122 my $not_in_cache = 0;
124 if( not defined( $filename ) )
126 $filename = Torrus::Renderer::newCacheFileName( $cachekey );
130 my $cachefile = $Torrus::Global::cacheDir.'/'.$filename;
132 if( ( not $not_in_cache ) and
134 $t_expires >= time() )
136 return ($cachefile, $mime_type, $t_expires - time());
139 my $method = 'render_' . $config_tree->getParam($view, 'view-type');
141 ($t_expires, $mime_type) =
142 $self->$method( $config_tree, $token, $view, $cachefile );
146 $self->{'options'} = undef;
150 if( defined($t_expires) and defined($mime_type) )
152 $self->setCache($cachekey, time(), $t_expires, $filename, $mime_type);
153 @ret = ($cachefile, $mime_type, $t_expires - time());
163 my $keystring = shift;
165 if( ref( $self->{'options'}->{'variables'} ) )
167 foreach my $name ( sort keys %{$self->{'options'}->{'variables'}} )
169 my $val = $self->{'options'}->{'variables'}->{$name};
170 $keystring .= ':' . $name . '=' . $val;
180 my $keystring = shift;
182 my $cacheval = $self->{'db'}->get( $keystring );
184 if( defined($cacheval) )
186 return split(':', $cacheval);
198 my $keystring = shift;
199 my $t_render = shift;
200 my $t_expires = shift;
201 my $filename = shift;
202 my $mime_type = shift;
204 $self->{'db'}->put( $keystring,
206 ($t_render, $t_expires, $filename, $mime_type)));
211 sub checkAndClearCache
214 my $config_tree = shift;
216 my $tree = $config_tree->treeName();
218 Torrus::TimeStamp::init();
219 my $known_ts = Torrus::TimeStamp::get($tree . ':renderer_cache');
220 my $actual_ts = $config_tree->getTimestamp();
221 if( $actual_ts >= $known_ts or
222 time() >= $known_ts + $Torrus::Renderer::cacheMaxAge )
225 Torrus::TimeStamp::setNow($tree . ':renderer_cache');
227 Torrus::TimeStamp::release();
235 Debug('Clearing renderer cache');
236 my $cursor = $self->{'db'}->cursor( -Write => 1 );
237 while( my ($key, $val) = $self->{'db'}->next( $cursor ) )
239 my($t_render, $t_expires, $filename, $mime_type) = split(':', $val);
241 unlink $Torrus::Global::cacheDir.'/'.$filename;
242 $self->{'db'}->c_del( $cursor );
245 Debug('Renderer cache cleared');
251 my $cachekey = shift;
252 return sprintf('%s_%.5d', md5_hex($cachekey), rand(1e5));
259 # Remove spaces in the head and tail.
263 # Unscreen special characters
264 $txt =~ s/{COLON}/:/ogm;
265 $txt =~ s/{SEMICOL}/;/ogm;
266 $txt =~ s/{PERCENT}/%/ogm;
268 $txt =~ s/\&/\&\;/ogm;
269 $txt =~ s/\</\<\;/ogm;
270 $txt =~ s/\>/\>\;/ogm;
271 $txt =~ s/\'/\&apos\;/ogm;
272 $txt =~ s/\"/\"\;/ogm;
284 # indent-tabs-mode: nil
285 # perl-indent-level: 4