| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # $Id: Tools.pm,v 1.7 2004/04/13 21:48:50 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_Content::Tools; | ||||||||||||||||||||||||||||
| 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.7 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | package WE_Content::Base; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | WE_Content::Tools - tools for content objects | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use WE_Content::Tools; | ||||||||||||||||||||||||||||
| 32 | $content_object->find(sub { ... }); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head2 METHODS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over 4 | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item get_structure_diffs($template) | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Return a list of differences against a template object. Only language | ||||||||||||||||||||||||||||
| 43 | data is compared. See L<Algorithm::Diff> for the output format. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | ▶ | sub get_structure_diffs { | ||||||||||||||||||||||||||
|
Subroutine
get_structure_diffs: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 6 | my($self, $template) = @_; | |||||||||||||||||||||||||||
| 49 | 6 | ▶ | die "Template should be a template" if !$template->{Type} eq 'template'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 6 | require Algorithm::Diff; | |||||||||||||||||||||||||||
| 51 | 6 | require Data::Dumper; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 6 | my %ret; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 6 | ▶ | while(my($lang, $langval) = each %{ $self->{Object}->{'data'} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | next unless (UNIVERSAL::isa($langval, 'HASH') && | ||||||||||||||||||||||||||||
| 57 | 28 | ▶ | exists $langval->{'ct'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
UNIVERSAL::isa($langval, "HASH") and exists $langval->{"ct"}
MC/DC50%1/2
UNIVERSAL::isa($langval, "HASH") and exists $langval->{"ct"}
UNIVERSAL::isa($langval, "HASH")
exists $langval->{"ct"}
Truth table66%2/3
A and B
A= UNIVERSAL::isa($langval, "HASH")B= exists $langval->{"ct"}
| |||||||||||||||||||||||||||||
| 58 | 8 | my $ct = $langval->{'ct'}; | |||||||||||||||||||||||||||
| 59 | 8 | my $template_ct = $template->{Object}{'ct'}; | |||||||||||||||||||||||||||
| 60 | my(@diffs) = Algorithm::Diff::diff | ||||||||||||||||||||||||||||
| 61 | ($template_ct, $ct, | ||||||||||||||||||||||||||||
| 62 | 64 | ▶ | sub { Data::Dumper->new([shift],['n'])->Sortkeys(1)->Dump } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 8 | ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 8 | $ret{$lang} = \@diffs; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | %ret; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =item upgrade($template) | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Upgrade the content file to the current $template. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub upgrade { | ||||||||||||||||||||||||||
|
Subroutine
upgrade: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | my($self, $template) = @_; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | die "Template should be a template" if !$template->{Type} eq 'template'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | require Storable; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $_upgrade = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 84 | 0 | my($ct, $template_ct) = @_; | |||||||||||||||||||||||||||
| 85 | 0 | for my $i (0 .. $#$ct) { | |||||||||||||||||||||||||||
| 86 | 0 | my $ct_node = $ct->[$i]; | |||||||||||||||||||||||||||
| 87 | 0 | my $tct_node = $template_ct->[$i]; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($ct_node->{type} eq $tct_node->{type} && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ct_node->{"type"} eq $tct_node->{"type"} and $ct_node->{"name"} eq $tct_node->{"name"}
MC/DC0%0/2
$ct_node->{"type"} eq $tct_node->{"type"} and $ct_node->{"name"} eq $tct_node->{"name"}
$ct_node->{"type"} eq $tct_node->{"type"}
$ct_node->{"name"} eq $tct_node->{"name"}
Truth table0%0/3
A and B
A= $ct_node->{"type"} eq $tct_node->{"type"}B= $ct_node->{"name"} eq $tct_node->{"name"}
| |||||||||||||||||||||||||||||
| 89 | $ct_node->{name} eq $tct_node->{name} | ||||||||||||||||||||||||||||
| 90 | ) { | ||||||||||||||||||||||||||||
| 91 | 0 | my $ct_cancontain = join("|", $ct_node->{cancontain}); | |||||||||||||||||||||||||||
| 92 | 0 | my $tct_cancontain = join("|", $tct_node->{cancontain}); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($ct_cancontain ne $tct_cancontain) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $ct_node->{cancontain} = Storable::dclone($tct_node->{cancontain}); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 0 | }; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | while(my($lang, $langval) = each %{ $self->{Object}->{'data'} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | next unless (UNIVERSAL::isa($langval, 'HASH') && | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | exists $langval->{'ct'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
UNIVERSAL::isa($langval, "HASH") and exists $langval->{"ct"}
MC/DC0%0/2
UNIVERSAL::isa($langval, "HASH") and exists $langval->{"ct"}
UNIVERSAL::isa($langval, "HASH")
exists $langval->{"ct"}
Truth table0%0/3
A and B
A= UNIVERSAL::isa($langval, "HASH")B= exists $langval->{"ct"}
| |||||||||||||||||||||||||||||
| 103 | 0 | my $ct = $langval->{'ct'}; | |||||||||||||||||||||||||||
| 104 | 0 | my $template_ct = $template->{Object}{'ct'}; | |||||||||||||||||||||||||||
| 105 | 0 | $_upgrade->($ct, $template_ct); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 8 | ▶ | sub simple_diff { | ||||||||||||||||||||||||||
|
Subroutine
simple_diff: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 8 | my($self, $self2) = @_; | |||||||||||||||||||||||||||
| 111 | 8 | require Algorithm::Diff; | |||||||||||||||||||||||||||
| 112 | 8 | my(@ret) = Algorithm::Diff::diff([$self->{Object}], [$self2->{Object}], \&_diff_key); | |||||||||||||||||||||||||||
| 113 | 8 | @ret; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2784 | ▶ | sub _diff_key { | ||||||||||||||||||||||||||
|
Subroutine
_diff_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 117 | 2784 | my($o) = @_; | |||||||||||||||||||||||||||
| 118 | 2784 | ▶ | if (ref $o eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 119 | 448 | my @s; | |||||||||||||||||||||||||||
| 120 | 448 | foreach my $key (sort keys %$o) { | |||||||||||||||||||||||||||
| 121 | 2080 | push @s, $key, _diff_key($o->{$key}); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 448 | "{".join("|", @s)."}"; # XXX may fail if there are "|" in the keys | |||||||||||||||||||||||||||
| 124 | } elsif (ref $o eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 125 | 512 | "[".join("|", map { _diff_key($_) } @$o)."]"; | |||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 1824 | $o; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item find($callback) | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Traverses the content object and calls C<$callback> for each node in | ||||||||||||||||||||||||||||
| 134 | the content tree. The following arguments will be supplied to the | ||||||||||||||||||||||||||||
| 135 | callback: | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =over | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item $object | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | C<$object> is aa reference to the current object. A change to this | ||||||||||||||||||||||||||||
| 142 | reference will also manipulate the original object. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item -parents => [$parent1, $parent2, ...] | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | A list of parent objects. The root object is not in the list. | ||||||||||||||||||||||||||||
| 147 | Descendants are appended to the right, that is, too find the parent | ||||||||||||||||||||||||||||
| 148 | use C<[-1]> as index, the grandfather is C<[-2]> and Adam is C<[0]>. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item -path => $pathstring | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | The C<$pathstring> can be evaluated to access the node. Example: | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | ->{'data'}->[0]->{'type'} | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item -dotted => $dotstring | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Same as C<-path>, but use a dot notation. Example: | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | data.0.type | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item -key => $key | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Only for hash items: C<$keys> is the current key. The value is in | ||||||||||||||||||||||||||||
| 165 | C<$object>. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =back | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | TODO: | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | implement prune | ||||||||||||||||||||||||||||
| 172 | suggest to add something similar to Data::Walker | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 10 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 10 | my($self, $wanted) = @_; | |||||||||||||||||||||||||||
| 178 | 10 | $self->_find($self->{Object}, $wanted, | |||||||||||||||||||||||||||
| 179 | -parents => [], -path => "", -dotted => ""); | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 866 | ▶ | sub _find { | ||||||||||||||||||||||||||
|
Subroutine
_find: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 183 | 866 | my($self, $o, $wanted, %args) = @_; | |||||||||||||||||||||||||||
| 184 | 866 | $wanted->($o, %args); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 866 | my %extra_args; | |||||||||||||||||||||||||||
| 187 | 866 | $extra_args{-parents} = [@{ $args{-parents} }, $o]; | |||||||||||||||||||||||||||
| 188 | 866 | ▶ | if (ref $o eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 189 | 160 | my $ii = 0; | |||||||||||||||||||||||||||
| 190 | 160 | ▶ | my $parent_dotted = $args{-dotted} ne "" ? "$args{-dotted}." : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 160 | foreach my $i (@$o) { | |||||||||||||||||||||||||||
| 192 | $self->_find($i, $wanted, | ||||||||||||||||||||||||||||
| 193 | 206 | -path => $args{-path}."->[$ii]", | |||||||||||||||||||||||||||
| 194 | -dotted => $parent_dotted.$ii, | ||||||||||||||||||||||||||||
| 195 | %extra_args); | ||||||||||||||||||||||||||||
| 196 | 206 | $ii++; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } elsif (ref $o eq 'HASH') { | ||||||||||||||||||||||||||||
| 199 | 140 | my @keys = keys %$o; | |||||||||||||||||||||||||||
| 200 | 140 | ▶ | my $parent_dotted = $args{-dotted} ne "" ? "$args{-dotted}." : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 140 | foreach my $k (@keys) { | |||||||||||||||||||||||||||
| 202 | 650 | my $v = $o->{$k}; | |||||||||||||||||||||||||||
| 203 | $self->_find($v, $wanted, | ||||||||||||||||||||||||||||
| 204 | -key => $k, | ||||||||||||||||||||||||||||
| 205 | 650 | -path => $args{-path}."->{'$k'}", # XXX quote? | |||||||||||||||||||||||||||
| 206 | -dotted => $parent_dotted.$k, | ||||||||||||||||||||||||||||
| 207 | %extra_args); | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 2 | 1; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | __END__ | ||||||||||||||||||||||||||||