OpenInteract2/Request.pm

filter: Statementstmt 21.7% 58 / 267 Branchbran 1.6% 2 / 122 Conditioncond 2.0% 1 / 48 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 36.3% 16 / 44 Podpod 3.8% 1 / 26 totaltotal 14.5% 78 / 537 CC 83 SCAR 40.6
CC
83
Cov
14%
assign_languages
240.0
upload
132.0
_set_upload
56.0
CRAP 58.0
« OpenInteract2/TT2/Provider.pm ↑ Summary OpenInteract2/DatasourceManager.pm »
1 package OpenInteract2::Request;
2  
3 # $Id: Request.pm,v 1.54 2005/03/17 14:57:58 sjn Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( OpenInteract2::ParamContainer Class::Factory Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Constants qw( :log SESSION_COOKIE );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use OpenInteract2::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 87 ▶use OpenInteract2::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 87 ▶use OpenInteract2::SessionManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 87 ▶use OpenInteract2::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 87 $OpenInteract2::Request::VERSION = sprintf("%d.%02d", q$Revision: 1.54 $ =~ /(\d+)\.(\d+)/);
19  
20 87 my ( $log );
21  
22 ########################################
23 # ACCESSORS
24  
25 87 my %FIELDS = map { $_ => 1 } qw(
26 now server_name remote_host
27 user_agent referer cookie_header language_header
28 url_absolute url_relative url_initial
29 session auth_user auth_group auth_is_admin auth_is_logged_in
30 );
31 87 __PACKAGE__->mk_accessors( keys %FIELDS );
32  
33 0 ▶sub get_skip_params { return %FIELDS }
Subroutine get_skip_params: not called CC=1 SCAR=6.9
Pod undocumented
34  
35 87 my ( $REQUEST_TYPE, $REQUEST_CLASS );
36  
37 1 ▶sub set_implementation_type {
Subroutine set_implementation_type: called CC=2 SCAR=8.5
Pod undocumented
38 1 my ( $class, $type ) = @_;
39 1 my $impl_class = eval { $class->get_factory_class( $type ) };
40 1 ▶ oi_error $@ if ( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
41 0 $REQUEST_TYPE = $type;
42 0 $REQUEST_CLASS = $impl_class;
43 0 return $impl_class;
44 }
45  
46 0 ▶sub get_implementation_type {
Subroutine get_implementation_type: not called CC=1 SCAR=6.9
Pod undocumented
47 0 return $REQUEST_TYPE;
48 }
49  
50 1 ▶sub new {
Subroutine new: called CC=3 SCAR=15.8
Pod documented
51 1 my ( $class, @params ) = @_;
52 1 ▶ $log ||= get_logger( LOG_REQUEST );
Condition33%1/3
$log ||= get_logger("OI2.REQUEST")
l!l&&r!l&&!r
010
MC/DC0%0/2
$log ||= get_logger("OI2.REQUEST")
$log get_logger("OI2.REQUEST")
Truth table33%1/3
A ||= B
A= $logB= get_logger("OI2.REQUEST")
ABresult
1X1
011
000
53 1 ▶ unless ( $REQUEST_CLASS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($REQUEST_CLASS) 1 0 1
54 1 $log->fatal( "No request implementation type set" );
55 1 oi_error 'Before creating an OpenInteract2::Request object you ',
56 'must set the request type with "set_implementation_type()"';
57 }
58 0 my $self = bless( { '_upload' => {},
59 '_param' => {},
60 '_cookie' => {} }, $REQUEST_CLASS );
61  
62 0 $self->init( @params );
63  
64 # Now that all the cookie/session/language data has been set by
65 # request impl, call the code that uses that to set other
66 # properties...
67  
68 0 $self->_parse_cookies;
69 0 $self->_create_session;
70  
71 0 $self->now( CTX->create_date() );
72  
73 0 CTX->request( $self );
74 0 return $self;
75 }
76  
77  
78 ########################################
79 # PARAMETERS
80  
81 0 ▶sub param_url_additional {
Subroutine param_url_additional: not called CC=4 SCAR=30.0
Pod undocumented
82 0 my ( $self, @added ) = @_;
83 0 ▶ if ( scalar @added ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @added) { } 0 0 0
84 0 $self->{_URL_PARAMS} = \@added;
85 }
86 else {
87 0 ▶ $self->{_URL_PARAMS} ||= [];
Condition0%0/2
$self->{"_URL_PARAMS"} ||= []
l!l
00
MC/DC0%0/1
$self->{"_URL_PARAMS"} ||= []
$self->{"_URL_PARAMS"}
Truth table0%0/2
A
A= $self->{"_URL_PARAMS"}
Aresult
11
00
88 }
89 0 ▶ return wantarray ? @{ $self->{_URL_PARAMS} } : $self->{_URL_PARAMS};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
90 }
91  
92 0 ▶sub param_toggled {
Subroutine param_toggled: not called CC=2 SCAR=17.9
Pod undocumented
93 0 my ( $self, $name ) = @_;
94 0 ▶ return ( defined $self->param( $name ) ) ? 'yes' : 'no';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->param($name) ? : 0 0 0
95 }
96  
97 0 ▶sub param_boolean {
Subroutine param_boolean: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ( $self, $name ) = @_;
99 0 ▶ return ( defined $self->param( $name ) ) ? 'TRUE' : 'FALSE';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->param($name) ? : 0 0 0
100 }
101  
102 0 ▶sub param_date {
Subroutine param_date: not called CC=5 SCAR=34.0
Pod undocumented
103 0 my ( $self, $name, $format ) = @_;
104 0 ▶ if ( $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 0 0 0
105 0 return $self->_parse_date_with_format( $name, $format );
106 }
107 0 my ( $y, $m, $d ) = ( $self->param( $name . '_year' ),
108 $self->param( $name . '_month' ),
109 $self->param( $name . '_day' ) );
110 0 ▶ return undef unless ( $y and $m and $d );
Branch: 1 branch
BranchTrueFalseTotal
unless $y and $m and $d 0 0 0
Condition0%0/6
$y and $m and $d
!ll&&!rl&&r
000
$y and $m
!ll&&!rl&&r
000
MC/DC0%0/3
$y and $m and $d
$y $m $d
Truth table0%0/4
A and B and C
A= $yB= $mC= $d
ABCresult
0XX0
10X0
1100
1111
111 0 return DateTime->new( year => $y,
112 month => $m,
113 day => $d );
114 }
115  
116 0 ▶sub param_datetime {
Subroutine param_datetime: not called CC=5 SCAR=34.0
Pod undocumented
117 0 my ( $self, $name, $format ) = @_;
118 0 ▶ if ( $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 0 0 0
119 0 return $self->_parse_date_with_format( $name, $format );
120 }
121 0 my $date = $self->param_date( $name );
122 0 ▶ return undef unless ( defined $date );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date 0 0 0
123 0 my ( $hour, $minute, $am_pm ) =
124 ( $self->param( $name . '_hour' ),
125 $self->param( $name . '_minute' ),
126 $self->param( $name . '_am_pm' ) );
127 0 ▶ if ( lc $am_pm eq 'pm' and $hour < 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $am_pm eq "pm" and $hour < 12) 0 0 0
Condition0%0/3
lc $am_pm eq "pm" and $hour < 12
!ll&&!rl&&r
000
MC/DC0%0/2
lc $am_pm eq "pm" and $hour < 12
lc $am_pm eq "pm" $hour < 12
Truth table0%0/3
A and B
A= lc $am_pm eq "pm"B= $hour < 12
ABresult
0X0
100
111
128 0 $hour += 12;
129 }
130 0 $date->set( hour => $hour,
131 minute => $minute );
132 0 return $date;
133 }
134  
135 0 ▶sub _parse_date_with_format {
Subroutine _parse_date_with_format: not called CC=3 SCAR=24.8
136 0 my ( $self, $name, $format ) = @_;
137 0 my $date_value = $self->param( $name );
138 0 ▶ unless ( $date_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date_value) 0 0 0
139 0 return undef;
140 }
141 0 my $parser = DateTime::Format::Strptime->new( pattern => $format,
142 on_error => 'croak' );
143 0 my $dt = eval { $parser->parse_datetime( $date_value ) };
144 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
145 0 oi_error "Failed to parse date '$date_value' with format '$format': $@";
146 }
147 0 return $dt;
148 }
149  
150  
151 ########################################
152 # PROPERTIES
153  
154 # shortcut
155 0 ▶sub auth_user_id {
Subroutine auth_user_id: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my ( $self ) = @_;
157 0 ▶ return ( $self->auth_is_logged_in ) ? $self->auth_user->id : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->auth_is_logged_in ? : 0 0 0
158 }
159  
160  
161 0 ▶sub assign_request_url {
Subroutine assign_request_url: not called CC=4 SCAR=30.0
Pod undocumented
162 0 my ( $self, $full_url_path ) = @_;
163 0 ▶ $log ||= get_logger( LOG_REQUEST );
Condition0%0/3
$log ||= get_logger("OI2.REQUEST")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.REQUEST")
$log get_logger("OI2.REQUEST")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.REQUEST")
ABresult
1X1
011
000
164 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Setting absolute URL '${full_url_path}'") 0 0 0
165 $log->info( "Setting absolute URL '$full_url_path'" );
166 0 $self->url_absolute( $full_url_path );
167 0 my $relative_url =
168 OpenInteract2::URL->parse_absolute_to_relative( $full_url_path );
169 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Setting relative URL '${relative_url}'") 0 0 0
170 $log->debug( "Setting relative URL '$relative_url'" );
171 0 $self->url_relative( $relative_url );
172 0 return $relative_url;
173 }
174  
175 0 ▶sub action_messages {
Subroutine action_messages: not called CC=3 SCAR=24.8
Pod undocumented
176 0 my ( $self, $name, $messages ) = @_;
177 0 ▶ return {} unless ( $name );
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
178  
179 0 ▶ if ( ref $messages eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $messages eq "HASH") 0 0 0
180 0 $self->{_action_msg}{ lc $name } = $messages;
181 }
182 0 return $self->{_action_msg}{ lc $name };
183 }
184  
185 0 ▶sub add_action_message {
Subroutine add_action_message: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my ( $self, $action_name, $error_name, $error ) = @_;
187 0 return $self->{_action_msg}{ lc $action_name }{ $error_name } = $error;
188 }
189  
190 0 ▶sub auth_clear {
Subroutine auth_clear: not called CC=2 SCAR=17.9
Pod undocumented
191 0 my ( $self ) = @_;
192 0 for ( qw( auth_user auth_group auth_is_admin auth_is_logged_in ) ) {
193 0 $self->{ $_ } = undef;
194 }
195 }
196  
197 ########################################
198 # UPLOADS
199  
200 0 ▶sub upload {
Subroutine upload: not called CC=11 SCAR=48.8
Pod undocumented
201 0 my ( $self, $name ) = @_;
202 0 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
203 0 ▶ if ( ! $self->{_upload}{ $name } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->{'_upload'}{$name}) { } 0 0 0
elsif (ref $self->{'_upload'}{$name} eq 'ARRAY' and wantarray) { } 0 0 0
204 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
205 }
206 ▶ elsif ( ref $self->{_upload}{ $name } eq 'ARRAY' and wantarray ) {
Condition0%0/3
ref $self->{"_upload"}{$name} eq "ARRAY" and wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"_upload"}{$name} eq "ARRAY" and wantarray
ref $self->{"_upload"}{$name} eq "ARRAY" wantarray
Truth table0%0/3
A and B
A= ref $self->{"_upload"}{$name} eq "ARRAY"B= wantarray
ABresult
0X0
100
111
207 0 return @{ $self->{_upload}{ $name } };
208 }
209 return wantarray ? ( $self->{_upload}{ $name } )
210 0 ▶ : $self->{_upload}{ $name };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
211 }
212 0 ▶ $self->{_upload} ||= {};
Condition0%0/2
$self->{"_upload"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"_upload"} ||= {}
$self->{"_upload"}
Truth table0%0/2
A
A= $self->{"_upload"}
Aresult
11
00
213 0 my @items = ();
214 0 foreach my $item ( values %{ $self->{_upload} } ) {
215 0 ▶ next unless ( $item );
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 0 0
216 0 ▶ if ( ref $item eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 0 0 0
217 0 push @items, @{ $item };
218 }
219 else {
220 0 push @items, $item;
221 }
222 }
223 0 return @items;
224 }
225  
226  
227 0 ▶sub _set_upload {
Subroutine _set_upload: not called CC=7 SCAR=40.3
228 0 my ( $self, $name, $value ) = @_;
229 0 ▶ $log ||= get_logger( LOG_REQUEST );
Condition0%0/3
$log ||= get_logger("OI2.REQUEST")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.REQUEST")
$log get_logger("OI2.REQUEST")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.REQUEST")
ABresult
1X1
011
000
230 0 ▶ unless ( $name and $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name and $value) 0 0 0
Condition0%0/3
$name and $value
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $value
$name $value
Truth table0%0/3
A and B
A= $nameB= $value
ABresult
0X0
100
111
231 0 $log->warn( "Called set_upload() without valid params",
232 "Name '$name' Value '", ref( $value ), "'" );
233 0 return undef;
234 }
235 $log->is_info &&
236 0 ▶ $log->info( "Adding upload $name" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Adding upload $name") 0 0 0
237 0 my @existing = $self->upload( $name );
238 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
239 0 push @existing, @{ $value };
240 }
241 else {
242 0 push @existing, $value;
243 }
244 0 ▶ $self->{_upload}{ $name } = ( scalar @existing > 1 )
Branch: 1 branch
BranchTrueFalseTotal
scalar @existing > 1 ? : 0 0 0
245 ? \@existing : $existing[0];
246 0 return $self->{_upload}{ $name };
247 }
248  
249  
250 0 ▶sub clean_uploads {
Subroutine clean_uploads: not called CC=6 SCAR=37.4
Pod undocumented
251 0 my ( $self ) = @_;
252 0 ▶ $log ||= get_logger( LOG_REQUEST );
Condition0%0/3
$log ||= get_logger("OI2.REQUEST")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.REQUEST")
$log get_logger("OI2.REQUEST")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.REQUEST")
ABresult
1X1
011
000
253  
254 0 my @uploads = $self->upload;
255 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Cleaning all uploads: ", scalar @uploads) 0 0 0
256 $log->info( "Cleaning all uploads: ", scalar @uploads );
257 0 foreach my $item ( @uploads ) {
258 0 my $filename = eval { $item->tmp_name };
259 0 ▶ next unless ( $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
260 0 ▶ unlink( $item->tmp_name ) if ( -f $filename );
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 0 0
261 }
262 }
263  
264 ########################################
265 # COOKIES (INBOUND)
266  
267 0 ▶sub cookie {
Subroutine cookie: not called CC=3 SCAR=24.8
Pod undocumented
268 0 my ( $self, $name, $value ) = @_;
269 0 ▶ unless ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
270 0 return keys %{ $self->{_cookie} };
271 }
272 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
273 0 $self->{_cookie}{ $name } = $value;
274 }
275 0 return $self->{_cookie}{ $name };
276 }
277  
278 0 ▶sub _parse_cookies {
Subroutine _parse_cookies: not called CC=4 SCAR=30.0
279 0 my ( $self, $parse_string ) = @_;
280 0 ▶ $parse_string ||= $self->cookie_header;
Condition0%0/3
$parse_string ||= $self->cookie_header
l!l&&r!l&&!r
000
MC/DC0%0/2
$parse_string ||= $self->cookie_header
$parse_string $self->cookie_header
Truth table0%0/3
A ||= B
A= $parse_stringB= $self->cookie_header
ABresult
1X1
011
000
281 0 ▶ if ( $parse_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse_string) 0 0 0
282 0 my $cookies = OpenInteract2::Cookie->parse( $parse_string );
283 0 ▶ while ( my ( $name, $cookie ) = each %{ $cookies } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$cookies;}) 0 0 0
284 0 $self->cookie( $name, $cookie->value );
285 }
286 }
287 0 return $self->cookie;
288 }
289  
290  
291 ########################################
292 # SESSION
293  
294 # This should create at least an empty hashref...
295  
296 0 ▶sub _create_session {
Subroutine _create_session: not called CC=1 SCAR=6.9
297 0 my ( $self ) = @_;
298 0 my $session_id = $self->cookie( SESSION_COOKIE );
299 0 my $session_info = CTX->lookup_session_config;
300 0 my $oi_session_class = $session_info->{class};
301 0 my $session = $oi_session_class->create( $session_id );
302 0 return $self->session( $session );
303 }
304  
305 ########################################
306 # THEME
307  
308 0 ▶sub theme {
Subroutine theme: not called CC=2 SCAR=17.9
Pod undocumented
309 0 my ( $self, $theme ) = @_;
310 0 ▶ if ( $theme ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($theme) 0 0 0
311 0 $self->{theme} = $theme;
312 0 $self->{theme_values} = $theme->all_values;
313 }
314 0 return $self->{theme};
315 }
316  
317 # 'theme_values' are only settable by setting 'theme'
318  
319 0 ▶sub theme_values {
Subroutine theme_values: not called CC=1 SCAR=6.9
Pod undocumented
320 0 my ( $self ) = @_;
321 0 return $self->{theme_values};
322 }
323  
324 # This should be called only after you've authenticated
325 # TODO: Modify to also lookup in session cache...
326  
327 0 ▶sub create_theme {
Subroutine create_theme: not called CC=7 SCAR=40.3
Pod undocumented
328 0 my ( $self ) = @_;
329 0 ▶ $log ||= get_logger( LOG_REQUEST );
Condition0%0/3
$log ||= get_logger("OI2.REQUEST")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.REQUEST")
$log get_logger("OI2.REQUEST")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.REQUEST")
ABresult
1X1
011
000
330 0 my $user = $self->auth_user;
331 0 ▶ unless ( $user ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
332 0 $log->warn( "Theme not created, no user authenticated" );
333 0 oi_error "Must authenticate before trying to fetch/create theme";
334 }
335 0 ▶ my $theme_id = $user->theme_id
Condition0%0/3
$user->theme_id || CTX()->lookup_default_object_id('theme')
l!l&&r!l&&!r
000
MC/DC0%0/2
$user->theme_id || CTX()->lookup_default_object_id('theme')
$user->theme_id CTX()->lookup_default_object_id('theme')
Truth table0%0/3
A || B
A= $user->theme_idB= CTX()->lookup_default_object_id('theme')
ABresult
1X1
011
000
336 || CTX->lookup_default_object_id( 'theme' );
337 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Trying to fetch theme with ID '${theme_id}'") 0 0 0
338 $log->info( "Trying to fetch theme with ID '$theme_id'" );
339 0 my $theme = eval {
340 0 CTX->lookup_object( 'theme' )->fetch( $theme_id )
341 };
342 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
343 0 $log->error( "Failed to fetch theme '$theme_id': $@" );
344 0 oi_error "Failed to fetch requested theme";
345 }
346 0 $self->theme( $theme );
347 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Loaded theme '${theme_id}' ok") 0 0 0
348 $log->info( "Loaded theme '$theme_id' ok" );
349 }
350  
351  
352 ########################################
353 # LANGUAGE
354  
355 0 ▶sub language {
Subroutine language: not called CC=2 SCAR=17.9
Pod undocumented
356 0 my ( $self ) = @_;
357 return wantarray
358 0 ▶ ? @{ $self->{_user_language} } : $self->{_user_language}[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
359 }
360  
361 0 ▶sub assign_languages {
Subroutine assign_languages: not called CC=15 SCAR=54.8
Pod undocumented
362 0 my ( $self, @assigned ) = @_;
363 0 ▶ if ( scalar @assigned ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @assigned) 0 0 0
364 0 delete $self->{_lang_handle}; # clear out cache
365 0 $self->{_user_language} = \@assigned;
366 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Request property 'language' assigned to: ", join(", ", @assigned)) 0 0 0
367 $log->debug( "Request property 'language' assigned to: ",
368 join( ', ', @assigned ) );
369 0 return;
370 }
371 0 my @lang = ();
372 0 my $lang_config = CTX->lookup_language_config;
373 0 ▶ if ( $self->auth_is_logged_in ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->auth_is_logged_in) { } 0 0 0
elsif (my $session_lang = $self->session->{'language'}) { } 0 0 0
elsif (my(@param_lang) = $self->param($lang_config->{'choice_param_name'})) { } 0 0 0
374 0 my $user_lang = $self->auth_user->language;
375 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Added language from logged in user: $user_lang") 0 0 0
376 $log->debug( "Added language from logged in user: $user_lang" );
377 0 ▶ push @lang, $user_lang if ( $user_lang );
Branch: 1 branch
BranchTrueFalseTotal
if $user_lang 0 0 0
378 }
379 elsif ( my $session_lang = $self->session->{language} ) {
380 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Added language from session: $session_lang") 0 0 0
381 $log->debug( "Added language from session: $session_lang" );
382 push @lang, ref( $session_lang ) eq 'ARRAY'
383 0 ▶ ? @{ $session_lang } : $session_lang;
Branch: 1 branch
BranchTrueFalseTotal
ref $session_lang eq 'ARRAY' ? : 0 0 0
384 }
385 elsif ( my @param_lang = $self->param( $lang_config->{choice_param_name} ) ) {
386 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Added language from request parameter ", qq['$lang_config->{"choice_param_name"}']) 0 0 0
387 $log->debug( "Added language from request parameter ",
388 "'$lang_config->{choice_param_name}'" );
389 0 push @lang, @param_lang;
390 }
391 else {
392 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug(qq[Added default language: $lang_config->{"default_language"}]) 0 0 0
393 $log->debug( "Added default language: $lang_config->{default_language}" );
394 0 push @lang, $lang_config->{default_language};
395 }
396  
397  
398 0 ▶ if ( my @browser_lang = $self->_find_browser_languages ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@browser_lang) = $self->_find_browser_languages) 0 0 0
399 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Added language to head from browser: ", join(", ", @browser_lang)) 0 0 0
400 $log->debug( "Added language to head from browser: ",
401 join( ', ', @browser_lang ) );
402 0 unshift @lang, @browser_lang;
403 }
404  
405 0 $self->{_user_language} = \@lang;
406 $log->is_debug &&
407 $log->debug( "Request property 'language' now: ",
408 0 ▶ join( ', ', @{ $self->{_user_language} } ) );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Request property 'language' now: ", join(", ", @{$self->{"_user_language"};})) 0 0 0
409 }
410  
411 0 ▶sub _find_browser_languages {
Subroutine _find_browser_languages: not called CC=7 SCAR=40.3
412 0 my ( $self ) = @_;
413 0 ▶ return () unless ( $self->language_header );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->language_header 0 0 0
414 0 ▶ $log ||= get_logger( LOG_REQUEST );
Condition0%0/3
$log ||= get_logger("OI2.REQUEST")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.REQUEST")
$log get_logger("OI2.REQUEST")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.REQUEST")
ABresult
1X1
011
000
415  
416 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Found the following language header from the browser: ", $self->language_header) 0 0 0
417 $log->debug( "Found the following language header from the browser: ",
418 $self->language_header );
419 0 my @raw_lang_info = split ( /\s*,\s*/, $self->language_header );
420 0 my @lang_data = ();
421 0 foreach my $lang_and_weight ( @raw_lang_info ) {
422 0 my ( $lang, $weight ) = split( ';', $lang_and_weight );
423 0 ▶ $weight ||= 1;
Condition0%0/2
$weight ||= 1
l!l
00
MC/DC0%0/1
$weight ||= 1
$weight
Truth table0%0/2
A
A= $weight
Aresult
11
00
424 0 $weight =~ s/^q=\s*//;
425 0 push @lang_data, [ $lang, $weight ];
426 }
427 0 my @langs = map { $_->[0] }
428 0 sort { $b->[1] <=> $a->[1] } @lang_data;
429 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Parsed browser header into following language tags: ", join(", ", @langs)) 0 0 0
430 $log->debug( "Parsed browser header into following language tags: ",
431 join( ', ', @langs ) );
432 0 return @langs;
433 }
434  
435 0 ▶sub language_handle {
Subroutine language_handle: not called CC=4 SCAR=26.6
Pod undocumented
436 0 my ( $self ) = @_;
437 0 ▶ unless ( $self->{_lang_handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_lang_handle"}) 0 0 0
438 0 ▶ $log ||= get_logger( LOG_REQUEST );
Condition0%0/3
$log ||= get_logger("OI2.REQUEST")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.REQUEST")
$log get_logger("OI2.REQUEST")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.REQUEST")
ABresult
1X1
011
000
439 0 my @langs = $self->language;
440 0 $log->info( "Languages for this request: ", join( ', ', @langs ) );
441 0 $self->{_lang_handle} = OpenInteract2::I18N->get_handle( @langs );
442 0 ▶ if ( $log->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->is_debug) 0 0 0
443 0 my $type = ref( $self->{_lang_handle} );
444 87 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
445 0 my @parents = @{ $type . '::ISA' };
446 0 $log->debug( "Language handle is of type '$type' with parents: ",
447 join( ', ', @parents ) );
448 }
449 }
450 0 return $self->{_lang_handle};
451 }
452  
453 ########################################
454 # FACTORY INFO
455  
456 0 ▶sub factory_log {
Subroutine factory_log: not called CC=1 SCAR=6.9
Pod undocumented
457 0 my ( $self, @msg ) = @_;
458 0 get_logger( LOG_REQUEST )->info( @msg );
459 }
460  
461 1 ▶sub factory_error {
Subroutine factory_error: called CC=1 SCAR=0.0
Pod undocumented
462 1 my ( $self, @msg ) = @_;
463 1 get_logger( LOG_REQUEST )->error( @msg );
464 1 die @msg, "\n";
465 }
466  
467  
468 ########################################
469 # OVERRIDE
470  
471 # Initialize new object
472 0 ▶sub init { oi_error 'Subclass must implement init()' }
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
473  
474 87 1;
475  
476 __END__
« OpenInteract2/TT2/Provider.pm ↑ Summary OpenInteract2/DatasourceManager.pm »