| 1 | package Data::Foswiki; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | our @EXPORT_OK = qw(serialise deserialise); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Data::Foswiki - Read and Write Foswiki topics | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Version 0.02 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Quickly read and write Foswiki topics into a hash | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use Data::Foswiki; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | #read | ||||||||||||||||||||||||||||
| 29 | my $fh; | ||||||||||||||||||||||||||||
| 30 | open($fh, '<', '/var/lib/foswiki/data/System/FAQSimultaneousEdits.txt') or die 'open failure'; | ||||||||||||||||||||||||||||
| 31 | my @topic_text = <$fh>; | ||||||||||||||||||||||||||||
| 32 | close($fh); | ||||||||||||||||||||||||||||
| 33 | my $topic = Data::Foswiki::Test2::deserialise(@topic_text); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | $topic->{TOPICINFO}{author} = 'NewUser'; | ||||||||||||||||||||||||||||
| 36 | $topic->{PARENT}{name} = 'WebHome'; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $topic->{TEXT} = "Some new text\n\n".$topic->{TEXT}; | ||||||||||||||||||||||||||||
| 39 | undef $topic->{TOPICMOVED}; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | $topic->{FIELD}{TopicTitle}{attributes} = 'H'; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | #add a new field that is not part of the form definition - if edited within foswiki, it willbe removed | ||||||||||||||||||||||||||||
| 44 | #but its useful for importing | ||||||||||||||||||||||||||||
| 45 | $topic->{FIELD}{NewField}{value} = 'test'; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | #write | ||||||||||||||||||||||||||||
| 48 | open($fh, '>', '/var/lib/foswiki/data/System/FAQNewFaq.txt') or die 'write failure'; | ||||||||||||||||||||||||||||
| 49 | print $fh Data::Foswiki::Test::serialise($topic); | ||||||||||||||||||||||||||||
| 50 | close($fh); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 deserialise($text|@stringarray) -> $hash_ref | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Parse a string, or array of strings and convert into a hash of the Foswiki topic's data | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | (apparently Perl can be faster reading a file into an array) | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | if you pass in an undef / empty string, you will get undef back | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | our $isValidEmbedding_func; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | my $METAINFOregex = qr/^\%META:(TOPICINFO){(.*)}\%\n?$/o; | |||||||||||||||||||||||||||
| 67 | 2 | my $METAPARENTregex = qr/^\%META:(TOPICPARENT){(.*)}\%\n?$/o; | |||||||||||||||||||||||||||
| 68 | 2 | my $METAregex = qr/^\%META:(\S*){(.*)}\%\n?$/o; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 7 | ▶ | sub deserialise { | ||||||||||||||||||||||||||
|
Subroutine
deserialise: called CC=29 SCAR=35.2
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | # use Data::Dumper; | ||||||||||||||||||||||||||||
| 72 | # die 'deserialise'.Dumper($_[0]) if (ref(\$_[0]) ne 'STRING'); | ||||||||||||||||||||||||||||
| 73 | 7 | my $topic; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 7 | ▶ | return $topic unless ( $#_ >= 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #convert a string into an array | ||||||||||||||||||||||||||||
| 78 | 5 | ▶ | if ( $#_ == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | return $topic if ( $_[0] eq '' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | if ( $_[0] =~ /\n/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | my @lines = split( /\n/, $_[0] ); | |||||||||||||||||||||||||||
| 82 | #split will not give you an empty trailing array element if \n is the last char in the string | ||||||||||||||||||||||||||||
| 83 | #TODO: do i really need to make a copy of the array? | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | push(@lines, '') if ($_[0] =~ m/\n$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1 | return deserialise( @lines ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | my $start = 0; | |||||||||||||||||||||||||||
| 90 | 4 | my $end = -1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #I can test $_[$start] rather than defined($_[$start]) | ||||||||||||||||||||||||||||
| 93 | # because an empty line still would not match the regex | ||||||||||||||||||||||||||||
| 94 | # first get rid of the leading META | ||||||||||||||||||||||||||||
| 95 | 4 | ▶ | if ( $_[$start] && $_[$start] =~ $METAINFOregex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[$start] and $_[$start] =~ /$METAINFOregex/
MC/DC50%1/2
$_[$start] and $_[$start] =~ /$METAINFOregex/
$_[$start]
$_[$start] =~ /$METAINFOregex/
Truth table66%2/3
A and B
A= $_[$start]B= $_[$start] =~ /$METAINFOregex/
| |||||||||||||||||||||||||||||
| 96 | 2 | my $hash = _readKeyValues($2); | |||||||||||||||||||||||||||
| 97 | 2 | ▶ | if (!$isValidEmbedding_func || &$isValidEmbedding_func(undef, $1, $hash)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $Data::Foswiki::isValidEmbedding_func or &$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
MC/DC0%0/2
not $Data::Foswiki::isValidEmbedding_func or &$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
not $Data::Foswiki::isValidEmbedding_func
&$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
Truth table33%1/3
not(A) or B
A= not $Data::Foswiki::isValidEmbedding_funcB= &$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
| |||||||||||||||||||||||||||||
| 98 | 2 | $topic->{$1} = $hash; | |||||||||||||||||||||||||||
| 99 | 2 | $start++; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #turns out that the trailing newline removeal code in LegacyMeta is terrible | ||||||||||||||||||||||||||||
| 104 | # it removes a trailing newline even when there is a TOPICPARENT, and when there are rejected META's too | ||||||||||||||||||||||||||||
| 105 | 4 | my $trailingMeta; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | if ( $_[$start] && $_[$start] =~ $METAPARENTregex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[$start] and $_[$start] =~ /$METAPARENTregex/
MC/DC100%2/2
$_[$start] and $_[$start] =~ /$METAPARENTregex/
$_[$start]
$_[$start] =~ /$METAPARENTregex/
Truth table100%3/3
A and B
A= $_[$start]B= $_[$start] =~ /$METAPARENTregex/
| |||||||||||||||||||||||||||||
| 108 | 1 | my $hash = _readKeyValues($2); | |||||||||||||||||||||||||||
| 109 | 1 | $trailingMeta++; | |||||||||||||||||||||||||||
| 110 | 1 | ▶ | if (!$isValidEmbedding_func || &$isValidEmbedding_func(undef, $1, $hash)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $Data::Foswiki::isValidEmbedding_func or &$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
MC/DC0%0/2
not $Data::Foswiki::isValidEmbedding_func or &$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
not $Data::Foswiki::isValidEmbedding_func
&$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
Truth table33%1/3
not(A) or B
A= not $Data::Foswiki::isValidEmbedding_funcB= &$Data::Foswiki::isValidEmbedding_func(undef, $1, $hash)
| |||||||||||||||||||||||||||||
| 111 | 1 | $topic->{$1} = $hash; | |||||||||||||||||||||||||||
| 112 | 1 | $start++; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | #then the trailing META | ||||||||||||||||||||||||||||
| 117 | 4 | ▶ | while ( $_[$end] && $_[$end] =~ $METAregex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[$end] and $_[$end] =~ /$METAregex/
MC/DC50%1/2
$_[$end] and $_[$end] =~ /$METAregex/
$_[$end]
$_[$end] =~ /$METAregex/
Truth table66%2/3
A and B
A= $_[$end]B= $_[$end] =~ /$METAregex/
| |||||||||||||||||||||||||||||
| 118 | #LegacyMeta compatibility hack :/ | ||||||||||||||||||||||||||||
| 119 | 8 | $trailingMeta++; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #should skip any TOPICINFO & TOPICPARENT, they are _only_ valid in one place in the file. | ||||||||||||||||||||||||||||
| 122 | 8 | ▶ | last if ( ( $1 eq 'TOPICINFO' ) || ( $1 eq 'TOPICPARENT' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$1 eq "TOPICINFO" or $1 eq "TOPICPARENT"
MC/DC50%1/2
$1 eq "TOPICINFO" or $1 eq "TOPICPARENT"
$1 eq "TOPICINFO"
$1 eq "TOPICPARENT"
Truth table66%2/3
A or B
A= $1 eq "TOPICINFO"B= $1 eq "TOPICPARENT"
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 7 | my $meta = _readKeyValues($2); | |||||||||||||||||||||||||||
| 125 | 7 | ▶ | if ($isValidEmbedding_func && ! &$isValidEmbedding_func(undef, $1, $meta)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Data::Foswiki::isValidEmbedding_func and not &$Data::Foswiki::isValidEmbedding_func(undef, $1, $meta)
MC/DC0%0/2
$Data::Foswiki::isValidEmbedding_func and not &$Data::Foswiki::isValidEmbedding_func(undef, $1, $meta)
$Data::Foswiki::isValidEmbedding_func
not &$Data::Foswiki::isValidEmbedding_func(undef, $1, $meta)
Truth table33%1/3
A and not(B)
A= $Data::Foswiki::isValidEmbedding_funcB= not &$Data::Foswiki::isValidEmbedding_func(undef, $1, $meta)
| |||||||||||||||||||||||||||||
| 126 | 0 | last; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | #I had hoped that we only removed the newlines if there was valid trailing meta... but no | ||||||||||||||||||||||||||||
| 130 | 7 | $trailingMeta++; | |||||||||||||||||||||||||||
| 131 | 7 | $end--; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 7 | ▶ | if ( $1 eq 'FORM' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 1 | $topic->{$1} = $meta; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 6 | ▶ | if ( exists( $meta->{name} ) && $1 ne 'FORM' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $meta->{"name"} and $1 ne "FORM"
MC/DC50%1/2
exists $meta->{"name"} and $1 ne "FORM"
exists $meta->{"name"}
$1 ne "FORM"
Truth table66%2/3
A and B
A= exists $meta->{"name"}B= $1 ne "FORM"
| |||||||||||||||||||||||||||||
| 138 | 5 | $topic->{$1}{ $meta->{name} } = $meta; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||
| 141 | 1 | $topic->{$1} = $meta; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | #there is an extra newline added between TEXT and any trailing meta | ||||||||||||||||||||||||||||
| 147 | 4 | ▶ | $end-- if ( $trailingMeta && $_[$end] =~ /^\n?$/o ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$trailingMeta and $_[$end] =~ /^\n?$/o
MC/DC100%2/2
$trailingMeta and $_[$end] =~ /^\n?$/o
$trailingMeta
$_[$end] =~ /^\n?$/o
Truth table100%3/3
A and B
A= $trailingMetaB= $_[$end] =~ /^\n?$/o
| |||||||||||||||||||||||||||||
| 148 | #$end-- if ( $_[$end] =~ /^\n?$/o ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 4 | ▶ | if ( defined($_[$start]) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | #TODO: not joining and just returning an arrayref is very much faster | ||||||||||||||||||||||||||||
| 153 | #but leaves the user to work out if there are \n's | ||||||||||||||||||||||||||||
| 154 | #perhaps this is a reson to wrap it in a class and provide a text() :/ | ||||||||||||||||||||||||||||
| 155 | $topic->{TEXT} = | ||||||||||||||||||||||||||||
| 156 | 3 | ▶ | join( ( ( $_[$start] =~ /\n/o ) ? '' : "\n" ), @_[ $start .. $#_ + $end + 1 ] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | #I'm not 100% sure about this, but if there's no trailing META, the unit tests suggest we need to add a \n | ||||||||||||||||||||||||||||
| 158 | #if ($end == -1) { | ||||||||||||||||||||||||||||
| 159 | # $topic->{TEXT} .= "\n"; | ||||||||||||||||||||||||||||
| 160 | #} | ||||||||||||||||||||||||||||
| 161 | #OMG THIS IS SO CRAP. | ||||||||||||||||||||||||||||
| 162 | 3 | ▶ | if (!$trailingMeta && $topic->{TEXT} =~ /^(%META:([^{]+){(.*)}%\n)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $trailingMeta and $topic->{"TEXT"} =~ /^(%META:([^{]+){(.*)}%\n)/
MC/DC0%0/2
not $trailingMeta and $topic->{"TEXT"} =~ /^(%META:([^{]+){(.*)}%\n)/
not $trailingMeta
$topic->{"TEXT"} =~ /^(%META:([^{]+){(.*)}%\n)/
Truth table66%2/3
not(A) and B
A= not $trailingMetaB= $topic->{"TEXT"} =~ /^(%META:([^{]+){(.*)}%\n)/
| |||||||||||||||||||||||||||||
| 163 | 0 | $topic->{TEXT} =~ s/\n$//s; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 4 | return $topic; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 serialise($hashref) -> string | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Serialise into a foswiki 'embedded' formatted string, ready for writing to disk. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Note: this does not take care of updating the topic revision and date data | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | ▶ | sub serialise { | ||||||||||||||||||||||||||
|
Subroutine
serialise: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 1 | my $topic = shift; | |||||||||||||||||||||||||||
| 179 | 1 | my @ordered_keys = qw/TOPICINFO TOPICPARENT TEXT FORM TOPICMOVED FIELD/; | |||||||||||||||||||||||||||
| 180 | 1 | my @topic_keys = keys(%$topic); | |||||||||||||||||||||||||||
| 181 | #use Data::Dumper; | ||||||||||||||||||||||||||||
| 182 | #print STDERR ">>>+>>>>>".Dumper($topic)."<<<<<<<\n"; | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | #I thought there was an extra \n added.. | ||||||||||||||||||||||||||||
| 185 | 1 | my $key_count = $#topic_keys; | |||||||||||||||||||||||||||
| 186 | 1 | my @text; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | my %done; | ||||||||||||||||||||||||||||
| 189 | 1 | foreach my $type ( @ordered_keys, @topic_keys ) { | |||||||||||||||||||||||||||
| 190 | 7 | ▶ | last if ($key_count < 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 6 | ▶ | if ( !$done{$type} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 6 | $done{$type} = 1; | |||||||||||||||||||||||||||
| 193 | 6 | ▶ | next unless (exists($topic->{$type})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | #use Data::Dumper; | ||||||||||||||||||||||||||||
| 195 | #print STDERR ">>>>>>>>".Dumper($topic->{$type})."<<<<<<<\n"; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 6 | $key_count--; | |||||||||||||||||||||||||||
| 198 | 6 | ▶ | if ( $type eq 'TEXT' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | #print STDERR "TEXT == ".ref($topic->{TEXT})."\n"; | ||||||||||||||||||||||||||||
| 200 | 1 | push( @text, $topic->{TEXT} ); | |||||||||||||||||||||||||||
| 201 | 1 | ▶ | push( @text, '') if ( $key_count >= 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | else { | ||||||||||||||||||||||||||||
| 204 | 5 | ▶ | next unless (keys(%{$topic->{$type}})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 5 | push( @text, _writeMeta( $type, $topic->{$type} ) ); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | #TODO: how about using wantarray to avoid the join? | ||||||||||||||||||||||||||||
| 211 | 1 | return join( "\n", @text ); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | #from Foswiki::Meta | ||||||||||||||||||||||||||||
| 215 | # STATIC Build a hash by parsing name=value comma separated pairs | ||||||||||||||||||||||||||||
| 216 | # SMELL: duplication of Foswiki::Attrs, using a different | ||||||||||||||||||||||||||||
| 217 | # system of escapes :-( | ||||||||||||||||||||||||||||
| 218 | 10 | ▶ | sub _readKeyValues { | ||||||||||||||||||||||||||
|
Subroutine
_readKeyValues: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 219 | 10 | my @arr = split( /="([^"]*)"\s*/, $_[0] ); | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | #if the last attribute is an empty string, we're a bit naf | ||||||||||||||||||||||||||||
| 222 | 10 | my $count = $#arr; | |||||||||||||||||||||||||||
| 223 | 10 | ▶ | push( @arr, '' ) unless ( $count % 2 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 10 | my $res; | |||||||||||||||||||||||||||
| 225 | 10 | ▶ | for ( my $i = 1 ; $i <= $count ; $i = $i + 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | $arr[$i] =~ s/%([\da-f]{2})/chr(hex($1))/geio; | |||||||||||||||||||||||||||
| 227 | 33 | $res->{ $arr[ $i - 1 ] } = $arr[$i]; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 10 | return $res; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 10 | ▶ | sub _writeMeta { | ||||||||||||||||||||||||||
|
Subroutine
_writeMeta: called CC=9 SCAR=22.5
| |||||||||||||||||||||||||||||
| 234 | 10 | my $type = shift; | |||||||||||||||||||||||||||
| 235 | 10 | my $hash = shift; | |||||||||||||||||||||||||||
| 236 | 10 | my $string = ''; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 10 | ▶ | while(my ($k, $v) = each(%$hash)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 14 | ▶ | if (ref($v) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 5 | ▶ | $string .= "\n" if ($string ne ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 5 | $string .= _writeMeta($type, $v); | |||||||||||||||||||||||||||
| 242 | } else { | ||||||||||||||||||||||||||||
| 243 | #not a multi-value META (ie, TOPICINFO, TOPICPARENT, FORM) | ||||||||||||||||||||||||||||
| 244 | 9 | last; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 10 | ▶ | if ($string eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 9 | $string .= '%META:' . $type . '{'; | |||||||||||||||||||||||||||
| 250 | 9 | ▶ | $string .= 'name="'.$hash->{name}.'" ' if (defined($hash->{name})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 9 | foreach (keys %$hash) { | |||||||||||||||||||||||||||
| 252 | 29 | ▶ | next if ($_ eq 'name'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 22 | $string .= $_.'="'._dataEncode( $hash->{$_} ).'" '; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | #chop($string); | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 9 | $string.= '}%'; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | #use Data::Dumper; | ||||||||||||||||||||||||||||
| 260 | #print STDERR ":::::".scalar(keys(%$hash))."::".Dumper($hash)."\n$string\n"; | ||||||||||||||||||||||||||||
| 261 | 10 | return $string; | |||||||||||||||||||||||||||
| 262 | ##################old code | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | my @elements = _writeKeyValues( $type, $hash ); | |||||||||||||||||||||||||||
| 265 | #use Data::Dumper; | ||||||||||||||||||||||||||||
| 266 | #die "=====$type --".Dumper($hash)."--".Dumper(\@elements)."--\n" if (!defined($elements[0])); | ||||||||||||||||||||||||||||
| 267 | 0 | ▶ | unless ( $elements[0] =~ /^%META/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 0 | return '%META:' . $type . '{' . join( ' ', @elements ) . '}%'; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 0 | return @elements; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | sub _writeKeyValues { | ||||||||||||||||||||||||||
|
Subroutine
_writeKeyValues: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 274 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 275 | 0 | my $hash = shift; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | my $name; | |||||||||||||||||||||||||||
| 278 | return map { | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 0 | ▶ | if ( ref( $hash->{$_} ) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | #META:TYPE{name=} hash of entries | ||||||||||||||||||||||||||||
| 283 | 0 | _writeMeta( $type, $hash->{$_} ); | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | else { | ||||||||||||||||||||||||||||
| 286 | 0 | $_ . '="' . _dataEncode( $hash->{$_} ) . '"'; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | 0 | } keys( %{$hash} ); | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | sub _dataDecode { | ||||||||||||||||||||||||||
|
Subroutine
_dataDecode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 292 | 0 | my $datum = shift; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | $datum =~ s/%([\da-f]{2})/chr(hex($1))/gei; | |||||||||||||||||||||||||||
| 295 | 0 | return $datum; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 22 | ▶ | sub _dataEncode { | ||||||||||||||||||||||||||
|
Subroutine
_dataEncode: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 299 | 22 | my $datum = shift; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | $datum =~ s/([%"\r\n{}])/'%'.sprintf('%02x',ord($1))/ge; | |||||||||||||||||||||||||||
| 302 | 22 | return $datum; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Sven Dowideit, C<< <SvenDowideit at fosiki.com> >> | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head1 BUGS | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Please report any bugs or feature requests to C<bug-data-foswiki at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 312 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Foswiki>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 313 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Foswiki support can be found in the #foswiki irc channel on L<irc://irc.freenode.net>, | ||||||||||||||||||||||||||||
| 321 | or from SvenDowideit L<mailto:SvenDowideit@fosiki.com> | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =head1 TO DO | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | make an XS version, and try a few different approaches to parsing and then benchmark them | ||||||||||||||||||||||||||||
| 329 | this would mean making this module into a facade to the other implementations. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | is it faster not to modify the array? (just keep start and end Text indexes?) | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | Copyright 2012 Sven Dowideit SvenDowideit@fosiki.com. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 338 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 339 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =cut | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 2 | 1; # End of Data::Foswiki::Test | |||||||||||||||||||||||||||