CPAN-MetaCurator
view release on metacpan or search on metacpan
lib/CPAN/MetaCurator/Import.pm view on Meta::CPAN
# -----------------------------------------------
sub populate_all_tables
{
my($self) = @_;
$self -> init_config;
$self -> init_db;
$self -> logger -> info('Populating some tables');
my($csv) = Text::CSV::Encoded -> new
({
allow_whitespace => 1,
encoding_in => 'utf-8',
strict => 1,
});
# Note: populate_topics_table() reads the constants table, so the latter must be populated first.
$self -> populate_constants_table($csv);
$self -> populate_topics_table;
$self -> logger -> info('Populated some tables');
$self -> logger -> info('-' x 50);
# Return 0 for OK and 1 for error.
return 0;
} # End of populate_all_tables.
# -----------------------------------------------
sub populate_constants_table
{
my($self, $csv) = @_;
my($path) = File::Spec -> catfile($self -> home_path, $self -> constants_csv_path);
my($table_name) = 'constants';
$self -> logger -> info("Start populate_constants_table()");
$self -> get_table_column_names(true, $table_name); # Populates $self -> column_names.
my($record_count) = $self -> import_csv_file($csv, $path, $table_name, 'name', 'value');
$self -> logger -> info("Populated '$table_name'. Record count: $record_count");
} # End of populate_constants_table.
# -----------------------------------------------
sub populate_topics_table
{
my($self) = @_;
my($data) = $self -> read_tiddlers_file;
my($record) = {parent_id => 1, text => 'Root', title => 'MetaCurator'}; # Parent is self.
my($table_name) = 'topics';
$self -> logger -> info("Start populate_topics_table()");
# We have just populated the constants table, so read it to get the names of the special (TiddlyWiki) paragraphs.
# Typically: GettingStarted|MainMenu.
my($root_id) = $self -> insert_hashref($table_name, $record);
my($pad) = $self -> pad; # For temporary use, during import.
$$pad{constants} = $self -> read_table('constants');
my($special_para_names);
for my $row (@{$$pad{constants} })
{
$special_para_names = $$row{value} if ($$row{name} eq 'special_para_names');
}
my($regexp) = qr/($special_para_names)/o;
my($id);
my($text, $title, $temp_text, $temp_title);
for my $index (0 .. $#$data)
{
$text = $$data[$index]{text};
$title = $$data[$index]{title};
if ($title =~ $regexp)
{
$self -> logger -> warn("Skipping paragraph: $1");
next;
}
$temp_text = $text || '';
$temp_title = $title || '';
$self -> logger -> debug("Skipping paragraph: temp_text: =>$temp_text<=. temp_title: =>$temp_title<=") if (! ($temp_text && $temp_title) );
$self -> logger -> info("populate_topics_table(). Missing title @ line: $index. name: $title"), next if (! defined $title);
$self -> logger -> info("populate_topics_table(). Missing text @ line: $index. text: $text"), next if ($text !~ m/^\"\"\"\no (.+)$/s);
$$record{parent_id} = $root_id;
$text = $1 if ($text =~ m/^\"\"\"\n(.+)$/s);
$$record{text} = $text;
$$record{title} = $title;
$id = $self -> insert_hashref($table_name, $record);
}
$$pad{$table_name} = $self -> read_table($table_name);
my($record_count) = $#{$$pad{$table_name} };
$self -> logger -> info("Populated '$table_name'. Record count: $record_count");
} # End of populate_topics_table;
# --------------------------------------------------
sub read_tiddlers_file
{
my($self) = @_;
$self -> init_config;
$self -> init_db;
my($file_name) = File::Spec -> catfile($self -> home_path, $self -> tiddlers_path);
( run in 1.501 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )