Muster/MetaDb.pm

filter: Statementstmt 4.7% 29 / 612 Branchbran 0.0% 0 / 262 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 18.0% 9 / 50 Podpod 100% 15 / 15 totaltotal 5.4% 53 / 973 CC 154 SCAR 51.7
CC
154
Cov
3%
_add_page_data
870.0
_generate_new_derived_tables
240.0
pagespec_translate
132.0
CRAP 175.1
« Muster/Hook/SqlReport.pm ↑ Summary Muster/Hook/Directives.pm »
1 package Muster::MetaDb;
2 26 $Muster::MetaDb::VERSION = '0.96';
3 #ABSTRACT: Muster::MetaDb - keeping meta-data about pages
4 =head1 NAME
5  
6 Muster::MetaDb - keeping meta-data about pages
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 SYNOPSIS
13  
14 use Muster::MetaDb;;
15  
16 =head1 DESCRIPTION
17  
18 Content Management System
19 keeping meta-data about pages.
20  
21 =cut
22  
23 26 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 26 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 26 ▶use Search::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 26 ▶use Sort::Naturally;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 26 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 26 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 26 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 26 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 METHODS
34  
35 =head2 init
36  
37 Set the defaults for the object if they are not defined already.
38  
39 =cut
40 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
41 0 my $self = shift;
42  
43 0 $self->{primary_fields} = [qw(title bald_name hairy_name date filetype is_binary pagesrcname extension filename parent_page grandparent_page)];
44 0 ▶ if (!defined $self->{metadb_db})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"metadb_db"}) 0 0 0
45 {
46 # give a default name
47 0 $self->{metadb_db} = 'muster.db3';
48 }
49 0 ▶ if (!defined $self->{route_prefix})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"route_prefix"}) 0 0 0
50 {
51 0 $self->{route_prefix} = '/'; # for absolute links
52 }
53  
54 0 return $self;
55  
56 } # init
57  
58 =head2 update_one_page
59  
60 Update the meta information for one page
61  
62 $self->update_one_page($page, %meta);
63  
64 =cut
65  
66 0 ▶sub update_one_page {
Subroutine update_one_page: not called CC=2 SCAR=17.9
Pod documented
67 0 my $self = shift;
68 0 my $pagename = shift;
69 0 my %args = @_;
70  
71 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
72 {
73 0 return undef;
74 }
75  
76 0 $self->_add_page_data($pagename, %args);
77  
78 } # update_one_page
79  
80 =head2 update_some_pages
81  
82 Update the meta information for more than one page
83  
84 $self->update_some_pages($page=>{...},$page2=>{}...);
85  
86 =cut
87  
88 0 ▶sub update_some_pages {
Subroutine update_some_pages: not called CC=2 SCAR=17.9
Pod documented
89 0 my $self = shift;
90 0 my %args = @_;
91  
92 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
93 {
94 0 return undef;
95 }
96  
97 0 $self->_update_some_entries(%args);
98  
99 } # update_some_pages
100  
101 =head2 update_all_pages
102  
103 Update the meta information for all pages.
104  
105 =cut
106  
107 0 ▶sub update_all_pages {
Subroutine update_all_pages: not called CC=2 SCAR=17.9
Pod documented
108 0 my $self = shift;
109 0 my %args = @_;
110  
111 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
112 {
113 0 return undef;
114 }
115  
116 0 $self->_update_all_entries(%args);
117  
118 } # update_all_pages
119  
120 =head2 delete_one_page
121  
122 Delete the meta information for one page
123  
124 $self->delete_one_page($page);
125  
126 =cut
127  
128 0 ▶sub delete_one_page {
Subroutine delete_one_page: not called CC=3 SCAR=24.8
Pod documented
129 0 my $self = shift;
130 0 my $pagename = shift;
131 0 my %args = @_;
132  
133 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
134 {
135 0 return undef;
136 }
137  
138 0 ▶ if ($self->page_exists($pagename))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->page_exists($pagename)) 0 0 0
139 {
140 0 return $self->_delete_page_from_db($pagename);
141 }
142  
143 0 return 0;
144 } # delete_one_page
145  
146 =head2 update_derived_tables
147  
148 Update the derived tables for all pages.
149  
150 =cut
151  
152 0 ▶sub update_derived_tables {
Subroutine update_derived_tables: not called CC=2 SCAR=17.9
Pod documented
153 0 my $self = shift;
154 0 my %args = @_;
155  
156 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
157 {
158 0 return undef;
159 }
160  
161 0 $self->_generate_derived_tables(%args);
162  
163 } # update_derived_tables
164  
165 =head2 page_or_file_info
166  
167 Get the info about one page. Returns undef if the page isn't there.
168  
169 my $meta = $self->page_or_file_info($pagename);
170  
171 =cut
172  
173 0 ▶sub page_or_file_info {
Subroutine page_or_file_info: not called CC=2 SCAR=17.9
Pod documented
174 0 my $self = shift;
175 0 my $pagename = shift;
176  
177 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
178 {
179 0 return undef;
180 }
181  
182 0 return $self->_get_page_meta($pagename);
183 } # page_or_file_info
184  
185 =head2 query
186  
187 Do a freeform query. This returns a reference to the first column of results.
188  
189 my $results = $self->query($query);
190  
191 =cut
192  
193 0 ▶sub query {
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
194 0 my $self = shift;
195 0 my $query = shift;
196  
197 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
198 {
199 0 return undef;
200 }
201  
202 0 return $self->_do_one_col_query($query);
203 } # query
204  
205 =head2 pagespec_translate
206  
207 Attempt to translate an IkiWiki-style pagespec into an SQL condition.
208  
209 =cut
210 0 ▶sub pagespec_translate {
Subroutine pagespec_translate: not called CC=11 SCAR=48.8
Pod documented
211 0 my $self = shift;
212 0 my $spec=shift;
213  
214 # Convert spec to SQL.
215 0 my $sql="";
216 0 ▶ while ($spec=~m{
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ / \s* # ignore whitespace ( # 1: match a single word \! # ! | \( # ( | \) # ) | \w+\([^\)]*\) # command(params) | [^\s()]+ # any other text ) \s* # ignore whitespace /gux) 0 0 0
217 \s* # ignore whitespace
218 ( # 1: match a single word
219 \! # !
220 |
221 \( # (
222 |
223 \) # )
224 |
225 \w+\([^\)]*\) # command(params)
226 |
227 [^\s()]+ # any other text
228 )
229 \s* # ignore whitespace
230 }gx)
231 {
232 0 my $word=$1;
233 0 ▶ if (lc $word eq 'and')
Branch: 5 branches
BranchTrueFalseTotal
if (lc $word eq 'and') { } 0 0 0
elsif (lc $word eq 'or') { } 0 0 0
elsif ($word eq '!') { } 0 0 0
elsif ($word eq '(' or $word eq ')') { } 0 0 0
elsif ($word =~ /^(\w+)\((.*)\)$/u) { } 0 0 0
234 {
235 0 $sql.=' AND';
236 }
237 elsif (lc $word eq 'or')
238 {
239 0 $sql.=' OR';
240 }
241 elsif ($word eq '!')
242 {
243 0 $sql.=' NOT';
244 }
245 ▶ elsif ($word eq "(" || $word eq ")")
Condition0%0/3
$word eq "(" or $word eq ")"
l!l&&r!l&&!r
000
MC/DC0%0/2
$word eq "(" or $word eq ")"
$word eq "(" $word eq ")"
Truth table0%0/3
A or B
A= $word eq "("B= $word eq ")"
ABresult
1X1
011
000
246 {
247 0 $sql.=' '.$word;
248 }
249 elsif ($word =~ /^(\w+)\((.*)\)$/)
250 {
251 0 my $func = $1;
252 0 my $func_args_str = $2;
253  
254 # field_item(column value)
255 0 ▶ if ($func eq 'field_item')
Branch: 2 branches
BranchTrueFalseTotal
if ($func eq 'field_item') { } 0 0 0
elsif ($func eq 'where' and $func_args_str) { } 0 0 0
256 {
257 0 my @func_args = split(' ', $func_args_str);
258 0 $sql .= sprintf(" %s = '%s' ",
259 $func_args[0], $func_args[1]);
260 }
261 # where(condition)
262 ▶ elsif ($func eq 'where' and $func_args_str)
Condition0%0/3
$func eq "where" and $func_args_str
!ll&&!rl&&r
000
MC/DC0%0/2
$func eq "where" and $func_args_str
$func eq "where" $func_args_str
Truth table0%0/3
A and B
A= $func eq "where"B= $func_args_str
ABresult
0X0
100
111
263 {
264 # put parens around it just in case
265 0 $sql.=' ( '.$func_args_str.' ) ';
266 }
267 }
268 else
269 {
270 0 $sql.=" page GLOB '$word'";
271 }
272 } # while
273  
274 0 return $sql;
275 } # pagespec_translate
276  
277 =head2 query_pagespec
278  
279 Do a query using an IkiWiki-style pagespec.
280 Always EXCLUDE pages starting with _ because they
281 are special hidden pages.
282  
283 my $results = $self->query($spec);
284  
285 =cut
286  
287 0 ▶sub query_pagespec {
Subroutine query_pagespec: not called CC=2 SCAR=17.9
Pod documented
288 0 my $self = shift;
289 0 my $spec = shift;
290  
291 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
292 {
293 0 return undef;
294 }
295 0 my $where = $self->pagespec_translate($spec);
296 0 my $query = "SELECT page FROM flatfields WHERE bald_name NOT GLOB '_*' AND ($where);";
297  
298 0 return $self->_do_one_col_query($query);
299 } # query_pagespec
300  
301 =head2 pagelist
302  
303 Query the database, return a list of all pages
304  
305 =cut
306  
307 0 ▶sub pagelist {
Subroutine pagelist: not called CC=2 SCAR=17.9
Pod documented
308 0 my $self = shift;
309 0 my %args = @_;
310  
311 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
312 {
313 0 return undef;
314 }
315  
316 0 return $self->_get_all_pagenames();
317 } # pagelist
318  
319 =head2 non_hidden_pagelist
320  
321 Query the database, return a list of all pages
322 NOT including _Header or _Footer pages.
323  
324 =cut
325  
326 0 ▶sub non_hidden_pagelist {
Subroutine non_hidden_pagelist: not called CC=2 SCAR=17.9
Pod documented
327 0 my $self = shift;
328 0 my %args = @_;
329  
330 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
331 {
332 0 return undef;
333 }
334  
335 0 my $pages = $self->_do_one_col_query("SELECT page FROM pagefiles WHERE is_binary IS NULL AND bald_name NOT GLOB '_*' ORDER BY page;");
336 0 return @{$pages};
337 } # non_hidden_pagelist
338  
339 =head2 total_pages
340  
341 Query the database, return the total number of records.
342  
343 =cut
344  
345 0 ▶sub total_pages {
Subroutine total_pages: not called CC=2 SCAR=17.9
Pod documented
346 0 my $self = shift;
347 0 my %args = @_;
348  
349 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
350 {
351 0 return undef;
352 }
353  
354 0 return $self->_total_pages(%args);
355 } # total_pages
356  
357 =head2 page_exists
358  
359 Does this page/file exist in the database?
360  
361 =cut
362  
363 0 ▶sub page_exists {
Subroutine page_exists: not called CC=4 SCAR=30.0
Pod documented
364 0 my $self = shift;
365 0 my $page = shift;
366  
367 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
368 {
369 0 return undef;
370 }
371 0 my $dbh = $self->{dbh};
372  
373 0 my $q = "SELECT COUNT(*) FROM pagefiles WHERE page = ?;";
374 0 my $sth = $self->_prepare($q);
375  
376 0 my $ret = $sth->execute($page);
377 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
378 {
379 0 croak "FAILED to execute '$q' $DBI::errstr";
380 }
381 0 my $total = 0;
382 0 my @row;
383 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
384 {
385 0 $total = $row[0];
386 }
387 0 return $total > 0;
388 } # page_exists
389  
390 =head2 bestlink
391  
392 Which page does the given link match, when linked from the given page?
393  
394 my $linkedpage = $self->bestlink($page,$link);
395  
396 =cut
397  
398 0 ▶sub bestlink {
Subroutine bestlink: not called CC=7 SCAR=40.3
Pod documented
399 0 my $self = shift;
400 0 my $page = shift;
401 0 my $link = shift;
402  
403 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
404 {
405 0 return undef;
406 }
407 0 my $dbh = $self->{dbh};
408  
409 # code based on IkiWiki
410 0 my $cwd=$page;
411 0 ▶ if ($link=~s/^\/+//)
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ s[^/+][]u) 0 0 0
412 {
413 # absolute links
414 0 $cwd="";
415 }
416 0 $link=~s/\/$//;
417  
418 # If the given link ends with .ext suffix
419 # the related page will be the thing without the suffix
420 0 $link =~ s/\.\w+$//;
421  
422 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cwd =~ s[/?[^/]+$][]u and do { do { my $l = $cwd; $l .= '/' if length $l; $l .= $link; my $page_exists = $self->page_exists($l); if ($page_exists) { return $l; } else { my $realpage = $self->_find_pagename($l); return $realpage if $realpage; } } } 0 0 0
423 0 my $l=$cwd;
424 0 ▶ $l.="/" if length $l;
Branch: 1 branch
BranchTrueFalseTotal
if length $l 0 0 0
425 0 $l.=$link;
426  
427 0 my $page_exists = $self->page_exists($l);
428 0 ▶ if ($page_exists)
Branch: 1 branch
BranchTrueFalseTotal
if ($page_exists) { } 0 0 0
429 {
430 0 return $l;
431 }
432 else
433 {
434 0 my $realpage = $self->_find_pagename($l);
435 0 ▶ return $realpage if $realpage;
Branch: 1 branch
BranchTrueFalseTotal
if $realpage 0 0 0
436 }
437 } while $cwd=~s{/?[^/]+$}{};
438  
439 # broken link
440 0 return "";
441 } # bestlink
442  
443 =head1 Helper Functions
444  
445 These are functions which are NOT exported by this plugin.
446  
447 =cut
448  
449 =head2 _connect
450  
451 Connect to the database
452 If we've already connected, do nothing.
453  
454 =cut
455  
456 0 ▶sub _connect {
Subroutine _connect: not called CC=5 SCAR=34.0
457 0 my $self = shift;
458  
459 0 my $old_dbh = $self->{dbh};
460 0 ▶ if ($old_dbh)
Branch: 1 branch
BranchTrueFalseTotal
if ($old_dbh) 0 0 0
461 {
462 0 return 1;
463 }
464  
465 # The database is expected to be an SQLite file
466 # and will be created if it doesn't exist
467 0 my $database = $self->{metadb_db};
468 0 ▶ if ($database)
Branch: 1 branch
BranchTrueFalseTotal
if ($database) { } 0 0 0
469 {
470 0 my $creating_db = 0;
471 0 ▶ if (!-r $database)
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $database) 0 0 0
472 {
473 0 $creating_db = 1;
474 }
475 0 my $dbh = DBI->connect("dbi:SQLite:dbname=$database", "", "");
476 0 ▶ if (!$dbh)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 0 0
477 {
478 0 croak "Can't connect to $database $DBI::errstr";
479 }
480 0 $dbh->{sqlite_unicode} = 1;
481 0 $self->{dbh} = $dbh;
482  
483 # Create the tables if they don't exist
484 0 $self->_create_tables();
485  
486 # cache for prepared statements
487 0 $self->{prepared} = {};
488 }
489 else
490 {
491 0 croak "No Database given." . Dump($self);
492 }
493  
494 0 return 1;
495 } # _connect
496  
497 =head2 _prepare
498  
499 Retrieve or create prepared statement handles.
500  
501 my $sth = $self->_prepare($q);
502  
503 =cut
504 0 ▶sub _prepare {
Subroutine _prepare: not called CC=4 SCAR=30.0
505 0 my $self = shift;
506 0 my $q = shift;
507  
508 0 my $sth;
509 0 ▶ if (exists $self->{prepared}->{$q}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'prepared'}{$q} and defined $self->{'prepared'}{$q}) { } 0 0 0
Condition0%0/3
exists $self->{"prepared"}{$q} and defined $self->{"prepared"}{$q}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"prepared"}{$q} and defined $self->{"prepared"}{$q}
exists $self->{"prepared"}{$q} defined $self->{"prepared"}{$q}
Truth table0%0/3
A and B
A= exists $self->{"prepared"}{$q}B= defined $self->{"prepared"}{$q}
ABresult
0X0
100
111
510 and defined $self->{prepared}->{$q})
511 {
512 0 $sth = $self->{prepared}->{$q};
513 }
514 else
515 {
516 0 $sth = $self->{dbh}->prepare($q);
517 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
518 {
519 0 confess "FAILED to prepare '$q' $DBI::errstr";
520 }
521 0 $self->{prepared}->{$q} = $sth;
522 }
523 0 return $sth;
524 } # _prepare
525  
526 =head2 _create_tables
527  
528 Create the initial tables in the database:
529  
530 pagefiles: (page, title, name, filetype, is_binary, filename, parent_page...)
531 links: (page, links_to)
532 deepfields: (page, field, value)
533  
534 =cut
535  
536 0 ▶sub _create_tables {
Subroutine _create_tables: not called CC=6 SCAR=37.4
537 0 my $self = shift;
538  
539 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
540  
541 0 my $dbh = $self->{dbh};
542  
543 0 my $q = "CREATE TABLE IF NOT EXISTS pagefiles (page PRIMARY KEY, " . join(',', @{$self->{primary_fields}}) . ");";
544 0 my $ret = $dbh->do($q);
545 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
546 {
547 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
548 }
549 0 $q = "CREATE TABLE IF NOT EXISTS links (page, links_to, UNIQUE(page, links_to));";
550 0 $ret = $dbh->do($q);
551 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
552 {
553 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
554 }
555 0 $q = "CREATE TABLE IF NOT EXISTS deepfields (page, field, value, UNIQUE(page, field));";
556 0 $ret = $dbh->do($q);
557 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
558 {
559 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
560 }
561 0 $q = "CREATE UNIQUE INDEX IF NOT EXISTS deepfields_index ON deepfields (page, field)";
562 0 $ret = $dbh->do($q);
563 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
564 {
565 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
566 }
567  
568 0 return 1;
569 } # _create_tables
570  
571 =head2 _generate_derived_tables
572  
573 The flatfields table may or may not need to be re-defined, depending on
574 whether there are new or deleted non-hidden fields since the last time
575 the flatfields table was updated. If the column definitions are unchanged,
576 one does not need to re-define the flatfields table, just update its contents.
577  
578 $self->_generate_derived_tables();
579  
580 =cut
581  
582 0 ▶sub _generate_derived_tables {
Subroutine _generate_derived_tables: not called CC=7 SCAR=40.3
583 0 my $self = shift;
584 0 my %args = @_;
585  
586 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
587  
588 0 my $dbh = $self->{dbh};
589  
590 # ---------------------------------------------------
591 # TABLE: flatfields
592 # ---------------------------------------------------
593
594 # Figure out if we need to re-define the table.
595 0 my $redefine_table = 0;
596
597 # The non-hidden fieldnames should be in alphabetical
598 # order; the column-names will be "page", followed
599 # by fieldnames in alphabetical order.
600 # Therefore, prepending "page" to the fieldnames
601 # should make them match, if the flatfields table
602 # does not need to be re-defined.
603 0 my @fieldnames = $self->_get_all_nonhidden_fieldnames();
604 0 unshift @fieldnames, 'page';
605 0 my @col_names = $self->_column_names('flatfields');
606  
607 # Check if these arrays are the same length
608 0 ▶ if (scalar @fieldnames != scalar @col_names)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @fieldnames != scalar @col_names) { } 0 0 0
609 {
610 0 $redefine_table = 1;
611 }
612 else # check if the contents match
613 {
614 0 ▶ for (my $i=0;
Branch: 1 branch
BranchTrueFalseTotal
if (not $redefine_table and $i < scalar @fieldnames) 0 0 0
Condition0%0/3
not $redefine_table and $i < scalar @fieldnames
!ll&&!rl&&r
000
MC/DC0%0/2
not $redefine_table and $i < scalar @fieldnames
not $redefine_table $i < scalar @fieldnames
Truth table0%0/3
not(A) and B
A= not $redefine_tableB= $i < scalar @fieldnames
ABresult
0X0
100
111
615 (!$redefine_table and $i < scalar @fieldnames);
616 $i++)
617 {
618 0 ▶ if ($fieldnames[$i] ne $col_names[$i])
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldnames[$i] ne $col_names[$i]) 0 0 0
619 {
620 0 $redefine_table = 1;
621 }
622 }
623 }
624 0 ▶ if ($redefine_table)
Branch: 1 branch
BranchTrueFalseTotal
if ($redefine_table) { } 0 0 0
625 {
626 0 $self->_generate_new_derived_tables();
627 }
628 else
629 {
630 0 $self->_update_derived_tables(%args);
631 }
632  
633 0 return 1;
634 } # _generate_derived_tables
635  
636 =head2 _update_derived_tables
637  
638 If the flatfields table definition hasn't been changed, it needs
639 to be updated using the data from the deepfields table.
640 Expects the deepfields table to be up to date, so this needs to be called
641 at the end of the scanning pass.
642 If the "pages" argument exists, just update those pages rather than
643 all the pages.
644  
645 $self->_update_derived_tables(pages=>{%the_pages});
646  
647 =cut
648  
649 0 ▶sub _update_derived_tables {
Subroutine _update_derived_tables: not called CC=11 SCAR=48.8
650 0 my $self = shift;
651 0 my %args = @_;
652  
653 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
654  
655 0 my $dbh = $self->{dbh};
656  
657 # ---------------------------------------------------
658 # TABLE: flatfields
659 # ---------------------------------------------------
660 0 print STDERR "Updating flatfields table\n";
661
662 # We need to insert/update values for existing pages,
663 # and delete values for non-existing pages.
664
665 # Prepare the insert-or-replace query
666 0 my @fieldnames = $self->_get_all_nonhidden_fieldnames();
667 0 my $placeholders = join ", ", ('?') x @fieldnames;
668 0 my $iq = 'INSERT OR REPLACE INTO flatfields (page, '
669 . join(", ", @fieldnames) . ') VALUES (?, ' . $placeholders . ');';
670 0 my $isth = $self->_prepare($iq);
671 0 ▶ if (!$isth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($isth) 0 0 0
672 {
673 0 croak __PACKAGE__ . " failed to prepare '$iq' : $DBI::errstr";
674 }
675  
676 # ============== Use transactions PROPERLY! ====================
677 # (Major bug when I tried to speed up updates by only committing 100
678 # records at a time, since with a large database, you could end up with
679 # a race condition, and the database would be incorrect!)
680 # ==============================================================
681 0 my $ret = $dbh->do("BEGIN TRANSACTION;");
682 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
683 {
684 0 croak __PACKAGE__ . " failed 'BEGIN TRANSACTION' : $DBI::errstr";
685 }
686 0 my $num_trans = 0;
687 my @pagefiles = (defined $args{pages}
688 0 ▶ ? sort keys %{$args{pages}}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'pages'} ? : 0 0 0
689 : $self->_get_all_pagefiles());
690 0 foreach my $page (@pagefiles)
691 {
692 0 my $meta = $self->_get_fields_for_page($page);
693  
694 0 my @values = ();
695 0 foreach my $fn (@fieldnames)
696 {
697 0 my $val = $meta->{$fn};
698 0 ▶ if (!defined $val)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (ref $val) { } 0 0 0
699 {
700 0 push @values, undef;
701 }
702 elsif (ref $val)
703 {
704 0 $val = join("|", @{$val});
705 0 push @values, $val;
706 }
707 else
708 {
709 0 push @values, $val;
710 }
711 }
712 # we now have values to insert
713 0 $ret = $isth->execute($page, @values);
714 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
715 {
716 0 croak __PACKAGE__ . " failed '$iq' (" . join(',', ($page, @values)) . "): $DBI::errstr";
717 }
718 0 $num_trans++;
719 } # for each page
720  
721 # Now, delete rows that don't have matching pages
722 0 my $dq = 'DELETE FROM flatfields WHERE page NOT IN (SELECT page FROM pagefiles);';
723 0 $ret = $dbh->do($dq);
724 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
725 {
726 0 croak "FAILED to execute '$dq' $DBI::errstr";
727 }
728  
729 # Commit ONLY when all updates are done!!!
730 0 $self->_commit();
731
732 0 print STDERR "Updated flatfields table\n";
733 0 return 1;
734 } # _update_derived_tables
735  
736 =head2 _generate_new_derived_tables
737  
738 If the definition of the flatfields table has changed, it needs to be recreated.
739 Create and populate the flatfields table using the data from the deepfields table.
740 Expects the deepfields table to be up to date, so this needs to be called
741 at the end of the scanning pass.
742  
743 $self->_generate_new_derived_tables();
744  
745 =cut
746  
747 0 ▶sub _generate_new_derived_tables {
Subroutine _generate_new_derived_tables: not called CC=15 SCAR=54.8
748 0 my $self = shift;
749  
750 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
751  
752 0 my $dbh = $self->{dbh};
753  
754 # ---------------------------------------------------
755 # TABLE: flatfields
756 # ---------------------------------------------------
757 0 print STDERR "Generating new flatfields table\n";
758
759 # Drop the table, as it is going to be re-defined.
760 0 my $q = "DROP TABLE IF EXISTS flatfields;";
761 0 my $ret = $dbh->do($q);
762 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
763 {
764 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
765 }
766 0 my @fieldnames = $self->_get_all_nonhidden_fieldnames();
767 0 say STDERR "FIELDS: ", join(', ', @fieldnames);
768 0 ▶ if (!@fieldnames)
Branch: 1 branch
BranchTrueFalseTotal
unless (@fieldnames) 0 0 0
769 {
770 0 croak __PACKAGE__ . " empty fieldnames";
771 }
772  
773 # need to define some fields as numeric
774 0 my @field_defs = ();
775 0 foreach my $field (@fieldnames)
776 {
777 0 ▶ if (exists $self->{field_types}->{$field})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'field_types'}{$field}) { } 0 0 0
778 {
779 0 push @field_defs, $field . ' ' . $self->{field_types}->{$field};
780 }
781 else
782 {
783 0 push @field_defs, $field;
784 }
785 }
786 0 ▶ if (!@field_defs)
Branch: 1 branch
BranchTrueFalseTotal
unless (@field_defs) 0 0 0
787 {
788 0 croak __PACKAGE__ . " empty field_defs";
789 }
790 0 $q = "CREATE TABLE IF NOT EXISTS flatfields (page PRIMARY KEY, "
791 . join(", ", @field_defs) .");";
792 0 $ret = $dbh->do($q);
793 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
794 {
795 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
796 }
797  
798 # prepare the insert query
799 0 my $placeholders = join ", ", ('?') x @fieldnames;
800 0 my $iq = 'INSERT INTO flatfields (page, '
801 . join(", ", @fieldnames) . ') VALUES (?, ' . $placeholders . ');';
802 0 my $isth = $self->_prepare($iq);
803 0 ▶ if (!$isth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($isth) 0 0 0
804 {
805 0 croak __PACKAGE__ . " failed to prepare '$iq' : $DBI::errstr";
806 }
807  
808 # --------------------------------
809 # Insert values for all the pages
810 # --------------------------------
811 # ============== Use transactions PROPERLY! ====================
812 # (Major bug when I tried to speed up updates by only committing one
813 # hundred records at a time, since with a large database, you could end up
814 # with a race condition, and the database would be incorrect!)
815 # ==============================================================
816 0 $ret = $dbh->do("BEGIN TRANSACTION;");
817 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
818 {
819 0 croak __PACKAGE__ . " failed 'BEGIN TRANSACTION' : $DBI::errstr";
820 }
821 0 my $num_trans = 0;
822 0 my @pagefiles = $self->_get_all_pagefiles();
823 0 foreach my $page (@pagefiles)
824 {
825 0 my $meta = $self->_get_fields_for_page($page);
826  
827 0 my @values = ();
828 0 foreach my $fn (@fieldnames)
829 {
830 0 my $val = $meta->{$fn};
831 0 ▶ if (!defined $val)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (ref $val) { } 0 0 0
832 {
833 0 push @values, undef;
834 }
835 elsif (ref $val)
836 {
837 0 $val = join("|", @{$val});
838 0 push @values, $val;
839 }
840 else
841 {
842 0 push @values, $val;
843 }
844 }
845 # we now have values to insert
846 0 $ret = $isth->execute($page, @values);
847 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
848 {
849 0 croak __PACKAGE__ . " failed '$iq' (" . join(',', ($page, @values)) . "): $DBI::errstr";
850 }
851 0 $num_trans++;
852  
853 } # for each page
854 0 $self->_commit();
855  
856 0 print STDERR "Generated flatfields table\n";
857 0 return 1;
858 } # _generate_new_derived_tables
859  
860 =head2 _drop_main_tables
861  
862 Drop all the tables in the database except the flatfields table (which will be done
863 just before it is recreated)
864 If one is doing a scan-all-pages pass, dropping and re-creating may be quicker than updating.
865  
866 =cut
867  
868 0 ▶sub _drop_main_tables {
Subroutine _drop_main_tables: not called CC=4 SCAR=30.0
869 0 my $self = shift;
870  
871 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
872  
873 0 my $dbh = $self->{dbh};
874  
875 0 foreach my $table (qw(pagefiles links deepfields))
876 {
877 0 my $q = "DROP TABLE IF EXISTS $table;";
878 0 my $ret = $dbh->do($q);
879 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
880 {
881 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
882 }
883 }
884  
885 0 return 1;
886 } # _drop_main_tables
887  
888 =head2 _update_some_entries
889  
890 Update some pages, adding new ones and updating existing ones.
891 This does NOT delete pages.
892  
893 $self->_update_some_entries($page=>{...},$page2=>{}...);
894  
895 =cut
896 0 ▶sub _update_some_entries {
Subroutine _update_some_entries: not called CC=3 SCAR=24.8
897 0 my $self = shift;
898 0 my %pages = @_;
899  
900 0 my $dbh = $self->{dbh};
901  
902 # --------------------------------------
903 # Update/add pages
904 # --------------------------------------
905 # ============== Use transactions PROPERLY! ====================
906 # (Major bug when I tried to speed up updates by only committing 100
907 # records at a time, since with a large database, you could end up with
908 # a race condition, and the database would be incorrect!)
909 # ==============================================================
910 0 my $ret = $dbh->do("BEGIN TRANSACTION;");
911 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
912 {
913 0 croak __PACKAGE__ . " failed 'BEGIN TRANSACTION' : $DBI::errstr";
914 }
915 0 my $num_trans = 0;
916 0 foreach my $pn (sort keys %pages)
917 {
918 0 print STDERR "UPDATING $pn\n";
919 0 $self->_add_page_data($pn, %{$pages{$pn}});
920 # do the commits in bursts
921 0 $num_trans++;
922 }
923 0 $self->_commit();
924  
925 0 print STDERR "UPDATING DONE\n";
926 } # _update_some_entries
927  
928 =head2 _update_all_entries
929  
930 Update all pages, adding new ones and deleting non-existent ones.
931 This expects that the pages passed in are the DEFINITIVE list of pages,
932 and if a page isn't in this list, it no longer exists.
933  
934 $self->_update_all_entries($page=>{...},$page2=>{}...);
935  
936 =cut
937 0 ▶sub _update_all_entries {
Subroutine _update_all_entries: not called CC=3 SCAR=24.8
938 0 my $self = shift;
939 0 my %pages = @_;
940  
941 0 my $dbh = $self->{dbh};
942  
943 # it may save time to drop all the tables and create them again
944 0 $self->_drop_main_tables();
945 0 $self->_create_tables();
946  
947 # --------------------------------------
948 # Update/add pages
949 # --------------------------------------
950 # ============== Use transactions PROPERLY! ====================
951 # (Major bug when I tried to speed up updates by only committing 100
952 # records at a time, since with a large database, you could end up with
953 # a race condition, and the database would be incorrect!)
954 # ==============================================================
955 0 my $ret = $dbh->do("BEGIN TRANSACTION;");
956 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
957 {
958 0 croak __PACKAGE__ . " failed 'BEGIN TRANSACTION' : $DBI::errstr";
959 }
960 0 my $num_trans = 0;
961 0 foreach my $pn (sort keys %pages)
962 {
963 0 print STDERR "UPDATING $pn\n";
964 0 $self->_add_page_data($pn, %{$pages{$pn}});
965 # do the commits in bursts
966 0 $num_trans++;
967 }
968 0 $self->_commit();
969  
970 0 print STDERR "UPDATING DONE\n";
971 } # _update_all_entries
972  
973 =head2 _commit
974  
975 Commit a pending transaction.
976  
977 $self->_commit();
978  
979 =cut
980 0 ▶sub _commit ($%) {
Subroutine _commit: not called CC=3 SCAR=24.8
981 0 my $self = shift;
982 0 my %args = @_;
983 0 my $meta = $args{meta};
984  
985 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
986  
987 0 my $ret = $self->{dbh}->do("COMMIT;");
988 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
if (not $ret) 0 0 0
989 {
990 0 croak __PACKAGE__ . " failed 'COMMIT' : $DBI::errstr";
991 }
992 } # _commit
993  
994 =head2 _get_all_pagefiles
995  
996 List of all pagefiles
997  
998 $dbtable->_get_all_pagefiles(%args);
999  
1000 =cut
1001  
1002 0 ▶sub _get_all_pagefiles {
Subroutine _get_all_pagefiles: not called CC=1 SCAR=6.9
1003 0 my $self = shift;
1004 0 my %args = @_;
1005  
1006 0 my $dbh = $self->{dbh};
1007 0 my $pages = $self->_do_one_col_query("SELECT page FROM pagefiles ORDER BY page;");
1008  
1009 0 return @{$pages};
1010 } # _get_all_pagefiles
1011  
1012 =head2 _get_all_pagenames
1013  
1014 List of all pagenames
1015  
1016 $dbtable->_get_all_pagenames();
1017  
1018 =cut
1019  
1020 0 ▶sub _get_all_pagenames {
Subroutine _get_all_pagenames: not called CC=1 SCAR=6.9
1021 0 my $self = shift;
1022  
1023 0 my $dbh = $self->{dbh};
1024 0 my $pages = $self->_do_one_col_query("SELECT page FROM pagefiles WHERE is_binary IS NULL ORDER BY page;");
1025  
1026 0 return @{$pages};
1027 } # _get_all_pagenames
1028  
1029 =head2 _get_all_nonhidden_fieldnames
1030  
1031 List of the unique non-hidden field-names from the deepfields table.
1032 Hidden field names start with '_' and are not supposed to be put into the flatfields table,
1033 though they can be queried from the deepfields table.
1034  
1035 @fieldnames = $self->_get_all_nonhidden_fieldnames();
1036  
1037 =cut
1038  
1039 0 ▶sub _get_all_nonhidden_fieldnames {
Subroutine _get_all_nonhidden_fieldnames: not called CC=1 SCAR=6.9
1040 0 my $self = shift;
1041  
1042 0 my $dbh = $self->{dbh};
1043 0 my $fields = $self->_do_one_col_query("SELECT DISTINCT field FROM deepfields WHERE field NOT GLOB '_*' ORDER BY field;");
1044  
1045 0 return @{$fields};
1046 } # _get_all_nonhidden_fieldnames
1047  
1048 =head2 _get_fields_for_page
1049  
1050 Get the field-value pairs for a single page from the deepfields table.
1051  
1052 $meta = $self->_get_fields_for_page($page);
1053  
1054 =cut
1055  
1056 0 ▶sub _get_fields_for_page {
Subroutine _get_fields_for_page: not called CC=5 SCAR=34.0
1057 0 my $self = shift;
1058 0 my $pagename = shift;
1059  
1060 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
1061 0 my $dbh = $self->{dbh};
1062 0 my $q = "SELECT field, value FROM deepfields WHERE page = ?;";
1063  
1064 0 my $sth = $self->_prepare($q);
1065 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1066 {
1067 0 croak "FAILED to prepare '$q' $DBI::errstr";
1068 }
1069 0 my $ret = $sth->execute($pagename);
1070 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1071 {
1072 0 croak "FAILED to execute '$q' $DBI::errstr";
1073 }
1074 0 my %meta = ();
1075 0 my @row;
1076 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
1077 {
1078 0 my $field = $row[0];
1079 0 my $value = $row[1];
1080 0 $meta{$field} = $value;
1081 }
1082  
1083 0 return \%meta;
1084 } # _get_fields_for_page
1085  
1086 =head2 _get_children_for_page
1087  
1088 Get the "child" pages for this page from the pagefiles table.
1089  
1090 $meta = $self->_get_children_for_page($page);
1091  
1092 =cut
1093  
1094 0 ▶sub _get_children_for_page {
Subroutine _get_children_for_page: not called CC=2 SCAR=17.9
1095 0 my $self = shift;
1096 0 my $pagename = shift;
1097  
1098 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
1099 0 my $dbh = $self->{dbh};
1100 0 my $children = $self->_do_one_col_query("SELECT page FROM pagefiles WHERE parent_page = '$pagename' AND is_binary IS NULL;");
1101  
1102 0 return $children;
1103 } # _get_children_for_page
1104  
1105 =head2 _get_attachments_for_page
1106  
1107 Get the "attachments" non-pages for this page from the pagefiles table.
1108  
1109 $meta = $self->_get_attachments_for_page($page);
1110  
1111 =cut
1112  
1113 0 ▶sub _get_attachments_for_page {
Subroutine _get_attachments_for_page: not called CC=2 SCAR=17.9
1114 0 my $self = shift;
1115 0 my $pagename = shift;
1116  
1117 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
1118 0 my $dbh = $self->{dbh};
1119 0 my $attachments = $self->_do_one_col_query("SELECT pagesrcname FROM pagefiles WHERE parent_page = '$pagename' AND is_binary IS NOT NULL;");
1120  
1121 0 return $attachments;
1122 } # _get_attachments_for_page
1123  
1124 =head2 _get_links_for_page
1125  
1126 Get the "links" pages for this page from the links table.
1127  
1128 $meta = $self->_get_links_for_page($page);
1129  
1130 =cut
1131  
1132 0 ▶sub _get_links_for_page {
Subroutine _get_links_for_page: not called CC=2 SCAR=17.9
1133 0 my $self = shift;
1134 0 my $pagename = shift;
1135  
1136 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
1137 0 my $dbh = $self->{dbh};
1138 0 my $links = $self->_do_one_col_query("SELECT links_to FROM links WHERE page = '$pagename'");
1139  
1140 0 return $links;
1141 } # _get_links_for_page
1142  
1143 =head2 _get_page_meta
1144  
1145 Get the meta-data for a single page.
1146  
1147 $meta = $self->_get_page_meta($page);
1148  
1149 =cut
1150  
1151 0 ▶sub _get_page_meta {
Subroutine _get_page_meta: not called CC=9 SCAR=45.0
1152 0 my $self = shift;
1153 0 my $pagename = shift;
1154  
1155 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
1156 0 my $dbh = $self->{dbh};
1157  
1158 0 my $q = "SELECT * FROM pagefiles WHERE page = ?;";
1159  
1160 0 my $sth = $self->_prepare($q);
1161 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1162 {
1163 0 croak "FAILED to prepare '$q' $DBI::errstr";
1164 }
1165 0 my $ret = $sth->execute($pagename);
1166 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1167 {
1168 0 croak "FAILED to execute '$q' $DBI::errstr";
1169 }
1170 # return the first matching row because there should be only one row
1171 0 my $meta = $sth->fetchrow_hashref;
1172 0 ▶ if (!$meta)
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta) 0 0 0
1173 {
1174 0 return undef;
1175 }
1176 # Note that there are three different kinds of files we have data on:
1177 # binary files: just the basic info in the pagefiles table
1178 # binary-files-with-filetypes: which have additional meta-data (for example, image file meta-data)
1179 # pages: which have additional meta-data, and also children, attachments, and links
1180 0 ▶ if ($meta->{filetype})
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"filetype"}) 0 0 0
1181 {
1182 # Now the rest of the meta, if this has meta
1183 # Get this from the deepfields table rather than the flatfields table
1184 # because the deepfields table includes "hidden" fields.
1185 0 my $more_meta = $self->_get_fields_for_page($pagename);
1186  
1187 0 foreach my $key (keys %{$more_meta})
1188 {
1189 0 ▶ if (!exists $meta->{$key})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $meta->{$key}) 0 0 0
1190 {
1191 0 $meta->{$key} = $more_meta->{$key};
1192 }
1193 }
1194  
1195 # binary file pages don't have links or children
1196 # and only one attachment: itself
1197 0 ▶ if ($meta->{is_binary})
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{'is_binary'}) { } 0 0 0
1198 {
1199 0 $meta->{attachments} = [$meta->{pagesrcname}];
1200 }
1201 else # non-binary pages have children, links and attachments
1202 {
1203 # get multi-valued fields from other tables
1204 0 $meta->{children} = $self->_get_children_for_page($pagename);
1205 0 $meta->{links} = $self->_get_links_for_page($pagename);
1206 0 $meta->{attachments} = $self->_get_attachments_for_page($pagename);
1207 }
1208 }
1209  
1210 0 return $meta;
1211 } # _get_page_meta
1212  
1213 =head2 _do_one_col_query
1214  
1215 Do a SELECT query, and return the first column of results.
1216 This is a freeform query, so the caller must be careful to formulate it correctly.
1217  
1218 my $results = $self->_do_one_col_query($query);
1219  
1220 =cut
1221  
1222 0 ▶sub _do_one_col_query {
Subroutine _do_one_col_query: not called CC=5 SCAR=34.0
1223 0 my $self = shift;
1224 0 my $q = shift;
1225  
1226 0 ▶ if ($q !~ /^SELECT /)
Branch: 1 branch
BranchTrueFalseTotal
unless ($q =~ /^SELECT /u) 0 0 0
1227 {
1228 # bad boy! Not a SELECT.
1229 0 return undef;
1230 }
1231 0 my $dbh = $self->{dbh};
1232  
1233 0 my $sth = $self->_prepare($q);
1234 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1235 {
1236 0 croak "FAILED to prepare '$q' $DBI::errstr";
1237 }
1238 0 my $ret = $sth->execute();
1239 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1240 {
1241 0 croak "FAILED to execute '$q' $DBI::errstr";
1242 }
1243 0 my @results = ();
1244 0 my @row;
1245 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
1246 {
1247 0 push @results, $row[0];
1248 }
1249 0 return \@results;
1250 } # _do_one_col_query
1251  
1252 =head2 _column_names
1253  
1254 Get the column names of the given table.
1255  
1256 my @col_names = $self->_column_names($table);
1257  
1258 =cut
1259  
1260 0 ▶sub _column_names {
Subroutine _column_names: not called CC=4 SCAR=30.0
1261 0 my $self = shift;
1262 0 my $tbl_name = shift;
1263  
1264 # The PRAGMA table_info returns ALL the information
1265 # about the given table, as a table with columns:
1266 # cid|name|type|notnull|dflt_value|pk
1267 # We are interested in the second column, "name".
1268 0 my $q = "PRAGMA table_info($tbl_name);";
1269 0 my $dbh = $self->{dbh};
1270  
1271 0 my $sth = $self->_prepare($q);
1272 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1273 {
1274 0 croak "FAILED to prepare '$q' $DBI::errstr";
1275 }
1276 0 my $ret = $sth->execute();
1277 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1278 {
1279 0 croak "FAILED to execute '$q' $DBI::errstr";
1280 }
1281 0 my @results = ();
1282 0 my @row;
1283 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
1284 {
1285 # The "name" is the second column.
1286 # Second column starting from 0 is 1
1287 0 push @results, $row[1];
1288 }
1289 0 return @results;
1290 } # _column_names
1291  
1292 =head2 _total_pagefiles
1293  
1294 Find the total records in the database.
1295  
1296 $dbtable->_total_pagefiles();
1297  
1298 =cut
1299  
1300 0 ▶sub _total_pagefiles {
Subroutine _total_pagefiles: not called CC=4 SCAR=30.0
1301 0 my $self = shift;
1302  
1303 0 my $dbh = $self->{dbh};
1304  
1305 0 my $q = "SELECT COUNT(*) FROM pagefiles;";
1306  
1307 0 my $sth = $self->_prepare($q);
1308 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1309 {
1310 0 croak "FAILED to prepare '$q' $DBI::errstr";
1311 }
1312 0 my $ret = $sth->execute();
1313 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1314 {
1315 0 croak "FAILED to execute '$q' $DBI::errstr";
1316 }
1317 0 my $total = 0;
1318 0 my @row;
1319 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
1320 {
1321 0 $total = $row[0];
1322 }
1323 0 return $total;
1324 } # _total_pagefiles
1325  
1326 =head2 _total_pages
1327  
1328 Find the total number of pages.
1329  
1330 $dbtable->_total_pages();
1331  
1332 =cut
1333  
1334 0 ▶sub _total_pages {
Subroutine _total_pages: not called CC=4 SCAR=30.0
1335 0 my $self = shift;
1336  
1337 0 my $dbh = $self->{dbh};
1338  
1339 0 my $q = "SELECT COUNT(*) FROM pagefiles WHERE is_binary IS NULL;";
1340  
1341 0 my $sth = $self->_prepare($q);
1342 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1343 {
1344 0 croak "FAILED to prepare '$q' $DBI::errstr";
1345 }
1346 0 my $ret = $sth->execute();
1347 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1348 {
1349 0 croak "FAILED to execute '$q' $DBI::errstr";
1350 }
1351 0 my $total = 0;
1352 0 my @row;
1353 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
1354 {
1355 0 $total = $row[0];
1356 }
1357 0 return $total;
1358 } # _total_pages
1359  
1360 =head2 _find_pagename
1361  
1362 Does this page exist in the database?
1363 This does a case-insensitive check if there isn't an exact match.
1364 Returns the real pagename if it is found, otherwise empty string.
1365  
1366 =cut
1367  
1368 0 ▶sub _find_pagename {
Subroutine _find_pagename: not called CC=7 SCAR=40.3
1369 0 my $self = shift;
1370 0 my $page = shift;
1371  
1372 0 ▶ if (!$self->_connect())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_connect) 0 0 0
1373 {
1374 0 return undef;
1375 }
1376 0 ▶ if ($self->page_exists($page))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->page_exists($page)) 0 0 0
1377 {
1378 0 return $page;
1379 }
1380  
1381 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
1382 0 my $dbh = $self->{dbh};
1383  
1384 # set both the column and the query to uppercase
1385 0 my $q = "SELECT page FROM pagefiles WHERE UPPER(page) = ?;";
1386 0 my $upper_page = uc($page);
1387  
1388 0 my $sth = $self->_prepare($q);
1389 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1390 {
1391 0 croak "FAILED to prepare '$q' $DBI::errstr";
1392 }
1393 0 my $ret = $sth->execute($upper_page);
1394 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1395 {
1396 0 croak "FAILED to execute '$q' $DBI::errstr";
1397 }
1398 0 my $realpage = '';
1399 0 my @row;
1400 0 ▶ while (@row = $sth->fetchrow_array)
Branch: 1 branch
BranchTrueFalseTotal
if (@row = $sth->fetchrow_array) 0 0 0
1401 {
1402 0 $realpage = $row[0];
1403 }
1404 0 return $realpage;
1405 } # _find_pagename
1406  
1407 =head2 _add_page_data
1408  
1409 Add metadata to db for one page.
1410  
1411 $self->_add_page_data($page, %meta);
1412  
1413 =cut
1414 0 ▶sub _add_page_data {
Subroutine _add_page_data: not called CC=29 SCAR=67.7
1415 0 my $self = shift;
1416 0 my $pagename = shift;
1417 0 my %meta = @_;
1418  
1419 0 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
1420 0 my $dbh = $self->{dbh};
1421  
1422 # ------------------------------------------------
1423 # TABLE: pagefiles
1424 # ------------------------------------------------
1425 0 my @values = ();
1426 0 foreach my $fn (@{$self->{primary_fields}})
1427 {
1428 0 my $val = $meta{$fn};
1429 0 ▶ if (!defined $val)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (ref $val) { } 0 0 0
1430 {
1431 0 push @values, undef;
1432 }
1433 elsif (ref $val)
1434 {
1435 0 $val = join("|", @{$val});
1436 0 push @values, $val;
1437 }
1438 else
1439 {
1440 0 push @values, $val;
1441 }
1442 }
1443  
1444 # Check if the page exists in the table
1445 # and do an INSERT or UPDATE depending on whether it does.
1446 # This is faster than REPLACE because it doesn't need
1447 # to rebuild indexes.
1448 0 my $page_exists = $self->page_exists($pagename);
1449 0 my $q;
1450 my $ret;
1451 0 ▶ if ($page_exists)
Branch: 1 branch
BranchTrueFalseTotal
if ($page_exists) { } 0 0 0
1452 {
1453 0 $q = "UPDATE pagefiles SET ";
1454 0 ▶ for (my $i=0; $i < @values; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 0 0 0
1455 {
1456 0 $q .= sprintf('%s = ?', $self->{primary_fields}->[$i]);
1457 0 ▶ if ($i + 1 < @values)
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < @values) 0 0 0
1458 {
1459 0 $q .= ", ";
1460 }
1461 }
1462 0 $q .= " WHERE page = '$pagename';";
1463 0 my $sth = $self->_prepare($q);
1464 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1465 {
1466 0 croak __PACKAGE__ . " failed to prepare '$q' : $DBI::errstr";
1467 }
1468 0 $ret = $sth->execute(@values);
1469 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1470 {
1471 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
1472 }
1473 }
1474 else
1475 {
1476 0 my $placeholders = join ", ", ('?') x @{$self->{primary_fields}};
1477 $q = 'INSERT INTO pagefiles (page, '
1478 0 . join(", ", @{$self->{primary_fields}}) . ') VALUES (?, ' . $placeholders . ');';
1479 0 my $sth = $self->_prepare($q);
1480 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1481 {
1482 0 croak __PACKAGE__ . " failed to prepare '$q' : $DBI::errstr";
1483 }
1484 0 $ret = $sth->execute($pagename, @values);
1485 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1486 {
1487 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
1488 }
1489 }
1490  
1491 # ------------------------------------------------
1492 # TABLE: links
1493 # ------------------------------------------------
1494 0 ▶ if (exists $meta{links} and defined $meta{links})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $meta{"links"} and defined $meta{"links"}) 0 0 0
Condition0%0/3
exists $meta{"links"} and defined $meta{"links"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $meta{"links"} and defined $meta{"links"}
exists $meta{"links"} defined $meta{"links"}
Truth table0%0/3
A and B
A= exists $meta{"links"}B= defined $meta{"links"}
ABresult
0X0
100
111
1495 {
1496 0 my @links = ();
1497 0 ▶ if (ref $meta{links})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $meta{'links'}) { } 0 0 0
1498 {
1499 0 @links = @{$meta{links}};
1500 }
1501 else # one scalar link
1502 {
1503 0 push @links, $meta{links};
1504 }
1505 0 foreach my $link (@links)
1506 {
1507 # the "OR IGNORE" allows duplicates to be silently discarded
1508 0 $q = "INSERT OR IGNORE INTO links(page, links_to) VALUES(?, ?);";
1509 0 my $sth = $self->_prepare($q);
1510 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1511 {
1512 0 croak __PACKAGE__ . " failed to prepare '$q' : $DBI::errstr";
1513 }
1514 0 $ret = $sth->execute($pagename, $link);
1515 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1516 {
1517 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
1518 }
1519 }
1520 }
1521 # ------------------------------------------------
1522 # TABLE: deepfields
1523 #
1524 # This is for all the meta-data about a page
1525 # apart from multi-valued things like links
1526 # Add both pages and non-pages, because non-pages like images
1527 # can also have extra meta-data.
1528 # ------------------------------------------------
1529  
1530 # Need to delete the fields that are no longer there
1531 0 my $oldmeta = $self->_get_fields_for_page($pagename);
1532 0 foreach my $field (sort keys %{$oldmeta})
1533 {
1534 0 ▶ if (!exists $meta{$field})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $meta{$field}) 0 0 0
1535 {
1536 0 $q = "DELETE FROM deepfields WHERE page = ? AND field = ?;";
1537 0 my $sth = $self->_prepare($q);
1538 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1539 {
1540 0 croak __PACKAGE__ . " failed to prepare '$q' : $DBI::errstr";
1541 }
1542 0 $ret = $sth->execute($pagename, $field);
1543 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1544 {
1545 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
1546 }
1547 }
1548 }
1549 # Now update with the new values
1550 0 foreach my $field (sort keys %meta)
1551 {
1552 0 ▶ if ($field ne 'links')
Branch: 1 branch
BranchTrueFalseTotal
if ($field ne "links") 0 0 0
1553 {
1554 0 my $value = $meta{$field};
1555 # force all field-names to be lower-case
1556 0 my $fieldname = lc($field);
1557  
1558 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
1559 0 ▶ if (ref $value eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value) { } 0 0 0
1560 {
1561 0 $value = join("|", @{$value});
1562 }
1563 elsif (ref $value)
1564 {
1565 0 $value = Dump($value);
1566 }
1567  
1568 0 $q = "INSERT OR REPLACE INTO deepfields(page, field, value) VALUES(?, ?, ?);";
1569 0 my $sth = $self->_prepare($q);
1570 0 ▶ if (!$sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
1571 {
1572 0 croak __PACKAGE__ . " failed to prepare '$q' : $DBI::errstr";
1573 }
1574 0 $ret = $sth->execute($pagename, $fieldname, $value);
1575 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1576 {
1577 0 croak __PACKAGE__ . " failed '$q' : $DBI::errstr";
1578 }
1579 }
1580 }
1581  
1582 0 return 1;
1583 } # _add_page_data
1584  
1585 0 ▶sub _delete_page_from_db {
Subroutine _delete_page_from_db: not called CC=3 SCAR=24.8
1586 0 my $self = shift;
1587 0 my $page = shift;
1588  
1589 0 my $dbh = $self->{dbh};
1590  
1591 0 foreach my $table (qw(pagefiles links deepfields flatfields))
1592 {
1593 0 my $q = "DELETE FROM $table WHERE page = ?;";
1594 0 my $sth = $self->_prepare($q);
1595 0 my $ret = $sth->execute($page);
1596 0 ▶ if (!$ret)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
1597 {
1598 0 croak __PACKAGE__, "FAILED query '$q' $DBI::errstr";
1599 }
1600 }
1601  
1602 0 return 1;
1603 } # _delete_page_from_db
1604  
1605 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=6 SCAR=37.4
1606 0 my $self = shift;
1607  
1608 0 ▶ if (exists $self->{dbh}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dbh"} and defined $self->{"dbh"} and ref $self->{"dbh"}) 0 0 0
Condition0%0/6
exists $self->{"dbh"} and defined $self->{"dbh"} and ref $self->{"dbh"}
!ll&&!rl&&r
000
exists $self->{"dbh"} and defined $self->{"dbh"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{"dbh"} and defined $self->{"dbh"} and ref $self->{"dbh"}
exists $self->{"dbh"} defined $self->{"dbh"} ref $self->{"dbh"}
Truth table0%0/4
A and B and C
A= exists $self->{"dbh"}B= defined $self->{"dbh"}C= ref $self->{"dbh"}
ABCresult
0XX0
10X0
1100
1111
1609 and defined $self->{dbh}
1610 and ref $self->{dbh})
1611 {
1612 0 ▶ if (exists $self->{prepared})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"prepared"}) 0 0 0
1613 {
1614 0 foreach my $q (keys %{$self->{prepared}})
1615 {
1616 0 delete $self->{prepared}->{$q};
1617 }
1618 }
1619 0 $self->{dbh}->disconnect();
1620 }
1621 } # DESTROY
1622  
1623 26 1; # End of Muster::MetaDb
1624 __END__
« Muster/Hook/SqlReport.pm ↑ Summary Muster/Hook/Directives.pm »