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