phpBB3导入用户的Python脚本

关联的数据表

在phpBB3中导入用户时, 需要处理的有两张表, 一个是 users, 一个是 user_group.

如果是新安装的论坛, 在每次导入之前, 用以下语句初始化:

DELETE FROM phpbb_users WHERE user_id > 47;
alter table phpbb_users  auto_increment = 48;

DELETE FROM phpbb_user_group where user_id > 48;
DELETE FROM phpbb_user_group where user_id = 0;

需要的最小数据集

对于users表

需要的最小字段为 `user_type`, `group_id`, `user_permissions`, `user_ip`, `user_regdate`, `username`, `username_clean`, `user_lastvisit`, `user_lastmark`, `user_posts`, `user_lang`, `user_timezone`, `user_dateformat`, `user_style`, `user_sig`, `user_occ`, `user_interests`

如果是新安装的论坛, 进需要对这些字段动态赋值: `user_ip`, `user_regdate`, `username`, `username_clean`, `user_lastvisit`, `user_lastmark`, `user_posts`, `user_sig`

对于user_group表

每一个user添加默认注册用户对应的组记录, 对于新安装的论坛, 添加一条对应组2的记录

Python脚本

print("\n" + '######## Start:' + str(i) + ', limit:' + str(limit) + ' ########')
users = tb_user_all.find().sort('registeredAt', 1).limit(limit).skip(i)
for user in users:
    try:
        with rbcommon.mysqlclient.cursor() as cursor:
            if (user['nick'] == 'User Not Found'):
                sql = 'INSERT IGNORE INTO `phpbb_users` (`user_type`, `group_id`, `user_permissions`, `user_ip`, `user_regdate`, `username`, ' \
                      '`username_clean`, `user_lastvisit`, `user_lastmark`, `user_posts`, `user_lang`, `user_timezone`, ' \
                      '`user_dateformat`, `user_style`, `user_sig`, `user_occ`, `user_interests`) ' \
                      'VALUES (0, 2, \'\', %s, %s, %s, %s, %s, %s, %s, \'en\', 8.00, \'|Y-m-d| G:i\', 1, %s, \'\', \'\')'
                cursor.execute(sql, (
                    user['ip'],
                    user['registeredAt'],
                    user['name'].strip(),
                    user['_id'].strip(),
                    user['registeredAt'],
                    user['registeredAt'],
                    user['posts'],
                    '' if (not 'signature' in user) else user['signature']))
            else:
                lastVisit = 0
                if (user['lastLogin'] == ''):
                    lastVisit = 0
                else:
                    lastVisit = int(time.mktime(time.strptime(user['lastLogin'], '%a %b %d %H:%M:%S %Y')))

                if (lastVisit == 0) and (len(user['lastActive']) > 0):
                    lastVisit = int(time.mktime(time.strptime(user['lastActive'], '%a %b %d %H:%M:%S %Y')))

                sql = 'INSERT IGNORE INTO `phpbb_users` (`user_type`, `group_id`, `user_permissions`, `user_ip`, `user_regdate`, `username`, ' \
                      '`username_clean`, `user_lastvisit`, `user_lastmark`, `user_posts`, `user_lang`, `user_timezone`, ' \
                      '`user_dateformat`, `user_style`, `user_sig`, `user_occ`, `user_interests`) ' \
                      'VALUES (0, 2, \'\', %s, %s, %s, %s, %s, %s, %s, \'en\', 8.00, \'|Y-m-d| G:i\', 1, %s, \'\', \'\')'
                cursor.execute(sql, (
                    user['ip'],
                    user['registeredAt'],
                    user['name'].strip(),
                    user['_id'].strip(),
                    lastVisit,
                    lastVisit,
                    user['posts'],
                    '' if (not 'signature' in user) else user['signature']))

            # phpbb_user_group
            lastId = cursor.lastrowid
            if (lastId == 0):
                print('Duplicate ID:>{}<'.format(user['name']))
                rbcommon.mysqlclient.rollback()
                continue
            print(lastId)

            sql = 'INSERT IGNORE INTO `phpbb_user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) ' \
                  'VALUES (2, %s, 0, 0)'
            cursor.execute(sql, (lastId))

            rbcommon.mysqlclient.commit()
    except Exception as e:
        print(json.dumps(user))
        traceback.print_exc()

.

导入结束后, 需要在后台首页, 重置全站的文章和用户统计数字.

.

猜你喜欢

转载自www.cnblogs.com/milton/p/10261522.html