This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.pm
859 lines (570 loc) · 21 KB
/
View.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
package View;
=head1 NAME
View (vw)
=head1 DESCRIPTION
This class is responsible for template management and binding of the
PI handlers in Actions to the PIs in the template and the invocation
of said PI handlers.
=head1 VERSION
$Id: View.pm,v 1.43 2010/02/03 20:33:32 pfarber Exp $
=head1 SYNOPSIS
my $vw = new View($C);
$vw->execute_view($C, $act);
$vw->output($C);
or
$self->output_HTTP($C, $data_ref, ['text/xml'] );
PRIVATE:
$vw->_install_PI_handlers($C, $act);
$vw->_run_PI_handlers($C);
$vw->_render_template($C);
=head1 METHODS
=over 8
=cut
BEGIN
{
if ($ENV{'HT_DEV'})
{
require "strict.pm";
strict::import();
}
}
use PI;
use Context;
use Utils;
use Action;
use Debug::DUtils;
use Utils::XSLT;
use Operation::Status;
use Utils;
use HTTP::Headers;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->_initialize(@_);
return $self;
}
# ---------------------------------------------------------------------
=item _initialize
Initialize View.
=cut
# ---------------------------------------------------------------------
sub _initialize {
my $self = shift;
my $C = shift;
$self->{'pio'} = new PI;
my $ab = $C->get_object('Bind');
$self->read_template($C, $ab);
$self->process_template_includes($C, $ab);
$self->parse_template_PIs($C, $ab);
}
# ---------------------------------------------------------------------
=item get_page
Description
=cut
# ---------------------------------------------------------------------
sub get_page {
my $self = shift;
my $page = $self->{'page'};
ASSERT($page, qq{View page not set});
return $page;
}
# ---------------------------------------------------------------------
=item execute_view
Handle the logic of the view including determination of whether the
application should redirect to produce the view or simply produce the
view inline.
=cut
# ---------------------------------------------------------------------
sub execute_view {
my $self = shift;
my ($C, $act) = @_;
# bind the Action's PIFiller to the template PIs
$self->_install_PI_handlers($C, $act);
# run the PI handlers
$self->_run_PI_handlers($C);
# transform XML -> HTML via XSLT
$self->_render_template($C);
}
# ---------------------------------------------------------------------
=item read_template
Read the text of the template for the action in question from the disk
=cut
# ---------------------------------------------------------------------
sub read_template {
my $self = shift;
my ($C, $ab) = @_;
my $template_name = $ab->get_action_template_name($C);
my ($template_ref, $full_path) = find_file($C, $template_name, 0);
$self->{'template_data_ref'} = $template_ref;
$self->{'template_name'} = $full_path;
}
# ---------------------------------------------------------------------
=item get_template_data
Template text data ref accessor.
=cut
# ---------------------------------------------------------------------
sub get_template_data {
my $self = shift;
my $C = shift;
return $self->{'template_data_ref'};
}
# ---------------------------------------------------------------------
=item get_template_name
Template name accessor.
=cut
# ---------------------------------------------------------------------
sub get_template_name {
my $self = shift;
my $C = shift;
return $self->{'template_name'};
}
# ---------------------------------------------------------------------
=item process_template_includes
Iteratively process chunk PIs until no more chunk PIs are seen as a
result of doing the inclusions. (Chunks can have chunks).
Note that only one level of infinite loop checking is performed,
i.e. A incl A is trapped and A incl B incl A is trapped but A incl B
incl B is not. Good enough for government work.
=cut
# ---------------------------------------------------------------------
sub process_template_includes {
my $self = shift;
my ($C, $ab) = @_;
my $template_name = $ab->get_action_template_name($C);
my $template_ref = $self->get_template_data($C);
my $chunkRegExp = '<\?CHUNK\s+(\w+)="(.*?)"(\s+(\w+)="(.*?)")?\?>';
while ( $$template_ref =~ s,$chunkRegExp,<sp0t>,s ) {
my $filename_param = $1;
my $filename_val = $2;
my $optional_param = $4;
my $optional_val = $5;
ASSERT($template_name ne $filename_val,
qq{Infinite recursion: parent file=$template_name, chunk file=$filename_val});
ASSERT($filename_param eq 'filename',
qq{CHUNK PI has invalid parameter name "$filename_param=$filename_val"});
ASSERT($optional_param eq 'optional',
qq{CHUNK PI has invalid parameter name "$optional_param=$optional_val"})
if ($optional_param);
my $optional = ($optional_val eq "1") ? "1" : "0";
my ($include_ref, $include_path) = find_file($C, $filename_val, $optional);
$$template_ref =~ s,<sp0t>,$$include_ref,s;
}
}
# ---------------------------------------------------------------------
=item parse_template_PIs
Parse and store an array of PI names found in the template for this
view.
=cut
# ---------------------------------------------------------------------
sub parse_template_PIs {
my $self = shift;
my $C = shift;
my $pio = $self->get_pio();
my $template_ref = $self->get_template_data($C);
my $PI_arr_ref = $pio->get_PIs($template_ref);
# remove XML PIs
@$PI_arr_ref = grep(!/^xml(-stylesheet)?/, @$PI_arr_ref);
my %PI_hash = map { $_ => 0 } @$PI_arr_ref;
$self->{'PIs'} = \%PI_hash;
}
# ---------------------------------------------------------------------
=item get_view_PIs
Return an array of PI names found in the template for this view.
=cut
# ---------------------------------------------------------------------
sub get_view_PIs {
my $self = shift;
my $C = shift;
return $self->{'PIs'};
}
# ---------------------------------------------------------------------
=item _install_PI_handlers
PRIVATE: Bind the PI handlers in PIFillers to the PIs in the template
for this view.
The PIFiller has a hash of code references to its PI handlers formed
at compile time via the CPAN Attribute::Handlers package.
The keys of the hash are the PIs that each of its PI handlers was
declared to handle.
Get this list and bind the code references to the actual PI
occurrencess in the template for this view.
=cut
# ---------------------------------------------------------------------
sub _install_PI_handlers {
my $self = shift;
my($C, $act) = @_;
my $pio = $self->get_pio();
my $view_PIs_hashref = $self->get_view_PIs($C);
DEBUG('pis,all',
sub {
my $s = qq{<h3>PIs found:</h3>};
foreach my $pi (keys %$view_PIs_hashref) {
$s .= qq{<h5>$pi</h5>};
}
return $s;
});
my $PI_to_handler_hashref = $act->get_PI_handler_mapping($C);
DEBUG('pis,all',
sub {
require Data::Dumper;
$Data::Dumper::Indent = 2;
$Data::Dumper::Deparse = 1;
my $dump = Data::Dumper->Dump( [$PI_to_handler_hashref], [qw($PI_to_handler_hashref)] );
Utils::map_chars_to_cers(\$dump) if Debug::DUtils::under_server();
return qq{<pre>$dump</pre>};
});
# assign a handler code ref to one of the PIs in the template
foreach my $handled_PI (keys %$PI_to_handler_hashref) {
my $PI_handler = $$PI_to_handler_hashref{$handled_PI};
# is this PI handler needed for a PI in this template?
if (exists($$view_PIs_hashref{$handled_PI})) {
# yes!
$$view_PIs_hashref{$handled_PI}++;
ASSERT($$view_PIs_hashref{$handled_PI} == 1,
qq{Duplicate handler for PI="$handled_PI" found in } . ref($act));
$pio->add_PI($handled_PI, $PI_handler, [$C, $act]);
}
else {
# no!
DEBUG('pis', qq{Unneeded PI handler for "$handled_PI" found in } . ref($act));
}
}
# Are any template PIs left unhandled?
foreach my $PI (keys %$view_PIs_hashref) {
DEBUG('pis', qq{PI="$PI" not handled for action=} . ref($act))
if (! $$view_PIs_hashref{$PI});
}
}
# ---------------------------------------------------------------------
=item _run_PI_handlers
Fire the PI handlers to fill the PI
=cut
# ---------------------------------------------------------------------
sub _run_PI_handlers {
my $self = shift;
my $C = shift;
return if (DEBUG('rawxml'));
__debug($C);
my $pio = $self->get_pio();
my $template_data_ref = $self->get_template_data($C);
$pio->process_PIs($template_data_ref);
}
# ---------------------------------------------------------------------
=item _set_transformed_xml
PRIVATE: Save the transformed template data
=cut
# ---------------------------------------------------------------------
sub _set_transformed_xml {
my $self = shift;
my $transformed_xml_ref = shift;
$self->{'transformed_xml'} = $transformed_xml_ref;
}
# ---------------------------------------------------------------------
=item _get_transformed_xml
PRIVATE: Get the saved transformed template data
=cut
# ---------------------------------------------------------------------
sub _get_transformed_xml {
my $self = shift;
return $self->{'transformed_xml'};
}
# ---------------------------------------------------------------------
=item __transform_paths
Support for debug=local switch for web paths. See also Vendor.pm for
functionality to paths to Perl modules.
Take a reference to the output string and map web paths containing
'//common-web/' to common-web submodule or local clone of mdp-web.git
depending on the value of DEBUG.
=cut
# ---------------------------------------------------------------------
sub __transform_paths {
my $self = shift;
my ($C, $ref) = @_;
if (DEBUG('local')) {
$$ref =~ s,//common-web/,/mdp-web/,g;
}
else {
my $app_name = $C->get_object('App')->get_app_name();
$$ref =~ s,//common-web/,/$app_name/common-web/,g;
}
}
# ---------------------------------------------------------------------
=item _render_template
PRIVATE: Transform the XML via XSLT
=cut
# ---------------------------------------------------------------------
sub _render_template
{
my $self = shift;
my $C = shift;
my $template_data_ref = $self->get_template_data($C);
my $template_name = $self->get_template_name($C);
# Debug message buffer support
handle_DEBUG_MESSAGES_PI($C, $template_data_ref);
if (DEBUG('xml,rawxml')) {
$$template_data_ref = Encode::encode_utf8($$template_data_ref);
# remove empty PI handlers to avoid browser rendering issues
Utils::remove_PI($template_data_ref);
$self->output_HTTP($C, $template_data_ref, 'text/xml' );
exit 0;
}
# POSSIBLY NOTREACHED
my $stylesheet_text_ref =
$self->build_virtual_stylesheet($C, $template_data_ref, $template_name);
if (DEBUG('xsl')) {
$self->output_HTTP($C, $stylesheet_text_ref, 'text/xml' );
exit 0;
}
# POSSIBLY NOTREACHED
if (DEBUG('xsltwrite')) {
my $config = $C->get_object('MdpConfig');
my $cache_dir = Utils::get_true_cache_dir($C, 'xsltwrite_cache_dir') . '/';
Utils::mkdir_path($cache_dir);
my $user = (Utils::Get_Remote_User() || 'anonymous-' . time());
my $partial_path = $cache_dir . $user;
my $xsl_filename = $partial_path . '.temp.xsl';
my $xml_filename = $partial_path . '.temp.xml';
write_data_to_file($stylesheet_text_ref, $xsl_filename);
write_data_to_file($template_data_ref, $xml_filename);
my $m = qq{wrote files: $xsl_filename, $xml_filename};
$self->output_HTTP($C, \$m);
exit 0;
}
# POSSIBLY NOTREACHED
my $parsed_xml =
Utils::XSLT::parse_xml($template_data_ref, $template_name);
my $transformed_xml_ref =
Utils::XSLT::transform_driver($parsed_xml, $template_name,
$stylesheet_text_ref);
# Serious magic
$self->__transform_paths($C, $transformed_xml_ref);
$self->_set_transformed_xml($transformed_xml_ref);
}
# ---------------------------------------------------------------------
=item P_output_data_HTTP
Description: Procedural interface to output data
=cut
# ---------------------------------------------------------------------
sub P_output_data_HTTP {
my ($C, $data_ref, $content_type, $status) = @_ ;
if (Debug::DUtils::xml_debugging_enabled()) {
Utils::remove_PI($data_ref, 'xml');
}
$content_type = 'text/html'
if (! $content_type);
$status = 200 unless ( $status );
my $charset = 'UTF-8';
Utils::add_header($C, 'Content-type' => qq{$content_type; charset=$charset});
unless ( $status == 404 ) {
my $ses = $C->get_object('Session', 1);
if ($ses) {
my $cookie = $ses->get_cookie();
Utils::add_header($C, 'Cookie' => $cookie);
}
my $auth = $C->get_object('Auth', 1);
if ($auth && $auth->isa_new_login()) {
my $cookie = Utils::get_user_status_cookie($C, $auth);
Utils::add_header($C, 'Cookie' => $cookie);
}
if ( ! $ENV{REMOTE_USER} ) {
if ($ses) {
my $entity_id;
if ( $entity_id = $ses->get_persistent('entity_id') ) {
Utils::add_header($C, 'X-HathiTrust-Renew', $entity_id);
Utils::add_header($C, 'X-HathiTrust-InCopyright', "lost_auth=$entity_id");
}
}
}
}
my $headers_ref = $C->get_object('HTTP::Headers');
print STDOUT "Status: $status" . $CGI::CRLF;
print STDOUT $headers_ref->as_string($CGI::CRLF);
print STDOUT $CGI::CRLF;
if ( ref $data_ref ) {
$$data_ref =~ s,^<!DOCTYPE [^>]+>,<!DOCTYPE html>,;
_add_ie_specific_code($data_ref);
print STDOUT $$data_ref;
}
}
# backward compatibility
sub P_output_none_HTTP {
my ($C) = @_ ;
P_output_data_HTTP($C, undef, undef, 204);
}
sub IF_LT_IE9 {
return <<END;
<!--[if lt IE 9]>
<script src="//ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
END
}
sub IF_LT_IE8 {
return <<END;
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="/common/unicorn/css/ie7.css" />
<script src="/common/unicorn/vendor/js/selectivizr.js" type="text/javascript"></script>
<![endif]-->
END
}
sub _add_ie_specific_code {
my ( $data_ref ) = @_;
my $html = IF_LT_IE9();
$$data_ref =~ s{<!--IE PRE-SETUP-->}{$html};
$html = IF_LT_IE8();
$$data_ref =~ s{<!--IE POST-SETUP-->}{$html};
}
# ---------------------------------------------------------------------
=item output
Description: interface to output data. Can be over-ridden to
call different output methods
=cut
# ---------------------------------------------------------------------
sub output {
my $self = shift;
my $C = shift;
my $content_type = shift;
my $transformed_xml_ref = $self->_get_transformed_xml($C);
$self->output_HTTP($C, $transformed_xml_ref, $content_type);
}
# ---------------------------------------------------------------------
=item output_HTTP
Description: interface to output data. Can be over-ridden to
call different output methods
=cut
# ---------------------------------------------------------------------
sub output_HTTP {
my $self = shift;
my ($C, $data_ref, $content_type) = @_ ;
View::P_output_data_HTTP($C, $data_ref, $content_type);
}
# ---------------------------------------------------------------------
=item build_virtual_stylesheet
Parse xml template xsl fallback list and construct in memory
stylesheet source. Compile it.
=cut
# ---------------------------------------------------------------------
sub build_virtual_stylesheet {
my $self = shift;
my ($C, $xml_page_ref, $xml_page_name) = @_;
# Get virtual stylesheet file list
my @xsl_filename_list;
my $parsed_xml = Utils::XSLT::parse_xml($xml_page_ref, $xml_page_name);
foreach my $xsl_filename_node ($parsed_xml->findnodes('//XslFileList/Filename')) {
my $xsl_path;
my $filename = $xsl_filename_node->findvalue('.');
$xsl_path = Utils::resolve_fallback_path($C, $filename, 0);
push(@xsl_filename_list, $xsl_path);
}
# Construct in-memory stylesheet
my $transform_template = $C->get_object('MdpConfig')->get('transform_template');
my ($stylesheet_text_ref, $stylesheet_path) = find_file($C, $transform_template, 0);
my $pio = new PI;
$pio->add_PI('XSL_FILE_LIST', 'handle_XSL_FILELIST_PI',
[$self, $C, \@xsl_filename_list]);
$pio->process_PIs($stylesheet_text_ref);
DEBUG('transform', qq{virtual XSL stylesheet derived from $transform_template});
return $stylesheet_text_ref;
}
# ---------------------------------------------------------------------
=item handle_XSL_FILELIST_PI
Local PI handler for processing a PI in the template for the virtual
stylesheet. Does not play the Perl Attribute::Handler game.
=cut
# ---------------------------------------------------------------------
sub handle_XSL_FILELIST_PI {
my $self = shift;
my ($C, $file_arr_ref) = @_;
my $s = '';
foreach my $f (@$file_arr_ref) {
$s .= wrap_string_in_tag('', 'xsl:import', [['href', $f]],
undef, 'singleton');
}
return $s;
}
# ---------------------------------------------------------------------
=item get_pio
PI object accessor
=cut
# ---------------------------------------------------------------------
sub get_pio {
my $self = shift;
return $self->{'pio'};
}
# ---------------------------------------------------------------------
=item handle_DEBUG_MESSAGES_PI
Place debug messages stored on session in the output stream.
NOTE: This is **NOT** a PI handler that obeys the Attribute::Handler
syntax in its declaration like normal PI handlers. It is called
*after* PI handling is complete so that any buffered debug statements
generated after PI handling can make it into the outpurt stream.
=cut
# ---------------------------------------------------------------------
sub handle_DEBUG_MESSAGES_PI {
my ($C, $data_ref) = @_;
return unless (
Debug::DUtils::debugging_enabled
&&
$C->has_object('Session')
);
my $ses = $C->get_object('Session');
# Dump session contents to the debug message buffer at the last
# possible instant before the <?DEBUG_MESSAGES?> PI is to be
# processed.
DEBUG('fullsession',
sub {
return $ses->session_dumper();
});
my $buffer_ref = $ses->get_persistent('debug_message_buffer');
$$data_ref =~ s,<\?DEBUG_MESSAGES\?>,($buffer_ref && $$buffer_ref) ? $$buffer_ref : '',e;
# Dispose messages
$ses->set_persistent('debug_message_buffer', undef);
}
# ---------------------------------------------------------------------
=item __debug
Description
=cut
# ---------------------------------------------------------------------
sub __debug {
my $C = shift;
DEBUG('version,all',
sub {
my $s = sprintf( "<h3>Perl version = %vd</h3>", $^V );
$s .= qq{<h3>main program version = $::VERSION</h3><br />};
return $s;
});
DEBUG('cgi,all',
sub {
my $cgi = $C->get_object('CGI');
my $s = $cgi->as_string();
return $s;
});
DEBUG('env,all',
sub {
my $s = Debug::DUtils::print_env('html');
return $s;
});
}
1;
__END__
=head1 AUTHOR
Phillip Farber, University of Michigan, pfarber@umich.edu
=head1 COPYRIGHT
Copyright 2007-10 ©, The Regents of The University of Michigan, All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=cut