| 1 | package CPAN::MetaCurator::Import; | ||
| 2 | |||
| 3 | use boolean; | ||
| 4 | use feature 'say'; | ||
| 5 | use parent 'CPAN::MetaCurator::Database'; | ||
| 6 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 7 | |||
| 8 | use Data::Dumper::Concise; # For Dumper(). | ||
| 9 | use DateTime::Tiny; | ||
| 10 | |||
| 11 | use File::Spec; | ||
| 12 | |||
| 13 | use Mew; | ||
| 14 | |||
| 15 | use Text::CSV::Encoded; | ||
| 16 | |||
| 17 | has -constants_csv_path => (Str, default => sub{return 'data/cpan.metacurator.constants.csv'}, chained => 1); | ||
| 18 | |||
| 19 | our $VERSION = '1.34'; | ||
| 20 | |||
| 21 | # ----------------------------------------------- | ||
| 22 | |||
| 23 | sub import_csv_file | ||
| 24 | { | ||
| 25 | my($self, $csv, $path, $table_name, $col_name_1, $col_name_2) = @_; | ||
| 26 | |||
| 27 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 28 | |||
| 29 | $csv -> column_names($csv -> getline($io) ); | ||
| 30 | |||
| 31 | my($count) = 0; | ||
| 32 | |||
| 33 | my($error_message); | ||
| 34 | my(%keys); | ||
| 35 | |||
| 36 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 37 | { | ||
| 38 | $count++; | ||
| 39 | |||
| 40 | for my $column (@{$self -> column_names}) | ||
| 41 | { | ||
| 42 | if (! defined $$item{$column}) | ||
| 43 | { | ||
| 44 | $self -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | if ($keys{$$item{$col_name_1} }) | ||
| 49 | { | ||
| 50 | $error_message = "$table_name. Duplicate $table_name.$col_name_1: $keys{$$item{$col_name_1} }"; | ||
| 51 | |||
| 52 | $self -> logger -> error($error_message); | ||
| 53 | |||
| 54 | die $error_message; | ||
| 55 | } | ||
| 56 | |||
| 57 | $keys{$$item{$col_name_1} } = $self -> insert_hashref | ||
| 58 | ( | ||
| 59 | $table_name, | ||
| 60 | { | ||
| 61 | id => $count, | ||
| 62 | $col_name_1 => $$item{$col_name_1}, | ||
| 63 | $col_name_2 => $$item{$col_name_2}, | ||
| 64 | } | ||
| 65 | ); | ||
| 66 | |||
| 67 | say "Stored $count records into '$table_name'" if ($count % 10000 == 0); | ||
| 68 | } | ||
| 69 | |||
| 70 | close $io; | ||
| 71 | |||
| 72 | return $count; | ||
| 73 | |||
| 74 | } # End of import_csv_file. | ||
| 75 | |||
| 76 | # ----------------------------------------------- | ||
| 77 | |||
| 78 | sub populate_all_tables | ||
| 79 | { | ||
| 80 | my($self) = @_; | ||
| 81 | |||
| 82 | $self -> init_config; | ||
| 83 | $self -> init_db; | ||
| 84 | $self -> logger -> info('Populating some tables'); | ||
| 85 | |||
| 86 | my($csv) = Text::CSV::Encoded -> new | ||
| 87 | ({ | ||
| 88 | allow_whitespace => 1, | ||
| 89 | encoding_in => 'utf-8', | ||
| 90 | strict => 1, | ||
| 91 | }); | ||
| 92 | |||
| 93 | # Note: populate_topics_table() reads the constants table, so the latter must be populated first. | ||
| 94 | |||
| 95 | $self -> populate_constants_table($csv); | ||
| 96 | $self -> populate_topics_table; | ||
| 97 | |||
| 98 | $self -> logger -> info('Populated some tables'); | ||
| 99 | $self -> logger -> info('-' x 50); | ||
| 100 | |||
| 101 | # Return 0 for OK and 1 for error. | ||
| 102 | |||
| 103 | return 0; | ||
| 104 | |||
| 105 | } # End of populate_all_tables. | ||
| 106 | |||
| 107 | # ----------------------------------------------- | ||
| 108 | |||
| 109 | sub populate_constants_table | ||
| 110 | { | ||
| 111 | my($self, $csv) = @_; | ||
| 112 | my($path) = File::Spec -> catfile($self -> home_path, $self -> constants_csv_path); | ||
| 113 | my($table_name) = 'constants'; | ||
| 114 | |||
| 115 | $self -> logger -> info("Start populate_constants_table()"); | ||
| 116 | $self -> get_table_column_names(true, $table_name); # Populates $self -> column_names. | ||
| 117 | |||
| 118 | my($record_count) = $self -> import_csv_file($csv, $path, $table_name, 'name', 'value'); | ||
| 119 | |||
| 120 | $self -> logger -> info("Populated '$table_name'. Record count: $record_count"); | ||
| 121 | |||
| 122 | } # End of populate_constants_table. | ||
| 123 | |||
| 124 | # ----------------------------------------------- | ||
| 125 | |||
| 126 | sub populate_topics_table | ||
| 127 | { | ||
| 128 | my($self) = @_; | ||
| 129 | my($data) = $self -> read_tiddlers_file; | ||
| 130 | my($record) = {parent_id => 1, text => 'Root', title => 'MetaCurator'}; # Parent is self. | ||
| 131 | my($table_name) = 'topics'; | ||
| 132 | |||
| 133 | $self -> logger -> info("Start populate_topics_table()"); | ||
| 134 | |||
| 135 | # We have just populated the constants table, so read it to get the names of the special (TiddlyWiki) paragraphs. | ||
| 136 | # Typically: FAQ|GettingStarted|MainMenu|TableOfContents | ||
| 137 | |||
| 138 | my($root_id) = $self -> insert_hashref($table_name, $record); | ||
| 139 | my($pad) = $self -> pad; # For temporary use, during import. | ||
| 140 | my($regexp) = $self -> get_special_para_names_regexp($pad); | ||
| 141 | |||
| 142 | my($id); | ||
| 143 | my($text, $title, $temp_text, $temp_title); | ||
| 144 | |||
| 145 | for my $index (0 .. $#$data) | ||
| 146 | { | ||
| 147 | $text = $$data[$index]{text}; | ||
| 148 | $title = $$data[$index]{title}; | ||
| 149 | |||
| 150 | if ($title =~ $regexp) | ||
| 151 | { | ||
| 152 | $self -> logger -> warn("Skipping paragraph: $1"); | ||
| 153 | |||
| 154 | next; | ||
| 155 | } | ||
| 156 | |||
| 157 | $temp_text = $text || ''; | ||
| 158 | $temp_title = $title || ''; | ||
| 159 | |||
| 160 | #$self -> logger -> warn("Skipping paragraph: temp_text: =>$temp_text<=. temp_title: =>$temp_title<="), next if (! ($temp_text && $temp_title) ); | ||
| 161 | |||
| 162 | $$record{parent_id} = $root_id; | ||
| 163 | $text = $1 if ($text =~ m/^\"\"\"\n(.+)$/s); | ||
| 164 | $$record{text} = $text; | ||
| 165 | $$record{title} = $title; | ||
| 166 | $id = $self -> insert_hashref($table_name, $record); | ||
| 167 | } | ||
| 168 | |||
| 169 | $$pad{$table_name} = $self -> read_table($table_name); | ||
| 170 | my($record_count) = $#{$$pad{$table_name} } + 1; | ||
| 171 | |||
| 172 | $self -> logger -> info("Populated '$table_name'. Record count: $record_count"); | ||
| 173 | |||
| 174 | } # End of populate_topics_table; | ||
| 175 | |||
| 176 | # -------------------------------------------------- | ||
| 177 | |||
| 178 | 1; | ||
| 179 | |||
| 180 | =pod | ||
| 181 | |||
| 182 | =head1 NAME | ||
| 183 | |||
| 184 | CPAN::MetaCurator::Import - Manage the cpan.metacurator.sqlite database | ||
| 185 | |||
| 186 | =head1 Machine-Readable Change Log | ||
| 187 | |||
| 188 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 189 | |||
| 190 | =head1 Version Numbers | ||
| 191 | |||
| 192 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 193 | |||
| 194 | =head1 Support | ||
| 195 | |||
| 196 | Email the author. | ||
| 197 | |||
| 198 | =head1 Author | ||
| 199 | |||
| 200 | L<CPAN::MetaCurator> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2025. | ||
| 201 | |||
| 202 | My homepage: L<https://savage.net.au/>. | ||
| 203 | |||
| 204 | =head1 Copyright | ||
| 205 | |||
| 206 | Australian copyright (c) 2025, Ron Savage. | ||
| 207 | |||
| 208 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 209 | you can redistribute them and/or modify them under the terms of | ||
| 210 | The Perl License, a copy of which is available at: | ||
| 211 | http://dev.perl.org/licenses/ | ||
| 212 | |||
| 213 | =cut |