Marky/DbTable.pm

filter: Statementstmt 13.3% 68 / 510 Branchbran 7.8% 17 / 218 Conditioncond 1.5% 1 / 66 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 36.1% 13 / 36 Podpod 100% 7 / 7 totaltotal 12.1% 106 / 871 CC 145 SCAR 60.3
CC
145
Cov
11%
_query_to_sql
1560.0
_build_where
380.0
_format_tag_collection
210.0
CRAP 417.7
↑ Summary Marky.pm »
1 package Marky::DbTable;
2 3 $Marky::DbTable::VERSION = '0.0604';
3 #ABSTRACT: Marky::DbTable - querying one database table
4  
5 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Search::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Sort::Naturally;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use HTML::TagCloud;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
19 1 my $class = shift;
20 1 my %parameters = (@_);
21 1 ▶ my $self = bless ({%parameters}, ref ($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
22  
23 1 $self->_set_defaults();
24  
25 1 return ($self);
26 } # new
27  
28  
29 0 ▶sub query_raw {
Subroutine query_raw: not called CC=2 SCAR=17.9
Pod documented
30 0 my $self = shift;
31 0 my %args = @_;
32  
33 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
34 {
35 0 return undef;
36 }
37  
38 0 my $data = $self->_search(%args);
39 0 return $data;
40 } # query_raw
41  
42  
43 0 ▶sub query {
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
44 0 my $self = shift;
45 0 my %args = @_;
46  
47 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
48 {
49 0 return undef;
50 }
51  
52 0 return $self->_process_request(%args);
53 } # query
54  
55  
56 0 ▶sub taglist {
Subroutine taglist: not called CC=2 SCAR=17.9
Pod documented
57 0 my $self = shift;
58 0 my %args = @_;
59  
60 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
61 {
62 0 return undef;
63 }
64  
65 0 return $self->_process_taglist(%args);
66 } # taglist
67  
68  
69 0 ▶sub tagcloud {
Subroutine tagcloud: not called CC=2 SCAR=17.9
Pod documented
70 0 my $self = shift;
71 0 my %args = @_;
72  
73 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
74 {
75 0 return undef;
76 }
77  
78 0 return $self->_process_tagcloud(%args);
79 } # tagcloud
80  
81  
82 0 ▶sub total_records {
Subroutine total_records: not called CC=2 SCAR=17.9
Pod documented
83 0 my $self = shift;
84 0 my %args = @_;
85  
86 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
87 {
88 0 return undef;
89 }
90  
91 0 return $self->_total_records(%args);
92 } # total_records
93  
94  
95 0 ▶sub what_error {
Subroutine what_error: not called CC=1 SCAR=6.9
Pod documented
96 0 my $self = shift;
97 0 my %args = @_;
98  
99 0 return $self->{error};
100 } # what_error
101  
102  
103 1 ▶sub _set_defaults {
Subroutine _set_defaults: called CC=18 SCAR=33.8
104 1 my $self = shift;
105  
106 1 ▶ $self->{route_prefix} = '' if !defined $self->{route_prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"route_prefix"} 1 0 1
107  
108 1 ▶ $self->{user} = '' if !defined $self->{user};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"user"} 1 0 1
109 1 ▶ $self->{password} = '' if !defined $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"password"} 1 0 1
110  
111 1 ▶ if (!defined $self->{database})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"database"}) 0 1 1
112 {
113 0 die "No database given";
114 }
115 1 ▶ if (!defined $self->{table})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"table"}) 0 1 1
116 {
117 0 die "No table given";
118 }
119 1 ▶ if (!defined $self->{columns})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"columns"}) 0 1 1
120 {
121 0 die "No columns given";
122 }
123 1 ▶ if (!defined $self->{sort_columns})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"sort_columns"}) 1 0 1
124 {
125 1 $self->{sort_columns} = $self->{columns};
126 }
127 1 ▶ $self->{tagfield} = 'tags' if !defined $self->{tagfield};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tagfield"} 1 0 1
128 1 ▶ $self->{default_limit} = 100 if !defined $self->{default_limit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"default_limit"} 1 0 1
129  
130 1 ▶ if (!defined $self->{row_template})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"row_template"}) 0 1 1
131 {
132 0 $self->{row_template} =<<'EOT';
133 <li>
134 <div class="linkcontainer">
135 <span class="linktitle">{$title}</span>
136 <div class="linkdescription">
137 {?description [$description:html]}
138 </div>
139 {?all_tags <div class="linktaglist">[$all_tags]</div>}
140 </div>
141 </li>
142 EOT
143 }
144  
145 1 ▶ if (!defined $self->{tags_template})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"tags_template"}) 1 0 1
146 {
147 1 $self->{tags_template} =<<'EOT';
148 <a href="{$url}/{?tags_query [$tags_query]}{?qquery ?[$qquery]}" class="tag {?not_in_list button}">{?not_in_list <span class="fa fa-tag"></span>} {$tag_label}{?num_tags ([$num_tags])}</a>
149 EOT
150 }
151 1 ▶ if (!defined $self->{tag_query_template})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"tag_query_template"}) 1 0 1
152 {
153 1 $self->{tag_query_template} =<<'EOT';
154 <a title="Remove tag" href="{$url}/{?tags_query [$tags_query]}?deltag={$tag}{?q &q=[$q]}{?p &p=[$p]}{?n &n=[$n]}{?sort_by &sort_by=[$sort_by]}{?sort_by2 &sort_by2=[$sort_by2]}{?sort_by3 &sort_by3=[$sort_by3]}{?sort_by4 &sort_by4=[$sort_by4]}{?where &where=[$where]}" class="tag button"><span class="fa fa-tag"></span> {$tag} <span class="remove fa fa-remove"></span></a>
155 EOT
156 }
157 1 ▶ if (!defined $self->{q_query_template})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"q_query_template"}) 1 0 1
158 {
159 1 $self->{q_query_template} =<<'EOT';
160 <a title="Remove term" href="{$url}/{?tags_query tags/[$tags_query]}?delterm={$qterm}{?q &q=[$q]}{?p &p=[$p]}{?n &n=[$n]}{?sort_by &sort_by=[$sort_by]}{?sort_by2 &sort_by2=[$sort_by2]}{?sort_by3 &sort_by3=[$sort_by3]}{?sort_by4 &sort_by4=[$sort_by4]}{?where &where=[$where]}" class="tag button"><span class="fa fa-question"></span> {$qterm} <span class="remove fa fa-close"></span></a>
161 EOT
162 }
163 1 ▶ if (!defined $self->{results_template})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"results_template"}) 1 0 1
164 {
165 1 $self->{results_template} =<<'EOT';
166 {?searchform [$searchform]}
167 {?pagination [$pagination]}
168 {?total <p>[$total] records found. Page [$p] of [$num_pages].</p>}
169 {?query <div class="query">[$query]</div>}
170 {?sql <p class="sql">[$sql]</p>}
171 {?result <div class="results fancy">[$result]</div>}
172 EOT
173 }
174 1 ▶ if (!defined $self->{pagination_template})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"pagination_template"}) 1 0 1
175 {
176 1 $self->{pagination_template} =<<'EOT';
177 <div class="pagination">
178 <span class="prev">{?prev_page <a title="Prev" class="prevnext" href="[$location]/[$tq]?p=[$prev_page]&n=[$n]&sort_by=[$sort_by]&sort_by2=[$sort_by2]&sort_by3=[$sort_by3]&sort_by4=[$sort_by4]&q=[$q]&where=[$where]">}<span class="fa fa-chevron-left"></span> Prev{?prev_page </a>}</span>
179 <span class="next">{?next_page <a title="Next" href="[$location]/[$tq]?p=[$next_page]&n=[$n]&sort_by=[$sort_by]&sort_by2=[$sort_by2]&sort_by3=[$sort_by3]&sort_by4=[$sort_by4]&q=[$q]&where=[$where]">}Next <span class="fa fa-chevron-right"></span>{?next_page </a>}</span>
180 </div>
181 EOT
182 }
183 1 ▶ if (!defined $self->{searchform})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"searchform"}) 1 0 1
184 {
185 # Note that this is a single-quotes here-document
186 # because I don't want to have to wrangle with escaping
187 # all the quotes and $ stuff.
188 # Therefore I'm putting an explicit placemarker for WHERE
189  
190 1 $self->{searchform} =<<'EOT';
191 <div class="searchform">
192 <form class="searcher" action="{$action}">
193 <span class="tagsin"><label class="fa fa-tags">Tags:</label> <input type="text" name="tags" value="{$tags}"></span>
194 <span class="anyin"><label class="fa fa-question">Any:</label> <input type="text" name="q" value="{$q}"/></span>
195 <span class="selector"><label>Pg:</label> {$selectP}</span>
196 WHERE
197 <span class="selector"><label>N:</label> {$selectN}</span>
198 <span class="selector"><label>Sort:</label> {$sorting}</span>
199 <input type="submit" value="Search">
200 </form></div>
201 EOT
202 # Replace the WHERE placemarker
203 1 my $whereness = '';
204 1 ▶ if ($self->{use_where})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_where"}) 0 1 1
205 {
206 0 $whereness =<<'EOW';
207 <div class="where"><label>Where:</label><textarea name="where" rows="3" cols="80">{$where}</textarea></div>
208 EOW
209 }
210 1 $self->{searchform} =~ s/WHERE/${whereness}/;
211 }
212 1 return $self;
213  
214 } # _set_defaults
215  
216  
217 0 ▶sub _connect {
Subroutine _connect: not called CC=5 SCAR=34.0
218 0 my $self = shift;
219  
220 0 my $old_dbh = $self->{dbh};
221 0 ▶ if ($old_dbh)
Branch: 1 branch
BranchTrueFalseTotal
if ($old_dbh) 0 0 0
222 {
223 0 return 1;
224 }
225  
226 # The database is either a DSN (data source name)
227 # or a file name. If it's a file name, assume it's SQLite
228 0 my $database = $self->{database};
229 0 ▶ if ($database)
Branch: 1 branch
BranchTrueFalseTotal
if ($database) { } 0 0 0
230 {
231 0 my $dsn = $database;
232 0 my $user = $self->{user};
233 0 my $pw = $self->{password};
234 0 ▶ if (-f $database)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $database) 0 0 0
235 {
236 0 $dsn = "dbi:SQLite:dbname=$database";
237 }
238 0 my $dbh = DBI->connect($dsn, $user, $pw);
239 0 ▶ if (!$dbh)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 0 0
240 {
241 0 $self->{error} = "Can't connect to $database $DBI::errstr";
242 0 return 0;
243 }
244 0 $self->{dbh} = $dbh;
245 }
246 else
247 {
248 0 $self->{error} = "No Database given." . Dump($self);
249 0 return 0;
250 }
251  
252 0 return 1;
253 } # _connect
254  
255  
256 0 ▶sub _search {
Subroutine _search: not called CC=10 SCAR=47.0
257 0 my $self = shift;
258 0 my %args = @_;
259  
260 0 my $dbh = $self->{dbh};
261  
262 # first find the total
263 0 my $q = $self->_query_to_sql(%args,get_total=>1);
264 0 my $sth = $dbh->prepare($q);
265 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
266 {
267 0 $self->{error} = "FAILED to prepare '$q' $DBI::errstr";
268 0 return undef;
269 }
270 0 my $ret = $sth->execute();
271 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
272 {
273 0 $self->{error} = "FAILED to execute '$q' $DBI::errstr";
274 0 return undef;
275 }
276 0 my @ret_rows=();
277 0 my $total = 0;
278 0 my @row;
279 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
280 {
281 0 $total = $row[0];
282 }
283 0 my $num_pages = 1;
284 0 ▶ if ($args{n})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"n"}) 0 0 0
285 {
286 0 $num_pages = ceil($total / $args{n});
287 0 ▶ $num_pages = 1 if $num_pages < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_pages < 1 0 0 0
288 }
289  
290 0 ▶ if ($total > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($total > 0) 0 0 0
291 {
292 0 $q = $self->_query_to_sql(%args,total=>$total);
293 0 $sth = $dbh->prepare($q);
294 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
295 {
296 0 $self->{error} = "FAILED to prepare '$q' $DBI::errstr";
297 0 return undef;
298 }
299 0 $ret = $sth->execute();
300 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
301 {
302 0 $self->{error} = "FAILED to execute '$q' $DBI::errstr";
303 0 return undef;
304 }
305  
306 0 ▶ while (my $hashref = $sth->fetchrow_hashref)
Branch: 1 branch
BranchTrueFalseTotal
if (my $hashref = $sth->fetchrow_hashref) 0 0 0
307 {
308 0 push @ret_rows, $hashref;
309 }
310 }
311 0 return {rows=>\@ret_rows,
312 total=>$total,
313 num_pages=>$num_pages,
314 sql=>$q};
315 } # _search
316  
317  
318 0 ▶sub _process_request {
Subroutine _process_request: not called CC=5 SCAR=34.0
319 0 my $self = shift;
320 0 my %args = @_;
321  
322 0 my $dbh = $self->{dbh};
323 0 my $location = $args{location};
324 0 ▶ $args{n} = 20 if !defined $args{n};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"n"} 0 0 0
325 0 my $tobj = Text::NeatTemplate->new();
326  
327 0 my $data = $self->_search(
328 %args
329 );
330 0 ▶ if (!defined $data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
331 {
332 0 return undef;
333 }
334  
335 0 my $searchform = $self->_format_searchform(
336 %args,
337 data=>$data,
338 );
339 0 my $pagination = $self->_format_pagination(
340 %args,
341 data=>$data,
342 );
343 my $result = $self->_format_rows(
344 %args,
345 rows=>$data->{rows},
346 total=>$data->{total},
347 tags_query=>$args{tags},
348 0 tags_action=>"$location/tags",
349 );
350 my %all_tags = $self->_create_taglist(
351 rows=>$data->{rows},
352 total=>$data->{total},
353 0 );
354 my $query_tags = $self->_format_taglist(
355 %args,
356 all_tags=>\%all_tags,
357 tags_query=>$args{tags},
358 0 tags_action=>"$location/tags",
359 );
360 my $tquery_str = $self->_format_tag_query(
361 %args,
362 tags_query=>$args{tags},
363 0 tags_action=>"$location/tags");
364 my $qquery_str = $self->_format_q_query(
365 %args,
366 tags_query=>$args{tags},
367 0 action=>$location);
368 0 my $query_str = join(' ', $tquery_str, $qquery_str);
369 my $html = $tobj->fill_in(
370 data_hash=>{
371 %args,
372 p=>($args{p} ? $args{p} : 1),
373 sql=>($args{show_sql} ? $data->{sql} : ''),
374 query=>$query_str,
375 result=>$result,
376 total=>$data->{total},
377 num_pages=>$data->{num_pages},
378 searchform=>$searchform,
379 pagination=>$pagination,
380 },
381 template=>$self->{results_template},
382 0 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$args{'p'} ? : 0 0 0
$args{'show_sql'} ? : 0 0 0
383  
384 return { results=>$html,
385 query_tags=>$query_tags,
386 searchform=>$searchform,
387 pagination=>$pagination,
388 total=>$data->{total},
389 num_pages=>$data->{num_pages},
390 0 };
391 } # _process_request
392  
393  
394 0 ▶sub _process_taglist {
Subroutine _process_taglist: not called CC=2 SCAR=17.9
395 0 my $self = shift;
396 0 my %args = @_;
397  
398 0 my $dbh = $self->{dbh};
399 0 my $location = $args{location};
400 0 ▶ $args{n} = 20 if !defined $args{n};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"n"} 0 0 0
401 0 my $tobj = Text::NeatTemplate->new();
402  
403 0 my $data = $self->_search(
404 %args
405 );
406  
407 my %all_tags = $self->_create_taglist(
408 rows=>$data->{rows},
409 total=>$data->{total},
410 0 );
411 0 my $count = keys %all_tags;
412 my $query_tags = $self->_format_taglist(
413 %args,
414 all_tags=>\%all_tags,
415 total_tags=>$count,
416 tags_query=>$args{tags},
417 0 tags_action=>"$location/tags",
418 );
419  
420 return { results=>$query_tags,
421 query_tags=>$query_tags,
422 total=>$data->{total},
423 total_tags=>$count,
424 num_pages=>$data->{num_pages},
425 0 };
426 } # _process_taglist
427  
428  
429 0 ▶sub _process_tagcloud {
Subroutine _process_tagcloud: not called CC=2 SCAR=17.9
430 0 my $self = shift;
431 0 my %args = @_;
432  
433 0 my $dbh = $self->{dbh};
434 0 my $location = $args{location};
435 0 ▶ $args{n} = 20 if !defined $args{n};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"n"} 0 0 0
436 0 my $tobj = Text::NeatTemplate->new();
437  
438 0 my $data = $self->_search(
439 %args
440 );
441  
442 my %all_tags = $self->_create_taglist(
443 rows=>$data->{rows},
444 total=>$data->{total},
445 0 );
446 0 my $count = keys %all_tags;
447 my $query_tags = $self->_format_taglist(
448 %args,
449 all_tags=>\%all_tags,
450 tags_query=>$args{tags},
451 0 tags_action=>"$location/tags",
452 );
453 my $tagcloud = $self->_format_tagcloud(
454 %args,
455 all_tags=>\%all_tags,
456 tags_query=>$args{tags},
457 0 tags_action=>"$location/tags",
458 );
459  
460 return { results=>$tagcloud,
461 query_tags=>$query_tags,
462 total=>$data->{total},
463 total_tags=>$count,
464 num_pages=>$data->{num_pages},
465 0 };
466 } # _process_tagcloud
467  
468  
469 0 ▶sub _total_records {
Subroutine _total_records: not called CC=4 SCAR=30.0
470 0 my $self = shift;
471  
472 0 my $dbh = $self->{dbh};
473  
474 0 my $q = $self->_query_to_sql(get_total=>1);
475  
476 0 my $sth = $dbh->prepare($q);
477 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
478 {
479 0 $self->{error} = "FAILED to prepare '$q' $DBI::errstr";
480 0 return undef;
481 }
482 0 my $ret = $sth->execute();
483 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
484 {
485 0 $self->{error} = "FAILED to execute '$q' $DBI::errstr";
486 0 return undef;
487 }
488 0 my $total = 0;
489 0 my @row;
490 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
491 {
492 0 $total = $row[0];
493 }
494 0 return $total;
495 } # _total_records
496  
497  
498 0 ▶sub _build_where {
Subroutine _build_where: not called CC=19 SCAR=59.4
499 0 my $self = shift;
500 0 my %args = @_;
501 0 my $field = $args{field};
502 0 my $query_string = $args{q};
503
504 # no query, no WHERE
505 0 ▶ if (!$query_string)
Branch: 1 branch
BranchTrueFalseTotal
unless ($query_string) 0 0 0
506 {
507 0 return '';
508 }
509  
510 0 my $sql_where = '';
511  
512 # If there is no field, it is a simple query string;
513 # the simple query string will search all columns in OR fashion
514 # that is (col1 GLOB term OR col2 GLOB term...) etc
515 # only allow for '-' prefix, not the complex Search::Query stuff
516 # Note that if this is a NOT term, the query clause needs to be
517 # (col1 NOT GLOB term AND col2 NOT GLOB term)
518 # and checking for NULL too
519 0 ▶ if (!$field)
Branch: 2 branches
BranchTrueFalseTotal
if (not $field) { } 0 0 0
elsif ($field eq 'tags' or $field eq $self->{'tagfield'}) { } 0 0 0
520 {
521 0 my @and_clauses = ();
522 0 my @terms = split(/[ +]/, $query_string);
523 0 ▶ for (my $i=0; $i < @terms; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @terms) 0 0 0
524 {
525 0 my $term = $terms[$i];
526 0 my $not = 0;
527 0 ▶ if ($term =~ /^-(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($term =~ /^-(.*)/u) 0 0 0
528 {
529 0 $term = $1;
530 0 $not = 1;
531 }
532 0 ▶ if ($not) # negative term, match NOT AND
Branch: 1 branch
BranchTrueFalseTotal
if ($not) { } 0 0 0
533 {
534 0 my @and_not_clauses = ();
535 0 foreach my $col (@{$self->{columns}})
536 {
537 0 my $clause = sprintf('(%s IS NULL OR %s NOT GLOB "*%s*")', $col, $col, $term);
538 0 push @and_not_clauses, $clause;
539 }
540 0 push @and_clauses, "(" . join(" AND ", @and_not_clauses) . ")";
541 }
542 else # positive term, match OR
543 {
544 0 my @or_clauses = ();
545 0 foreach my $col (@{$self->{columns}})
546 {
547 0 my $clause = sprintf('%s GLOB "*%s*"', $col, $term);
548 0 push @or_clauses, $clause;
549 }
550 0 push @and_clauses, "(" . join(" OR ", @or_clauses) . ")";
551 }
552 }
553 0 $sql_where = join(" AND ", @and_clauses);
554 }
555 ▶ elsif ($field eq 'tags'
Condition0%0/3
$field eq "tags" or $field eq $self->{"tagfield"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$field eq "tags" or $field eq $self->{"tagfield"}
$field eq "tags" $field eq $self->{"tagfield"}
Truth table0%0/3
A or B
A= $field eq "tags"B= $field eq $self->{"tagfield"}
ABresult
1X1
011
000
556 or $field eq $self->{tagfield})
557 {
558 0 my $tagfield = $self->{tagfield};
559 0 my @and_clauses = ();
560 0 my @terms = split(/[ +]/, $query_string);
561 0 ▶ for (my $i=0; $i < @terms; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @terms) 0 0 0
562 {
563 0 my $term = $terms[$i];
564 0 my $not = 0;
565 0 my $equals = 1; # make tags match exactly by default
566 0 ▶ if ($term =~ /^-(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($term =~ /^-(.*)/u) 0 0 0
567 {
568 0 $term = $1;
569 0 $not = 1;
570 }
571 # use * for a glob marker
572 0 ▶ if ($term =~ /^\*(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($term =~ /^\*(.*)/u) 0 0 0
573 {
574 0 $term = $1;
575 0 $equals = 0;
576 }
577 0 ▶ if ($not and !$equals)
Branch: 3 branches
BranchTrueFalseTotal
if ($not and not $equals) { } 0 0 0
elsif ($not and $equals) { } 0 0 0
elsif ($equals) { } 0 0 0
Condition0%0/3
$not and not $equals
!ll&&!rl&&r
000
MC/DC0%0/2
$not and not $equals
$not not $equals
Truth table0%0/3
A and not(B)
A= $notB= not $equals
ABresult
0X0
100
111
578 {
579 0 my $clause = sprintf('(%s IS NULL OR %s NOT GLOB "*%s*")', $tagfield, $tagfield, $term);
580 0 push @and_clauses, $clause;
581 }
582 ▶ elsif ($not and $equals) # negative term, match NOT AND
Condition0%0/3
$not and $equals
!ll&&!rl&&r
000
MC/DC0%0/2
$not and $equals
$not $equals
Truth table0%0/3
A and B
A= $notB= $equals
ABresult
0X0
100
111
583 {
584 0 my $clause = sprintf('(%s IS NULL OR (%s != "%s" AND %s NOT GLOB "%s|*" AND %s NOT GLOB "*|%s|*" AND %s NOT GLOB "*|%s"))',
585 $tagfield,
586 $tagfield, $term,
587 $tagfield, $term,
588 $tagfield, $term,
589 $tagfield, $term,
590 );
591 0 push @and_clauses, $clause;
592 }
593 elsif ($equals) # positive term, match OR
594 {
595 0 my $clause = sprintf('(%s = "%s" OR %s GLOB "%s|*" OR %s GLOB "*|%s|*" OR %s GLOB "*|%s")',
596 $tagfield, $term,
597 $tagfield, $term,
598 $tagfield, $term,
599 $tagfield, $term,
600 );
601 0 push @and_clauses, $clause;
602 }
603 else
604 {
605 0 my $clause = sprintf('%s GLOB "*%s*"', $tagfield, $term);
606 0 push @and_clauses, $clause;
607 }
608 }
609 0 $sql_where = join(" AND ", @and_clauses);
610 }
611 else # other columns
612 {
613 0 my $parser = Search::Query->parser(
614 query_class => 'SQL',
615 query_class_opts => {
616 like => 'GLOB',
617 wildcard => '*',
618 fuzzify2 => 1,
619 },
620 null_term => 'NULL',
621 default_field => $field,
622 default_op => '~',
623 fields => [$field],
624 );
625 0 my $query = $parser->parse($args{q});
626 0 $sql_where = $query->stringify;
627 }
628  
629 0 ▶ return ($sql_where ? "(${sql_where})" : '');
Branch: 1 branch
BranchTrueFalseTotal
$sql_where ? : 0 0 0
630 } # _build_where
631  
632  
633 0 ▶sub _query_to_sql {
Subroutine _query_to_sql: not called CC=39 SCAR=73.5
634 0 my $self = shift;
635 0 my %args = @_;
636  
637 0 my $p = $args{p};
638 0 my $items_per_page = $args{n};
639 0 ▶ my $total = ($args{total} ? $args{total} : 0);
Branch: 1 branch
BranchTrueFalseTotal
$args{'total'} ? : 0 0 0
640 # RANDOM sort overrides all other sorts (because it doesn't make sense otherwise)
641 # RANDOM also overrides the default sort.
642 0 my $order_by = '';
643 0 ▶ if (($args{sort_by} and $args{sort_by} eq 'RANDOM')
Branch: 5 branches
BranchTrueFalseTotal
if ($args{'sort_by'} and $args{'sort_by'} eq 'RANDOM' or $args{'sort_by2'} and $args{'sort_by2'} eq 'RANDOM' or $args{'sort_by3'} and $args{'sort_by3'} eq 'RANDOM' or $args{'sort_by4'} and $args{'sort_by4'} eq 'RANDOM') { } 0 0 0
elsif ($args{'sort_by'} and $args{'sort_by2'} and $args{'sort_by3'} and $args{'sort_by4'}) { } 0 0 0
elsif ($args{'sort_by'} and $args{'sort_by2'} and $args{'sort_by3'}) { } 0 0 0
elsif ($args{'sort_by'} and $args{'sort_by2'}) { } 0 0 0
elsif ($args{'sort_by'}) { } 0 0 0
Condition0%0/21
$args{"sort_by"} and $args{"sort_by"} eq "RANDOM" or $args{"sort_by2"} and $args{"sort_by2"} eq "RANDOM" or $args{"sort_by3"} and $args{"sort_by3"} eq "RANDOM" or $args{"sort_by4"} and $args{"sort_by4"} eq "RANDOM"
l!l&&r!l&&!r
000
$args{"sort_by"} and $args{"sort_by"} eq "RANDOM" or $args{"sort_by2"} and $args{"sort_by2"} eq "RANDOM" or $args{"sort_by3"} and $args{"sort_by3"} eq "RANDOM"
l!l&&r!l&&!r
000
$args{"sort_by"} and $args{"sort_by"} eq "RANDOM" or $args{"sort_by2"} and $args{"sort_by2"} eq "RANDOM"
l!l&&r!l&&!r
000
$args{"sort_by"} and $args{"sort_by"} eq "RANDOM"
!ll&&!rl&&r
000
$args{"sort_by2"} and $args{"sort_by2"} eq "RANDOM"
!ll&&!rl&&r
000
$args{"sort_by3"} and $args{"sort_by3"} eq "RANDOM"
!ll&&!rl&&r
000
$args{"sort_by4"} and $args{"sort_by4"} eq "RANDOM"
!ll&&!rl&&r
000
MC/DC0%0/8
$args{"sort_by"} and $args{"sort_by"} eq "RANDOM" or $args{"sort_by2"} and $args{"sort_by2"} eq "RANDOM" or $args{"sort_by3"} and $args{"sort_by3"} eq "RANDOM" or $args{"sort_by4"} and $args{"sort_by4"} eq "RANDOM"
$args{"sort_by"} $args{"sort_by"} eq "RANDOM" $args{"sort_by2"} $args{"sort_by2"} eq "RANDOM" $args{"sort_by3"} $args{"sort_by3"} eq "RANDOM" $args{"sort_by4"} $args{"sort_by4"} eq "RANDOM"
Truth table0%0/31
A and B or C and D or E and F or G and H
A= $args{"sort_by"}B= $args{"sort_by"} eq "RANDOM"C= $args{"sort_by2"}D= $args{"sort_by2"} eq "RANDOM"E= $args{"sort_by3"}F= $args{"sort_by3"} eq "RANDOM"G= $args{"sort_by4"}H= $args{"sort_by4"} eq "RANDOM"
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X1011XX1
100X11XX1
101011XX1
0X0X0X111
0X0X10111
0X100X111
0X1010111
100X0X111
100X10111
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
644 or ($args{sort_by2} and $args{sort_by2} eq 'RANDOM')
645 or ($args{sort_by3} and $args{sort_by3} eq 'RANDOM')
646 or ($args{sort_by4} and $args{sort_by4} eq 'RANDOM'))
647 {
648 0 $order_by = 'RANDOM()';
649 }
650 ▶ elsif ($args{sort_by} and $args{sort_by2} and $args{sort_by3} and $args{sort_by4})
Condition0%0/9
$args{"sort_by"} and $args{"sort_by2"} and $args{"sort_by3"} and $args{"sort_by4"}
!ll&&!rl&&r
000
$args{"sort_by"} and $args{"sort_by2"} and $args{"sort_by3"}
!ll&&!rl&&r
000
$args{"sort_by"} and $args{"sort_by2"}
!ll&&!rl&&r
000
MC/DC0%0/4
$args{"sort_by"} and $args{"sort_by2"} and $args{"sort_by3"} and $args{"sort_by4"}
$args{"sort_by"} $args{"sort_by2"} $args{"sort_by3"} $args{"sort_by4"}
Truth table0%0/5
A and B and C and D
A= $args{"sort_by"}B= $args{"sort_by2"}C= $args{"sort_by3"}D= $args{"sort_by4"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
651 {
652 0 $order_by = join(', ', $args{sort_by}, $args{sort_by2}, $args{sort_by3}, $args{sort_by4});
653 }
654 ▶ elsif ($args{sort_by} and $args{sort_by2} and $args{sort_by3})
Condition0%0/6
$args{"sort_by"} and $args{"sort_by2"} and $args{"sort_by3"}
!ll&&!rl&&r
000
$args{"sort_by"} and $args{"sort_by2"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"sort_by"} and $args{"sort_by2"} and $args{"sort_by3"}
$args{"sort_by"} $args{"sort_by2"} $args{"sort_by3"}
Truth table0%0/4
A and B and C
A= $args{"sort_by"}B= $args{"sort_by2"}C= $args{"sort_by3"}
ABCresult
0XX0
10X0
1100
1111
655 {
656 0 $order_by = join(', ', $args{sort_by}, $args{sort_by2}, $args{sort_by3});
657 }
658 ▶ elsif ($args{sort_by} and $args{sort_by2})
Condition0%0/3
$args{"sort_by"} and $args{"sort_by2"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"sort_by"} and $args{"sort_by2"}
$args{"sort_by"} $args{"sort_by2"}
Truth table0%0/3
A and B
A= $args{"sort_by"}B= $args{"sort_by2"}
ABresult
0X0
100
111
659 {
660 0 $order_by = join(', ', $args{sort_by}, $args{sort_by2});
661 }
662 elsif ($args{sort_by})
663 {
664 0 $order_by = $args{sort_by};
665 }
666 else
667 {
668 0 $order_by = join(', ', @{$self->{default_sort}});
669 }
670  
671 0 my $offset = 0;
672 0 ▶ if ($p and $items_per_page)
Branch: 1 branch
BranchTrueFalseTotal
if ($p and $items_per_page) 0 0 0
Condition0%0/3
$p and $items_per_page
!ll&&!rl&&r
000
MC/DC0%0/2
$p and $items_per_page
$p $items_per_page
Truth table0%0/3
A and B
A= $pB= $items_per_page
ABresult
0X0
100
111
673 {
674 0 $offset = ($p - 1) * $items_per_page;
675 0 ▶ if ($total > 0 and $offset >= $total)
Branch: 2 branches
BranchTrueFalseTotal
if ($total > 0 and $offset >= $total) { } 0 0 0
elsif ($offset <= 0) { } 0 0 0
Condition0%0/3
$total > 0 and $offset >= $total
!ll&&!rl&&r
000
MC/DC0%0/2
$total > 0 and $offset >= $total
$total > 0 $offset >= $total
Truth table0%0/3
A and B
A= $total > 0B= $offset >= $total
ABresult
0X0
100
111
676 {
677 0 $offset = $total - 1;
678 }
679 elsif ($offset <= 0)
680 {
681 0 $offset = 0;
682 }
683 }
684  
685 0 my @and_clauses = ();
686 0 foreach my $col (@{$self->{columns}})
687 {
688 0 ▶ if ($args{$col})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$col}) 0 0 0
689 {
690 0 my $clause = $self->_build_where(field=>$col, q=>$args{$col});
691 0 push @and_clauses, $clause;
692 }
693 }
694 0 ▶ if ($args{'tags'} and $self->{tagfield} ne 'tags')
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"tags"} and $self->{"tagfield"} ne "tags") 0 0 0
Condition0%0/3
$args{"tags"} and $self->{"tagfield"} ne "tags"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"tags"} and $self->{"tagfield"} ne "tags"
$args{"tags"} $self->{"tagfield"} ne "tags"
Truth table0%0/3
A and B
A= $args{"tags"}B= $self->{"tagfield"} ne "tags"
ABresult
0X0
100
111
695 {
696 0 my $clause = $self->_build_where(field=>'tags', q=>$args{'tags'});
697 0 push @and_clauses, $clause;
698 }
699  
700 0 ▶ if ($args{q})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"q"}) 0 0 0
701 {
702 0 my $clause = $self->_build_where(field=>'', q=>$args{q});
703 0 push @and_clauses, $clause;
704 }
705 # a freeform where condition
706 0 ▶ if ($args{where})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"where"}) 0 0 0
707 {
708 0 push @and_clauses, $args{where};
709 }
710 # if there's an extra condition in the configuration, add it here
711 0 ▶ if ($self->{extra_cond})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extra_cond"}) 0 0 0
712 {
713 0 ▶ if (@and_clauses)
Branch: 1 branch
BranchTrueFalseTotal
if (@and_clauses) { } 0 0 0
714 {
715 0 push @and_clauses, "(" . $self->{extra_cond} . ")";
716 }
717 else
718 {
719 0 push @and_clauses, $self->{extra_cond};
720 }
721 }
722 0 my $sql_where = join(" AND ", @and_clauses);
723  
724 0 my $q = '';
725 0 ▶ if ($args{get_total})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'get_total'}) { } 0 0 0
726 {
727 0 $q = "SELECT COUNT(*) FROM " . $self->{table};
728 0 ▶ $q .= " WHERE $sql_where" if $sql_where;
Branch: 1 branch
BranchTrueFalseTotal
if $sql_where 0 0 0
729 }
730 else
731 {
732 0 $q = "SELECT * FROM " . $self->{table};
733 0 ▶ $q .= " WHERE $sql_where" if $sql_where;
Branch: 1 branch
BranchTrueFalseTotal
if $sql_where 0 0 0
734 0 ▶ $q .= " ORDER BY $order_by" if $order_by;
Branch: 1 branch
BranchTrueFalseTotal
if $order_by 0 0 0
735 0 ▶ $q .= " LIMIT $items_per_page" if $items_per_page;
Branch: 1 branch
BranchTrueFalseTotal
if $items_per_page 0 0 0
736 0 ▶ $q .= " OFFSET $offset" if $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $offset 0 0 0
737 }
738  
739 0 return $q;
740 } # _query_to_sql
741  
742  
743 0 ▶sub _format_searchform {
Subroutine _format_searchform: not called CC=11 SCAR=48.8
744 0 my $self = shift;
745 0 my %args = @_;
746  
747 0 my $data = $args{data};
748 0 my $location = $args{location};
749 0 my $tobj = Text::NeatTemplate->new();
750  
751 0 my $selectN = '';
752 0 my @os = ();
753 0 push @os, '<select name="n">';
754 0 foreach my $limit (qw(10 20 50 100))
755 {
756 0 ▶ if ($limit == $args{n})
Branch: 1 branch
BranchTrueFalseTotal
if ($limit == $args{'n'}) { } 0 0 0
757 {
758 0 push @os, "<option value='$limit' selected>$limit</option>";
759 }
760 else
761 {
762 0 push @os, "<option value='$limit'>$limit</option>";
763 }
764 }
765 0 push @os, '</select>';
766 0 $selectN = join("\n", @os);
767  
768 0 my $total = $data->{total};
769 0 my $num_pages = $data->{num_pages};
770 0 ▶ if ($args{p} > $num_pages)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"p"} > $num_pages) 0 0 0
771 {
772 0 $args{p} = 1;
773 }
774  
775 0 my $selectP = '';
776 0 @os = ();
777 0 push @os, '<select name="p">';
778 0 ▶ for (my $p = 1; $p <= $num_pages; $p++)
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= $num_pages) 0 0 0
779 {
780 0 ▶ if ($p == $args{p})
Branch: 1 branch
BranchTrueFalseTotal
if ($p == $args{'p'}) { } 0 0 0
781 {
782 0 push @os, "<option value='$p' selected>$p</option>";
783 }
784 else
785 {
786 0 push @os, "<option value='$p'>$p</option>";
787 }
788 }
789 0 push @os, '</select>';
790 0 $selectP = join("\n", @os);
791  
792 0 my $db = $args{db};
793 0 my $sorting = '';
794 0 @os = ();
795 0 foreach my $sf (qw(sort_by sort_by2 sort_by3 sort_by4))
796 {
797 0 push @os, "<select name='$sf'>";
798 0 push @os, "<option value=''> </option>";
799 0 foreach my $s (sort @{$self->{sort_columns}})
800 {
801 0 ▶ if ($s eq $args{$sf})
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq $args{$sf}) { } 0 0 0
802 {
803 0 push @os, "<option value='$s' selected>$s</option>";
804 }
805 else
806 {
807 0 push @os, "<option value='$s'>$s</option>";
808 }
809 # sert DESCending makes no sense for RANDOM
810 0 ▶ if ($s ne 'RANDOM')
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne "RANDOM") 0 0 0
811 {
812 0 my $s_desc = "${s} DESC";
813 0 ▶ if ($s_desc eq $args{$sf})
Branch: 1 branch
BranchTrueFalseTotal
if ($s_desc eq $args{$sf}) { } 0 0 0
814 {
815 0 push @os, "<option value='$s_desc' selected>$s_desc</option>";
816 }
817 else
818 {
819 0 push @os, "<option value='$s_desc'>$s_desc</option>";
820 }
821 }
822 }
823 0 push @os, '</select>';
824 }
825 0 $sorting = join("\n", @os);
826  
827 my $searchform = $tobj->fill_in(
828 data_hash=>{
829 %args,
830 action=>$location,
831 selectN=>$selectN,
832 selectP=>$selectP,
833 sorting=>$sorting,
834 },
835 template=>$self->{searchform},
836 0 );
837  
838 0 return $searchform;
839 } # _format_searchform
840  
841  
842 0 ▶sub _format_pagination {
Subroutine _format_pagination: not called CC=6 SCAR=37.4
843 0 my $self = shift;
844 0 my %args = @_;
845  
846 0 my $data = $args{data};
847 0 my $location = $args{location};
848 0 my $tobj = Text::NeatTemplate->new();
849  
850 0 my $total = $data->{total};
851 0 my $num_pages = $data->{num_pages};
852 0 ▶ if ($args{p} > $num_pages)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"p"} > $num_pages) 0 0 0
853 {
854 0 $args{p} = $num_pages;
855 }
856 0 ▶ if ($args{p} < 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"p"} < 1) 0 0 0
857 {
858 0 $args{p} = 1;
859 }
860 0 my $prev_page = $args{p} - 1;
861 0 ▶ if ($prev_page < 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_page < 1) 0 0 0
862 {
863 0 $prev_page = 0;
864 }
865 0 my $next_page = $args{p} + 1;
866 0 ▶ if ($next_page > $num_pages)
Branch: 1 branch
BranchTrueFalseTotal
if ($next_page > $num_pages) 0 0 0
867 {
868 0 $next_page = 0;
869 }
870 0 my $tq = '';
871 0 ▶ if ($args{tags})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"tags"}) 0 0 0
872 {
873 0 $tq = 'tags/' . $args{tags};
874 }
875  
876 my $pagination = $tobj->fill_in(
877 data_hash=>{
878 %args,
879 tq=>$tq,
880 prev_page=>$prev_page,
881 next_page=>$next_page,
882 },
883 template=>$self->{pagination_template},
884 0 );
885  
886 0 return $pagination;
887 } # _format_pagination
888  
889  
890 0 ▶sub _format_rows {
Subroutine _format_rows: not called CC=2 SCAR=17.9
891 0 my $self = shift;
892 0 my %args = @_;
893  
894 0 my @rows = @{$args{rows}};
895 0 my $total = $args{total};
896  
897 0 my @out = ();
898 0 push @out, '<ul>';
899 0 my $tobj = Text::NeatTemplate->new();
900 0 foreach my $row_hash (@rows)
901 {
902 # format the tags, then format the row
903 # may need to remove trailing empty tags
904 0 my $proper_tags = $row_hash->{$self->{tagfield}};
905 0 $proper_tags =~ s/^[|]//;
906 0 $proper_tags =~ s/[|]$//;
907 0 my @tags = split(/\|/, $proper_tags);
908 0 my $tags_str = $self->_format_tag_collection(
909 %args,
910 in_list=>0,
911 tags_array=>\@tags);
912 0 $row_hash->{all_tags} = $tags_str;
913 0 $row_hash->{route_prefix} = $self->{route_prefix};
914 my $text = $tobj->fill_in(data_hash=>$row_hash,
915 0 template=>$self->{row_template});
916 0 push @out, $text;
917 }
918 0 push @out, "</ul>\n";
919  
920 0 my $results = join("\n", @out);
921  
922 0 return $results;
923 } # _format_rows
924  
925  
926 0 ▶sub _create_taglist {
Subroutine _create_taglist: not called CC=4 SCAR=30.0
927 0 my $self = shift;
928 0 my %args = @_;
929  
930 0 my @rows = @{$args{rows}};
931  
932 0 my %all_tags = ();
933 0 foreach my $row_hash (@rows)
934 {
935 # iterate over the tags
936 0 my @tags = split(/\|/, $row_hash->{$self->{tagfield}});
937 0 foreach my $tag (@tags)
938 {
939 0 ▶ if ($tag)
Branch: 1 branch
BranchTrueFalseTotal
if ($tag) 0 0 0
940 {
941 0 $all_tags{$tag}++;
942 }
943 }
944 }
945 0 return %all_tags;
946 } # _create_taglist
947  
948  
949 0 ▶sub _format_tagcloud {
Subroutine _format_tagcloud: not called CC=4 SCAR=30.0
950 0 my $self = shift;
951 0 my %args = @_;
952  
953 0 my $cloud = HTML::TagCloud->new(levels=>30);
954 0 my @out = ();
955 0 push @out, '<div id="tagcloud">';
956 0 foreach my $tag (nsort keys %{$args{all_tags}})
957 {
958 0 my $tq = '';
959 0 ▶ if (!$args{tags_query})
Branch: 2 branches
BranchTrueFalseTotal
if (not $args{'tags_query'}) { } 0 0 0
elsif ($args{'tags_query'} =~ /\Q$tag\E/u) { } 0 0 0
960 {
961 0 $tq = $tag;
962 }
963 elsif ($args{tags_query} =~ /\Q$tag\E/)
964 {
965 # this tag is already in the query
966 0 $tq = $args{tags_query};
967 }
968 else
969 {
970 0 $tq = "$args{tags_query}+${tag}";
971 }
972 0 my $tag_url = "$args{location}/tags/$tq";
973 0 $cloud->add($tag, $tag_url, $args{all_tags}->{$tag});
974 }
975 0 my $tc = $cloud->html_and_css();
976 0 push @out, $tc;
977 0 push @out, "</div>\n";
978  
979 0 my $taglist = join("\n", @out);
980  
981 0 return $taglist;
982 } # _format_tagcloud
983  
984  
985 0 ▶sub _format_taglist {
Subroutine _format_taglist: not called CC=4 SCAR=30.0
986 0 my $self = shift;
987 0 my %args = @_;
988  
989 0 my @out = ();
990 0 push @out, '<div id="alltags">';
991 0 ▶ if (exists $args{total_tags}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"total_tags"} and defined $args{"total_tags"} and $args{"total_tags"}) 0 0 0
Condition0%0/6
exists $args{"total_tags"} and defined $args{"total_tags"} and $args{"total_tags"}
!ll&&!rl&&r
000
exists $args{"total_tags"} and defined $args{"total_tags"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $args{"total_tags"} and defined $args{"total_tags"} and $args{"total_tags"}
exists $args{"total_tags"} defined $args{"total_tags"} $args{"total_tags"}
Truth table0%0/4
A and B and C
A= exists $args{"total_tags"}B= defined $args{"total_tags"}C= $args{"total_tags"}
ABCresult
0XX0
10X0
1100
1111
992 and defined $args{total_tags}
993 and $args{total_tags})
994 {
995 0 push @out, "<p>Tag-count: $args{total_tags}</p>";
996 }
997 0 push @out, "<ul id='listtag'>\n";
998 0 my $tl = $self->_format_tag_collection(
999 %args,
1000 in_list=>1,
1001 );
1002 0 push @out, $tl;
1003 0 push @out, "</ul>\n";
1004 0 push @out, "</div>\n";
1005  
1006 0 my $taglist = join("\n", @out);
1007  
1008 0 return $taglist;
1009 } # _format_taglist
1010  
1011  
1012 0 ▶sub _format_tag_collection {
Subroutine _format_tag_collection: not called CC=14 SCAR=53.5
1013 0 my $self = shift;
1014 0 my %args = @_;
1015  
1016 0 my $tags_query = $args{tags_query};
1017 0 my $tags_action = $args{tags_action};
1018 0 ▶ my @tags = ($args{all_tags} ? nsort keys %{$args{all_tags}} : nsort @{$args{tags_array}});
Branch: 1 branch
BranchTrueFalseTotal
$args{'all_tags'} ? : 0 0 0
1019 0 my $qquery = '';
1020 0 my @qq = ();
1021 0 ▶ push @qq, "q=$args{q}" if $args{q};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"q"} 0 0 0
1022 0 ▶ push @qq, "n=$args{n}" if $args{n};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"n"} 0 0 0
1023 0 ▶ push @qq, "sort_by=$args{sort_by}" if $args{sort_by};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"sort_by"} 0 0 0
1024 0 ▶ push @qq, "sort_by2=$args{sort_by2}" if $args{sort_by2};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"sort_by2"} 0 0 0
1025 0 ▶ push @qq, "sort_by3=$args{sort_by3}" if $args{sort_by3};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"sort_by3"} 0 0 0
1026 0 ▶ push @qq, "sort_by4=$args{sort_by4}" if $args{sort_by4};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"sort_by4"} 0 0 0
1027 0 my $qquery = join('&', @qq);
1028  
1029 0 my $tobj = Text::NeatTemplate->new();
1030 0 my @out = ();
1031 0 foreach my $tag (@tags)
1032 {
1033 0 my $tag_label = $tag;
1034 0 $tag_label =~ s/-/ /g; # remove dashes
1035 0 my $tq = '';
1036 0 ▶ if (!$tags_query)
Branch: 2 branches
BranchTrueFalseTotal
if (not $tags_query) { } 0 0 0
elsif ($tags_query =~ /\Q$tag\E/u) { } 0 0 0
1037 {
1038 0 $tq = $tag;
1039 }
1040 elsif ($tags_query =~ /\Q$tag\E/)
1041 {
1042 # this tag is already in the query
1043 0 $tq = $tags_query;
1044 }
1045 else
1046 {
1047 0 $tq = "${tags_query}+${tag}";
1048 }
1049 0 ▶ push @out, "<li>" if $args{in_list};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"in_list"} 0 0 0
1050 push @out, $tobj->fill_in(data_hash=>{tag=>$tag,
1051 tag_label=>$tag_label,
1052 num_tags=>(defined $args{all_tags} ? $args{all_tags}->{$tag} : undef),
1053 in_list=>$args{in_list},
1054 not_in_list=>!$args{in_list},
1055 tags_query=>$tq,
1056 qquery=>$qquery,
1057 url=>$tags_action},
1058 0 ▶ template=>$self->{tags_template});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'all_tags'} ? : 0 0 0
1059 0 ▶ push @out, "</li>\n" if $args{in_list};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"in_list"} 0 0 0
1060 }
1061  
1062 0 my $taglist = join("\n", @out);
1063  
1064 0 return $taglist;
1065 } # _format_tag_collection
1066  
1067  
1068 0 ▶sub _format_tag_query {
Subroutine _format_tag_query: not called CC=4 SCAR=30.0
1069 0 my $self = shift;
1070 0 my %args = @_;
1071  
1072 0 my $tags_query = $args{tags_query};
1073 0 my $tags_action = $args{tags_action};
1074 0 my @terms = split(/[ +]/, $tags_query);
1075  
1076 0 my $tobj = Text::NeatTemplate->new();
1077 0 my @out = ();
1078 0 foreach my $tag (@terms)
1079 {
1080 0 my $tq = '';
1081 0 ▶ if (!$tags_query)
Branch: 2 branches
BranchTrueFalseTotal
if (not $tags_query) { } 0 0 0
elsif ($tags_query =~ /\Q$tag\E/u) { } 0 0 0
1082 {
1083 0 $tq = $tag;
1084 }
1085 elsif ($tags_query =~ /\Q$tag\E/)
1086 {
1087 # this tag is already in the query
1088 0 $tq = $tags_query;
1089 }
1090 else
1091 {
1092 0 $tq = "${tags_query}+${tag}";
1093 }
1094 push @out, $tobj->fill_in(data_hash=>{
1095 %args,
1096 tag=>$tag,
1097 tags_query=>$tq,
1098 url=>$tags_action},
1099 0 template=>$self->{tag_query_template});
1100 }
1101  
1102 0 my $taglist = join("\n", @out);
1103  
1104 0 return $taglist;
1105 } # _format_tag_query
1106  
1107  
1108 0 ▶sub _format_q_query {
Subroutine _format_q_query: not called CC=3 SCAR=24.8
1109 0 my $self = shift;
1110 0 my %args = @_;
1111  
1112 0 ▶ if (!$args{q})
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"q"}) 0 0 0
1113 {
1114 0 return '';
1115 }
1116 0 my @terms = split(/[ +]/, $args{q});
1117  
1118 0 my $tobj = Text::NeatTemplate->new();
1119 0 my @out = ();
1120 0 foreach my $term (@terms)
1121 {
1122 push @out, $tobj->fill_in(data_hash=>{
1123 %args,
1124 qterm=>$term,
1125 tags_query=>$args{tags_query},
1126 qquery=>$args{q},
1127 url=>$args{action}},
1128 0 template=>$self->{q_query_template});
1129 }
1130  
1131 0 my $qlist = join("\n", @out);
1132  
1133 0 return $qlist;
1134 } # _format_q_query
1135  
1136 3 1; # End of Marky::DbTable
1137  
1138 __END__
↑ Summary Marky.pm »