PageLookup.pm

filter: Statementstmt 99.3% 148 / 149 Branchbran 73.3% 44 / 60 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 22 / 22 Podpod 100% 15 / 15 totaltotal 89.6% 234 / 261 CC 39 SCAR 21.5
CC
39
Cov
90%
pagelookup
17.5
pagelookup_config
12.6
pagelookup_sql
4.0
CRAP 8.6
« PageLookup/Loop.pm ↑ Summary PageLookup/Href.pm »
1 package CGI::Application::Plugin::PageLookup;
2  
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use CGI::Application::Plugin::Forward;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # Items to export into callers namespace by default. Note: do not export
10 # names by default without a very good reason. Use EXPORT_OK instead.
11 # Do not simply export all your public functions/methods/constants.
12 17 @EXPORT_OK = qw(
13 pagelookup_config
14 pagelookup_get_config
15 pagelookup_set_charset
16 pagelookup_prefix
17 pagelookup_sql
18 pagelookup
19 pagelookup_notfound
20 pagelookup_set_expiry
21 pagelookup_default_lang
22 pagelookup_404
23 pagelookup_msg_param
24 pagelookup_rm
25 xml_sitemap_rm
26 xml_sitemap_sql
27 xml_sitemap_base_url
28 );
29 17 %EXPORT_TAGS = (all => \@EXPORT_OK);
30  
31 =head1 NAME
32  
33 CGI::Application::Plugin::PageLookup - Database driven model framework for CGI::Application
34  
35 =head1 VERSION
36  
37 Version 1.8
38  
39 =cut
40  
41 17 our $VERSION = '1.8';
42  
43 =head1 DESCRIPTION
44  
45 A model component for CGI::Application built around a table that has one row for each
46 page and that provides support for multiple languages and the 'dot' notation in templates.
47  
48 =head1 SYNOPSIS
49  
50 package MyCGIApp base qw(CGI::Application);
51 use CGI::Application::Plugin::PageLookup qw(:all);
52  
53 # Anything but the simplest usage depends on "dot" notation.
54 use HTML::Template::Pluggable;
55 use HTML::Template::Plugin::Dot;
56  
57 sub cgiapp_init {
58 my $self = shift;
59  
60 # pagelookup depends CGI::Application::DBH;
61 $self->dbh_config(......); # whatever arguments are appropriate
62
63 $self->html_tmpl_class('HTML::Template::Pluggable');
64  
65 $self->pagelookup_config(
66  
67 # prefix defaults to 'cgiapp_'.
68 prefix => 'mycgiapp_',
69  
70 # load smart dot-notation objects
71 objects =>
72 {
73 # Support for TMPL_LOOP
74 loop => 'CGI::Application::Plugin::PageLookup::Loop',
75  
76 # Decoupling external and internal representations of URLs
77 href => 'CGI::Application::Plugin::PageLookup::Href',
78  
79 # Page specific and site wide parameters
80 value => 'CGI::Application::Plugin::PageLookup::Value',
81  
82 # We have defined a MyCGIApp::method method
83 method => 'create_custom_object',
84  
85 # We can also handle CODE refs
86 callback => sub {
87 my $self = shift;
88 my $page_id = shift;
89 my $template = shift;
90 ........
91 }
92  
93 },
94  
95 # remove certain fields before sending the parameters to the template.
96 remove =>
97 [
98 'custom_col1',
99 'priority'
100 ],
101  
102 xml_sitemap_base_url => 'http://www.mytestsite.org'
103  
104 );
105  
106 }
107  
108 sub create_custom_object {
109 my $self = shift;
110 my $page_id = shift;
111 my $template = shift;
112 my $name = shift;
113 return ........... # smart object that can be used for dot notation
114 }
115  
116 sub setup {
117 my $self = shift;
118  
119 $self->run_modes({
120 'pagelookup' => 'pagelookup_rm',
121 'xml_sitemap' => 'xml_sitemap',
122 'extra_stuff' => 'extra_stuff'
123 });
124 ............
125 }
126  
127 sub extra_stuff {
128 my $self = shift;
129  
130 # do page lookup
131 my $template_obj = $self->pagelookup($page_id,
132 handle_notfound=>0, # force function to return undef if page not found
133 objects=> ....); # but override config for this run mode alone.
134  
135 return $self->notfound($page_id) unless $template_obj;
136  
137 # More custom stuff
138 $template_obj->param( .....);
139  
140 return $template_obj->output;
141
142 }
143  
144 =head1 DATABASE
145  
146 Something like the following schema is assumed. In general each column on these tables
147 corresponds to a template parameter that needs to be on every page on the website and each row in the join
148 corresponds to a page on the website. The exact types are not required and can be changed
149 but these are the recommended values. The lang and internalId columns combined should be as unique as the pageId
150 column. They are used to link the different language versions of the same page and also the page with
151 nearby pages in the same language. The lang column is used to join the two pages. The lang and collation fields expect
152 to find some template structure like this:
153 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<TMPL_VAR NAME="lang">-<TMPL_VAR NAME="collation">">
154 ...
155 </html>
156 The priority, lastmod and changefreq columns are used in XML sitemaps as defined by http://www.sitemaps.org/protocol.php.
157 The changefreq field is also used in setting the expiry header. Since these fields are not expected to be in general usage,
158 by default they are deleted just before being sent to the template. The lineage and rank columns are used by menu/sitemap
159 functionality and together should be unique.
160  
161 =over
162  
163 =item Table: cgiapp_structure
164  
165 Field Type Null Key Default Extra
166 ------------ ------------------------------------------------------------------- ---- ---- ------- -----
167 internalId unsigned numeric(10,0) NO PRI NULL
168 template varchar(20) NO NULL
169 lastmod date NO NULL
170 changefreq enum('always','hourly','daily','weekly','monthly','yearly','never') NO NULL
171 priority decimal(3,3) YES NULL
172 lineage varchar(255) NO UNI NULL
173 rank unsigned numeric(10,0) NO UNI NULL
174  
175 =item Table: cgiapp_pages
176  
177 Field Type Null Key Default Extra
178 ------------ ------------------------------------------------------------------- ---- ---- ------- -----
179 pageId varchar(255) NO UNI NULL
180 lang varchar(2) NO PRI NULL
181 internalId unsigned numeric(10,0) NO PRI NULL
182  
183 + any custom columns that the web application might require.
184  
185 =item Table: cgiapp_lang
186  
187 Field Type Null Key Default Extra
188 ------------ ------------------------------------------------------------------- ---- ---- ------- -----
189 lang varchar(2) NO PRI NULL
190 collation varchar(2) NO NULL
191  
192 + any custom columns that the web application might require.
193  
194 =back
195  
196 =head1 EXPORT
197  
198 These functions can be optionally imported into the CGI::Application or related namespace.
199  
200 pagelookup_config
201 pagelookup_get_config
202 pagelookup_set_charset
203 pagelookup_prefix
204 pagelookup_sql
205 pagelookup
206 pagelookup_notfound
207 pagelookup_set_expiry
208 pagelookup_default_lang
209 pagelookup_404
210 pagelookup_msg_param
211 pagelookup_rm
212 xml_sitemap_rm
213 xml_sitemap_sql
214 xml_sitemap_base_url
215  
216 Use the tag :all to export all of them.
217  
218 =head1 FUNCTIONS
219  
220 =head2 pagelookup_config
221  
222 This function defines the default behaviour of the plugin, though this can be overridden for specific runmodes.
223 The possible arguments are as follows:
224  
225 =over
226  
227 =item prefix
228  
229 This sets the prefix used in the database schema. It defaults to 'cgiapp_'.
230  
231 =item handle_notfound
232  
233 If set (which it is by default), the pagelookup function will return
234 the results of calling pagelookup_notfound when a pagelookup fails. If not set
235 the runmode must handle page lookup failures itself which it will identify
236 because the pagelookup function will return undef.
237  
238 =item expiry
239  
240 If set (which it is by default), the pagelookup function will set the appropriate
241 expiry header based upon the changefreq column.
242  
243 =item remove
244  
245 This points to an array ref of fields that are not expected to be required by the template.
246 It defaults to template, pageId and internalId, changefreq.
247  
248 =item objects
249  
250 This points to a hash ref. Each key is a parameter name (upto the dot). The value
251 is something that defines a smart object as described in L<HTML::Template::Plugin::Dot>.
252 The point about a smart object is that usually it defines an AUTOLOAD function so if the template
253 has <TMPL_VAR NAME="object.getcarter"> and the pagelookup_config has mapped object to some
254 object $MySmartObject then the method $MySmartObject->getcarter() will be called. Alternatively
255 there may be no AUTOLOAD function but the smart object may have methods that take additional arguments.
256 This way the template can be much more decoupled from the structure of the database.
257  
258 There are three ways a smart object can be defined. Firstly if the value is a CODE ref,
259 then the ref is passed 1.) the reference to the CGI::Application object; 2.) the page id; 3.) the template,
260 4.) the parameter name 5.) any argument overrides. Otherwise if the CGI::Application has the value as a method, then the method is called with
261 the same arguments as above. Finally the value is assumed to be the name of a module and the new constructor
262 of the supposed module is called with the same arguments. A typical smart object might be coded as follows:
263  
264 package MySmartObject;
265  
266 sub new {
267 my $class = shift;
268 my $self = .....
269 ......
270 return bless $self, $class;
271 }
272  
273 # If you do not have this, then HTML::Template::Plugin::Dot will not know that you can!
274 # [Note really can is supposed to return a subroutine ref, but this works in this context.]
275 sub can { return 1; }
276  
277 # This is the function that actually produces the value to be inserted into the template.
278 sub AUTOLOAD {
279 my $self = shift;
280 my $method = $AUTOLOAD;
281 if ($method =~ s/^MySmartObject\:\:(.+)$/) {
282 $method = $1; # Now we have what is in the template.
283 }
284 else {
285 ....
286 }
287 .....
288 return $value;
289 }
290  
291 Note that the smart object does not have access to HASH ref because the data is changing at the point
292 it would be used and so is non-deterministic.
293  
294 =item charset
295  
296 This is a string defining the character encoding. This defaults to 'utf-8'.
297  
298 =item template_params
299  
300 This is a hashref containing additional parameters that are to be passed to the load_templ function.
301  
302 =item default_lang
303  
304 This is a two letter code and defaults to 'en'. It is used when creating a notfound page when a language
305 cannot otherwise be guessed.
306  
307 =item status_404
308  
309 This is the internal id corresponding to the not found page.
310  
311 =item msg_param
312  
313 This is the parameter used to store error messages.
314  
315 =item xml_sitemap_base_url
316  
317 This is the url for the whole site. It is mandatory to set this if you want XML sitemaps (which you should).
318  
319 =back
320  
321 =cut
322  
323 71 ▶sub pagelookup_config {
Subroutine pagelookup_config: called CC=12 SCAR=25.3
Pod documented
324 71 my $self = shift;
325 71 my %args = @_;
326  
327 71 ▶ croak "Calling pagelookup_config after the pagelookup has already been configured" if defined $self->{__cgi_application_plugin_pagelookup};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"__cgi_application_plugin_pagelookup"} 0 71 71
328  
329 71 ▶ $args{prefix} = "cgiapp_" unless exists $args{prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"prefix"} 68 3 71
330 71 ▶ $args{handle_notfound} = 1 unless exists $args{handle_notfound};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"handle_notfound"} 71 0 71
331 71 ▶ $args{expiry} = 1 unless exists $args{expiry};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"expiry"} 71 0 71
332 71 ▶ $args{remove} = ['template', 'pageId', 'internalId', 'changefreq'] unless exists $args{remove};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"remove"} 0 71 71
333 71 ▶ $args{objects} = {} unless exists $args{objects};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"objects"} 22 49 71
334 71 ▶ $args{template_params} = {} unless exists $args{template_params};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"template_params"} 43 28 71
335 71 ▶ $args{default_lang} = 'en' unless exists $args{default_lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"default_lang"} 71 0 71
336 71 ▶ $args{status_404} = '404' unless exists $args{status_404};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"status_404"} 38 33 71
337 71 ▶ $args{msg_param} = 'pagelookup_message' unless exists $args{msg_param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"msg_param"} 35 36 71
338 71 ▶ $args{charset} = 'utf-8' unless exists $args{charset};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"charset"} 71 0 71
339  
340 71 $self->{__cgi_application_plugin_pagelookup} = \%args;
341  
342 71 $self->pagelookup_set_charset();
343 71 return;
344 }
345  
346 =head2 pagelookup_get_config
347  
348 Returns config including any overrides passed in as arguments.
349  
350 =cut
351  
352 434 ▶sub pagelookup_get_config {
Subroutine pagelookup_get_config: called CC=1 SCAR=0.0
Pod documented
353 434 my $self = shift;
354 434 my %args = (%{$self->{__cgi_application_plugin_pagelookup}}, @_);
355 434 return %args;
356 }
357  
358 =head2 pagelookup_set_charset
359  
360 This function sets the character set based upon the config.
361  
362 =cut
363  
364 71 ▶sub pagelookup_set_charset {
Subroutine pagelookup_set_charset: called CC=1 SCAR=0.0
Pod documented
365 71 my $self = shift;
366 71 my %args = $self->pagelookup_get_config(@_);
367 71 $self->header_props(-encoding=>$args{charset},-charset=>$args{charset});
368 71 return;
369 }
370  
371 =head2 pagelookup_prefix
372  
373 This function returns the prefix that is used on the database for all the tables.
374 The prefix can of course be overridden.
375  
376 =cut
377  
378 256 ▶sub pagelookup_prefix {
Subroutine pagelookup_prefix: called CC=1 SCAR=0.0
Pod documented
379 256 my $self = shift;
380 256 my %args = $self->pagelookup_get_config(@_);
381 256 return $args{prefix};
382 }
383  
384 =head2 pagelookup_sql
385  
386 This function returns the SQL that is used to lookup a specific page.
387 It takes a single argument which is usually expected to be a pageId.
388 This may also be taken in the form of a HASH ref having two fields: internalId and lang.
389  
390 =cut
391  
392 66 ▶sub pagelookup_sql {
Subroutine pagelookup_sql: called CC=4 SCAR=14.0
Pod documented
393 66 my $self = shift;
394 66 my $page_id = shift;
395 66 my $prefix = $self->pagelookup_prefix(@_);
396 66 ▶ if (ref($page_id) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $page_id eq "HASH") 12 54 66
397 12 ▶ croak "internalId expected" unless exists $page_id->{internalId};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $page_id->{"internalId"} 0 12 12
398 12 ▶ croak "lang expected" unless exists $page_id->{lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $page_id->{"lang"} 0 12 12
399 12 return "SELECT s.template, s.changefreq, p.*, l.* FROM ${prefix}pages p, ${prefix}lang l, ${prefix}structure s WHERE p.lang = l.lang AND p.lang = '$page_id->{lang}' AND p.internalId = s.internalId AND p.internalId = $page_id->{internalId}";
400 }
401 54 return "SELECT s.template, s.changefreq, p.*, l.* FROM ${prefix}pages p, ${prefix}lang l, ${prefix}structure s WHERE p.lang = l.lang AND p.pageId = '$page_id' AND p.internalId = s.internalId";
402 }
403  
404 =head2 pagelookup
405  
406 This is the function that does the heavy lifting. It takes a page id and optionally some
407 arguments overriding the default config. Then the sequence of events is as follows:
408 1.) Lookup up the various parameters from the database.
409 2.) If this fails then exit either handling or just returning undef according to instructions.
410 3.) Load the template object.
411 4.) Set the expiry header unless instructed not to.
412 5.) Load the smart objects that are mentioned in the template.
413 6.) Remove unwanted parameters.
414 7.) Put the parameters into the template object.
415 8.) Return the now partially or completely filled template object.
416  
417 The page id may also be taken in the form of a HASH ref having two fields: internalId and lang.
418  
419 =cut
420  
421 66 ▶sub pagelookup {
Subroutine pagelookup: called CC=17 SCAR=28.6
Pod documented
422 66 my $self = shift;
423 66 my $page_id = shift;
424 66 my @inargs = @_;
425 66 my %args = $self->pagelookup_get_config(@inargs);
426 66 my $dbh = $self->dbh();
427 66 ▶ my $sth = $dbh->prepare($self->pagelookup_sql($page_id, @inargs)) || croak $dbh->errstr;
Condition50%1/2
$dbh->prepare($self->pagelookup_sql($page_id, @inargs)) || croak($dbh->errstr)
l!l
660
MC/DC0%0/1
$dbh->prepare($self->pagelookup_sql($page_id, @inargs)) || croak($dbh->errstr)
$dbh->prepare($self->pagelookup_sql($page_id, @inargs))
Truth table50%1/2
A
A= $dbh->prepare($self->pagelookup_sql($page_id, @inargs))
Aresult
11
00
428 66 ▶ $sth->execute || croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak($dbh->errstr) 66 0 66
429 66 my $hash_ref = $sth->fetchrow_hashref;
430  
431 # check if page was found
432 66 ▶ unless ($hash_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash_ref) 10 56 66
433 10 ▶ croak $dbh->errstr if $dbh->err;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->err 0 10 10
434 10 $sth->finish;
435  
436 10 ▶ if ($args{handle_notfound}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"handle_notfound"}) 10 0 10
437 10 return $self->pagelookup_notfound($page_id, @inargs);
438 }
439  
440 0 return undef;
441 }
442  
443 56 ▶ $page_id = $hash_ref->{pageId} if ref($page_id) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $page_id eq "HASH" 12 44 56
444  
445 # Load the template
446 56 my $template = $self->load_tmpl($hash_ref->{template}, %{$args{template_params}});
447  
448 # Get a list of smart objects mentioned in the template
449 56 my %smart_objects_actually_used = ();
450 56 foreach my $o ($template->query()) {
451 315 ▶ if ($o =~ /^([a-zA-Z_]\w+)\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o =~ /^([a-zA-Z_]\w+)\./) 105 210 315
452 105 $smart_objects_actually_used{$1} = 1;
453 }
454 }
455  
456 # Set the expiry headers
457 56 ▶ $self->pagelookup_set_expiry($hash_ref, @inargs) if $args{expiry};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"expiry"} 56 0 56
458  
459 # create the smart objects
460 56 foreach my $okey (keys %{$args{objects}}) {
461 75 ▶ next unless $smart_objects_actually_used{$okey};
Branch: 1 branch
BranchTrueFalseTotal
unless $smart_objects_actually_used{$okey} 9 66 75
462 66 my $ovalue = $args{objects}->{$okey};
463 66 my $object = undef;
464  
465 66 ▶ if (ref($ovalue) eq "CODE") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ovalue eq 'CODE') { } 10 56 66
elsif ($self->can($ovalue)) { } 9 47 56
466 10 $object = &$ovalue($self, $page_id, $template, $okey, @inargs);
467 }
468 elsif ($self->can($ovalue)) {
469 9 $object = $self->$ovalue($page_id, $template, $okey, @inargs);
470 }
471 else {
472 17 ▶ use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
473 47 $object = eval {
474 47 $ovalue->require;
475 47 return $ovalue->new($self, $page_id, $template, $okey, @inargs);
476 };
477 47 ▶ croak "Could not create smart object: $okey: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 47 47
478 }
479  
480 66 ▶ $hash_ref->{$okey} = $object if $object;
Branch: 1 branch
BranchTrueFalseTotal
if $object 66 0 66
481  
482 }
483  
484 # remove unwanted parameters
485 56 foreach my $remove (@{$args{remove}}) {
486 286 delete $hash_ref->{$remove};
487 }
488  
489 # we cannot think of anything else to stop us from inserting the parameters into the template
490 56 $template->param(%$hash_ref);
491 56 return $template;
492 }
493  
494 =head2 pagelookup_rm
495  
496 This function is a generic run mode. It takes a page id and tries to do everything else.
497 Of course most of the work is done by pagelookup.
498  
499 =cut
500  
501 56 ▶sub pagelookup_rm {
Subroutine pagelookup_rm: called CC=3 SCAR=11.3
Pod documented
502 56 my $self = shift;
503 56 ▶ my $page_id = $self->param('pageid') || return $self->forward($self->start_mode());
Condition50%1/2
$self->param('pageid') || (return $self->forward($self->start_mode))
l!l
560
MC/DC0%0/1
$self->param('pageid') || (return $self->forward($self->start_mode))
$self->param('pageid')
Truth table50%1/2
A
A= $self->param('pageid')
Aresult
11
00
504 56 my $template = $self->pagelookup($page_id);
505 56 ▶ croak "no template returned: $page_id" unless $template;
Branch: 1 branch
BranchTrueFalseTotal
unless $template 0 56 56
506 56 return $template->output;
507 }
508  
509 =head2 xml_sitemap_rm
510  
511 This method is intended to be installed as a sitemap. Since the format is fixed, it is self-contained and does not load
512 templates from files. Note if a page as a null priority then it is not put in the sitemap.
513 For this function to work it is necessary to set the base BASE_URL parameter.
514  
515 =cut
516  
517 1 ▶sub xml_sitemap_rm {
Subroutine xml_sitemap_rm: called CC=3 SCAR=11.0
Pod documented
518 1 my $self = shift;
519 1 my $dbh = $self->dbh();
520 1 my $base_url = $self->xml_sitemap_base_url();
521 1 my $sql = $self->xml_sitemap_sql();
522 1 ▶ my $sth = $dbh->prepare($sql) || croak $dbh->errstr;
Condition50%1/2
$dbh->prepare($sql) || croak($dbh->errstr)
l!l
10
MC/DC0%0/1
$dbh->prepare($sql) || croak($dbh->errstr)
$dbh->prepare($sql)
Truth table50%1/2
A
A= $dbh->prepare($sql)
Aresult
11
00
523 1 ▶ $sth->execute or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak($dbh->errstr) 1 0 1
524 1 my $hash_ref = $sth->fetchall_arrayref({});
525 1 my $template =<<"EOS"
526 <?xml version="1.0" encoding="UTF-8"?>
527 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
528 <TMPL_LOOP NAME="urls">
529 <url>
530 <loc>$base_url<TMPL_VAR NAME="pageId"></loc>
531 <lastmod><TMPL_VAR NAME="lastmod"></lastmod>
532 <changefreq><TMPL_VAR NAME="changefreq"></changefreq>
533 <priority><TMPL_VAR NAME="priority"></priority>
534 </url>
535 </TMPL_LOOP>
536 </urlset>
537 EOS
538 ;
539 1 my $t = $self->load_tmpl(\$template);
540 1 $t->param(urls=>$hash_ref);
541 1 $self->header_add( -type => 'text/xml', -charset=>'utf-8' );
542 1 return $t->output;
543 }
544  
545 =head2 pagelookup_notfound
546  
547 This function takes a page id which has failed a page lookup and tries to find the best fitting
548 404 page. First of all it attempts to find the correct by language by assuming that if the first three
549 characters of the page id consists of two characters followed by a '/'. If this matches then the first
550 two characters are taken to be the language. If that fails then the language is taken to be $self->pagelookup_default_lang.
551 Then the relevant 404 page is looked up by language and internal id. The internalId is taken to be $self->pagelookup_404 .
552 Of course it is assumed that this page lookup cannot fail. The header 404 status is added
553 to the header and the original page id is inserted into the $self->pagelookup_msg_param parameter.
554 If this logic does not match your URL structure you can omit exporting this function or turn notfound handling off
555 and implement your own logic.
556  
557 =cut
558  
559 10 ▶sub pagelookup_notfound {
Subroutine pagelookup_notfound: called CC=3 SCAR=11.0
Pod documented
560 10 my $self = shift;
561 10 my $page_id = shift;
562 10 my @inargs = @_;
563 10 my %args = $self->pagelookup_get_config(@inargs);
564  
565 # Best guess at language
566 10 my $lang = $self->pagelookup_default_lang(@inargs);
567 10 ▶ if ($page_id =~ /^(\w\w)\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_id =~ m[^(\w\w)/]) 8 2 10
568 8 $lang = $1;
569 }
570  
571 10 ▶ my $template = $self->pagelookup({lang=>$lang, internalId => $self->pagelookup_404}, handle_notfound=>0) || croak "failed to construct 'not found' page";
Condition50%1/2
$self->pagelookup({'lang', $lang, 'internalId', $self->pagelookup_404}, 'handle_notfound', 0) || croak(q[failed to construct 'not found' page])
l!l
100
MC/DC0%0/1
$self->pagelookup({'lang', $lang, 'internalId', $self->pagelookup_404}, 'handle_notfound', 0) || croak(q[failed to construct 'not found' page])
$self->pagelookup({'lang', $lang, 'internalId', $self->pagelookup_404}, 'handle_notfound', 0)
Truth table50%1/2
A
A= $self->pagelookup({'lang', $lang, 'internalId', $self->pagelookup_404}, 'handle_notfound', 0)
Aresult
11
00
572 10 $template->param( $self->pagelookup_msg_param(@inargs) => $page_id);
573 10 $self->header_add( -status => 404 );
574 10 return $template;
575  
576 }
577  
578 =head2 pagelookup_set_expiry
579  
580 This function sets the expiry header based upon the hash_ref.
581  
582 =cut
583  
584 56 ▶sub pagelookup_set_expiry {
Subroutine pagelookup_set_expiry: called CC=2 SCAR=6.9
Pod documented
585 56 my $self = shift;
586 56 my $hash_ref = shift;
587 56 ▶ my $changefreq = $hash_ref->{changefreq} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $changefreq = $hash_ref->{"changefreq"} or return 42 14 56
588 42 my %mapping = (always=>'-1d', hourly=>'+1h', daily=>'+1d', weekly=>'+7d', monthly=>'+1M', yearly=>'+1y', never=>'+3y');
589 42 $self->header_add(-expires=>$mapping{$changefreq});
590 42 return;
591 }
592  
593 =head2 pagelookup_default_lang
594  
595 This returns the default language code.
596  
597 =cut
598  
599 10 ▶sub pagelookup_default_lang {
Subroutine pagelookup_default_lang: called CC=1 SCAR=0.0
Pod documented
600 10 my $self = shift;
601 10 my %args = $self->pagelookup_get_config(@_);
602 10 return $args{default_lang};
603 }
604  
605 =head2 pagelookup_404
606  
607 This returns the core id used by 404 pages.
608  
609 =cut
610  
611 10 ▶sub pagelookup_404 {
Subroutine pagelookup_404: called CC=1 SCAR=0.0
Pod documented
612 10 my $self = shift;
613 10 my %args = $self->pagelookup_get_config(@_);
614 10 return $args{status_404};
615 }
616  
617 =head2 pagelookup_msg_param
618  
619 This returns the parameter that pagelookup uses for inserting error messages.
620  
621 =cut
622  
623 10 ▶sub pagelookup_msg_param {
Subroutine pagelookup_msg_param: called CC=1 SCAR=0.0
Pod documented
624 10 my $self = shift;
625 10 my %args = $self->pagelookup_get_config(@_);
626 10 return $args{msg_param};
627 }
628  
629 =head2 xml_sitemap_sql
630  
631 This returns the SQL used to get the XML sitemap data.
632  
633 =cut
634  
635 1 ▶sub xml_sitemap_sql {
Subroutine xml_sitemap_sql: called CC=1 SCAR=0.0
Pod documented
636 1 my $self = shift;
637 1 my $prefix = $self->pagelookup_prefix(@_);
638 1 return "SELECT pageId, lastmod, changefreq, priority FROM ${prefix}pages p, ${prefix}structure s WHERE priority IS NOT NULL AND p.internalId = s.internalId ORDER BY priority DESC";
639 }
640  
641 =head2 xml_sitemap_base_url
642  
643 This returns the base url used in XML sitemaps.
644  
645 =cut
646  
647 1 ▶sub xml_sitemap_base_url {
Subroutine xml_sitemap_base_url: called CC=2 SCAR=7.1
Pod documented
648 1 my $self = shift;
649 1 my %args = $self->pagelookup_get_config(@_);
650 1 ▶ return $args{xml_sitemap_base_url} || croak "no xml sitemp base url set";
Condition50%1/2
$args{'xml_sitemap_base_url'} || croak('no xml sitemp base url set')
l!l
10
MC/DC0%0/1
$args{'xml_sitemap_base_url'} || croak('no xml sitemp base url set')
$args{'xml_sitemap_base_url'}
Truth table50%1/2
A
A= $args{'xml_sitemap_base_url'}
Aresult
11
00
651 }
652  
653  
654 =head1 AUTHOR
655  
656 Nicholas Bamber, C<< <nicholas at periapt.co.uk> >>
657  
658 =head1 BUGS
659  
660 Currently errors are not trapped early enough and hence error messages are less informative than they might be.
661  
662 Also we are working on validating the code against more L<DBI> drivers. Currently mysql and SQLite are known to work.
663 It is known to be incompatible with postgres, which should be fixed in the next release. This may entail schema changes.
664 It is also known to be in incompatible with L<DBD::DBM>, apparently on account of a join across three tables.
665 The SQL is not ANSI standard and that is one possible change. Another approach may be to make the schema configurable.
666  
667 Please report any bugs or feature requests to C<bug-cgi-application-plugin-pagelookup at rt.cpan.org>, or through
668 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-PageLookup>. I will be notified, and then you'll
669 automatically be notified of progress on your bug as I make changes.
670  
671 =head1 SUPPORT
672  
673 You can find documentation for this module with the perldoc command.
674  
675 perldoc CGI::Application::Plugin::PageLookup
676  
677  
678 You can also look for information at:
679  
680 =over 4
681  
682 =item * RT: CPAN's request tracker
683  
684 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Application-Plugin-PageLookup>
685  
686 =item * AnnoCPAN: Annotated CPAN documentation
687  
688 L<http://annocpan.org/dist/CGI-Application-Plugin-PageLookup>
689  
690 =item * CPAN Ratings
691  
692 L<http://cpanratings.perl.org/d/CGI-Application-Plugin-PageLookup>
693  
694 =item * Search CPAN
695  
696 L<http://search.cpan.org/dist/CGI-Application-Plugin-PageLookup/>
697  
698 =back
699  
700  
701 =head1 ACKNOWLEDGEMENTS
702  
703 Thanks to JavaFan for suggesting the use of L<Test::Database>. Thanks to Philippe Bruhat
704 for help with getting Test::Database to work more smoothly.
705  
706 =head1 COPYRIGHT & LICENSE
707  
708 Copyright 2009 Nicholas Bamber.
709  
710 This program is free software; you can redistribute it and/or modify it
711 under the terms of either: the GNU General Public License as published
712 by the Free Software Foundation; or the Artistic License.
713  
714 See http://dev.perl.org/licenses/ for more information.
715  
716  
717 =cut
718  
719 17 1; # End of CGI::Application::Plugin::PageLookup
« PageLookup/Loop.pm ↑ Summary PageLookup/Href.pm »