lib/Kwiki/Pages.pm

filter: Statementstmt 25.4% 45 / 177 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 22.7% 10 / 44 Podpod 0.0% 0 / 12 totaltotal 18.3% 55 / 300 CC 29 SCAR 20.6
CC
29
Cov
19%
all_since
20.0
metadata
20.0
kwiki_link
20.0
CRAP 7.9
« lib/Kwiki/Preferences.pm ↑ Summary lib/Kwiki/Archive.pm »
1 package Kwiki::Pages;
2 2 ▶use Kwiki::Base -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use mixin 'Kwiki::Installer';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 const inline_classes => [qw(Kwiki::Page Kwiki::PageMeta)];
6 2 const class_id => 'pages';
7 2 const class_title => 'Kwiki Pages';
8 2 const page_class => 'Kwiki::Page';
9 2 const meta_class => 'Kwiki::PageMeta';
10  
11 2 field current =>
12 -init => '$self->new_page($self->current_id)';
13  
14 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
Pod undocumented
15 grep {
16 0 $_->is_readable
17 } map {
18 0 $self->new_page($_->filename)
19 } io($self->current->database_directory)->all_files;
20 }
21  
22 0 ▶sub all_ids_newest_first {
Subroutine all_ids_newest_first: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my $path = $self->current->database_directory;
24 # XXX Unix speed hack should be worked around for win32
25 0 map {chomp; $_} `ls -1t $path`;
26 }
27  
28 0 ▶sub recent_by_count {
Subroutine recent_by_count: not called CC=2 SCAR=17.9
Pod undocumented
29 0 my ($count) = @_;
30 0 my @page_ids = $self->all_ids_newest_first;
31 0 ▶ splice(@page_ids, $count)
Branch: 1 branch
BranchTrueFalseTotal
if @page_ids > $count 0 0 0
32 if @page_ids > $count;
33 0 map { $self->new_page($_) } @page_ids;
34 }
35  
36 0 ▶sub all_since {
Subroutine all_since: not called CC=4 SCAR=30.0
Pod undocumented
37 0 my ($minutes) = @_;
38 0 my @pages_since;
39 0 for my $page_id ($self->all_ids_newest_first) {
40 0 my $page = $self->new_page($page_id);
41 0 ▶ last if $page->age_in_minutes > $minutes;
Branch: 1 branch
BranchTrueFalseTotal
if $page->age_in_minutes > $minutes 0 0 0
42 0 ▶ next unless $page->is_readable;
Branch: 1 branch
BranchTrueFalseTotal
unless $page->is_readable 0 0 0
43 0 push @pages_since, $page;
44 }
45 0 return @pages_since;
46 }
47  
48 0 ▶sub current_id {
Subroutine current_id: not called CC=2 SCAR=17.9
Pod undocumented
49 0 ▶ my $page_name =
Condition0%0/3
$self->hub->cgi->page_name || $self->hub->config->main_page
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->hub->cgi->page_name || $self->hub->config->main_page
$self->hub->cgi->page_name $self->hub->config->main_page
Truth table0%0/3
A || B
A= $self->hub->cgi->page_nameB= $self->hub->config->main_page
ABresult
1X1
011
000
50 $self->hub->cgi->page_name ||
51 $self->hub->config->main_page;
52 0 $self->name_to_id($page_name);
53 }
54  
55 1 ▶sub name_to_id {
Subroutine name_to_id: called CC=1 SCAR=0.0
Pod undocumented
56 1 my $id = $self->uri_escape(shift);
57 }
58  
59 0 ▶sub name_to_title {
Subroutine name_to_title: not called CC=1 SCAR=6.9
Pod undocumented
60 0 (shift);
61 }
62  
63 0 ▶sub id_to_uri {
Subroutine id_to_uri: not called CC=1 SCAR=6.9
Pod undocumented
64 0 (shift);
65 }
66  
67 0 ▶sub id_to_title {
Subroutine id_to_title: not called CC=1 SCAR=6.9
Pod undocumented
68 0 $self->uri_unescape(shift);
69 }
70  
71 0 ▶sub new_page {
Subroutine new_page: not called CC=2 SCAR=17.9
Pod undocumented
72 0 my $page_id = shift;
73 0 ▶ return if length $page_id > 255;
Branch: 1 branch
BranchTrueFalseTotal
if length $page_id > 255 0 0 0
74 0 my $page = $self->page_class->new(id => $page_id);
75 0 $page->metadata($self->new_metadata($page_id));
76 0 return $page;
77 }
78  
79 1 ▶sub new_from_name {
Subroutine new_from_name: called CC=2 SCAR=11.6
Pod undocumented
80 1 my $page_name = shift;
81 1 my $id = $self->name_to_id($page_name);
82 0 my $page = $self->new_page($id);
83 0 ▶ return unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 0 0 0
84 0 $page->title($self->name_to_title($page_name));
85 0 return $page;
86 }
87  
88 0 ▶sub new_metadata {
Subroutine new_metadata: not called CC=2 SCAR=17.9
Pod undocumented
89 0 ▶ my $page_id = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $page_id = shift() or die 0 0 0
90 0 $self->meta_class->new(id => $page_id);
91 }
92  
93 package Kwiki::Page;
94 2 ▶use Kwiki::ContentObject qw(-base !io);
Subroutine BEGIN: called CC=1 SCAR=0.0
95 2 ▶use Kwiki ':char_classes';
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 2 field class_id => 'page';
98 2 field title =>
99 -init => '$self->hub->pages->id_to_title($self->id)';
100 2 field uri =>
101 -init => '$self->hub->pages->id_to_uri($self->id)';
102  
103 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
104 return (
105 0 page_uri => $self->uri,
106 page_title => $self->title,
107 );
108 }
109  
110 0 ▶sub database_directory {
Subroutine database_directory: not called CC=1 SCAR=6.9
111 0 $self->hub->config->database_directory;
112 }
113  
114 0 ▶sub content {
Subroutine content: not called CC=3 SCAR=24.8
115 0 ▶ return $self->{content} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
116 0 ▶ return $self->{content} if defined $self->{content};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"content"} 0 0 0
117 0 $self->load_content;
118 0 return $self->{content};
119 }
120  
121 0 ▶sub metadata {
Subroutine metadata: not called CC=4 SCAR=30.0
122 0 ▶ return $self->{metadata} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
123 $self->{metadata} ||=
124 0 ▶ $self->meta_class->new(id => $self->id);
Condition0%0/3
$self->{"metadata"} ||= $self->meta_class->new("id", $self->id)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"metadata"} ||= $self->meta_class->new("id", $self->id)
$self->{"metadata"} $self->meta_class->new("id", $self->id)
Truth table0%0/3
A ||= B
A= $self->{"metadata"}B= $self->meta_class->new("id", $self->id)
ABresult
1X1
011
000
125 0 ▶ return $self->{metadata} if $self->{metadata}->loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"metadata"}->loaded 0 0 0
126 0 $self->load_metadata;
127 0 return $self->{metadata};
128 }
129  
130 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
131 0 $self->metadata->update($self);
132 0 return $self;
133 }
134  
135 0 ▶sub kwiki_link {
Subroutine kwiki_link: not called CC=4 SCAR=30.0
136 0 my ($label) = @_;
137 0 my $page_uri = $self->uri;
138 0 ▶ $label = $self->title
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 0 0
139 unless defined $label;
140 0 ▶ return $label unless $self->is_readable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_readable 0 0 0
141 0 my $script = $self->hub->config->script_name;
142 0 ▶ my $class = $self->active
Branch: 1 branch
BranchTrueFalseTotal
$self->active ? : 0 0 0
143 ? '' : ' class="empty"';
144 0 qq(<a href="$script?$page_uri"$class>$label</a>);
145 }
146  
147 0 ▶sub edit_by_link {
Subroutine edit_by_link: not called CC=3 SCAR=24.8
148 0 ▶ my $user_name = $self->metadata->edit_by || 'UnknownUser';
Condition0%0/2
$self->metadata->edit_by || 'UnknownUser'
l!l
00
MC/DC0%0/1
$self->metadata->edit_by || 'UnknownUser'
$self->metadata->edit_by
Truth table0%0/2
A
A= $self->metadata->edit_by
Aresult
11
00
149 0 ▶ $user_name = $self->hub->config->user_default_name
Branch: 1 branch
BranchTrueFalseTotal
if $user_name =~ /[^$Kwiki::Page::ALPHANUM]/ 0 0 0
150 if $user_name =~ /[^$ALPHANUM]/;
151 0 my $page = $self->hub->pages->new_page($user_name);
152 0 $page->kwiki_link;
153 }
154  
155 0 ▶sub edit_time {
Subroutine edit_time: not called CC=3 SCAR=24.8
156 0 ▶ my $edit_time = $self->metadata->edit_unixtime ||
Condition0%0/3
$self->metadata->edit_unixtime || $self->modified_time
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->metadata->edit_unixtime || $self->modified_time
$self->metadata->edit_unixtime $self->modified_time
Truth table0%0/3
A || B
A= $self->metadata->edit_unixtimeB= $self->modified_time
ABresult
1X1
011
000
157 $self->modified_time;
158 0 ▶ return $self->hub->have_plugin('time_zone')
Branch: 1 branch
BranchTrueFalseTotal
$self->hub->have_plugin('time_zone') ? : 0 0 0
159 ? $self->hub->time_zone->format($edit_time)
160 : $self->format_time($edit_time);
161 }
162  
163 0 ▶sub format_time {
Subroutine format_time: not called CC=2 SCAR=17.9
164 0 my $unix_time = shift;
165 0 my $formatted = scalar gmtime $unix_time;
166 0 ▶ $formatted .= ' GMT'
Branch: 1 branch
BranchTrueFalseTotal
unless $formatted =~ /GMT$/ 0 0 0
167 unless $formatted =~ /GMT$/;
168 0 return $formatted;
169 }
170  
171 #XXX This is a bad idea. io is the IO::All constructor. Making it into a
172 # method is problematic
173 0 ▶sub io {
Subroutine io: not called CC=1 SCAR=6.9
174 0 Kwiki::io($self->file_path)->file;
175 }
176  
177 0 ▶sub modified_time {
Subroutine modified_time: not called CC=2 SCAR=17.9
178 0 ▶ $self->io->mtime || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->io->mtime or 0 0 0 0
179 }
180  
181 0 ▶sub age {
Subroutine age: not called CC=1 SCAR=6.9
182 0 $self->age_in_minutes;
183 }
184  
185 0 ▶sub age_in_minutes {
Subroutine age_in_minutes: not called CC=1 SCAR=6.9
186 0 $self->age_in_seconds / 60;
187 }
188  
189 0 ▶sub age_in_seconds {
Subroutine age_in_seconds: not called CC=3 SCAR=24.8
190 0 ▶ return $self->{age_in_seconds} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
191 0 ▶ return $self->{age_in_seconds} if defined $self->{age_in_seconds};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"age_in_seconds"} 0 0 0
192 0 my $path = $self->database_directory;
193 0 my $page_id = $self->id;
194 0 return $self->{age_in_seconds} = (time - $self->modified_time);
195 }
196  
197 0 ▶sub to_html {
Subroutine to_html: not called CC=2 SCAR=17.9
198 0 ▶ my $content = @_ ? shift : $self->content;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
199 0 $self->hub->formatter->text_to_html($content);
200 }
201  
202 0 ▶sub history {
Subroutine history: not called CC=1 SCAR=6.9
203 0 $self->hub->archive->history($self);
204 }
205  
206 0 ▶sub revision_number {
Subroutine revision_number: not called CC=1 SCAR=6.9
207 0 $self->hub->archive->revision_number($self);
208 }
209  
210 0 ▶sub revision_numbers {
Subroutine revision_numbers: not called CC=1 SCAR=6.9
211 0 $self->hub->archive->revision_numbers($self, @_);
212 }
213  
214 package Kwiki::PageMeta;
215 2 ▶use Spoon::MetadataObject -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 2 ▶use Kwiki::Plugin -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 2 const class_id => 'page_metadata';
219 2 field loaded => 0;
220  
221 2 field edit_by => '';
222 2 field edit_time => '';
223 2 field edit_unixtime => '';
224 2 field edit_address => '';
225  
226 0 ▶sub sort_order {
Subroutine sort_order: not called CC=1 SCAR=6.9
227 0 qw(edit_by edit_time edit_unixtime edit_address)
228 }
229  
230 0 ▶sub file_path {
Subroutine file_path: not called CC=1 SCAR=6.9
231 0 join '/', $self->plugin_directory, $self->id;
232 }
233  
234 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
235 0 $self->loaded(1);
236 0 my $file_path = $self->file_path;
237 0 ▶ return unless -f $file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file_path 0 0 0
238 0 my $hash = $self->parse_yaml_file($file_path);
239 0 $self->from_hash($hash);
240 }
241  
242 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
243 0 my $page = shift;
244 0 $self->edit_by($self->hub->users->current->name);
245 0 my $unixtime = time;
246 0 $self->edit_time(scalar gmtime($unixtime));
247 0 $self->edit_unixtime($unixtime);
248 0 $self->edit_address($self->get_edit_address);
249 0 return $self;
250 }
251  
252 0 ▶sub get_edit_address {
Subroutine get_edit_address: not called CC=3 SCAR=24.8
253 $ENV{HTTP_X_FORWARDED_FOR} ||
254 $ENV{REMOTE_ADDR} ||
255 0 ▶ '';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"HTTP_X_FORWARDED_FOR"} or $ENV{"REMOTE_ADDR"} or "" 0 0 0
Condition0%0/3
$ENV{"HTTP_X_FORWARDED_FOR"} or $ENV{"REMOTE_ADDR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"HTTP_X_FORWARDED_FOR"} or $ENV{"REMOTE_ADDR"}
$ENV{"HTTP_X_FORWARDED_FOR"} $ENV{"REMOTE_ADDR"}
Truth table0%0/3
A or B
A= $ENV{"HTTP_X_FORWARDED_FOR"}B= $ENV{"REMOTE_ADDR"}
ABresult
1X1
011
000
256 }
257  
258 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
259 0 my $file_path = $self->file_path;
260 0 my $hash = $self->to_hash;
261 0 $self->print_yaml_file($file_path, $hash);
262 }
263  
264 package Kwiki::Pages;
265  
266 2 __DATA__
« lib/Kwiki/Preferences.pm ↑ Summary lib/Kwiki/Archive.pm »