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