Nakamura/Content.pm

filter: Statementstmt 46.4% 104 / 224 Branchbran 34.2% 24 / 70 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.3% 19 / 27 Podpod 66.6% 10 / 15 totaltotal 46.0% 157 / 341 CC 39 SCAR 35.4
CC
39
Cov
43%
run
82.0
view_attribute
72.0
upload_file
34.1
CRAP 34.6
↑ Summary Nakamura/Authn.pm »
1 #!/usr/bin/perl -w
2  
3 package Sakai::Nakamura::Content;
4  
5 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Getopt::Long qw(:config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sakai::Nakamura;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Sakai::Nakamura::Authn;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Sakai::Nakamura::ContentUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use base qw(Apache::Sling::Content);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 require Exporter;
19  
20 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our @EXPORT_OK = qw(run);
23  
24 1 our $VERSION = '0.13';
25  
26 #{{{sub new
27 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28 2 my ( $class, @args ) = @_;
29 2 my $content = $class->SUPER::new(@args);
30  
31 # Add a class variable to track the last content path seen:
32 1 $content->{'Path'} = q{};
33  
34 # Add a class variable to track the last comment made:
35 1 $content->{'Comment'} = q{};
36 1 bless $content, $class;
37 1 return $content;
38 }
39  
40 #}}}
41  
42 #{{{ sub command_line
43 0 ▶sub command_line {
Subroutine command_line: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my ( $class, @ARGV ) = @_;
45 0 my $nakamura = Sakai::Nakamura->new;
46 0 my $config = $class->config( $nakamura, @ARGV );
47 0 my $authn = new Sakai::Nakamura::Authn( \$nakamura );
48 0 return $class->run( $nakamura, $config );
49 }
50  
51 #}}}
52  
53 #{{{sub comment_add
54 0 ▶sub comment_add {
Subroutine comment_add: not called CC=3 SCAR=24.8
Pod undocumented
55 0 my ( $content, $comment, $remote_dest ) = @_;
56 $remote_dest =
57 defined $remote_dest
58 ? Apache::Sling::URL::strip_leading_slash($remote_dest)
59 0 ▶ : $content->{'Path'};
Branch: 1 branch
BranchTrueFalseTotal
defined $remote_dest ? : 0 0 0
60  
61 my $res = Apache::Sling::Request::request(
62 \$content,
63 Sakai::Nakamura::ContentUtil::comment_add_setup(
64 0 $content->{'BaseURL'}, $remote_dest, $comment
65 )
66 );
67 0 my $success = Sakai::Nakamura::ContentUtil::comment_add_eval($res);
68 0 ▶ my $message = (
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
69 $success
70 ? 'Comment added'
71 : 'Problem adding comment to content'
72 );
73 0 $content->set_results( "$message", $res );
74 0 return $success;
75 }
76  
77 #}}}
78  
79 #{{{sub config
80  
81 1 ▶sub config {
Subroutine config: called CC=2 SCAR=7.0
Pod documented
82 1 my ( $class, $nakamura, @ARGV ) = @_;
83 1 my $content_config = $class->config_hash( $nakamura, @ARGV );
84  
85 1 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions($content_config, "auth=s", "help|?", "log|L=s", "man|M", "pass|p=s", "threads|t=s", "url|U=s", "user|u=s", "verbose|v+", "add|a", "additions|A=s", "copy|c", "delete|d", "exists|e", "filename|n=s", "local|l=s", "move|m", "property|P=s", "remote|r=s", "remote-source|S=s", "replace|R", "view|V", "view-copyright=s", "view-description=s", "view-tags=s", "view-title=s", "view-visibility=s") or $class->help 1 0 1
86 $content_config, 'auth=s',
87 'help|?', 'log|L=s',
88 'man|M', 'pass|p=s',
89 'threads|t=s', 'url|U=s',
90 'user|u=s', 'verbose|v+',
91 'add|a', 'additions|A=s',
92 'copy|c', 'delete|d',
93 'exists|e', 'filename|n=s',
94 'local|l=s', 'move|m',
95 'property|P=s', 'remote|r=s',
96 'remote-source|S=s', 'replace|R',
97 'view|V', 'view-copyright=s',
98 'view-description=s', 'view-tags=s',
99 'view-title=s', 'view-visibility=s'
100 ) or $class->help();
101  
102 1 return $content_config;
103 }
104  
105 #}}}
106  
107 #{{{sub config_hash
108  
109 1 ▶sub config_hash {
Subroutine config_hash: called CC=1 SCAR=0.0
Pod undocumented
110 1 my ( $class, $nakamura, @ARGV ) = @_;
111 1 my $view_copyright;
112 my $view_description;
113 1 my $view_tags;
114 1 my $view_title;
115 1 my $view_visibility;
116 1 my $content_config = $class->SUPER::config_hash( $nakamura, @ARGV );
117 1 $content_config->{'view-copyright'} = \$view_copyright;
118 1 $content_config->{'view-description'} = \$view_description;
119 1 $content_config->{'view-tags'} = \$view_tags;
120 1 $content_config->{'view-title'} = \$view_title;
121 1 $content_config->{'view-visibility'} = \$view_visibility;
122  
123 1 return $content_config;
124 }
125  
126 #}}}
127  
128 #{{{ sub help
129 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod undocumented
130  
131 1 print <<"EOF";
132 Usage: perl $0 [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
133 The following options are accepted:
134  
135 --additions or -A (file) - File containing list of content to be uploaded.
136 --add or -a - Add content.
137 --auth (type) - Specify auth type. If ommitted, default is used.
138 --copy or -c - Copy content.
139 --delete or -d - Delete content.
140 --filename or -n (filename) - Specify file name to use for content upload.
141 --help or -? - view the script synopsis and options.
142 --local or -l (localPath) - Local path to content to upload.
143 --log or -L (log) - Log script output to specified log file.
144 --man or -M - view the full script documentation.
145 --move or -m - Move content.
146 --pass or -p (password) - Password of user performing content manipulations.
147 --property or -P (property) - Specify property to set on node.
148 --remote or -r (remoteNode) - specify remote destination under JCR root to act on.
149 --remote-source or -S (remoteSrc) - specify remote source node under JCR root to act on.
150 --replace or -R - when copying or moving, overwrite remote destination if it exists.
151 --threads or -t (threads) - Used with -A, defines number of parallel
152 processes to have running through file.
153 --url or -U (URL) - URL for system being tested against.
154 --user or -u (username) - Name of user to perform content manipulations as.
155 --verbose or -v or -vv or -vvv - Increase verbosity of output.
156 --view or -V (actOnContent) - view details for specified content in json format.
157 --view-copyright (remoteNode) - view copyright for specified remote content.
158 --view-description (remoteNode) - view description for specified remote content.
159 --view-tags (remoteNode) - view tags for specified remote content.
160 --view-title (remoteNode) - view title for specified remote content.
161 --view-visibility (remoteNode) - view visibility setting for specified remote content.
162  
163 Options may be merged together. -- stops processing of options.
164 Space is not required between options and their arguments.
165 For full details run: perl $0 --man
166 EOF
167  
168 1 return 1;
169  
170 }
171  
172 #}}}
173  
174 #{{{sub run
175 2 ▶sub run {
Subroutine run: called CC=16 SCAR=44.1
Pod documented
176 2 my ( $content, $nakamura, $config ) = @_;
177 2 ▶ if ( !defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config) 1 1 2
178 1 croak 'No content config supplied!';
179 }
180 1 $nakamura->check_forks;
181 1 ${ $config->{'remote'} } =
182 1 Apache::Sling::URL::strip_leading_slash( ${ $config->{'remote'} } );
183 1 ${ $config->{'remote-source'} } = Apache::Sling::URL::strip_leading_slash(
184 1 ${ $config->{'remote-source'} } );
185 my $authn =
186 defined $nakamura->{'Authn'}
187 1 ▶ ? ${ $nakamura->{'Authn'} }
Branch: 1 branch
BranchTrueFalseTotal
defined $nakamura->{'Authn'} ? : 1 0 1
188 : new Sakai::Nakamura::Authn( \$nakamura );
189  
190 1 my $success = 1;
191  
192 0 ▶ if ( $nakamura->{'Help'} ) { $content->help(); }
Branch: 3 branches
BranchTrueFalseTotal
if ($nakamura->{'Help'}) { } 0 1 1
elsif ($nakamura->{'Man'}) { } 0 1 1
elsif (defined ${$$config{'additions'};}) { } 0 1 1
193 0 elsif ( $nakamura->{'Man'} ) { $content->man(); }
194 1 elsif ( defined ${ $config->{'additions'} } ) {
195 my $message =
196 0 "Adding content from file \"" . ${ $config->{'additions'} } . "\":\n";
197 0 Apache::Sling::Print::print_with_lock( "$message", $nakamura->{'Log'} );
198 0 my @childs = ();
199 0 for my $i ( 0 .. $nakamura->{'Threads'} ) {
200 0 my $pid = fork;
201 0 ▶ if ($pid) { push @childs, $pid; } # parent
Branch: 2 branches
BranchTrueFalseTotal
if ($pid) { } 0 0 0
elsif ($pid == 0) { } 0 0 0
202 elsif ( $pid == 0 ) { # child
203 # Create a new separate user agent per fork in order to
204 # ensure cookie stores are separate, then log the user in:
205 0 $authn->{'LWP'} = $authn->user_agent( $nakamura->{'Referer'} );
206 0 $authn->login_user();
207 my $content =
208 new Sakai::Nakamura::Content( \$authn, $nakamura->{'Verbose'},
209 0 $nakamura->{'Log'} );
210 0 $content->upload_from_file( ${ $config->{'additions'} },
211 0 $i, $nakamura->{'Threads'} );
212 0 exit 0;
213 }
214 else {
215 0 croak "Could not fork $i!";
216 }
217 }
218 0 foreach (@childs) { waitpid $_, 0; }
219 }
220 else {
221 1 ▶ if ( defined ${ $config->{'local'} } ) {
Branch: 6 branches
BranchTrueFalseTotal
if (defined ${$$config{'local'};}) { } 0 1 1
elsif (defined ${$$config{'view-copyright'};}) { } 0 1 1
elsif (defined ${$$config{'view-description'};}) { } 0 1 1
elsif (defined ${$$config{'view-tags'};}) { } 0 1 1
elsif (defined ${$$config{'view-title'};}) { } 0 1 1
elsif (defined ${$$config{'view-visibility'};}) { } 0 1 1
222 0 $authn->login_user();
223 $content =
224 new Sakai::Nakamura::Content( \$authn, $nakamura->{'Verbose'},
225 0 $nakamura->{'Log'} );
226 0 $success = $content->upload_file( ${ $config->{'local'} } );
227 0 Apache::Sling::Print::print_result($content);
228 }
229 1 elsif ( defined ${ $config->{'view-copyright'} } ) {
230 0 $authn->login_user();
231 $content =
232 new Sakai::Nakamura::Content( \$authn, $nakamura->{'Verbose'},
233 0 $nakamura->{'Log'} );
234 $success =
235 0 $content->view_copyright( ${ $config->{'view-copyright'} } );
236 0 Apache::Sling::Print::print_result($content);
237 }
238 1 elsif ( defined ${ $config->{'view-description'} } ) {
239 0 $authn->login_user();
240 $content =
241 new Sakai::Nakamura::Content( \$authn, $nakamura->{'Verbose'},
242 0 $nakamura->{'Log'} );
243 $success =
244 0 $content->view_description( ${ $config->{'view-description'} } );
245 0 Apache::Sling::Print::print_result($content);
246 }
247 1 elsif ( defined ${ $config->{'view-tags'} } ) {
248 0 $authn->login_user();
249 $content =
250 new Sakai::Nakamura::Content( \$authn, $nakamura->{'Verbose'},
251 0 $nakamura->{'Log'} );
252 0 $success = $content->view_tags( ${ $config->{'view-tags'} } );
253 0 Apache::Sling::Print::print_result($content);
254 }
255 1 elsif ( defined ${ $config->{'view-title'} } ) {
256 0 $authn->login_user();
257 $content =
258 new Sakai::Nakamura::Content( \$authn, $nakamura->{'Verbose'},
259 0 $nakamura->{'Log'} );
260 0 $success = $content->view_title( ${ $config->{'view-title'} } );
261 0 Apache::Sling::Print::print_result($content);
262 }
263 1 elsif ( defined ${ $config->{'view-visibility'} } ) {
264 0 $authn->login_user();
265 $content =
266 new Sakai::Nakamura::Content( \$authn, $nakamura->{'Verbose'},
267 0 $nakamura->{'Log'} );
268 $success =
269 0 $content->view_visibility( ${ $config->{'view-visibility'} } );
270 0 Apache::Sling::Print::print_result($content);
271 }
272 else {
273 1 $success = $content->SUPER::run( $nakamura, $config );
274 }
275 }
276 1 return $success;
277 }
278  
279 #}}}
280  
281 #{{{sub upload_file
282 1 ▶sub upload_file {
Subroutine upload_file: called CC=6 SCAR=35.3
Pod documented
283 1 my ( $content, $local_path ) = @_;
284 1 my $filename = q{};
285 my $res = Apache::Sling::Request::request(
286 \$content,
287 Apache::Sling::ContentUtil::upload_file_setup(
288 1 $content->{'BaseURL'}, $local_path,
289 'system/pool/createfile', $filename
290 )
291 );
292 0 my $success = Apache::Sling::ContentUtil::upload_file_eval($res);
293  
294 # Check whether initial upload succeeded:
295 0 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
296 0 croak
297 "Content: \"$local_path\" upload to /system/pool/createfile failed!";
298 }
299  
300 # Obtain path from POST response body:
301 0 my $content_path = ( ${$res}->content =~ m/"_path":"([^"]*)"/x )[0];
302 0 ▶ if ( !defined $content_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content_path) 0 0 0
303 0 croak 'Content path not found in JSON response to file upload';
304 }
305 0 $content_path = "p/$content_path";
306 my $content_filename =
307 0 ( ${$res}->content =~ m/"sakai:pooled-content-file-name":"([^"]*)"/x )[0];
308 0 ▶ if ( !$content_filename =~ /.*\..*/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$content_filename =~ /.*\..*/x) 0 0 0
309 0 croak "Content filename: '$content_filename' has no file extension";
310 }
311 0 my $content_fileextension = ( $content_filename =~ m/([^.]+)$/x )[0];
312  
313 # Add Meta data for file:
314 $res = Apache::Sling::Request::request(
315 \$content,
316 Sakai::Nakamura::ContentUtil::add_file_metadata_setup(
317 0 $content->{'BaseURL'}, "$content_path",
318 $content_filename, $content_fileextension
319 )
320 );
321 0 $success = Sakai::Nakamura::ContentUtil::add_file_metadata_eval($res);
322  
323 # Check whether adding metadata succeeded:
324 0 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
325 0 croak "Adding metadata for \"$content_path\" failed!";
326 }
327  
328 # Add permissions on file:
329 $res = Apache::Sling::Request::request(
330 \$content,
331 Sakai::Nakamura::ContentUtil::add_file_perms_setup(
332 0 $content->{'BaseURL'}, "$content_path"
333 )
334 );
335 0 $success = Sakai::Nakamura::ContentUtil::add_file_perms_eval($res);
336  
337 # Check whether setting file permissions succeeded:
338 0 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
339 0 croak "Adding file perms for \"$content_path\" failed!";
340 }
341 0 my $message =
342 "File upload of \"$local_path\" to \"$content_path\" succeeded";
343 0 $content->set_results( "$message", $res );
344 0 $content->{'Path'} = $content_path;
345 0 return $success;
346 }
347  
348 #}}}
349  
350 #{{{sub upload_from_file
351 5 ▶sub upload_from_file {
Subroutine upload_from_file: called CC=9 SCAR=23.2
Pod documented
352 5 my ( $content, $file, $fork_id, $number_of_forks ) = @_;
353 5 ▶ $fork_id = defined $fork_id ? $fork_id : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $fork_id ? : 0 5 5
354 5 ▶ $number_of_forks = defined $number_of_forks ? $number_of_forks : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $number_of_forks ? : 0 5 5
355 5 my $count = 0;
356 5 ▶ if ( !defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 2 3 5
357 2 croak 'File to upload from not defined';
358 }
359 3 ▶ if ( open my ($input), '<', $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $input, '<', $file) { } 2 1 3
360 2 ▶ while (<$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $input)) 1 1 2
361 1 ▶ if ( $fork_id == ( $count++ % $number_of_forks ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fork_id == $count++ % $number_of_forks) 1 0 1
362 1 chomp;
363 1 ▶ $_ =~ /^\s*(\S.*?)\s*$/msx
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^\s*(\S.*?)\s*$/msx or croak("/Problem parsing content to add: '${_}'") 0 1 1
364 or croak "/Problem parsing content to add: '$_'";
365 0 my $local_path = $1;
366 0 $content->upload_file($local_path);
367 0 Apache::Sling::Print::print_result($content);
368 }
369 }
370 1 ▶ close $input or croak 'Problem closing input!';
Branch: 1 branch
BranchTrueFalseTotal
close $input or croak("Problem closing input!") 1 0 1
371 }
372 else {
373 1 croak "Problem opening file: '$file'";
374 }
375 1 return 1;
376 }
377  
378 #}}}
379  
380 #{{{sub view_attribute
381 0 ▶sub view_attribute {
Subroutine view_attribute: not called CC=8 SCAR=42.8
Pod undocumented
382 0 my ( $content, $remote_dest, $attribute_name, $nakamura_name, $missing_ok )
383 = @_;
384 $remote_dest =
385 defined $remote_dest
386 ? Apache::Sling::URL::strip_leading_slash($remote_dest)
387 0 ▶ : $content->{'Path'};
Branch: 1 branch
BranchTrueFalseTotal
defined $remote_dest ? : 0 0 0
388  
389 # By default the attribute must be present in the full JSON:
390 0 ▶ $missing_ok = defined $missing_ok ? $missing_ok : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $missing_ok ? : 0 0 0
391 0 my $json_success = $content->view_full_json($remote_dest);
392 0 ▶ if ( !$json_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_success) 0 0 0
393 0 return $json_success;
394 }
395 0 my $content_json = from_json( $content->{'Message'} );
396 0 my $attribute = $content_json->{$nakamura_name};
397  
398 # merge an array attribute into a string:
399 0 ▶ if ( ref($attribute) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attribute eq "ARRAY") 0 0 0
400 0 $attribute = join( ',', @{$attribute} );
401 }
402  
403 # If the attribute is undefined but allowed to be
404 # missing then set it to an empty string:
405 0 ▶ if ( !defined $attribute && $missing_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $attribute and $missing_ok) 0 0 0
Condition0%0/3
not defined $attribute and $missing_ok
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $attribute and $missing_ok
not defined $attribute $missing_ok
Truth table0%0/3
not(A) and B
A= not defined $attributeB= $missing_ok
ABresult
0X0
100
111
406 0 $attribute = q{};
407 }
408 0 my $success = defined $attribute;
409 0 ▶ $content->{'Message'} =
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
410 $success ? $attribute : "Problem viewing $attribute_name";
411 0 return $success;
412 }
413  
414 #}}}
415  
416 #{{{sub view_copyright
417 0 ▶sub view_copyright {
Subroutine view_copyright: not called CC=1 SCAR=6.9
Pod documented
418 0 my ( $content, $remote_dest ) = @_;
419 0 my $success =
420 $content->view_attribute( $remote_dest, 'copyright', 'sakai:copyright' );
421 0 return $success;
422 }
423  
424 #}}}
425  
426 #{{{sub view_description
427 0 ▶sub view_description {
Subroutine view_description: not called CC=1 SCAR=6.9
Pod documented
428 0 my ( $content, $remote_dest ) = @_;
429 0 my $success = $content->view_attribute( $remote_dest, 'description',
430 'sakai:description', 1 );
431 0 return $success;
432 }
433  
434 #}}}
435  
436 #{{{sub view_tags
437 0 ▶sub view_tags {
Subroutine view_tags: not called CC=1 SCAR=6.9
Pod documented
438 0 my ( $content, $remote_dest ) = @_;
439 0 my $success =
440 $content->view_attribute( $remote_dest, 'tags', 'sakai:tags', 1 );
441 0 return $success;
442 }
443  
444 #}}}
445  
446 #{{{sub view_title
447 0 ▶sub view_title {
Subroutine view_title: not called CC=1 SCAR=6.9
Pod documented
448 0 my ( $content, $remote_dest ) = @_;
449 0 my $success = $content->view_attribute( $remote_dest, 'title',
450 'sakai:pooled-content-file-name' );
451 0 return $success;
452 }
453  
454 #}}}
455  
456 #{{{sub view_visibility
457 0 ▶sub view_visibility {
Subroutine view_visibility: not called CC=1 SCAR=6.9
Pod documented
458 0 my ( $content, $remote_dest ) = @_;
459 0 my $success = $content->view_attribute( $remote_dest, 'visibility',
460 'sakai:permissions' );
461 0 return $success;
462 }
463  
464 #}}}
465  
466 1 1;
467  
468 __END__
↑ Summary Nakamura/Authn.pm »