phpBB3导入版面的Python脚本
关联的数据表
在phpBB3中导入版面时, 需要处理的有两张表, 一个是 forums, 一个是 acl_groups.
如果是干净的论坛, 可以不保留安装时填入的默认分区和版面, 直接用以下语句初始化:
-- 清空 forums 表
TRUNCATE phpbb_forums;
-- 清空 acl_groups 表
TRUNCATE phpbb3015.phpbb_acl_groups;
-- 填入初始化权限
INSERT INTO `phpbb_acl_groups` VALUES (1,0,85,0,1),(1,0,93,0,1),(1,0,111,0,1),(5,0,0,5,0),(5,0,0,1,0),(2,0,0,6,0),(3,0,0,6,0),(4,0,0,5,0),(4,0,0,10,0),(7,0,0,23,0);
如果是已经存在版面, 并且需要保留版面的论坛, 则仅需要记下当前的最大right_id
SELECT MAX(right_id) FROM phpbb_forums
.
需要的最小数据集
需要的最小字段为 `forum_id`, `parent_id`, `left_id`, `right_id`, `forum_name`, `forum_type`
构造版面数据
phpBB3的版面为单个父节点的树状结构, 使用了parent_id, left_id, right_id 来标识版面间的层级关系以及排序顺序. 在构造版面数据时, 需要的就是采集最小数据集, 并正确生成parent_id, left_id和right_id. 下面的例子使用的版面, 原数据是分区 + 版面的结构, 分区有层级关系, 版面有层级关系, 分区的ID与版面的ID有重叠, 并且分区与版面之间存在多个父节点的情况. 需要在生成中进行调整.
创建一个可用的ID序列, 用于将分区ID映射到可用ID序列上
数量不大的话, 这一步可以通过手工完成, 根据分区的数量, 观察版面的ID序列, 列出可用的ID做成list
availableIds = [3, 8, 11, 12, 13, 27, 30, ...]
将分区加入版面列表
遍历分区, 将旧ID映射到新ID上, 需要两次遍历, 第二次遍历时构造父子关系, children变量用于在最后生成left_id和right_id
boardsDict = {} # The mapping between Id => board
# Build the section map
allSections = rbcommon.tb_section.find({}).sort('rank', 1)
boards = [] # Record all boards
topBoards = [] # the root board Ids
sectionMap = {} # The mapping between old section Id => new board Id, for assigning new Ids for the sections
cusor = 0
for section in allSections:
sectionMap[str(section['_id'])] = availableIds[cusor]
newId = availableIds[cusor]
board = {
'oid': section['_id'], 'oldPid': section['parentId'],
'_id': newId, 'name2': section['name2'], 'is_folder': 'true',
'desc': section['desc'],
'children': []
}
boards.append(board)
boardsDict[board['_id']] = board
cusor += 1
for board in boards:
if (board['oldPid'] != 0):
board['parentId'] = sectionMap[str(board['oldPid'])]
parent = boardsDict[board['parentId']]
parent['children'].append(board['_id'])
else:
board['parentId'] = 0
topBoards.append(board['_id'])
for board in boards:
print('oid:{}, oldPid:{}, _id:{}, parentId:{}, children:{}'.format(board['oid'], board['oldPid'], board['_id'], board['parentId'], board['children']))
将版面加入列表
# Build the boards
mongoBoards = rbcommon.tb_board.find({})
for mongoBoard in mongoBoards:
board = {
'oid': mongoBoard['_id'], 'oldPid': 0, 'parentId': 0,
'_id': mongoBoard['_id'], 'name2': mongoBoard['name2'], 'is_folder': mongoBoard['is_folder'],
'desc': mongoBoard['name'],
'children': []
}
boards.append(board)
if (board['_id'] in boardsDict.keys()):
print('Error: {}'.format(board['_id']))
exit
boardsDict[board['_id']] = board
完善版面层级关系
# Build the boards tree
allSectionToBoards = rbcommon.tb_section_to_board.find({})
for s2b in allSectionToBoards:
if (s2b['parentId'] == 0):
# parent is section
parentId = sectionMap[str(s2b['sectionId'])]
parent = boardsDict[parentId]
board = boardsDict[s2b['boardId']]
# avoid the multiple parent
if (board['parentId'] > 0):
print('Duplicate {} for {}, board:{}'.format(parentId, board['parentId'], s2b['boardId']))
continue
board['parentId'] = parentId
parent['children'].append(s2b['boardId'])
else:
# parent is board
parent = boardsDict[s2b['parentId']]
board = boardsDict[s2b['boardId']]
# avoid the multiple parent
if (board['parentId'] > 0):
print('Duplicate {} for {}, board:{}'.format(s2b['parentId'], board['parentId'], s2b['boardId']))
continue
board['parentId'] = s2b['parentId']
parent['children'].append(s2b['boardId']) print("All boards:")
for board in boards:
print('oid:{}, oldPid:{}, _id:{}, parentId:{}, folder:{}, children:{}'.format(
board['oid'], board['oldPid'], board['_id'], board['parentId'], board['is_folder'], board['children']))
使用递归填充left_id和right_id
其中counter的取值, 如果是干净的论坛并且前面已经执行了truncate, 就将counter设成1, 否则设成前面得到的right_id最大值 + 1. 这样新导入的分区和版面都会出现在原有分区和版面的下方
# Build the leftId and rightId
markLeftAndRight(topBoards)
print("Marked boards:")
for board in boards:
print('_id:{}, parentId:{}, left:{}, right:{}, folder:{}, children:{}'.format(
board['_id'], board['parentId'], board['leftId'], board['rightId'], board['is_folder'], board['children'])) # 用于递归的方法
def markLeftAndRight(idList):
global counter
for id in idList:
board = boardsDict[id]
if ('leftId' in board):
print('Error: {}'.format(id))
exit
board['leftId'] = counter
counter += 1
if (len(board['children']) > 0):
markLeftAndRight(board['children'])
board['rightId'] = counter
counter += 1
.
写入MySQL
用pymsql写入mysql, 每写入一个版面, 同时写入对应的权限, 注意分区和版面的默认权限数据是不一样的.
# Write it to MySQL
for board in boards:
try:
with rbcommon.mysqlclient.cursor() as cursor:
# insert forum
sql = '''INSERT INTO `phpbb_forums` (`forum_id`, `parent_id`, `left_id`, `right_id`, `forum_parents`, `forum_name`, `forum_desc`,
`forum_rules`, `forum_type`)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)'''
cursor.execute(sql, (
board['_id'],
board['parentId'],
board['leftId'],
board['rightId'],
'',
board['name2'],
board['desc'],
'',
0 if (board['is_folder'] == 'true') else 1))
rbcommon.mysqlclient.commit()
# insert acl_group
if (board['is_folder'] == 'true'):
sql = 'INSERT INTO `phpbb_acl_groups` VALUES (1,%s,0,17,0),(2,%s,0,17,0),(3,%s,0,17,0),(6,%s,0,17,0)'
cursor.execute(sql, (
board['_id'], board['_id'], board['_id'], board['_id']))
rbcommon.mysqlclient.commit()
else:
sql = 'INSERT INTO `phpbb_acl_groups` VALUES (1,%s,0,17,0),(2,%s,0,15,0),(3,%s,0,15,0),(4,%s,0,21,0),(5,%s,0,14,0),(5,%s,0,10,0),(6,%s,0,19,0),(7,%s,0,24,0)'
cursor.execute(sql, (
board['_id'], board['_id'], board['_id'], board['_id'], board['_id'], board['_id'], board['_id'], board['_id']))
rbcommon.mysqlclient.commit() except Exception as e:
print(e)
数据导入后, 使用管理员帐号先在后台清空缓存, 再查看和编辑版面
phpBB3导入版面的Python脚本的更多相关文章
- phpBB3导入用户的Python脚本
关联的数据表 在phpBB3中导入用户时, 需要处理的有两张表, 一个是 users, 一个是 user_group. 如果是新安装的论坛, 在每次导入之前, 用以下语句初始化: DELETE FRO ...
- phpBB3导入帖子的Python脚本
关联的数据表 在phpBB3中导入用户时, 需要处理的有两张表, 一个是 topics, 一个是 posts.为了方便与原数据关联, 需要在这两个表上新增一个字段并建立唯一索引 ALTER TABLE ...
- C#调用python脚本
因项目需要,需要使用C#控制台程序执行python脚本,查询各种资料后可以成功调用了,记录一下,以备后面遗忘. 只尝试了两种调用方式,第一种只适用于python脚本中不包含第三方模块的情况,第二种针对 ...
- Python实用案例,Python脚本,Python实现每日更换“必应图片”为“桌面壁纸”
往期回顾 Python实现自动监测Github项目并打开网页 Python实现文件自动归类 Python实现帮你选择双色球号码 前言: 今天我们就利用python脚本实现每日更换"必应图片& ...
- Python脚本控制的WebDriver 常用操作 <六> 打印当前页面的title及url
下面将使用WebDriver来答应浏览器页面的title和访问的地址信息 测试用例场景 测试中,访问1个页面然后判断其title是否符合预期是很常见的1个用例: 假设1个页面的title应该是'hel ...
- python 脚本查看微信把你删除的好友--win系统版
PS:目测由于微信改动,该脚本目前不起作用 下面截图来自原作者0x5e 相信大家在微信上一定被上面的这段话刷过屏,群发消息应该算是微信上流传最广的找到删除好友的方法了.但群发消息不仅仅会把通讯录里面所 ...
- *** Python版一键安装脚本
本脚本适用环境:系统支持:CentOS 6,7,Debian,Ubuntu内存要求:≥128M日期:2018 年 02 月 07 日 关于本脚本:一键安装 Python 版 *** 的最新版.友情提示 ...
- zabbix3.4用Python脚本Excel批量导入主机
1.安装xlrd读取Excel文件 1.1. 下载setuptools-38.2.4.zip,上传至zabbix服务器解压安装,下载地址:https://pypi.python.org/package ...
- 自己来编写一份 Python 脚本 第一版
解决问题 我们已经探索了 Python 语言中的许多部分,现在我们将通过设计并编写一款程序来了解如何把这些部分组合到一起.这些程序一定是能做到一些有用的事情.这节的Python教程就是教大家方法去学习 ...
随机推荐
- 001.CDN概述
一 互联网应用质量概述 1.1 互联网应用质量 互联网应用质量指标--QoE,其主要指标: 服务成功率:指用户所请求的服务成功完成的几率. 服务建立时间:指从服务请求到服务呈现所花费的时间,并且会因为 ...
- shell 自加
Linux Shell中写循环时,常常要用到变量的自增,现在总结一下整型变量自增的方法.我所知道的,bash中,目前有五种方法:1. i=`expr $i + 1`;2. let i+=1;3. (( ...
- [PA2015]Rozstaw szyn
[PA2015]Rozstaw szyn 题目大意: 一棵\(n(n\le5\times10^5)\)个点的树,其中有\(m\)个结点是叶子结点.叶子结点权值已知,你可以自己决定其余结点的权值,定义整 ...
- 浏览器JS报错Uncaught RangeError Maximum call stack size exceeded
JavaScript错误:Uncaught RangeError: Maximum call stack size exceeded 堆栈溢出 原因:有小类到大类的递归查询导致溢出 解决方法思想: A ...
- html页面布局之table布局:
table布局: table来做整体页面的布局,布局技巧归纳如下: (1)按照设计图的尺寸设置表格的宽高以及单元格的宽高 (2)将表格的border.cellpadding.cellspacing全部 ...
- 使用iscroll,无法正常滑动的原因
iscroll的dom元素的结构是固定的,swiper是容器,scroll是需要滚动的容器,list是滚动的内容 <div class="swiper"> <di ...
- 逻辑回归与神经网络还有Softmax regression的关系与区别
本文讨论的关键词:Logistic Regression(逻辑回归).Neural Networks(神经网络) 之前在学习LR和NN的时候,一直对它们独立学习思考,就简单当做是机器学习中的两个不同的 ...
- Linux之nginx反向代理三台web
作业三:nginx反向代理三台web 实现基于轮询的方式调度三台web,并验证结果 实现基于权重的方式调度三台web,并验证结果 实现基于hash的方式调用三台web,并验证结果 [root@loca ...
- JS_高程6.面向对象的程序设计(2)创建对象_3 构造函数存在的问题
# 上次讲到用构造函数的模式来创建对象,相对于工厂模式,解决可对象识别的问题. function Person(name,age,job){ this.name=name; this.age=age; ...
- JS_高程5.引用类型(5)Array类型的操作方法
一.操作方法 1.concat()方法 基于当前数组中的所有项创建一个新数组.具体说,是先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组.在没有给concat() ...