File Coverage

blib/lib/WWW/Scraper/Wikipedia/ISO3166/Database.pm
Criterion Covered Total %
statement 27 95 28.4
branch 0 12 0.0
condition n/a
subroutine 9 23 39.1
pod 13 14 92.8
total 49 144 34.0


line stmt bran cond sub pod time code
1             package WWW::Scraper::Wikipedia::ISO3166::Database;
2              
3 1     1   104624 use parent 'WWW::Scraper::Wikipedia::ISO3166';
  1         1  
  1         4  
4 1     1   39 use strict;
  1         1  
  1         14  
5 1     1   3 use warnings;
  1         1  
  1         23  
6              
7 1     1   615 use DBD::SQLite;
  1         17565  
  1         5  
8              
9 1     1   27 use DBI;
  1         1  
  1         32  
10              
11 1     1   458 use DBIx::Admin::CreateTable;
  1         2343  
  1         5  
12              
13 1     1   399 use File::Slurper 'read_dir';
  1         1393  
  1         43  
14              
15 1     1   5 use Moo;
  1         1  
  1         3  
16              
17 1     1   156 use Types::Standard qw/Any HashRef Str/;
  1         1  
  1         5  
18              
19             has attributes =>
20             (
21             default => sub{return {AutoCommit => 1, RaiseError => 1, sqlite_unicode => 1} },
22             is => 'rw',
23             isa => HashRef,
24             required => 0,
25             );
26              
27             has creator =>
28             (
29             default => sub{return ''},
30             is => 'rw',
31             isa => Any,
32             required => 0,
33             );
34              
35             has dbh =>
36             (
37             default => sub{return ''},
38             is => 'rw',
39             isa => Any,
40             required => 0,
41             );
42              
43             has dsn =>
44             (
45             default => sub{return ''},
46             is => 'rw',
47             isa => Str,
48             required => 0,
49             );
50              
51             has engine =>
52             (
53             default => sub{return ''},
54             is => 'rw',
55             isa => Str,
56             required => 0,
57             );
58              
59             has password =>
60             (
61             default => sub{return ''},
62             is => 'rw',
63             isa => Str,
64             required => 0,
65             );
66              
67             has time_option =>
68             (
69             default => sub{return ''},
70             is => 'rw',
71             isa => Str,
72             required => 0,
73             );
74              
75             has username =>
76             (
77             default => sub{return ''},
78             is => 'rw',
79             isa => Str,
80             required => 0,
81             );
82              
83             our $VERSION = '2.00';
84              
85             # -----------------------------------------------
86              
87             sub BUILD
88             {
89 0     0 0   my($self) = @_;
90              
91 0           $self -> dsn('dbi:SQLite:dbname=' . $self -> sqlite_file);
92 0 0         $self -> dbh(DBI -> connect($self -> dsn, $self -> username, $self -> password, $self -> attributes) ) || die $DBI::errstr;
93 0           $self -> dbh -> do('PRAGMA foreign_keys = ON');
94              
95 0           $self -> creator
96             (
97             DBIx::Admin::CreateTable -> new
98             (
99             dbh => $self -> dbh,
100             verbose => 0,
101             )
102             );
103              
104 0 0         $self -> engine
105             (
106             $self -> creator -> db_vendor =~ /(?:Mysql)/i ? 'engine=innodb' : ''
107             );
108              
109 0 0         $self -> time_option
110             (
111             $self -> creator -> db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : ''
112             );
113              
114             } # End of BUILD.
115              
116             # -----------------------------------------------
117              
118             sub find_subcountry_downloads
119             {
120 0     0 1   my($self) = @_;
121 0           my(@file) = read_dir('data');
122              
123 0           return [grep{length == 2} grep{s/^.+\.([A-Z]{2,2})\..+$/$1/; $_} @file];
  0            
  0            
  0            
124              
125             } # End of find_subcountry_downloads.
126              
127             # ----------------------------------------------
128              
129             sub get_country_count
130             {
131 0     0 1   my($self) = @_;
132              
133 0           return ($self -> dbh -> selectrow_array('select count(*) from countries') )[0];
134              
135             } # End of get_country_count.
136              
137             # -----------------------------------------------
138              
139             sub get_statistics
140             {
141 0     0 1   my($self) = @_;
142              
143             return
144             {
145             countries_in_db => $self -> get_country_count,
146 0           has_subcounties => $#{$self -> who_has_subcountries} + 1,
147             subcountries_in_db => $self -> get_subcountry_count,
148             subcountry_categories_in_db => $self -> get_subcountry_category_count,
149 0           subcountry_files_downloaded => scalar @{$self -> find_subcountry_downloads},
  0            
150             subcountry_info_in_db => $self -> get_subcountry_info_count,
151             };
152              
153             } # End of get_statistics.
154              
155             # ----------------------------------------------
156              
157             sub get_subcountry_count
158             {
159 0     0 1   my($self) = @_;
160              
161 0           return ($self -> dbh -> selectrow_array('select count(*) from subcountries') )[0];
162              
163             } # End of get_subcountry_count.
164              
165             # ----------------------------------------------
166              
167             sub get_subcountry_category_count
168             {
169 0     0 1   my($self) = @_;
170              
171 0           return ($self -> dbh -> selectrow_array('select count(*) from subcountry_categories') )[0];
172              
173             } # End of get_subcountry_category_count.
174              
175             # ----------------------------------------------
176              
177             sub get_subcountry_info_count
178             {
179 0     0 1   my($self) = @_;
180              
181 0           return ($self -> dbh -> selectrow_array('select count(*) from subcountry_info') )[0];
182              
183             } # End of get_subcountry_info_count.
184              
185             # ----------------------------------------------
186              
187             sub read_countries_table
188             {
189 0     0 1   my($self) = @_;
190 0           my($sth) = $self -> dbh -> prepare('select * from countries');
191              
192 0           $sth -> execute;
193 0           $sth -> fetchall_hashref('id');
194              
195             } # End of read_countries_table.
196              
197             # ----------------------------------------------
198              
199             sub read_subcountries_table
200             {
201 0     0 1   my($self) = @_;
202 0           my($sth) = $self -> dbh -> prepare('select * from subcountries');
203              
204 0           $sth -> execute;
205 0           $sth -> fetchall_hashref('id');
206              
207             } # End of read_subcountries_table.
208              
209             # ----------------------------------------------
210              
211             sub read_subcountry_categories_table
212             {
213 0     0 1   my($self) = @_;
214 0           my($sth) = $self -> dbh -> prepare('select * from subcountry_categories');
215              
216 0           $sth -> execute;
217 0           $sth -> fetchall_hashref('id');
218              
219             } # End of read_subcountry_categories_table.
220              
221             # ----------------------------------------------
222              
223             sub read_subcountry_info_table
224             {
225 0     0 1   my($self) = @_;
226 0           my($sth) = $self -> dbh -> prepare('select * from subcountry_info');
227              
228 0           $sth -> execute;
229 0           $sth -> fetchall_hashref('id');
230              
231             } # End of read_subcountry_info_table.
232              
233             # -----------------------------------------------
234              
235             sub report_Australian_statistics
236             {
237 0     0 1   my($self) = @_;
238 0           my($countries) = $self -> read_countries_table;
239              
240 0           my($index);
241              
242 0           for my $i (keys %$countries)
243             {
244 0 0         if ($$countries{$i}{name} eq 'Australia')
245             {
246 0           $index = $i;
247              
248 0           last;
249             }
250             }
251              
252 0           my($subcountries) = $self -> read_subcountries_table;
253              
254 0           my(@states);
255              
256 0           for my $i (keys %$subcountries)
257             {
258 0 0         if ($$subcountries{$i}{country_id} == $index)
259             {
260 0           push @states, $$subcountries{$i};
261             }
262             }
263              
264 0           @states = sort{$$a{sequence} <=> $$b{sequence} } @states;
  0            
265              
266 0           $self -> log(info => "$$_{sequence}: $$_{name}") for @states;
267              
268             # Return 0 for success and 1 for failure.
269              
270 0           return 0;
271              
272             } # End of report_Australian_statistics.
273              
274             # -----------------------------------------------
275              
276             sub report_statistics
277             {
278 0     0 1   my($self) = @_;
279 0           my($count) = $self -> get_statistics;
280              
281 0           $self -> log(info => $_) for map{"$_ => $$count{$_}"} sort keys %$count;
  0            
282              
283             # Return 0 for success and 1 for failure.
284              
285 0           return 0;
286              
287             } # End of report_statistics.
288              
289             # ----------------------------------------------
290              
291             sub who_has_subcountries
292             {
293 0     0 1   my($self) = @_;
294 0           my($countries) = $self -> read_countries_table;
295              
296 0           my(@has);
297              
298 0           for my $id (keys %$countries)
299             {
300 0 0         push @has, $id if ($$countries{$id}{has_subcountries} eq 'Yes');
301             }
302              
303 0           return [@has];
304              
305             } # End of who_has_subcountries.
306              
307             # -----------------------------------------------
308              
309             1;
310              
311             =pod
312              
313             =head1 NAME
314              
315             WWW::Scraper::Wikipedia::ISO3166::Database - The interface to www.scraper.wikipedia.iso3166.sqlite
316              
317             =head1 Synopsis
318              
319             See L for a long synopsis.
320              
321             =head1 Description
322              
323             Documents the methods end-users need to access the SQLite database,
324             I, which ships with this distro.
325              
326             See L for a long description.
327              
328             See scripts/export.as.csv.pl, scripts/export.as.html.pl and scripts/report.statistics.pl.
329              
330             =head1 Distributions
331              
332             This module is available as a Unix-style distro (*.tgz).
333              
334             See http://savage.net.au/Perl-modules.html for details.
335              
336             See http://savage.net.au/Perl-modules/html/installing-a-module.html for
337             help on unpacking and installing.
338              
339             =head1 Constructor and initialization
340              
341             new(...) returns an object of type C.
342              
343             This is the class's contructor.
344              
345             Usage: C<< WWW::Scraper::Wikipedia::ISO3166::Database -> new() >>.
346              
347             This method takes a hash of options.
348              
349             Call C as C<< new(option_1 => value_1, option_2 => value_2, ...) >>.
350              
351             Available options:
352              
353             =over 4
354              
355             =item o attributes => $hash_ref
356              
357             This is the hashref of attributes passed to L's I method.
358              
359             Default: {AutoCommit => 1, RaiseError => 1, sqlite_unicode => 1}
360              
361             =back
362              
363             =head1 Methods
364              
365             This module is a sub-class of L and consequently inherits its methods.
366              
367             =head2 attributes($hashref)
368              
369             Get or set the hashref of attributes passes to L's I method.
370              
371             Also, I is an option to L.
372              
373             =head2 find_subcountry_downloads()
374              
375             Returns an arrayref of 2-letter codes of countries whose subcountry page has been downloaded to
376             data/*$code2.html.
377              
378             =head2 get_country_count()
379              
380             Returns the result of: 'select count(*) from countries'.
381              
382             =head2 get_statistics()
383              
384             Returns a hashref of database statistics:
385              
386             {
387             countries_in_db => 249,
388             has_subcounties => 200,
389             subcountries_in_db => 5297,
390             subcountry_cagegories_in_db => 77,
391             subcountry_files_downloaded => 249,
392             subcountry_info_in_db => 352,
393             }
394              
395             Called by L.
396              
397             =head2 get_subcountry_count()
398              
399             Returns the result of: 'select count(*) from subcountries'.
400              
401             =head2 get_subcountry_category_count()
402              
403             Returns the result of: 'select count(*) from subcountry_categories'.
404              
405             =head2 get_subcountry_info_count()
406              
407             Returns the result of: 'select count(*) from subcountry_info'.
408              
409             =head2 new()
410              
411             See L.
412              
413             =head2 read_countries_table()
414              
415             Returns a hashref of hashrefs for this SQL: 'select * from countries'.
416              
417             The key of the hashref is the primary key (integer) of the I table.
418              
419             This is discussed further in L.
420              
421             =head2 read_subcountries_table
422              
423             Returns a hashref of hashrefs for this SQL: 'select * from subcountries'.
424              
425             The key of the hashref is the primary key (integer) of the I table.
426              
427             This is discussed further in L.
428              
429             =head2 read_subcountry_categories_table
430              
431             Returns a hashref of hashrefs for this SQL: 'select * from subcountry_categories'.
432              
433             The key of the hashref is the primary key (integer) of the I table.
434              
435             This is discussed further in L.
436              
437             =head2 read_subcountry_info_table
438              
439             Returns a hashref of hashrefs for this SQL: 'select * from subcountry_info'.
440              
441             The key of the hashref is the primary key (integer) of the I table.
442              
443             This info is a text summary of each country's subcountries, and is taken literally from the 3rd
444             column - 'Subdivisions assigned codes' - of L.
445              
446             This is discussed further in L.
447              
448             =head2 report_Australian_statistics
449              
450             Logs some info for Australia. Does not call L.
451              
452             =head2 report_statistics()
453              
454             Logs various database statistics at the I level.
455              
456             Calls L. See that module for what this module reports.
457              
458             =head2 who_has_subcountries()
459              
460             Returns an arrayref of primary keys (integers) in the I table, of those countries who have
461             subcountry entries in the I table.
462              
463             =head1 FAQ
464              
465             For the database schema, etc, see L.
466              
467             =head1 References
468              
469             See L.
470              
471             =head1 Support
472              
473             Email the author, or log a bug on RT:
474              
475             L.
476              
477             =head1 Author
478              
479             C was written by Ron Savage Iron@savage.net.auE> in 2012.
480              
481             Home page: L.
482              
483             =head1 Copyright
484              
485             Australian copyright (c) 2012 Ron Savage.
486              
487             All Programs of mine are 'OSI Certified Open Source Software';
488             you can redistribute them and/or modify them under the terms of
489             The Artistic License, a copy of which is available at:
490             http://www.opensource.org/licenses/index.html
491              
492              
493             =cut