实现Discuz!帖子内容多版块同步更新功能, 可通过以下技术方案实现:
1. 数据关联设计
- 创建扩展表pre_common_syncpost存储主帖与同步帖的映射关系
字段: maintid(主帖ID), synctids(同步帖ID集合), lastupdate(最后同步时间)
2. 发帖流程扩展
在帖子发布时通过Hook扩展:
- // source/plugin/syncpost/hook.class.php
- class plugin_syncpost{
- public function post_publish_extra(){
- $tid = $_G['tid'];
- $selectedfids = $_GET['syncfids']; // 获取用户选择的同步版块
- foreach($selectedfids as $fid){
- $newtid = $this->_clone_thread($tid, $fid);
- C::t('common_syncpost')->insert(array(
- 'maintid' => $tid,
- 'synctid' => $newtid
- ));
- }
- }
-
- private function _clone_thread($sourceTid, $targetFid){
- // 复制主帖数据到新主题
- $thread = C::t('forum_thread')->fetch($sourceTid);
- unset($thread['tid']);
- $newtid = C::t('forum_thread')->insert($thread, true);
-
- // 复制帖子内容
- $post = C::t('forum_post')->fetch_threadpost_by_tid($sourceTid);
- $post['tid'] = $newtid;
- C::t('forum_post')->insert('tid:'.$newtid, $post);
-
- return $newtid;
- }
- }
复制代码
3. 同步更新机制
在帖子编辑时触发同步:
- class plugin_syncpost{
- public function post_update_extra(){
- $tid = $_G['tid'];
- $synctids = C::t('common_syncpost')->fetch_all_by_maintid($tid);
-
- $mainPost = C::t('forum_post')->fetch_threadpost_by_tid($tid);
- foreach($synctids as $synctid){
- C::t('forum_post')->update_post(
- $synctid,
- $mainPost['message'],
- $mainPost['subject']
- );
- updateforumcount($synctid['fid']);
- }
- }
- }
复制代码
4. 前端交互增强
在发帖页面增加版块选择组件:
- // template/default/forum/post_editor_extra.htm
- <div class="sync-box">
- <h3>同步发布到其他版块</h3>
- <ul class="fid-list">
- <!--{loop $forumlist $forum}-->
- <li><input type="checkbox" name="syncfids[]" value="$forum['fid']"> $forum['name']</li>
- <!--{/loop}-->
- </ul>
- </div>
复制代码
5. 注意事项
- 权限校验: 同步操作前需验证用户是否具备目标版块的发帖权限
- 附件处理: 采用引用方式关联附件避免重复上传
- 性能优化: 使用内存表存储映射关系, 批量更新时控制每次处理数量
- 冲突处理: 当同步帖被单独修改时记录冲突日志
- 索引维护: 更新后重建相关版块的搜索索引
该方案通过扩展Discuz!的标准发帖流程, 在不修改核心代码的情况下实现内容同步功能。开发者可根据具体需求调整同步策略, 建议在测试环境充分验证后部署到生产环境。对于更复杂的同步需求, 可参考DZ-X技术文档中的分布式数据同步方案。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。 |