blib/lib/Yukki/Model/File.pm

filter: Statementstmt 24.8% 39 / 157 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 34.3% 11 / 32 Podpod 100% 21 / 21 totaltotal 26.8% 71 / 264 SCAR 61.7
CC
29
Cov
19%
store
56.0
title
42.0
rename
20.0
CRAP 480.1
« blib/lib/Yukki/Web/Controller/Page.pm ↑ Summary blib/lib/Yukki/Web/Controller/Login.pm »
1 package Yukki::Model::File;
2 3 $Yukki::Model::File::VERSION = '0.991_002'; # TRIAL
3  
4 3 ▶$Yukki::Model::File::VERSION = '0.991002';use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 extends 'Yukki::Model';
9  
10 3 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Digest::SHA1 qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Number::Bytes::Human qw( format_bytes );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use LWP::MediaTypes qw( guess_media_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Types::Standard qw( Maybe Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Yukki::Error qw( http_throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # ABSTRACT: the model for loading and saving files in the wiki
21  
22  
23 3 has path => (
24 is => 'ro',
25 isa => Str,
26 required => 1,
27 );
28  
29  
30 3 has filetype => (
31 is => 'ro',
32 isa => Maybe[Str],
33 required => 1,
34 default => 'yukki',
35 );
36  
37  
38 3 has repository => (
39 is => 'ro',
40 isa => class_type('Yukki::Model::Repository'),
41 required => 1,
42 handles => {
43 'make_blob' => 'make_blob',
44 'make_blob_from_file' => 'make_blob_from_file',
45 'find_root' => 'find_root',
46 'branch' => 'branch',
47 'show' => 'show',
48 'make_tree' => 'make_tree',
49 'commit_tree' => 'commit_tree',
50 'update_root' => 'update_root',
51 'find_path' => 'find_path',
52 'fetch_size' => 'fetch_size',
53 'repository_name' => 'name',
54 'author_name' => 'author_name',
55 'author_email' => 'author_email',
56 'log' => 'log',
57 'diff_blobs' => 'diff_blobs',
58 },
59 );
60  
61  
62 sub BUILDARGS {
63 0 ▶ my $class = shift;
Subroutine BUILDARGS: not called CC=3 SCAR=24.8
Pod documented
64  
65 0 my %args;
66 0 ▶ if (@_ == 1) { %args = %{ $_[0] }; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
67 0 else { %args = @_; }
68  
69 0 ▶ if ($args{full_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"full_path"}) 0 0 0
70 0 my $full_path = delete $args{full_path};
71  
72 0 my ($path, $filetype) = $full_path =~ m{^(.*)(?:\.(\w+))?$};
73  
74 0 $args{path} = $path;
75 0 $args{filetype} = $filetype;
76 }
77  
78 0 return \%args;
79 }
80  
81  
82 sub full_path {
83 0 ▶ my $self = shift;
Subroutine full_path: not called CC=2 SCAR=17.9
Pod documented
84  
85 0 my $full_path;
86 0 ▶ if (defined $self->filetype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->filetype) { } 0 0 0
87 0 $full_path = join '.', $self->path, $self->filetype;
88 }
89 else {
90 0 $full_path = $self->path;
91 }
92  
93 0 return $full_path;
94 }
95  
96  
97 sub file_name {
98 0 ▶ my $self = shift;
Subroutine file_name: not called CC=1 SCAR=6.9
Pod documented
99 0 my $full_path = $self->full_path;
100 0 my ($file_name) = $full_path =~ m{([^/]+)$};
101 0 return $file_name;
102 }
103  
104  
105 sub file_id {
106 0 ▶ my $self = shift;
Subroutine file_id: not called CC=1 SCAR=6.9
Pod documented
107 0 return sha1_hex($self->file_name);
108 }
109  
110  
111 sub object_id {
112 0 ▶ my $self = shift;
Subroutine object_id: not called CC=1 SCAR=6.9
Pod documented
113 0 return $self->find_path($self->full_path);
114 }
115  
116  
117 sub title {
118 0 ▶ my $self = shift;
Subroutine title: not called CC=6 SCAR=37.4
Pod documented
119  
120 0 ▶ if ($self->filetype eq 'yukki') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filetype eq "yukki") 0 0 0
121 0 LINE: for my $line ($self->fetch) {
122 0 ▶ if ($line =~ /^#\s*(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^#\s*(.*)$/u) { } 0 0 0
elsif ($line =~ /:/u) { } 0 0 0
123 0 return $1;
124 }
125 elsif ($line =~ /:/) {
126 0 my ($name, $value) = split m{\s*:\s*}, $line, 2;
127 0 ▶ return $value if lc($name) eq 'title';
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq "title" 0 0 0
128 }
129 else {
130 0 last LINE;
131 }
132 }
133 }
134  
135 0 my $title = $self->file_name;
136 0 $title =~ s/\.(\w+)$//g;
137 0 return $title;
138 }
139  
140  
141 sub file_size {
142 0 ▶ my $self = shift;
Subroutine file_size: not called CC=1 SCAR=6.9
Pod documented
143 0 return $self->fetch_size($self->full_path);
144 }
145  
146  
147 sub formatted_file_size {
148 0 ▶ my $self = shift;
Subroutine formatted_file_size: not called CC=1 SCAR=6.9
Pod documented
149 0 return format_bytes($self->file_size);
150 }
151  
152  
153 sub media_type {
154 0 ▶ my $self = shift;
Subroutine media_type: not called CC=1 SCAR=6.9
Pod documented
155 0 return guess_media_type($self->full_path);
156 }
157  
158  
159 sub store {
160 0 ▶ my ($self, $params) = @_;
Subroutine store: not called CC=7 SCAR=40.3
Pod documented
161 0 my $path = $self->full_path;
162  
163 0 my (@parts) = split m{/}, $path;
164 0 my $blob_name = $parts[-1];
165  
166 0 my $object_id;
167 0 ▶ if ($params->{content}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'content'}) { } 0 0 0
elsif ($params->{'filename'}) { } 0 0 0
168 0 $object_id = $self->make_blob($blob_name, $params->{content});
169 }
170 elsif ($params->{filename}) {
171 0 $object_id = $self->make_blob_from_file($blob_name, $params->{filename});
172 }
173 0 ▶ http_throw("unable to create blob for $path") unless $object_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $object_id 0 0 0
174  
175 0 my $old_tree_id = $self->find_root;
176 0 ▶ http_throw("unable to locate original tree ID for ".$self->branch)
Branch: 1 branch
BranchTrueFalseTotal
unless $old_tree_id 0 0 0
177 unless $old_tree_id;
178  
179 0 my $new_tree_id = $self->make_tree($old_tree_id, \@parts, $object_id);
180 0 ▶ http_throw("unable to create the new tree containing $path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $new_tree_id 0 0 0
181 unless $new_tree_id;
182  
183 0 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
184 0 ▶ http_throw("unable to commit the new tree containing $path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $commit_id 0 0 0
185 unless $commit_id;
186  
187 0 $self->update_root($old_tree_id, $commit_id);
188 }
189  
190  
191 sub rename {
192 0 ▶ my ($self, $params) = @_;
Subroutine rename: not called CC=4 SCAR=30.0
Pod documented
193 0 my $old_path = $self->full_path;
194  
195 0 my (@new_parts) = split m{/}, $params->{full_path};
196 0 my (@old_parts) = split m{/}, $old_path;
197 0 my $blob_name = $old_parts[-1];
198  
199 0 my $object_id = $self->object_id;
200  
201 0 my $old_tree_id = $self->find_root;
202 0 ▶ http_throw("unable to locate original tree ID for ".$self->branch)
Branch: 1 branch
BranchTrueFalseTotal
unless $old_tree_id 0 0 0
203 unless $old_tree_id;
204  
205 0 my $new_tree_id = $self->make_tree(
206 $old_tree_id, \@old_parts, \@new_parts, $object_id);
207 0 ▶ http_throw("unable to create the new tree renaming $old_path to $params->{full_path}\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $new_tree_id 0 0 0
208 unless $new_tree_id;
209  
210 0 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
211 0 ▶ http_throw("unable to commit the new tree renaming $old_path to $params->{full_path}\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $commit_id 0 0 0
212 unless $commit_id;
213  
214 0 $self->update_root($old_tree_id, $commit_id);
215  
216 return Yukki::Model::File->new(
217 app => $self->app,
218 repository => $self->repository,
219 full_path => $params->{full_path},
220 0 );
221 }
222  
223  
224 sub remove {
225 0 ▶ my ($self, $params) = @_;
Subroutine remove: not called CC=4 SCAR=30.0
Pod documented
226 0 my $old_path = $self->full_path;
227  
228 0 my (@old_parts) = split m{/}, $old_path;
229  
230 0 my $old_tree_id = $self->find_root;
231 0 ▶ http_throw("unable to locate original tree ID for ".$self->branch)
Branch: 1 branch
BranchTrueFalseTotal
unless $old_tree_id 0 0 0
232 unless $old_tree_id;
233  
234 0 my $new_tree_id = $self->make_tree($old_tree_id, \@old_parts);
235 0 ▶ http_throw("unable to create the new tree removing $old_path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $new_tree_id 0 0 0
236 unless $new_tree_id;
237  
238 0 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
239 0 ▶ http_throw("unable to commit the new tree removing $old_path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $commit_id 0 0 0
240 unless $commit_id;
241  
242 0 $self->update_root($old_tree_id, $commit_id);
243 }
244  
245  
246 sub exists {
247 0 ▶ my $self = shift;
Subroutine exists: not called CC=1 SCAR=6.9
Pod documented
248  
249 0 my $path = $self->full_path;
250 0 return $self->find_path($path);
251 }
252  
253  
254 sub fetch {
255 0 ▶ my $self = shift;
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
256  
257 0 my $path = $self->full_path;
258 0 my $object_id = $self->find_path($path);
259  
260 0 ▶ return unless defined $object_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object_id 0 0 0
261  
262 0 return $self->show($object_id);
263 }
264  
265  
266 sub has_format {
267 0 ▶ my ($self, $media_type) = @_;
Subroutine has_format: not called CC=4 SCAR=30.0
Pod documented
268 0 ▶ $media_type //= $self->media_type;
Condition0%0/3
$media_type //= $self->media_type
l!l&&r!l&&!r
000
MC/DC0%0/2
$media_type //= $self->media_type
$media_type $self->media_type
Truth table0%0/3
A //= B
A= $media_typeB= $self->media_type
ABresult
1X1
011
000
269  
270 0 my @formatters = $self->app->formatter_plugins;
271 0 for my $formatter (@formatters) {
272 0 ▶ return 1 if $formatter->has_format($media_type);
Branch: 1 branch
BranchTrueFalseTotal
if $formatter->has_format($media_type) 0 0 0
273 }
274  
275 0 return '';
276 }
277  
278  
279 sub fetch_formatted {
280 0 ▶ my ($self, $ctx, $position) = @_;
Subroutine fetch_formatted: not called CC=4 SCAR=30.0
Pod documented
281 0 ▶ $position //= 0;
Condition0%0/2
$position //= 0
l!l
00
MC/DC0%0/1
$position //= 0
$position
Truth table0%0/2
A
A= $position
Aresult
11
00
282  
283 0 my $media_type = $self->media_type;
284  
285 0 my $formatter;
286 0 for my $plugin ($self->app->formatter_plugins) {
287 0 ▶ return $plugin->format({
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->has_format($media_type) 0 0 0
288 context => $ctx,
289 file => $self,
290 position => $position,
291 }) if $plugin->has_format($media_type);
292 }
293  
294 0 return $self->fetch;
295 }
296  
297  
298 sub history {
299 0 ▶ my $self = shift;
Subroutine history: not called CC=1 SCAR=6.9
Pod documented
300 0 return $self->log($self->full_path);
301 }
302  
303  
304 sub diff {
305 0 ▶ my ($self, $object_id_1, $object_id_2) = @_;
Subroutine diff: not called CC=1 SCAR=6.9
Pod documented
306 0 return $self->diff_blobs($self->full_path, $object_id_1, $object_id_2);
307 }
308  
309  
310 sub file_preview {
311 0 ▶ my ($self, %params) = @_;
Subroutine file_preview: not called CC=1 SCAR=6.9
Pod documented
312  
313 0 Class::Load::load_class('Yukki::Model::FilePreview');
314 0 return Yukki::Model::FilePreview->new(
315 %params,
316 app => $self->app,
317 repository => $self->repository,
318 path => $self->path,
319 );
320 }
321  
322  
323 sub list_files {
324 0 ▶ my ($self) = @_;
Subroutine list_files: not called CC=1 SCAR=6.9
Pod documented
325 0 return $self->repository->list_files($self->path);
326 }
327  
328  
329 sub parent {
330 0 ▶ my $self = shift;
Subroutine parent: not called CC=2 SCAR=17.9
Pod documented
331  
332 0 my @parts = split m{/}, $self->path;
333 0 ▶ return if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 0 0 0
334  
335 0 pop @parts;
336 0 return Yukki::Model::File->new(
337 app => $self->app,
338 repository => $self->repository,
339 path => join('/', @parts),
340 );
341 }
342  
343  
344 3 1;
345  
346 __END__
347  
348 =pod
349  
350 =encoding UTF-8
351  
352 =head1 NAME
353  
354 Yukki::Model::File - the model for loading and saving files in the wiki
355  
356 =head1 VERSION
357  
358 version 0.991_002
359  
360 =head1 SYNOPSIS
361  
362 my $repository = $app->model('Repository', { repository => 'main' });
363 my $file = $repository->file({
364 path => 'foobar',
365 filetype => 'yukki',
366 });
367  
368 =head1 DESCRIPTION
369  
370 Tools for fetching files from the git repository and storing them there.
371  
372 =head1 EXTENDS
373  
374 L<Yukki::Model>
375  
376 =head1 ATTRIBUTES
377  
378 =head2 path
379  
380 This is the path to the file in the repository, but without the file suffix.
381  
382 =head2 filetype
383  
384 The suffix of the file. Defaults to "yukki".
385  
386 =head2 repository
387  
388 This is the the L<Yukki::Model::Repository> the file will be fetched from or
389 stored into.
390  
391 =head1 METHODS
392  
393 =head2 BUILDARGS
394  
395 Allows C<full_path> to be given instead of C<path> and C<filetype>.
396  
397 =head2 full_path
398  
399 This is the complete path to the file in the repository with the L</filetype>
400 tacked onto the end.
401  
402 =head2 file_name
403  
404 This is the base name of the file.
405  
406 =head2 file_id
407  
408 This is a SHA-1 of the file name in hex.
409  
410 =head2 object_id
411  
412 This is the git object ID of the file blob.
413  
414 =head2 title
415  
416 This is the title for the file. For most files this is the file name. For files with the "yukki" L</filetype>, the title metadata or first heading found in the file is used.
417  
418 =head2 file_size
419  
420 This is the size of the file in bytes.
421  
422 =head2 formatted_file_size
423  
424 This returns a human-readable version of the file size.
425  
426 =head2 media_type
427  
428 This is the MIME type detected for the file.
429  
430 =head2 store
431  
432 $file->store({
433 content => 'text to put in file...',
434 comment => 'comment describing the change',
435 });
436  
437 # OR
438  
439 $file->store({
440 filename => 'file.pdf',
441 comment => 'comment describing the change',
442 });
443  
444 This stores a new version of the file, either from the given content string or a
445 named local file.
446  
447 =head2 rename
448  
449 my $new_file = $file->rename({
450 full_path => 'renamed/to/path.yukki',
451 comment => 'renamed the file',
452 });
453  
454 Renames the file within the repository. When complete, this method returns a reference to the L<Yukki::Model::File> object representing the new path.
455  
456 =head2 remove
457  
458 $self->remove({ comment => 'removed the file' });
459  
460 Removes the file from the repostory. The file is not permanently deleted as it still exists in the version history. However, as of this writing, the API here does not provide any means for getting at a deleted file.
461  
462 =head2 exists
463  
464 Returns true if the file exists in the repository already.
465  
466 =head2 fetch
467  
468 my $content = $self->fetch;
469 my @lines = $self->fetch;
470  
471 Returns the contents of the file.
472  
473 =head2 has_format
474  
475 my $yes_or_no = $self->has_format($media_type);
476  
477 Returns true if the named media type has a format plugin.
478  
479 =head2 fetch_formatted
480  
481 my $html_content = $self->fetch_formatted($ctx);
482  
483 Returns the contents of the file. If there are any configured formatter plugins for the media type of the file, those will be used to return the file.
484  
485 =head2 history
486  
487 my @revisions = $self->history;
488  
489 Returns a list of revisions. Each revision is a hash with the following keys:
490  
491 =over
492  
493 =item object_id
494  
495 The object ID of the commit.
496  
497 =item author_name
498  
499 The name of the commti author.
500  
501 =item date
502  
503 The date the commit was made.
504  
505 =item time_ago
506  
507 A string showing how long ago the edit took place.
508  
509 =item comment
510  
511 The comment the author made about the comment.
512  
513 =item lines_added
514  
515 Number of lines added.
516  
517 =item lines_removed
518  
519 Number of lines removed.
520  
521 =back
522  
523 =head2 diff
524  
525 my @chunks = $self->diff('a939fe...', 'b7763d...');
526  
527 Given two object IDs, returns a list of chunks showing the difference between two revisions of this path. Each chunk is a two element array. The first element is the type of chunk and the second is any detail for that chunk.
528  
529 The types are:
530  
531 "+" This chunk was added to the second revision.
532 "-" This chunk was removed in the second revision.
533 " " This chunk is the same in both revisions.
534  
535 =head2 file_preview
536  
537 my $file_preview = $self->file_preview(
538 content => $content,
539 );
540  
541 Takes this file and returns a L<Yukki::Model::FilePreview> object, with the file contents "replaced" by the given content.
542  
543 =head2 list_files
544  
545 my @files = $self->list_files;
546  
547 List the files attached to/under this file path.
548  
549 =head2 parent
550  
551 my $parent = $self->parent;
552  
553 Return a L<Yukki::Model::File> representing the parent path of the current file within the current repository. For example, if the current L<path> is:
554  
555 foo/bar/baz.pdf
556  
557 the parent of it will be:
558  
559 foo/bar.yukki
560  
561 This returns C<undef> if the current file is at the root of the repository.
562  
563 =head2 branch
564  
565 Returns the repository branch to which this file belongs.
566  
567 =head1 AUTHOR
568  
569 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
570  
571 =head1 COPYRIGHT AND LICENSE
572  
573 This software is copyright (c) 2017 by Qubling Software LLC.
574  
575 This is free software; you can redistribute it and/or modify it under
576 the same terms as the Perl 5 programming language system itself.
577  
578 =cut
« blib/lib/Yukki/Web/Controller/Page.pm ↑ Summary blib/lib/Yukki/Web/Controller/Login.pm »