كود:
#!/usr/bin/perl
BEGIN {
unshift(@INC, "/myPath");
}
use w3t;
use w3tvars qw(%config);
use w3ttheme qw(%theme);
use strict;
##########################################################################
## UBB --> WWWThreads Pro 5.1.4 Import script.
## Author: Alex Krohn (alex@gossamer-threads.com)
## Created: February 26, 1998
## Modified by: Matt Reinfeldt, May 16, 2000
## Modified by: Rick Baker, December 10, 2000
##
## Description: This script will import an existing UBB forum into an
## empty wwwthreads database.
## Usage:
## - copy this script into your main wwwthreads folder
## - chmod 755 ubb_import.pl
## - verify the path to perl is the same as in Line 1
## - run the wwwthreads installation (configure your w3tvars.pm and
## your w3ttheme.pm, create the database, and run ./createtable.pl)
## - configure the $member_dir, $board_file, $board_path, and $mod_file
## variables listed below
## - save and exit to command line... run the script (./ubb_import.pl)
##
## Notes:
## - If you have private forums you will need to go into the password
## protected subdirectory and move all of the post files one directory
## up. Next you will need to gunzip all of the post files. If this
## is not done, the posts in the private forums will not be
## imported.
##########################################################################
# Path to your UBB Files.
my $member_dir = '/www/...../......./Members';
my $board_file = '/www/..../....';
my $board_path = '/www/.../...';
my $mod_file = '/www/.../...';
# Connect to the database.
&w3t::db_connect;
print "Importing Members ...\n";
&import_members ($member_dir);
print "Done\n";
print "Importing Categories ...\n";
&import_cats ($board_path);
print "Done\n";
print "Creating Boards ... \n";
my @boards = &create_boards ($board_file, $mod_file, $board_path);
print "Done\n";
print "Importing Messages ...\n";
&import_messages ($board_path, @boards);
print "Done\n";
# All done..
&w3t::db_disconnect;
##########################################################################
## modified by: Matt Reinfeldt 5/17/2000
sub import_members {
my $dir = shift;
opendir (MEM, "$dir") or die "Can't open $dir. Reason: $!";
my ($file, $text, $query, $count, $user, $pass, $email, $url, $status2,
$occupation, $location, $posts, $status, $interests, $signup,
$showemail, $bio);
while (defined ($file = readdir(MEM))) {
next if ($file =~ /^\.\.?$/);
next if ($file !~/^[0123456789]/);
open (FILE, "<$dir/$file") or die "Can't open $file. Reason: $!";
$text = join ("", <FILE>);
($user, $pass, $email, $url, $status2, $occupation, $location,
$posts, $status, $interests, $signup, $showemail, $bio) =
split (/\n/, $text, 12);
close FILE;
print "\nWARN: Skipping $file. No User or email.\n" and
next if (!$user or !$email);
$user =~ s/_/ /g;
$user = $w3t::dbh->quote($user);
$pass = $w3t::dbh->quote($pass);
$email = $w3t::dbh->quote($email);
$url = $w3t::dbh->quote($url);
$bio = $w3t::dbh->quote($bio);
## Alex didn't have these set, so I'll go ahead and do it...
my $EReplies_q = $w3t::dbh->quote("Off");
my $PFormat = $w3t::dbh->quote($theme{'post_format'});
my $Color_q = $w3t::dbh->quote($theme{'textcol'});
my $Groups;
## let's set the correct groups...
if ($status eq "Administrator") {
$Groups = "-1-3-";
}
if ($status eq "Moderator") {
$Groups = "-2-3-";
}
if ($status eq "User") {
$Groups = "-3-";
}
my $Groups_q = $w3t::dbh->quote($Groups);
## need to grab the correct title for the number of posts
## make sure you are running this script from your main wwwthreads
## directory (where w3t.pm wwwthreads.pl reside)
## ... it will not work otherwise!!!
# ---------------------
# Set the default title
open(TITLES,"$config{'path'}/filters/usertitles") or die "Can't open $config{'path'}/filters/usertitles";
my @titles = <TITLES>;
close (TITLES);
my ($post,$utitle) = split(/\t/,$titles[0]);
chomp $utitle;
my $Currtitle = $utitle;
my $UserTitle_q = $w3t::dbh -> quote($utitle);
my $NewTitle = $Currtitle;
open (TITLES,"filters/usertitles");
my @titles = <TITLES>;
close (TITLES);
for (my $i=0; $i<=$#titles; $i++) {
my ($req_posts,$title) = split(/\t/,$titles[$i]);
chop ($title);
if ($posts >= $req_posts) {
$NewTitle = $title;
}
}
if ($NewTitle ne $Currtitle) {
$UserTitle_q = $w3t::dbh->quote($NewTitle);
}
## need to convert the signup date....
my $date = $w3t::dbh->quote(&convert_time($signup));
## set the correct type for user status
($status eq 'Administrator') ? ($status = "'Administrator'") :
($status = "'User'");
## don't forget to change the admincolor!
if ( $status eq "'Administrator'" ) {
$Color_q = $w3t::dbh->quote($theme{'admincolor'});
}
## run sql code
$query = qq!
INSERT INTO w3t_Users (U_Username, U_Password, U_Email,
U_TotalPosts, U_Laston, U_Status, U_Sort, U_Display,
U_View, U_PostsPer, U_EReplies, U_Post_Format, U_Registered,
U_RegEmail, U_Groups, U_Title, U_Color, U_Bio, U_Homepage)
VALUES ($user, $pass, $email, $posts, 0, $status,
'$theme{'sort'}', '$theme{'postlist'}', '$theme{'threaded'}',
'$theme{'postsperpage'}', $EReplies_q, $PFormat, $date,
$email, $Groups_q, $UserTitle_q, $Color_q, $url, $bio)
!;
$w3t::dbh->do ($query) ||
die "Can't insert user. Query: $query. Reason: DBI::errstr";
$count++;
($count % 10) or (print "$count ");
($count % 100) or (print "\n");
}
closedir MEM;
}
sub import_cats {
my $path = shift;
open (FILE,"$path/categories.file");
while (<FILE>) {
chop;
my ($Blah,$CatName,$Number) = split(/\|\|\|/,$_,3);
my $CatName = $w3t::dbh -> quote($CatName);
my $query = qq!
INSERT INTO w3t_Category (Cat_Title,Cat_Number,Cat_Description)
VALUES ($CatName,'$Number','')
!;
$w3t::dbh -> do($query) or die "Can't do $query. Reason: $DBI::errstr.";
}
close (FILE);
}
##########################################################################
## modified by: Matt Reinfeldt 5/17/2000
sub create_boards {
my ($file, $mod_file, $boardpath) = @_;
my ($category, $title, $description, $status, $html, $ubbcode,
$limitedwrite, $forumpw, $forumnum, $titlegraphic, $forumheader,
$ubbimage, $notify);
my ($keyword, $moderator, $query, @boards);
my %mods = &load_moderators ($mod_file);
my $time = time();
my ($write_perm, $read_perm);
open (BOARDS, "$file") || die "Can't open boards file '$file'. Reason: $!";
while (<BOARDS>) {
chomp;
($category, $title, $description, $status, $html, $ubbcode,
$limitedwrite, $forumpw, $forumnum, $titlegraphic, $forumheader,
$ubbimage, $notify) = split /\|/;
$title = $w3t::dbh->quote($title);
$description = $w3t::dbh->quote($description);
$keyword = $w3t::dbh->quote("Forum$forumnum");
my @moderators = split(/\|\|\^\|\|/,$mods{"Forum$forumnum"});
for (my $i=0; $i <=$#moderators; $i++) {
$moderator = $moderators[$i];
$moderator =~ s/\\\././igs;
my $moderator_q = $w3t::dbh->quote($moderator);
my $query = qq~
UPDATE w3t_Users
SET U_Status = 'Moderator'
WHERE U_Username = $moderator_q
AND U_Status <> 'Administrator'
~;
$w3t::dbh->do($query) or die "Can't do $query";
my $query = qq~
INSERT INTO w3t_Moderators
(Mod_Username,Mod_Board)
VALUES
($moderator_q,$keyword)
~;
$w3t::dbh->do($query) or die "Can't do $query";
}
$moderator = $w3t::dbh->quote($mods{"Forum$forumnum"});
($html eq 'is not') ? ($html = "'Off'") : ($html = "'On'");
($ubbcode eq 'is not') ? ($ubbcode = "'Off'") : ($ubbcode = "'On'");
$read_perm = $w3t::dbh->quote("-1-2-3-4-");
if ($limitedwrite eq "private") {
$write_perm = $w3t::dbh->quote("-1-2-");
$read_perm = $w3t::dbh->quote("-1-2-");
} else {
$write_perm = $w3t::dbh->quote("-1-2-3-");
}
# Make sure we have something.
($title) or (print "No title for line:\n\t$_\nSkipping ...\n" and next);
# Grab the category name
my $query = qq!
SELECT Cat_Title
FROM w3t_Category
WHERE Cat_Number = $category
!;
my $sth = $w3t::dbh->prepare($query);
$sth -> execute();
my ($catname) = $sth -> fetchrow_array;
$catname = $w3t::dbh->quote($catname);
# Insert the board into w3t_Boards table.
$query = qq!
INSERT INTO w3t_Boards (Bo_Title, Bo_Description, Bo_Keyword,
Bo_Moderated, Bo_Total, Bo_Last, Bo_HTML, Bo_Write_Perm,
Bo_Read_Perm, Bo_Created, Bo_Markup,Bo_Cat,Bo_CatName)
VALUES ($title, $description, $keyword, 'no', 0, 0, $html,
$write_perm, $read_perm, $time, $ubbcode,$category,$catname)
!;
$w3t::dbh->do($query) ||
die "Can't create board: $query. Reason: $DBI::errstr";
print "Board: $title Created\n";
push (@boards, "Forum$forumnum");
}
close BOARDS;
return @boards;
}
##########################################################################
## modified by: Matt Reinfeldt 5/17/2000
sub import_messages {
my ($board_path, @boards) = @_;
my $bcount = 0;
my ($closed, $notes, $replies, $username, $subject, $status, $number,
$username, $date, $time, $email, $message);
my ($post, $stats, $father, $sth, $query, $id, $lastpost);
foreach my $board (@boards) {
my $maintime = 0;
$board =~s/'//igs;
print "\nImporting messages from $board ... \n";
opendir (BOARDIR, "$board_path/$board") ||
die "Can't open board dir: $board_path/$board. Reason: $!";
$bcount = 0;
while (defined ($post = readdir (BOARDIR))) {
$board =~s/'//igs;
next unless ($post =~ /^[0123456789]/);
open (POST, "$board_path/$board/$post") ||
die "Can't open post: $board_path/$board/$post. Reason: $!";
$stats = <POST>;
($closed, $notes, $replies, $username, $subject) =
split /\|\|/, $stats;
$father = <POST>;
($status, $number, $username, $date, $time, $email, $message) =
split /\|\|/, $father;
if (!$subject) {
print "WARN: Couldn't parse subject: $post\n";
next;
}
$board = $w3t::dbh->quote($board);
$username = $w3t::dbh->quote($username);
$message = $w3t::dbh->quote($message);
$subject = $w3t::dbh->quote($subject);
$time = &convert_time ($date, $time);
($status == 'Z') ? ($status = 1) : ($status = 0);
if ($time > $maintime) { $maintime = $time; }
($time > $lastpost) and ($lastpost = $time);
$query = qq!
SELECT U_Title,U_Color
FROM w3t_Users
WHERE U_Username=$username
!;
my $sth = $w3t::dbh->prepare($query);
$sth -> execute();
my ($title,$color) = $sth -> fetchrow_array;
$title = $w3t::dbh -> quote($title);
$color = $w3t::dbh -> quote($color);
my $MainUsername = $username;
$query = qq!
INSERT INTO w3t_Posts (B_Board, B_Parent, B_Main, B_Posted,
B_Last_Post, B_Username, B_IP, B_Subject, B_Body, B_Mail,
B_Kept, B_Status, B_Replies, B_UTitle, B_Color)
VALUES ($board, 0, 0, $time, $time, $username, NULL, $subject,
$message, NULL, 'Off', $status, 0, $title,$color)
!;
$sth = $w3t::dbh->prepare($query) ||
die "Can't post: $query. Reason: $DBI::errstr";
$sth->execute() ||
die "Can't execute: $query. Reason: $DBI::errstr";
$id = $sth->{'mysql_insertid'};
$query = qq!
UPDATE w3t_Posts SET B_Main = $id
WHERE B_Number = $id
!;
$w3t::dbh->do($query) ||
die "Can't update: $query. Reason: $DBI::errstr";
$query = qq!
UPDATE w3t_Boards
SET Bo_Total = Bo_Total + 1,
Bo_Threads = Bo_Threads + 1,
Bo_Last = $time
WHERE Bo_Keyword = $board
!;
$w3t::dbh -> do($query) or die "Can't do $query. Reason: $DBI::errstr.";
$bcount++;
($bcount % 10) or (print "$bcount ");
($bcount % 100) or (print "\n");
# Add all the replies.
$subject =~ s/^'(.+)/'Re: $1/;
while (defined ($post = <POST>)) {
($status, $number, $username, $date, $time, $email, $message) =
split /\|\|/, $post;
$message or next;
$time = &convert_time ($date, $time);
$username = $w3t::dbh->quote($username);
$message = $w3t::dbh->quote($message);
($status == 'Z') ? ($status = 1) : ($status = 0);
$query = qq!
SELECT U_Title,U_Color
FROM w3t_Users
WHERE U_Username=$username
!;
my $sth = $w3t::dbh->prepare($query);
$sth -> execute();
my ($title,$color) = $sth -> fetchrow_array;
$title = $w3t::dbh -> quote($title);
$color = $w3t::dbh -> quote($color);
$query = qq!
INSERT INTO w3t_Posts(B_Board, B_Parent, B_Main, B_Posted,
B_Last_Post, B_Username, B_IP, B_Subject, B_Body,
B_Mail, B_Kept, B_Status,B_ParentUser,B_Color,B_UTitle)
VALUES ($board, $id, $id, $time, $time, $username, NULL,
$subject, $message, NULL, 'Off', $status,$MainUsername,$color,$title)
!;
$sth = $w3t::dbh->prepare($query) ||
die "Can't post followup: $query. Reason: $DBI::errstr";
$sth->execute() ||
die "Can't execute: $query. Reason: $DBI::errstr";
($time > $lastpost) and ($lastpost = $time);
if ($time > $maintime) { $maintime = $time; }
$query = qq!
UPDATE w3t_Boards
SET Bo_Total = Bo_Total + 1,
Bo_Last = $time
WHERE Bo_Keyword = $board
!;
$w3t::dbh -> do($query) or die "Can't do $query. Reason: $DBI::errstr.";
$bcount++;
($bcount % 10) or (print "$bcount ");
($bcount % 100) or (print "\n");
$query = qq!
UPDATE w3t_Posts
SET B_Last_Post = $time
WHERE B_Main = $id
!;
$w3t::dbh -> do($query) or die "Can't do $query. Reason: $DBI::errstr.";
}
close POST;
}
closedir BOARDIR;
if ($board !~/^'/) { $board = $w3t::dbh->quote($board); }
$query = qq!
UPDATE w3t_Boards
SET Bo_Last = $maintime
WHERE Bo_Keyword = $board
!;
$w3t::dbh->do($query) ||
die "Can't update board: $query. Reason: $!";
}
}
sub convert_time {
# ------------------------------------------------
use Time::Local;
my ($date, $time) = @_;
if (!$date) { return time; }
my ($mon, $day, $year) = split /-/, $date;
my ($hour, $min, $ampm) = $time =~ /(\d+):(\d+)\s*(.+)/;
$mon--;
($ampm eq "PM") and ($hour = $hour + 12); ($hour == 24) and ($hour = 0);
my $time;
eval { $time = timelocal (0, $min, $hour, $day, $mon, $year); };
if ($@) {
die "In: '$date' - '$time'.\nParse: (0, $min, $hour, $day, $mon, $year)\nErr: $@\n"; }
return $time;
}
sub load_moderators {
# ------------------------------------------------
my $mod_file = shift;
my %mods = ();
open (MOD, "<$mod_file") ||
die "Can't open moderator file '$mod_file'. Reason: $!";
while (<MOD>) {
chomp;
/\$(Forum\d+)Moderator\s*=\s*"([^"]+)"/ and ($mods{$1} = $2);
}
close MOD;
return %mods;
}