Labyrinth/Metadata.pm

filter: Statementstmt 30.0% 34 / 113 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 61.5% 8 / 13 Podpod 100% 5 / 5 totaltotal 23.0% 47 / 204 SCAR 65.0
CC
35
Cov
20%
MetaSearch
306.0
MetaGet
56.0
MetaCloud
56.0
CRAP 662.2
« Labyrinth/MLUtils.pm ↑ Summary Labyrinth/Writer.pm »
1 package Labyrinth::Metadata;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 $VERSION = '5.31';
8  
9 =head1 NAME
10  
11 Labyrinth::Metadata - Metadata Management for Labyrinth.
12  
13 =cut
14  
15 # -------------------------------------
16 # Export Details
17  
18 12 require Exporter;
19 12 @ISA = qw(Exporter);
20  
21 12 %EXPORT_TAGS = (
22 'all' => [ qw( MetaSearch MetaSave MetaGet MetaCloud MetaTags ) ]
23 );
24  
25 12 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26 12 @EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
27  
28 #----------------------------------------------------------------------------
29 # Libraries
30  
31 12 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 ▶use Labyrinth::Globals;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 12 ▶use Labyrinth::DBUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 12 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 12 ▶use HTML::TagCloud;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 #----------------------------------------------------------------------------
39 # Variables
40  
41 # type: 0 = optional, 1 = mandatory
42 # html: 0 = none, 1 = text, 2 = textarea
43  
44 12 my %fields = (
45 title => { type => 1, html => 1 },
46 tagline => { type => 0, html => 1 },
47 );
48  
49 #----------------------------------------------------------------------------
50 # Public Interface Functions
51  
52 =head1 FUNCTIONS
53  
54 =over 4
55  
56 =item MetaSearch(%hash)
57  
58 Provides the IDs for the given metadata search. The sqlkeys are one or more
59 keys into the phrasebook. Requires a hash of parameters:
60  
61 keys => \@sqlkeys
62 meta => \@metadata
63 full => 1
64 limit => $limit
65 order => $order
66 sort => $sort_order
67  
68 'key' and 'meta' are mandatory, all other key/value pairs are optional. If
69 'full' is provided with a non-zero value, a full text search is performed. If
70 a limit is given then only that number of records will be returned if more are
71 available. In order to suitably sort the records you can porovide the ORDER BY
72 string via the 'order' hash key.
73  
74 =item MetaSave
75  
76 Records the metadata with the given sqlkey for the name record id.
77  
78 =item MetaGet
79  
80 Gets the metadata for the given id.
81  
82 =item MetaCloud
83  
84 Returns the XHTML snippet to display a Metadata Tag Cloud.
85  
86 =item MetaTags
87  
88 Returns the list of tags attributed to a entry type and section ids.
89  
90 =back
91  
92 =cut
93  
94 sub MetaSearch {
95 0 ▶ my %hash = @_;
Subroutine MetaSearch: not called CC=17 SCAR=57.2
Pod documented
96  
97 0 ▶ my $keys = $hash{'keys'} || return ();
Condition0%0/2
$hash{'keys'} || (return ())
l!l
00
MC/DC0%0/1
$hash{'keys'} || (return ())
$hash{'keys'}
Truth table0%0/2
A
A= $hash{'keys'}
Aresult
11
00
98 0 my $meta = lc join(",", map {"'$_'"} @{$hash{meta}});
99 0 my $data = lc join("|", @{$hash{meta}});
100 0 ▶ my $full = $hash{'full'} || 0;
Condition0%0/2
$hash{'full'} || 0
l!l
00
MC/DC0%0/1
$hash{'full'} || 0
$hash{'full'}
Truth table0%0/2
A
A= $hash{'full'}
Aresult
11
00
101  
102 0 LogDebug("MetaSearch: keys=[@$keys], meta=[$meta], full=$full");
103  
104 0 ▶ return () unless(@$keys && $meta);
Branch: 1 branch
BranchTrueFalseTotal
unless @$keys and $meta 0 0 0
Condition0%0/3
@$keys and $meta
!ll&&!rl&&r
000
MC/DC0%0/2
@$keys and $meta
@$keys $meta
Truth table0%0/3
A and B
A= @$keysB= $meta
ABresult
0X0
100
111
105  
106 0 ▶ my $where = $hash{'where'} ? "AND $hash{'where'}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$hash{'where'} ? : 0 0 0
107 0 ▶ my $limit = $hash{'limit'} ? "LIMIT $hash{'limit'}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$hash{'limit'} ? : 0 0 0
108 0 ▶ my $order = $hash{'order'} ? "ORDER BY $hash{'order'}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$hash{'order'} ? : 0 0 0
109  
110 0 my %res;
111 0 for my $key (@$keys) {
112 0 ▶ if($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) { } 0 0 0
113 # full text searching
114 0 my @rs = $dbi->GetQuery('hash',"MetaDetail$key",{meta=>$meta, data => $data, where => $where, limit => $limit, order => $order});
115 0 for(@rs) {$res{$_->{id}} = $_};
116 } else {
117 0 my @rs = $dbi->GetQuery('hash',"MetaSearch$key",{meta=>$meta, data => $data, where => $where, limit => $limit, order => $order});
118 0 for(@rs) {$res{$_->{id}} = $_};
119 }
120 }
121  
122 0 my @res;
123 0 ▶ if($hash{'order'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{'order'}) { } 0 0 0
124 0 ▶ if($hash{'order'} eq 'createdate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{'order'} eq 'createdate') { } 0 0 0
125 0 @res = map {$res{$_}} sort {int($res{$a}->{createdate}) <=> int($res{$b}->{createdate})} keys %res;
126 } else {
127 0 @res = map {$res{$_}} sort {$res{$a}->{$hash{'order'}} cmp $res{$b}->{$hash{'order'}}} keys %res;
128 }
129 } else {
130 0 @res = map {$res{$_}} keys %res;
131 }
132  
133 0 ▶ if($hash{'sort'} && $hash{'sort'} =~ /^desc/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"sort"} and $hash{"sort"} =~ /^desc/) 0 0 0
Condition0%0/3
$hash{"sort"} and $hash{"sort"} =~ /^desc/
!ll&&!rl&&r
000
MC/DC0%0/2
$hash{"sort"} and $hash{"sort"} =~ /^desc/
$hash{"sort"} $hash{"sort"} =~ /^desc/
Truth table0%0/3
A and B
A= $hash{"sort"}B= $hash{"sort"} =~ /^desc/
ABresult
0X0
100
111
134 0 @res = reverse @res;
135 }
136  
137 0 ▶ if($hash{'limit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"limit"}) 0 0 0
138 0 splice(@res,$hash{'limit'});
139 }
140  
141 0 return @res;
142 }
143  
144 sub MetaSave {
145 0 ▶ my $id = shift || return;
Subroutine MetaSave: not called CC=5 SCAR=34.0
Pod documented
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
146 0 ▶ my $keys = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
147 0 my @meta = @_;
148  
149 0 LogDebug("MetaSave: $id,[@meta]");
150  
151 0 my $count = 0;
152 0 for my $key (@$keys) {
153 0 $dbi->DoQuery("MetaDelete$key",$id);
154 0 $dbi->DoQuery("MetaUpdate$key",$id,lc($_)) for(@meta);
155 0 $count += scalar(@meta);
156 }
157  
158 0 return $count;
159 }
160  
161 sub MetaGet {
162 0 ▶ my ($id,$key) = @_;
Subroutine MetaGet: not called CC=7 SCAR=40.3
Pod documented
163  
164 0 ▶ if($id && $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $key) 0 0 0
Condition0%0/3
$id and $key
!ll&&!rl&&r
000
MC/DC0%0/2
$id and $key
$id $key
Truth table0%0/3
A and B
A= $idB= $key
ABresult
0X0
100
111
165 0 my @meta;
166 0 my @rows = $dbi->GetQuery('array',"MetaGet$key",$id);
167 0 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) 0 0 0
168 0 push @meta, $_->[1] for(@rows);
169 0 ▶ return @meta if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
170 0 return join(" ",sort @meta);
171 }
172 }
173  
174 0 ▶ return () if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
175 0 return;
176 }
177  
178 sub MetaCloud {
179 0 ▶ my %hash = @_;
Subroutine MetaCloud: not called CC=7 SCAR=40.3
Pod documented
180  
181 0 ▶ my $key = $hash{'key'} || return;
Condition0%0/2
$hash{'key'} || (return)
l!l
00
MC/DC0%0/1
$hash{'key'} || (return)
$hash{'key'}
Truth table0%0/2
A
A= $hash{'key'}
Aresult
11
00
182 0 ▶ my $sectionid = $hash{'sectionid'} || return;
Condition0%0/2
$hash{'sectionid'} || (return)
l!l
00
MC/DC0%0/1
$hash{'sectionid'} || (return)
$hash{'sectionid'}
Truth table0%0/2
A
A= $hash{'sectionid'}
Aresult
11
00
183 0 ▶ my $actcode = $hash{'actcode'} || return;
Condition0%0/2
$hash{'actcode'} || (return)
l!l
00
MC/DC0%0/1
$hash{'actcode'} || (return)
$hash{'actcode'}
Truth table0%0/2
A
A= $hash{'actcode'}
Aresult
11
00
184  
185 0 ▶ my $path = $settings{'urlmap-'.$actcode} || "$tvars{cgipath}/pages.cgi?act=$actcode&amp;data=";
Condition0%0/2
$Labyrinth::Metadata::settings{'urlmap-' . $actcode} || "$Labyrinth::Metadata::tvars{'cgipath'}/pages.cgi?act=$actcode&amp;data="
l!l
00
MC/DC0%0/1
$Labyrinth::Metadata::settings{'urlmap-' . $actcode} || "$Labyrinth::Metadata::tvars{'cgipath'}/pages.cgi?act=$actcode&amp;data="
$Labyrinth::Metadata::settings{'urlmap-' . $actcode}
Truth table0%0/2
A
A= $Labyrinth::Metadata::settings{'urlmap-' . $actcode}
Aresult
11
00
186  
187 0 my $cloud = HTML::TagCloud->new(levels=>10);
188 0 my @rsa = $dbi->GetQuery('hash',"MetaCloud$key",{ids => $sectionid});
189 0 for(@rsa) {
190 0 $cloud->add($_->{metadata}, $path . $_->{metadata}, $_->{count});
191 }
192  
193 0 my $html = $cloud->html();
194 0 ▶ while($html =~ m!((<a href="$path)([^"]+)">)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /((<a href="$path)([^"]+)">)/) 0 0 0
195 0 my ($href,$link1,$link2) = ($1,$2,$3);
196 0 $html =~ s!$href!$link1$link2" title="Meta search for '$link2'">!sgi;
197 }
198  
199 0 return $html;
200 }
201  
202 sub MetaTags {
203 0 ▶ my %hash = @_;
Subroutine MetaTags: not called CC=3 SCAR=24.8
Pod documented
204  
205 0 ▶ my $key = $hash{'key'} || return;
Condition0%0/2
$hash{'key'} || (return)
l!l
00
MC/DC0%0/1
$hash{'key'} || (return)
$hash{'key'}
Truth table0%0/2
A
A= $hash{'key'}
Aresult
11
00
206 0 ▶ my $sectionid = $hash{'sectionid'} || return;
Condition0%0/2
$hash{'sectionid'} || (return)
l!l
00
MC/DC0%0/1
$hash{'sectionid'} || (return)
$hash{'sectionid'}
Truth table0%0/2
A
A= $hash{'sectionid'}
Aresult
11
00
207  
208 0 my @rows = $dbi->GetQuery('hash',"MetaCloud$key",{ids => $sectionid});
209 0 my @tags = map {$_->{metadata}} @rows;
210 0 return @tags;
211 }
212  
213 12 1;
214  
215 __END__
216  
217 =head1 SEE ALSO
218  
219 Labyrinth
220  
221 =head1 AUTHOR
222  
223 Barbie, <barbie@missbarbell.co.uk> for
224 Miss Barbell Productions, L<http://www.missbarbell.co.uk/>
225  
226 =head1 COPYRIGHT & LICENSE
227  
228 Copyright (C) 2002-2015 Barbie for Miss Barbell Productions
229 All Rights Reserved.
230  
231 This module is free software; you can redistribute it and/or
232 modify it under the Artistic License 2.0.
233  
234 =cut
« Labyrinth/MLUtils.pm ↑ Summary Labyrinth/Writer.pm »