| 1 | package OpenInteract2::TT2::Provider; | ||
| 2 | |||
| 3 | # $Id: Provider.pm,v 1.6 2005/03/18 04:09:51 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( Template::Provider ); | ||
| 7 | use Digest::MD5; | ||
| 8 | use File::Spec; | ||
| 9 | use Log::Log4perl qw( get_logger ); | ||
| 10 | use OpenInteract2::Constants qw( :log ); | ||
| 11 | use OpenInteract2::Context qw( CTX ); | ||
| 12 | |||
| 13 | $OpenInteract2::TT2::Provider::VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/); | ||
| 14 | |||
| 15 | my ( $log ); | ||
| 16 | |||
| 17 | use constant DEFAULT_MAX_CACHE_TIME => 60 * 30; | ||
| 18 | use constant DEFAULT_TEMPLATE_EXTENSION => 'template'; | ||
| 19 | use constant DEFAULT_PACKAGE_TEMPLATE_DIR => 'template/'; | ||
| 20 | use constant DEFAULT_TEMPLATE_TYPE => 'filesystem'; | ||
| 21 | |||
| 22 | # Copied from Template::Provider since they're not exported | ||
| 23 | |||
| 24 | use constant PREV => 0; | ||
| 25 | use constant NAME => 1; | ||
| 26 | use constant DATA => 2; | ||
| 27 | use constant LOAD => 3; | ||
| 28 | use constant NEXT => 4; | ||
| 29 | use constant STAT => 5; | ||
| 30 | |||
| 31 | # This should return a two-item list: the first is the template to be | ||
| 32 | # processed, the second is an error (if any). $name is a simple name | ||
| 33 | # of a template, which in our case is often of the form | ||
| 34 | # 'package::template_name'. | ||
| 35 | |||
| 36 | sub fetch { | ||
| 37 | my ( $self, $text ) = @_; | ||
| 38 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 39 | my ( $name ); | ||
| 40 | |||
| 41 | # if scalar or glob reference, then get a unique name to cache by | ||
| 42 | |||
| 43 | if ( ref( $text ) eq 'SCALAR' ) { | ||
| 44 | $log->is_debug && | ||
| 45 | $log->debug( "anonymous template passed in" ); | ||
| 46 | $name = $self->_get_anon_name( $text ); | ||
| 47 | } | ||
| 48 | elsif ( ref( $text ) eq 'GLOB' ) { | ||
| 49 | $log->is_debug && | ||
| 50 | $log->debug( "GLOB passed in to fetch" ); | ||
| 51 | $name = $self->_get_anon_name( $text ); | ||
| 52 | } | ||
| 53 | |||
| 54 | # Otherwise, it's a 'package::template' name or a unique filename | ||
| 55 | # found in '$WEBSITE_DIR/template', both of which are handled in | ||
| 56 | # _load() below. Also check that the template name doesn't have | ||
| 57 | # any invalid characters (e.g., '../../../etc/passwd') | ||
| 58 | |||
| 59 | else { | ||
| 60 | $log->is_debug && | ||
| 61 | $log->debug( "info passed in [$text] is site filename or ", | ||
| 62 | "package::template; will check file system" ); | ||
| 63 | $name = $text; | ||
| 64 | undef $text; | ||
| 65 | eval { $self->_validate_template_name( $name ) }; | ||
| 66 | if ( $@ ) { | ||
| 67 | return ( $@, Template::Constants::STATUS_ERROR ); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | # If we have a directory to compile the templates to, create a | ||
| 72 | # unique filename for this template | ||
| 73 | |||
| 74 | # Just keep the compile name the same as the name passed | ||
| 75 | # in, replacing '::' with '-' | ||
| 76 | |||
| 77 | my ( $compile_file ); | ||
| 78 | |||
| 79 | if ( $self->{COMPILE_DIR} ) { | ||
| 80 | my $ext = $self->{COMPILE_EXT} || '.ttc'; | ||
| 81 | my $compile_name = $name; | ||
| 82 | $compile_name =~ s/::/-/g; | ||
| 83 | $compile_file = File::Spec->catfile( $self->{COMPILE_DIR}, | ||
| 84 | $compile_name . $ext ); | ||
| 85 | $log->is_debug && | ||
| 86 | $log->debug( "compiled output filename [$compile_file]" ); | ||
| 87 | } | ||
| 88 | |||
| 89 | my ( $data, $error ); | ||
| 90 | |||
| 91 | # caching disabled (cache size is 0) so load and compile but don't cache | ||
| 92 | |||
| 93 | if ( $self->{SIZE} == 0 ) { | ||
| 94 | $log->is_debug && | ||
| 95 | $log->debug( "fetch template [$name] [caching disabled]" ); | ||
| 96 | ( $data, $error ) = $self->_load( $name, $text ); | ||
| 97 | ( $data, $error ) = $self->_compile( $data, $compile_file ) unless ( $error ); | ||
| 98 | $data = $data->{data} unless ( $error ); | ||
| 99 | } | ||
| 100 | |||
| 101 | # cached entry exists, so refresh slot and extract data | ||
| 102 | |||
| 103 | elsif ( $name and ( my $cache_slot = $self->{LOOKUP}{ $name } ) ) { | ||
| 104 | $log->is_debug && | ||
| 105 | $log->debug( "fetch template [$name] ", | ||
| 106 | "[cached (limit: $self->{SIZE})]" ); | ||
| 107 | ( $data, $error ) = $self->_refresh( $cache_slot ); | ||
| 108 | $data = $cache_slot->[ DATA ] unless ( $error ); | ||
| 109 | } | ||
| 110 | |||
| 111 | # nothing in cache so try to load, compile and cache | ||
| 112 | |||
| 113 | else { | ||
| 114 | $log->is_debug && | ||
| 115 | $log->debug( "fetch template ( $name ) ", | ||
| 116 | "[uncached (limit: $self->{SIZE})]" ); | ||
| 117 | ( $data, $error ) = $self->_load( $name, $text ); | ||
| 118 | ( $data, $error ) = $self->_compile( $data, $compile_file ) unless ( $error ); | ||
| 119 | $data = $self->_store( $name, $data ) unless ( $error ); | ||
| 120 | } | ||
| 121 | |||
| 122 | return( $data, $error ); | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | # NOTE: You should NEVER even check to see if $name exists anywhere | ||
| 127 | # else on the filesystem besides under the $WEBSITE_DIR. The | ||
| 128 | # SiteTemplate object takes care of this, but it's just another | ||
| 129 | # warning... | ||
| 130 | # | ||
| 131 | # From Template::Provider -- here's what the hashref includes: | ||
| 132 | # | ||
| 133 | # name filename or $content, if provided, or 'input text', etc. | ||
| 134 | # text template text | ||
| 135 | # time modification time of file, or current time for handles/strings | ||
| 136 | # (we also use this for the 'last_update' field of an SPOPS object) | ||
| 137 | # load time file/object was loaded (now!) | ||
| 138 | |||
| 139 | sub _load { | ||
| 140 | my ( $self, $name, $content ) = @_; | ||
| 141 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 142 | $log->is_debug && | ||
| 143 | $log->debug( "_load(@_[1 .. $#_])" ); | ||
| 144 | |||
| 145 | # If no name, $self->{TOLERANT} being true means we can decline | ||
| 146 | # safely. Otherwise return an error. We might modify this in the | ||
| 147 | # future to not even check TOLERANT -- if it's not defined we | ||
| 148 | # don't want anything to do with it, and nobody else should either | ||
| 149 | # (NYAH!). Note that $name should be defined even if we're doing a | ||
| 150 | # scalar ref or glob template | ||
| 151 | |||
| 152 | unless ( defined $name ) { | ||
| 153 | if ( $self->{TOLERANT} ) { | ||
| 154 | $log->is_debug && | ||
| 155 | $log->debug( "No name passed in and TOLERANT set, ", | ||
| 156 | "so decline" ); | ||
| 157 | return ( undef, Template::Constants::STATUS_DECLINED ); | ||
| 158 | } | ||
| 159 | $log->is_debug && | ||
| 160 | $log->debug( "No name passed in and TOLERANT not set ", | ||
| 161 | "so return error" ); | ||
| 162 | return ( "No template", Template::Constants::STATUS_ERROR ); | ||
| 163 | } | ||
| 164 | |||
| 165 | # is this an anonymous template? if so, return it | ||
| 166 | |||
| 167 | # Note: it would be cool if we could figure out where 'name' is | ||
| 168 | # passed to and have it deal with references properly, and then | ||
| 169 | # propogate that reference through to processing, etc. | ||
| 170 | |||
| 171 | if ( ref( $content ) eq 'SCALAR' ) { | ||
| 172 | $log->is_debug && | ||
| 173 | $log->debug( "Nothing to load: template is scalar ref." ); | ||
| 174 | return ({ name => $name, | ||
| 175 | text => $$content, | ||
| 176 | time => time, | ||
| 177 | load => 0 }, undef ); | ||
| 178 | } | ||
| 179 | |||
| 180 | if ( ref( $content ) eq 'GLOB' ) { | ||
| 181 | $log->is_debug && | ||
| 182 | $log->debug( "Load template from glob (file) ref" ); | ||
| 183 | local $/ = undef; | ||
| 184 | return ({ name => 'file handle', | ||
| 185 | text => <$content>, | ||
| 186 | time => time, | ||
| 187 | load => 0 }, undef ); | ||
| 188 | } | ||
| 189 | |||
| 190 | my ( $content_template, $data ); | ||
| 191 | eval { | ||
| 192 | $content_template = | ||
| 193 | CTX->lookup_class( 'template' )->fetch( $name ); | ||
| 194 | unless ( $content_template ) { | ||
| 195 | die "Template with name [$name] not found.\n"; | ||
| 196 | } | ||
| 197 | $data = { 'name' => $content_template->full_filename, | ||
| 198 | 'text' => $content_template->contents, | ||
| 199 | 'time' => $content_template->modified_on->epoch, | ||
| 200 | 'load' => time }; | ||
| 201 | |||
| 202 | }; | ||
| 203 | if ( $@ ) { | ||
| 204 | return ( $@, Template::Constants::STATUS_ERROR ); | ||
| 205 | } | ||
| 206 | return ( $data, undef ); | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | # Override so we can use OI-configured value for seeing whether we | ||
| 211 | # need to refresh | ||
| 212 | |||
| 213 | sub _refresh { | ||
| 214 | my ( $self, $slot ) = @_; | ||
| 215 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 216 | |||
| 217 | $log->is_debug && $log->debug( "_refresh([ @$slot ])" ); | ||
| 218 | |||
| 219 | # If the cache time has expired reload the entry | ||
| 220 | |||
| 221 | my $do_reload = 0; | ||
| 222 | my $tt_config = CTX->lookup_content_generator_config( 'TT' ); | ||
| 223 | my $max_cache_time = $tt_config->{cache_expire} | ||
| 224 | || DEFAULT_MAX_CACHE_TIME; | ||
| 225 | my ( $data, $error ); | ||
| 226 | my $elapsed_time = $slot->[ DATA ]->{time} - time; | ||
| 227 | if ( $elapsed_time > $max_cache_time ) { | ||
| 228 | $log->is_debug && | ||
| 229 | $log->debug( "Doing refresh for ", $slot->[ NAME ], " ", | ||
| 230 | "because $elapsed_time > $max_cache_time" ); | ||
| 231 | ( $data, $error ) = $self->_load( $slot->[ NAME ] ); | ||
| 232 | unless ( $error ) { | ||
| 233 | ( $data, $error ) = $self->_compile( $data ); | ||
| 234 | unless ( $error ) { | ||
| 235 | $slot->[ DATA ] = $data->{data}; | ||
| 236 | $slot->[ LOAD ] = $data->{time}; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | # remove existing slot from usage chain... | ||
| 242 | |||
| 243 | unless( $self->{ HEAD } == $slot ) { | ||
| 244 | if ( $slot->[ PREV ] ) { | ||
| 245 | $slot->[ PREV ][ NEXT ] = $slot->[ NEXT ]; | ||
| 246 | } | ||
| 247 | else { | ||
| 248 | $self->{HEAD} = $slot->[ NEXT ]; | ||
| 249 | } | ||
| 250 | if ( $slot->[ NEXT ] ) { | ||
| 251 | $slot->[ NEXT ][ PREV ] = $slot->[ PREV ]; | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | $self->{TAIL} = $slot->[ PREV ]; | ||
| 255 | } | ||
| 256 | |||
| 257 | # ... and add to start of list | ||
| 258 | my $head = $self->{HEAD}; | ||
| 259 | $head->[ PREV ] = $slot if ( $head ); | ||
| 260 | $slot->[ PREV ] = undef; | ||
| 261 | $slot->[ NEXT ] = $head; | ||
| 262 | $self->{HEAD} = $slot; | ||
| 263 | } | ||
| 264 | |||
| 265 | return ( $data, $error ); | ||
| 266 | } | ||
| 267 | |||
| 268 | |||
| 269 | # Ensure there aren't any funny characters | ||
| 270 | |||
| 271 | sub _validate_template_name { | ||
| 272 | my ( $self, $name ) = @_; | ||
| 273 | if ( $name =~ m|\.\.| ) { | ||
| 274 | die "Template name must not have any directory tree symbols (e.g., '..')"; | ||
| 275 | } | ||
| 276 | if ( $name =~ m|^/| ) { | ||
| 277 | die "Template name must not begin with an absolute path symbol"; | ||
| 278 | } | ||
| 279 | return 1; | ||
| 280 | } | ||
| 281 | |||
| 282 | ######################################## | ||
| 283 | # ANONYMOUS TEMPLATE NAME | ||
| 284 | |||
| 285 | # store names for non-named templates by using a unique fingerprint of | ||
| 286 | # the template text as a hash key | ||
| 287 | |||
| 288 | my $ANON_NUM = 0; | ||
| 289 | my %ANON_TEMPLATE = (); | ||
| 290 | |||
| 291 | sub _get_anon_name { | ||
| 292 | my ( $self, $text ) = @_; | ||
| 293 | my $key = Digest::MD5::md5_hex( ref( $text ) ? $$text : $text ); | ||
| 294 | return $ANON_TEMPLATE{ $key } if ( exists $ANON_TEMPLATE{ $key } ); | ||
| 295 | return $ANON_TEMPLATE{ $key } = 'anon_' . ++$ANON_NUM; | ||
| 296 | } | ||
| 297 | |||
| 298 | |||
| 299 | 1; | ||
| 300 | |||
| 301 | __END__ |