WE/Util/Support.pm

filter: Statementstmt 5.1% 14 / 270 Branchbran 0.8% 1 / 122 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 17.6% 3 / 17 Podpod 0.0% 0 / 7 totaltotal 4.0% 18 / 441 CC 84 SCAR 52.2
CC
84
Cov
4%
__ANON__
380.0
create_folder_tree
272.0
__ANON__
210.0
CRAP 184.2
« WE/Util/Htaccess.pm ↑ Summary WE_Frontend/MainCommon.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Support.pm,v 1.10 2006/04/18 21:39:44 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE::Util::Support;
18  
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 $VERSION = sprintf("%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/);
22  
23 =head1 NAME
24  
25 WE::Util::Support - support functions for the WE::DB framework
26  
27 =head1 SYNOPSIS
28  
29 use WE::Util::Support;
30  
31 =head1 DESCRIPTION
32  
33 =cut
34  
35 package WE::DB::Obj;
36 2 ▶use WE::DB::Obj;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # Backward compatibility...
39 2 ▶if (!WE::DB::Obj->can('idify_params')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("WE::DB::Obj"->can("idify_params")) 0 2 2
40 #warn "Define alias idify_params";
41 0 ▶ *idify_params = sub { shift->_idify_params(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 }
43  
44 =head2 METHODS in the WE::DB::Obj package
45  
46 =over 4
47  
48 =item create_folder_tree(...)
49  
50 Create a folder tree from file or string.
51  
52 Options are:
53  
54 =over 4
55  
56 =item -string => $string
57  
58 The string with the folder information.
59  
60 =item -file => $file
61  
62 A file with the folder information. Either -string or -file have to exist.
63  
64 =item -rootid => $rootid
65  
66 The root object id to put the top-most folder in. If missing, then the
67 root obejct of the database will be used.
68  
69 =item -standardargs => { ... }
70  
71 Add standard arguments for each created object. The standard arguments
72 will be overwritten by the arguments in the folder information
73 string/file.
74  
75 =back
76  
77 The file or string should contain a list of attributes, prefixed with
78 a "-" and their values. All keys and values should be separated by
79 spaces. To mask spaces in attribute values, just use double quotes.
80 The folder structure is controlled by indentation, that is, top-level
81 folder are not indented, the second-level folders have one space
82 indentation and so on.
83  
84 For the B<Title> attribute, there is an exception to handle language
85 dependent strings: the first two letters should form the ISO 638 code
86 for the language, followed by a colon, followed by the language
87 dependent string. You can specify as many language strings as you
88 like. If you do not need language dependent Title strings, then just
89 use this attribute as the others.
90  
91 Examples for B<Title>:
92  
93 -Title "language independent string" -Name a_name
94 -Title "en:english" "de:german" "it:italian" -Name b_name
95  
96 =cut
97  
98 0 ▶sub create_folder_tree {
Subroutine create_folder_tree: not called CC=16 SCAR=56.1
Pod undocumented
99 0 my($self, %args) = @_;
100 0 my $string = $args{'-string'};
101 0 ▶ if (!defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 0 0
102 0 ▶ if (exists $args{-file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'-file'}) { } 0 0 0
103 0 ▶ open(F, $args{-file}) or die "Can't open $args{-file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, $args{"-file"} or die qq[Can't open $args{"-file"}: $!] 0 0 0
104 0 local $/ = undef;
105 0 $string = <F>;
106 0 close F;
107 } else {
108 0 die "Either -file or -string must be given";
109 }
110 }
111 0 my $rootid = $args{'-rootid'};
112 0 ▶ if (!defined $rootid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rootid) 0 0 0
113 0 $rootid = $self->root_object->Id;
114 }
115 0 ▶ my %std_args = ($args{'-standardargs'} ? %{$args{'-standardargs'}} : ());
Branch: 1 branch
BranchTrueFalseTotal
$args{'-standardargs'} ? : 0 0 0
116  
117 # first pass: just check the indentation
118 0 my $last_indent = 0;
119 0 foreach my $line (split /\n/, $string) {
120 0 $line =~ /^(\s*)/;
121 0 my $indent = length $1;
122 0 ▶ if ($indent > $last_indent+1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent > $last_indent + 1) 0 0 0
123 0 die "Indentation error in line <$line>\n";
124 }
125 0 $last_indent = $indent;
126 }
127  
128 # second pass: do the right thing
129 0 require Text::ParseWords;
130 0 my @indent2objid;
131 0 $indent2objid[0] = $rootid;
132 0 foreach my $line (split /\n/, $string) {
133 0 $line =~ s/^(\s*)//;
134 0 my $indent = length $1;
135 0 my $parentid = $indent2objid[$indent];
136 0 ▶ if (!defined $parentid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parentid) 0 0 0
137 0 die "Parse error in line $line? Check the indentation!\n";
138 }
139 0 my %token;
140 0 my(@token) = Text::ParseWords::shellwords($line);
141  
142 # special handling for languagestrings in Title:
143 0 ▶ for(my $i=0; $i<=$#token; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#token) 0 0 0
144 0 my($key, $val) = @token[$i, $i+1];
145 0 ▶ if ($key eq '-Title') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '-Title') { } 0 0 0
146 0 ▶ if ($val =~ /^(..):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(..):(.*)/) { } 0 0 0
147 0 my($lang, $string) = ($1,$2);
148 0 require WE::Util::LangString;
149 0 my $ls = WE::Util::LangString->new;
150 0 $ls->{$lang} = $string;
151 0 $i+=2;
152 0 while (1) {
153 0 $val = $token[$i];
154 0 ▶ if (!defined $val || $val =~ /^-/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val or $val =~ /^-/) { } 0 0 0
elsif ($val =~ /^(..):(.*)/) { } 0 0 0
Condition0%0/3
not defined $val or $val =~ /^-/
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $val or $val =~ /^-/
not defined $val $val =~ /^-/
Truth table0%0/3
not(A) or B
A= not defined $valB= $val =~ /^-/
ABresult
1X1
011
000
155 # next option/no option => write langstring
156 0 $token{$key} = $ls;
157 0 last;
158 } elsif ($val =~ /^(..):(.*)/) {
159 0 my($lang, $string) = ($1,$2);
160 0 $ls->{$lang} = $string;
161 0 $i++;
162 } else {
163 0 die "Parse error in line $line: either language string or new option expected, but got $val";
164 }
165 }
166 } else {
167 0 $token{$key} = $val;
168 0 $i+=2;
169 }
170 } else {
171 0 $token{$key} = $val;
172 0 $i+=2;
173 }
174 }
175  
176 0 my %args = (%std_args, %token);
177 0 $args{'-parent'} = $parentid;
178 0 my $fldr = $self->insert_folder(%args); # XXX handle langstrings?
179 0 my $newobjid = $fldr->Id;
180 0 $indent2objid[$indent+1] = $newobjid;
181 }
182 }
183  
184 =item change_order($p_id, \@child_ids)
185  
186 For the parent collection C<$p_id> the children will be sorted
187 according to the array reference C<@child_ids>. Children of C<$p_id>
188 which are not in C<@child_ids> will be put to the back. Throws an
189 error if one of C<@child_ids> is not a child of C<$p_id>.
190  
191 =cut
192  
193 0 ▶sub change_order {
Subroutine change_order: not called CC=1 SCAR=6.9
Pod undocumented
194 0 my($self, $p_id, $child_ids_ref) = @_;
195 0 my(@child_ids) = @$child_ids_ref;
196 0 $self->idify_params($p_id, @child_ids);
197  
198 # The connect is here used as a locking mechanism.
199 $self->connect_if_necessary
200 0 ▶ (sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
201 0 my $p_stored_obj = $self->_get_stored_obj($p_id);
202 0 ▶ die "Can't get stored object for <$p_id>" if !$p_stored_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_stored_obj 0 0 0
203 # first check whether the children are really the children:
204 0 my @real_children = $self->children_ids($p_id);
205 0 my(%real_children) = map { ($_=>1) } @real_children;
206 0 foreach my $c_id (@child_ids) {
207 0 ▶ if (!exists $real_children{$c_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $real_children{$c_id}) 0 0 0
208 0 die "The object <$c_id> is not child of <$p_id>!";
209 }
210 }
211 0 my @new_child_list = @child_ids;
212 0 foreach (@child_ids) { delete $real_children{$_} }
213 # put missing ids to the back
214 0 ▶ if (keys %real_children) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %real_children) 0 0 0
215 0 warn "The following ids are unhandled by change_order: @{[ keys %real_children ]}";
216 # iterate over array to preserver old order
217 0 foreach (@real_children) {
218 0 ▶ if (exists $real_children{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $real_children{$_}) 0 0 0
219 0 push @new_child_list, $_;
220 0 delete $real_children{$_};
221 }
222 }
223 0 ▶ if (keys %real_children) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %real_children) 0 0 0
224 0 die "Strange! There are still some keys unhandled: @{[ keys %real_children ]}";
225 }
226 }
227  
228 0 $p_stored_obj->[WE::DB::Obj::CHILDREN] = \@new_child_list;
229 0 $self->_store_stored_obj($p_stored_obj);
230 0 });
231 }
232  
233 =item get_position_array($obj_id, [%args])
234  
235 For a given object C<$obj_id>, return an array with the positions of
236 this object in its parent, and the positions of all predecessors.
237  
238 The following options are recognized for C<%args>:
239  
240 =over 4
241  
242 =item -base => C<$base>
243  
244 The positions are by default 0-based, but can be changed with the
245 optional argument C<$base>.
246  
247 =item -filter => C<$sub>
248  
249 Specify an optional filter for the recursion process. The filter
250 options should be a reference to a subroutine. This subroutine is
251 called with two arguments: the object database reference and the id of
252 an object to test. If the object should be included into the position
253 counting, then 1 should be returned, otherwise 0.
254  
255 =item -indexdoc => C<$bool>
256  
257 If true, then the IndexDoc attribute is used for determining the
258 document position.
259  
260 =back
261  
262 Note that only the first parent is used, if there are objects with
263 multiple parents.
264  
265 =cut
266  
267 0 ▶sub get_position_array {
Subroutine get_position_array: not called CC=13 SCAR=52.0
Pod undocumented
268 0 my($self, $obj_id, %args) = @_;
269 0 ▶ my $base = defined $args{-base} ? $args{-base} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-base'} ? : 0 0 0
270 0 my $filter = $args{-filter};
271 0 $self->idify_params($obj_id);
272 0 my($p_id) = ($self->parent_ids($obj_id))[0];
273 0 ▶ return () if !defined $p_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p_id 0 0 0
274 0 my(@children_ids) = $self->children_ids($p_id);
275  
276 0 ▶ if ($args{'-indexdoc'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-indexdoc"}) 0 0 0
277 0 my $p_obj = $self->get_object($p_id);
278 0 ▶ if (!$p_obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p_obj) 0 0 0
279 0 die "Can't get object for id $p_id";
280 }
281 0 ▶ if ($p_obj->{IndexDoc} && $p_obj->{IndexDoc} == $obj_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_obj->{"IndexDoc"} and $p_obj->{"IndexDoc"} == $obj_id) 0 0 0
Condition0%0/3
$p_obj->{"IndexDoc"} and $p_obj->{"IndexDoc"} == $obj_id
!ll&&!rl&&r
000
MC/DC0%0/2
$p_obj->{"IndexDoc"} and $p_obj->{"IndexDoc"} == $obj_id
$p_obj->{"IndexDoc"} $p_obj->{"IndexDoc"} == $obj_id
Truth table0%0/3
A and B
A= $p_obj->{"IndexDoc"}B= $p_obj->{"IndexDoc"} == $obj_id
ABresult
0X0
100
111
282 0 return $self->get_position_array($p_id, %args);
283 }
284 }
285  
286 0 my $this_pos;
287 0 my $pos = 0;
288 0 for my $c_id (@children_ids) {
289 0 ▶ if ($filter && !$filter->($self, $c_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and not &$filter($self, $c_id)) 0 0 0
Condition0%0/3
$filter and not &$filter($self, $c_id)
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and not &$filter($self, $c_id)
$filter not &$filter($self, $c_id)
Truth table0%0/3
A and not(B)
A= $filterB= not &$filter($self, $c_id)
ABresult
0X0
100
111
290 0 next;
291 }
292 0 ▶ if ($c_id eq $obj_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_id eq $obj_id) 0 0 0
293 0 $this_pos = $pos + $base;
294 0 last;
295 }
296 0 $pos++;
297 }
298 0 ▶ if (!defined $this_pos) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this_pos) 0 0 0
299 0 my $err = "Strange: can't find object <$obj_id> in the children collection of its parent <$p_id>.";
300 0 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 0 0 0
301 0 $err .= "\nMaybe the filter was to strict?";
302 }
303 0 die $err;
304 }
305 0 ($self->get_position_array($p_id, -filter => $filter, -base => $base), $this_pos);
306 }
307  
308 =item check_integrity
309  
310 Return a C<WE::DB::Obj::Fsck> object with lists of inconsistencies in
311 the C<WE::DB::Obj> database.
312  
313 =cut
314  
315 0 ▶sub check_integrity {
Subroutine check_integrity: not called CC=2 SCAR=17.9
Pod undocumented
316 0 my $self = shift;
317 0 my $contentdb = shift;
318  
319 0 my @undef_values;
320 my @broken_values;
321 0 my @not_existing_children;
322 0 my @not_existing_parents;
323 0 my @not_existing_versions;
324 0 my @not_referenced;
325 0 my @wrong_ids;
326 0 my @child_parent_mismatches;
327 0 my @doc_object_without_content;
328  
329 0 my $root_object_missing = 0;
330  
331 0 my %referenced;
332  
333 0 my $root_obj = $self->root_object;
334 0 ▶ if (!$root_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $root_obj) { } 0 0 0
335 # This is fatal, don't do any other checks
336 0 $root_object_missing = 1;
337 0 goto RETURN;
338 } else {
339 0 $referenced{$root_obj->Id} = [];
340 }
341  
342 0 ▶ $self->connect_if_necessary(sub {
Subroutine __ANON__: not called CC=19 SCAR=59.4
343 0 my @keys = grep { !/^_/ } keys %{ $self->{DB} };
344 # XXX Can't use while...each --- segfault with 5.8.0
345 0 for my $k (@keys) {
346 0 my $v = $self->{DB}{$k};
347 0 ▶ if (!defined $v) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $v) { } 0 0 0
elsif (ref $v ne 'ARRAY' or @$v != 4) { } 0 0 0
348 0 push @undef_values, $k;
349 ▶ } elsif (ref $v ne 'ARRAY' || @$v != 4) {
Condition0%0/3
ref $v ne "ARRAY" or @$v != 4
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $v ne "ARRAY" or @$v != 4
ref $v ne "ARRAY" @$v != 4
Truth table0%0/3
A or B
A= ref $v ne "ARRAY"B= @$v != 4
ABresult
1X1
011
000
350 0 push @broken_values, $k;
351 } else {
352 0 ▶ if ($v->[OBJECT]->{Id} ne $k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->[0]{"Id"} ne $k) 0 0 0
353 0 push @wrong_ids, $k;
354 }
355 0 my(@children_ids) = @{$v->[CHILDREN]};
356 0 my(@parent_ids) = @{$v->[PARENTS]};
357 0 my(@version_ids) = @{$v->[VERSIONS]};
358 0 for my $def (["c", \@children_ids, \@not_existing_children],
359 ["p", \@parent_ids, \@not_existing_parents],
360 ["v", \@version_ids, \@not_existing_versions],
361 ) {
362 0 my($type, $ids, $res) = @$def;
363 0 for my $id (@$ids) {
364 0 ▶ if (!exists $self->{DB}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'DB'}{$id}) { } 0 0 0
365 0 push @$res, [$k, $id];
366 } else {
367 0 ▶ if ($type ne "p") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "p") 0 0 0
368 0 push @{$referenced{$id}}, $k;
369 }
370 }
371 }
372 }
373 0 for my $id (@children_ids) {
374 0 ▶ if (exists $self->{DB}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"DB"}{$id}) 0 0 0
375 0 my $c = $self->{DB}{$id};
376 CHECK_PARENT: {
377 0 for my $p_id (@{$c->[PARENTS]}) {
378 0 ▶ if ($p_id eq $k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_id eq $k) 0 0 0
379 0 last CHECK_PARENT;
380 }
381 }
382 0 push @child_parent_mismatches, [$k, $id];
383 }
384 }
385 }
386 0 ▶ if ($contentdb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contentdb) 0 0 0
387 0 my $o = $v->[WE::DB::Obj::OBJECT];
388 0 ▶ if ($o->is_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->is_doc) 0 0 0
389 0 my $f = $contentdb->filename($o);
390 0 ▶ if (!-e $f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $f) 0 0 0
391 0 push @doc_object_without_content, $o->Id;
392 }
393 }
394 }
395 }
396 }
397  
398 0 for my $k (@keys) {
399 0 ▶ if (!exists $referenced{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $referenced{$k}) 0 0 0
400 0 push @not_referenced, $k;
401 }
402 }
403 0 });
404  
405 RETURN:
406 0 @not_existing_children = sort {$a->[0]<=>$b->[0]} @not_existing_children;
407 0 @not_existing_parents = sort {$a->[0]<=>$b->[0]} @not_existing_parents;
408 0 @not_existing_versions = sort {$a->[0]<=>$b->[0]} @not_existing_versions;
409 0 @not_referenced = sort {$a<=>$b} @not_referenced;
410  
411 0 bless { "undef_values" => \@undef_values,
412 "broken_values" => \@broken_values,
413 "wrong_ids" => \@wrong_ids,
414 "not_existing_children" => \@not_existing_children,
415 "not_existing_parents" => \@not_existing_parents,
416 "not_existing_versions" => \@not_existing_versions,
417 "not_referenced" => \@not_referenced,
418 "child_parent_mismatches" => \@child_parent_mismatches,
419 "doc_object_without_content" => \@doc_object_without_content,
420 "root_object_missing" => $root_object_missing,
421 }, 'WE::DB::Obj::Fsck';
422 }
423  
424 =item repair_database($errors, %args)
425  
426 Take a C<WE::DB::Obj::Fsck> object with lists of inconsistencies and
427 tries to repair the C<WE::DB::Obj> database. C<%args> may be:
428  
429 =over
430  
431 =item -verbose
432  
433 Be verbose if set to a true value.
434  
435 =back
436  
437 =cut
438  
439 # errors is the return value of check_integrity() (a WE::DB::Obj::Fsck object)
440 0 ▶sub repair_database {
Subroutine repair_database: not called CC=2 SCAR=17.9
Pod undocumented
441 0 my($self, $errors, %args) = @_;
442 0 ▶ my $v = 1 if $args{-verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-verbose"} 0 0 0
443 0 my $root_object_id = $args{-rootobjectid};
444 0 ▶ $self->connect_if_necessary(sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
445 0 ▶ if ($errors->{root_object_missing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors->{"root_object_missing"}) 0 0 0
446 0 ▶ if (!defined $root_object_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $root_object_id) 0 0 0
447 0 die "rootobjectid not specified";
448 }
449 0 $self->{DB}{'_root_object'} = $root_object_id;
450 }
451 0 foreach my $id (@{ $errors->{"not_referenced"} }) {
452 0 ▶ warn "Remove object $id from database\n" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
453 0 delete $self->{DB}{$id};
454 }
455 0 for my $def ([CHILDREN, "not_existing_children", "child"],
456 [PARENTS, "not_existing_parents", "parent"],
457 [VERSIONS, "not_existing_versions", "version"],
458 ) {
459 0 my($index, $key, $label) = @$def;
460 0 for my $def2 (@{ $errors->{$key} }) {
461 0 my($id, $refid) = @$def2;
462 0 my $o = $self->{DB}{$id};
463 0 ▶ if (!$o) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $o) { } 0 0 0
464 0 warn "Object $id does not exist anymore, skipping...\n";
465 } else {
466 0 ▶ warn "Remove $label $refid from $id\n" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
467 0 @{$o->[$index]} = grep { $_ ne $refid } @{$o->[$index]};
468 0 $self->{DB}{$id} = $o;
469 }
470 }
471 }
472 0 ▶ if ($errors->{doc_object_without_content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors->{"doc_object_without_content"}) 0 0 0
473 0 for my $id (@{ $errors->{doc_object_without_content} }) {
474 0 ▶ warn "Remove doc object $id from database (no content file)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
475 if $v;
476 0 delete $self->{DB}{$id};
477 }
478 }
479 0 ▶ warn "Cannot repair undef_values\n" if @{ $errors->{"undef_values"} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$errors->{"undef_values"};} 0 0 0
480 0 ▶ warn "Cannot repair broken_values\n" if @{ $errors->{"broken_values"} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$errors->{"broken_values"};} 0 0 0
481 0 });
482 }
483  
484 =back
485  
486 =cut
487  
488 package WE::DB::Content;
489  
490 =head2 METHODS in the WE::DB::Content package
491  
492 =over
493  
494 =item check_integrity($objdb)
495  
496 Return a C<WE::DB::Content::Fsck> object with lists of inconsistencies
497 in the C<WE::DB::Content> database. The check is done against the
498 C<WE::DB::Obj> database C<$objdb>.
499  
500 =cut
501  
502 0 ▶sub check_integrity {
Subroutine check_integrity: not called CC=1 SCAR=6.9
Pod undocumented
503 0 my($self, $objdb) = @_;
504  
505 0 my @extra_files;
506 my @unreferenced_files;
507  
508 0 ▶ $objdb->connect_if_necessary(sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
509 0 ▶ opendir(D, $self->Directory) or die "Can't open " . $self->Directory . ": $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir D, $self->Directory or die q[Can't open ] . $self->Directory . ": $!" 0 0 0
510 0 ▶ while(my $f = readdir D) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = readdir D)) 0 0 0
511 0 ▶ next if $f =~ /^\.\.?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /^\.\.?$/ 0 0 0
512 0 my($id) = $f =~ /^(\d+)\./;
513 0 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 0 0 0
514 0 ▶ if (!exists $objdb->{DB}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $objdb->{"DB"}{$id}) 0 0 0
515 0 push @unreferenced_files, $f;
516 }
517 } else {
518 0 ▶ if ($f !~ /^(\.svn|CVS|\.cvsignore|\.keep_me|RCS)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f =~ /^(\.svn|CVS|\.cvsignore|\.keep_me|RCS)$/) 0 0 0
519 0 push @extra_files, $f;
520 }
521 }
522 }
523 0 closedir D;
524 0 });
525  
526 0 bless {"unreferenced_files" => \@unreferenced_files,
527 "extra_files" => \@extra_files,
528 }, 'WE::DB::Content::Fsck';
529 }
530  
531 =item repair_database($errors, %args)
532  
533 Take a C<WE::DB::Content::Fsck> object with lists of inconsistencies
534 and tries to repair the C<WE::DB::Content> database. C<%args> may be:
535  
536 =over
537  
538 =item -verbose
539  
540 Be verbose if set to a true value.
541  
542 =back
543  
544 =cut
545  
546 # errors is the return value of check_integrity() (a
547 # WE::DB::Content::Fsck object)
548 0 ▶sub repair_database {
Subroutine repair_database: not called CC=5 SCAR=34.0
Pod undocumented
549 0 my($self, $errors, %args) = @_;
550 0 ▶ my $v = 1 if $args{-verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-verbose"} 0 0 0
551 0 require File::Spec;
552 0 for my $file (@{ $errors->{unreferenced_files} }) {
553 0 my $path = File::Spec->catfile($self->Directory, $file);
554 0 warn "unlink $path\n";
555 0 ▶ unlink $path or warn "Can't unlink $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or warn "Can't unlink ${path}: $!" 0 0 0
556 }
557 0 ▶ if (@{ $errors->{extra_files} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$errors->{"extra_files"};}) 0 0 0
558 0 warn "Please remove extra files manually, e.g. with
559 0 cd @{[ $self->Directory ]} && rm -i @{ $errors->{extra_files} }
560 ";
561 }
562 }
563  
564  
565 package WE::DB::Fsck;
566  
567 0 ▶sub has_errors {
Subroutine has_errors: not called CC=5 SCAR=34.0
568 0 my $errors = shift;
569 0 foreach (values %$errors) {
570 0 ▶ if (ref $_ eq 'ARRAY' && @$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "ARRAY" and @$_) 0 0 0
Condition0%0/3
ref $_ eq "ARRAY" and @$_
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq "ARRAY" and @$_
ref $_ eq "ARRAY" @$_
Truth table0%0/3
A and B
A= ref $_ eq "ARRAY"B= @$_
ABresult
0X0
100
111
571 0 return 1;
572 }
573 }
574 0 ▶ if ($errors->has_fatal_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors->has_fatal_errors) 0 0 0
575 0 return 1;
576 }
577 0 0;
578 }
579  
580 0 ▶sub has_fatal_errors {
Subroutine has_fatal_errors: not called CC=2 SCAR=17.9
581 0 my $errors = shift;
582 0 ▶ return $errors->{root_object_missing} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$errors->{'root_object_missing'} ? : 0 0 0
583 }
584  
585 package WE::DB::Obj::Fsck;
586 2 @WE::DB::Obj::Fsck::ISA = 'WE::DB::Fsck';
587  
588 package WE::DB::Content::Fsck;
589 2 @WE::DB::Content::Fsck::ISA = 'WE::DB::Fsck';
590  
591 2 1;
592  
593 __END__
« WE/Util/Htaccess.pm ↑ Summary WE_Frontend/MainCommon.pm »