# ================================================================== # Gossamer Forum - Advanced web community # # Website : http://gossamer-threads.com/ # Support : http://gossamer-threads.com/scripts/support/ # Revision : $Id: Generic.pm,v 1.23 2002/03/16 06:20:22 adrian Exp $ # # Copyright (c) 2001 Gossamer Threads Inc. All Rights Reserved. # Redistribution in part or in whole strictly prohibited. Please # see LICENSE file for full details. # ================================================================== package GForum::Import::Generic; BEGIN { eval { require Time::HiRes; import Time::HiRes qw/time/; }; } use strict; use GForum qw/:user $DB $CFG/; sub new { my $class = shift; my $self = { source => '', debug => 0, logfile => '', append_data => 0, add_bad_email => 0, close_forums => 0, error => '', start_time => time, preserve_post_id => 0, administrators => {}, # List of imported administrators. moderators => {}, # List of imported moderators and their user groups. registered => {}, # List of imported registered users and their user groups. users_not_added => {}, # Duplicate users who were not imported (Username, UBB User ID => { signature => Signature, reason => Reason }. posts_not_added => {}, # Deleted or invalid posts which were not imported (WWWThreads B_Number) => Reason. groups_not_added => {}, # Groups not used (eg. Administrator group) (WWWThreads G_Id) => Reason. users => {}, # Imported forum's user id (WWWThreads U_Username, UBB User ID) => GForum user_id categories => {}, # Imported forum's categories (WWWThreads Cat_Number) => GForum cat_id forums => {}, # Imported forums (WWWThreads Bo_Keyword) => { id => GForum forum_id, style => GForum forum_style } posts => {}, # Imported posts (WWWThreads B_Number) => GForum post_id groups => {}, # Imported groups (WWWThreads G_Id) => GForum group_id user_titles => {}, # User titles and the number of posts before the title is used. anonymous => {}, # GForum anonymous user to use for any anonymous posts. { id => GForum user_id, username => GForum name } @_ }; return bless ($self, $class); } sub pre_import { my $self = shift; # Turn off GForum debug to make the import run faster, unless debug level is at 4. if ($self->{debug} >= 4) { $DB->{_debug} = 1; } else { $DB->{_debug} = 0; } return 1; } sub post_import { my $self = shift; return 1; } sub print_summary { my $self = shift; my $end_time = time; my $total_time = $end_time - $self->{start_time}; my $total_time_secs = $total_time; my $total_time_mins = int($total_time_secs / 60); my $total_time_hours = int($total_time_mins / 60); $total_time_mins = $total_time_mins - $total_time_hours * 60; $total_time_secs = $total_time_secs - $total_time_hours * 60 * 60 - $total_time_mins * 60; $self->debug("\nImport Summary:", 0); $self->debug("===============", 0); $self->debug(sprintf ("Import Time: %d hours, %d mins, %.2f secs\n", $total_time_hours, $total_time_mins, $total_time_secs), 0); $self->debug("User Import Summary:", 0); $self->debug("====================", 0); my $display_max_users = 200; my $total_admins = scalar keys %{$self->{administrators}}; $self->debug("$total_admins Administrator(s):", 0); if ($total_admins < 1) { $self->debug("\n", 0); } elsif ($total_admins <= $display_max_users) { $self->debug(join(', ', keys %{$self->{administrators}}) . "\n", 0); } else { $self->debug(join(', ', keys %{$self->{administrators}}) . "\n", 'fo'); $self->debug("Too many to display!\n", 'so'); } my $total_mods = scalar keys %{$self->{moderators}}; $self->debug("$total_mods Moderator(s):", 0); if ($total_mods < 1) { $self->debug("\n", 0); } elsif ($total_mods <= $display_max_users) { $self->debug(join(', ', keys %{$self->{moderators}}) . "\n", 0); } else { $self->debug(join(', ', keys %{$self->{moderators}}) . "\n", 'fo'); $self->debug("Too many to display!\n", 'so'); } my $total_users = scalar keys %{$self->{users}}; $self->debug("$total_users User(s):", 0); if ($total_users < 1) { $self->debug("\n", 0); } elsif ($total_users <= $display_max_users) { $self->debug(join(', ', keys %{$self->{users}}) . "\n", 0); } else { $self->debug(join(', ', keys %{$self->{users}}) . "\n", 'fo'); $self->debug("Too many to display!\n", 'so'); } my $total_not_added = scalar keys %{$self->{users_not_added}}; $self->debug("$total_not_added User(s) Not Imported:", 0); if ($total_not_added < 1) { $self->debug("\n", 0); } elsif ($total_not_added <= $display_max_users) { $self->debug(join('', map { "$_ ($self->{users_not_added}{$_}{reason})\n" } keys %{$self->{users_not_added}}), 0); } else { $self->debug(join('', map { "$_ ($self->{users_not_added}{$_}{reason})\n" } keys %{$self->{users_not_added}}), 'fo'); $self->debug("Too many to display!\n", 'so'); } $self->debug("Post Import Summary:", 0); $self->debug("====================", 0); $self->debug(((scalar keys %{$self->{posts}}) . " Post(s)."), 0); my $posts_not_added = scalar keys %{$self->{posts_not_added}}; $self->debug("$posts_not_added Post(s) Not Imported.", 0); if ($posts_not_added) { $self->debug(join('', map { "$_: $self->{posts_not_added}{$_}\n" } keys %{$self->{posts_not_added}}), 'fo'); } } sub show_progress { my ($self, $total, $curr_rec, $dots) = @_; return if (!$total); my $curr_percent = $curr_rec / $total * 100; if (($curr_percent >= $dots) && ($total > 1000)) { # Show progress only if there are more than 1000 records. return $dots++ if $dots >= 100; if ($dots < 0) { $dots = 0; print "\n\t"; } my $num_dots = $curr_percent - $dots; for (my $i = 1; $i <= $num_dots; $i++) { if ($dots % 25 == 0) { print $dots, '%'; } else { print "."; } $dots++; } if ($curr_rec == $total) { print "100% "; } } return $dots; } sub get_error { my $self = shift; return $self->{error}; } sub debug { # $debug_msg: debug message to output if needed. # $debug_level: 0 = output to screen and logfile (if turned on) regardless of debug level. # >1 = output to screen and logfile (if turned on) when $self->{debug_level} is greater or equal to this value. # 'so' = output to screen only. # 'fo' = output to logfile only (if turned on). my ($self, $debug_msg, $debug_level) = @_; if ($debug_level eq 'so') { print "$debug_msg\n"; } elsif ($debug_level eq 'fo') { print LOGFILE "$debug_msg\n" if $self->{logfile}; } elsif (not $debug_level) { print LOGFILE "$debug_msg\n" if $self->{logfile}; print "$debug_msg\n"; } elsif ($debug_level <= $self->{debug}) { if ($self->{logfile}) { print LOGFILE "$debug_msg\n"; } else { print "$debug_msg\n"; } } return 1; } sub clear_db { my $self = shift; # TODO check that these tables are correct! my @TABLES = qw/User CustomDict Guest Category Forum UserNew ForumModerator ForumSubscriber Post PostNew PostView ThreadWatch Expanded Message SentMessage PostAttachment MessageAttachment TempAttachment ForumGroup UserGroup Online EditLog Session Remember ForumBan EmailTemplate MailingIndex EmailRecipient/; foreach my $table (@TABLES) { next if ($table eq 'Grouping'); $self->debug("Deleting table: $table", 2); $DB->table($table)->delete_all(); } # Special case is the Grouping table. GForum install creates these by default, so we don't want to delete them. my $table = $DB->table('Grouping'); my $cond = GT::SQL::Condition->new(group_id => 'IN' => [@$CFG{qw/id_group_guest id_group_not_validated id_group_registered id_group_moderator/}])->not; $table->delete($cond); return 1; } # Returns true if first version # is smaller than the second version #. sub version_comp { my ($self, $vers_lt, $vers_gt) = @_; my ($lt_major, $lt_minor, $lt_mtm) = $vers_lt =~ /(\d+)\.?(\d+)?\.?(\d+)?/; my ($gt_major, $gt_minor, $gt_mtm) = $vers_gt =~ /(\d+)\.?(\d+)?\.?(\d+)?/; $lt_major ||= 0; $lt_minor ||= 0; $lt_mtm ||= 0; $gt_major ||= 0; $gt_minor ||= 0; $gt_mtm ||= 0; if ($lt_major < $gt_major) { return 1; } elsif ($lt_major > $gt_major) { return 0; } if ($lt_minor < $gt_minor) { return 1; } elsif ($lt_minor > $gt_minor) { return 0; } if ($lt_mtm < $gt_mtm) { return 1; } return 0; } 1;