blib/lib/Apache/TinyCP.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Apache::TinyCP;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = "1.01";
6  
7 0 ▶use Apache::Constants qw(:common :response);
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use Cache::File;
10 use Template;
11 use Text::KwikiFormatish;
12 use POSIX qw(strftime);
13  
14 =head1 NAME
15  
16 Apache::TinyCP - a tiny content provider to put up content really quickly
17  
18 =head1 SYNOPSIS
19  
20 In your Apache configuration:
21  
22 PerlModule Apache::TinyCP
23
24 <Location />
25 SetHandler perl-script
26 PerlHandler Apache::TinyCP
27  
28 DirectoryIndex HomePage
29  
30 PerlSetVar ContentDir /home/www/content
31 PerlSetVar TemplateDir /home/www/templates
32 PerlSetVar CacheDir /home/www/var/contentcache
33 PerlSetVar ModTimeFormat %d.%b.%y
34 </Location>
35  
36 In your C<ContentDir> directory, a file named F<HomePage>, along with other
37 content files:
38  
39 Hello, world! *This* is the HomePage!
40
41 Here's a link to AnotherPage.
42
43 See [=Text::KwikiFormatish] for more info on the default formatter
44  
45 In your C<TemplateDir> directory, a file named F<layout>:
46  
47 <html>
48 <head>
49 <title>My Site - [% title %]</title>
50 </head>
51 <body>
52 <h1>[% title %]</h1>
53 <hr/>
54 [% content %]
55 <hr/>
56 <p>Last modified: [% modtime %]</p>
57 </body>
58 </html>
59  
60 =head1 DESCRIPTION
61  
62 This module is a very simple handler that takes files from F<ContentDir>,
63 formats them somehow, and stamps on a header and footer using the template file
64 F<template> in F<TemplateDir>. The default formatter is L<Text::KwikiFormatish>
65 and the end result is somewhat of a pseudo-wiki web site. It was created
66 because I enjoyed the ease and functionality of Kwiki text for creating
67 content, but wanted something like a single-user wiki.
68  
69 To set this up, create the three directories and set them as C<TemplateDir>,
70 C<ContentDir> and C<CacheDir> in the Apache configuration, like in
71 L<"SYNOPSIS">.
72  
73 By default the content files located in F<ContentDir> are formatted with
74 L<Text::KwikiFormatish> and cached with L<Cache::File>, which uses the path
75 specified at F<CacheDir> to store cache files. The default templating system is
76 L<Template::Toolkit>. The handler uses the F<layout> template wrapper in
77 C<TemplateDir>. The tags C<[% content %]>, C<[% title %]> and C<[% modified %]>
78 are replaced by the formatted content, the filename, and the formatted
79 modification date (respectively).
80  
81 You could probably do this by writing a formatting handler and filtering that
82 through L<Apache::Template>, though I'm not sure how the caching would work.
83 You might also want to check out L<Apache::Sandwich>. If any of these ways
84 works for you, more power to ya.
85  
86 My goal was to have all my content files in one place (not F<htdocs>) as well
87 as use wiki text for the rapid creation of content for my site.
88  
89 =head1 EXTENDING
90  
91 =head2 Changing the Formatter
92  
93 Say you wanted to write your pages in POD instead of Kwikish text. You would
94 create a custom package, override the C<format_content()> method and use that
95 as your PerlHandler. In your Apache configuration:
96  
97 <Perl>
98 use Pod::Simple::HTML ();
99
100 package Local::TinyCP;
101 use base qw( Apache::TinyCP );
102
103 sub format_content {
104 my ( $self, $data, $r ) = @_;
105 my $output;
106
107 my $parser = Pod::Simple::HTML->new;
108 $parser->output_string( \$output );
109 $parser->parse_string_document( $data );
110
111 return $output;
112 }
113 </Perl>
114
115 <Location />
116 ...
117 PerlHandler Local::TinyCP
118 ...
119 </Location>
120  
121 =head2 Futher Customization
122  
123 Here are all of the methods you can override. All methods are passed C<$self>,
124 the name of the package, as the first argument and sometimes C<$r>, the
125 L<Apache> request object.
126  
127 =over 4
128  
129 =cut
130  
131 # package global to ensure only one cache object per interpreter
132 our $CACHE;
133  
134 =item * get_filename( $self, $r )
135  
136 Returns the absolute path to the content file.
137  
138 =cut
139  
140 sub get_filename {
141 my ( $self, $r ) = @_;
142  
143 # simply use the ContentDir directive as the basedir
144 my $uri = $r->uri;
145 for ($uri) {
146 s#^/+##; # leading slash
147 s#[^[:print:]]##g; # unprintables
148 s#\.\./##g; # dots, for /../../..
149 }
150 return $r->dir_config('ContentDir') . "/$uri";
151 }
152  
153 =item * get_content( $self, $filename, $r )
154  
155 Returns the formatted content to be inserted into the C<[% content %]> template
156 variable. By default, this formats with L<Text::KwikiFormatish> and caches the
157 formatted content using L<Cache::File>.
158  
159 =cut
160  
161 sub get_content {
162 my ( $self, $filename, $r ) = @_;
163  
164 # ensure single instance of cache
165 unless ( defined $CACHE ) {
166  
167 # no cache object? create our cache object
168 $CACHE = Cache::File->new(
169 cache_root => $r->dir_config('CacheDir'),
170 default_expires => '100 days',
171 );
172  
173 # augh! no cache object?!!
174 unless ($CACHE) {
175 $r->log_reason( "Couldn't create cache object", $filename );
176 return;
177 }
178 }
179  
180 # try to get cached content if we can, make up arbitrary cache keys
181 my $content;
182 my $cache_key = $filename . '|' . ( stat $filename )[9];
183 unless ( $content = $CACHE->get($cache_key) ) {
184  
185 # okay, no cached content.
186 # start by getting the file's contents
187 my $data;
188 if ( open( FH, '<' . $filename ) ) {
189 $data = join( '', <FH> );
190 close FH;
191 }
192 else {
193 $r->log_reason( "Couldn't open file: $!", $filename );
194 return;
195 }
196  
197 #$r->log_error("*** Caching $cache_key");
198  
199 # format it
200 $content = $self->format_content( $data, $r );
201  
202 # save it to the cache as the uri plus the timestamp
203 $CACHE->set( $cache_key, $content );
204 }
205  
206 return $content;
207 }
208  
209 =item * format_content( $self, $data, $r )
210  
211 Return the formatted version of C<$data>, the contents of the content file.
212  
213 This method is only used by C<L<get_content>>. If you override
214 C<L<get_content>>, you will not need to override this method.
215  
216 =cut
217  
218 sub format_content {
219 my ( $self, $data, $r ) = @_;
220 return Text::KwikiFormatish::format($data);
221 }
222  
223 =item * print_content( $self, $content, $r )
224  
225 Print the given C<$content> to STDOUT. By default this method wraps the content
226 in a template, and provides two extra variables: C<[% title %]>, the name of
227 the document without a leading slash, and C<[% modtime %]>, the modification
228 time of the document. The modification time format is configurable -- see
229 L<"SYNOPSIS">.
230  
231 =cut
232  
233 sub print_content {
234 my ( $self, $content, $r ) = @_;
235  
236 # setup title to be passed
237 my $title = $r->uri;
238 $title =~ s#^/##;
239  
240 # create a template toolkit object, use TemplateDir as basedir
241 my $tt = Template->new(
242 { INCLUDE_PATH => $r->dir_config('TemplateDir'),
243 RECURSION => 1,
244 WRAPPER => 'layout',
245 }
246 );
247  
248 # no object? must be a Template::Toolkit error
249 unless ($tt) {
250 $r->log_error("Template::new() error: $Template::ERROR");
251 return;
252 }
253  
254 # go ahead and print the content!
255 unless (
256 $tt->process(
257 \$content,
258 { title => $title,
259 modified => $r->pnotes('modified'),
260 }
261 )
262 )
263 {
264 $r->log_error( "Template content error: " . $tt->error );
265 return;
266 }
267  
268 # everything was okay!
269 # "this is the EVERYTHING'S OKAY alarm!"
270 return 1;
271 }
272  
273 =item * get_content_type( $self )
274  
275 Return the content type returned. Default is C<text/html>.
276  
277 This method is only used by C<L<print_content>>. If you override
278 C<L<print_content>>, you will not need to override this method.
279  
280 =cut
281  
282 sub get_content_type {'text/html'}
283  
284 =item * handler( $self, $r )
285  
286 mod_per1 handler method. You do not need to override this method unless you
287 want to change the core logic.
288  
289 =cut
290  
291 sub handler ($$) {
292 my ( $self, $r ) = @_;
293  
294 # lookup what file we're looking for, return declined
295 # if we can't find the page, or forbidden if not -r
296 my $filename = $self->get_filename($r)
297 or return SERVER_ERROR;
298  
299 unless ( -e $filename ) {
300 return DECLINED;
301 }
302 unless ( -r $filename ) {
303 $r->log_reason( "File exists but not readable", $filename );
304 return FORBIDDEN;
305 }
306  
307 # get the modification time, put in $r->pnotes('modified')
308 if ( -e $filename ) {
309 $r->pnotes(
310 modified => strftime(
311 ( $r->dir_config('ModTimeFormat') || '%F' ),
312 localtime( ( stat $filename )[9] )
313 )
314 );
315 }
316  
317 # get the formatted content
318 my $content = $self->get_content( $filename, $r )
319 or return SERVER_ERROR;
320  
321 # print the formatted content
322 $r->send_http_header( $self->get_content_type );
323 return $self->print_content( $content, $r ) ? OK: SERVER_ERROR;
324 }
325  
326 =back
327  
328 =head1 AUTHOR
329  
330 Ian Langworth, C<< <ian@cpan.org> >>
331  
332 =head1 SEE ALSO
333  
334 L<Cache::File>, L<Template>, L<Text::KwikiFormatish>
335  
336 =head1 LICENSE
337  
338 This is free software. You may use it and redistribute it under the same terms
339 as Perl itself.
340  
341 =cut
342  
343 74;
344  
↑ Summary