平方X

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2707|回复: 0

Discuz 模拟发贴流程的php类。

[复制链接]

414

主题

709

帖子

3602

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3602
QQ
发表于 2014-12-25 08:52:07 | 显示全部楼层 |阅读模式
【转载声明】转帖注明出处,是对原作者最基本的尊重,感谢原作者的辛劳和分享。
本帖转自:http://www.php230.com/discuz-data-acquisition-and-posting.html.

.
这个东东真的不容易百度到,我原以为discuz会提供接口嘞,可惜愚笨未找到。
在搜索直接往数据库写的时候,搜索到一堆curl的,不是我需要的。
后来搜索 discuz php模拟发贴,终于搜到了原作者的这篇分享,稍加修改就可以适应最新的discuz,
假以时日,我也自己看看发贴流程,我主要是没找到在哪看,笨。
.

一直想弄个discuz的数据采集程序,这2天研究了下discuz发帖涉及的几个数据库表,这里分享一下自己的处理方法。
discuz发表主题设计的几个表:(这里列出了主要的几个相关的表)
1、主题表 pre_forum_thread:这个表一个主要数据就是 tid 主题ID
2、post 分表协调表 pre_forum_post_tableid:这里需要获取一个自增的 pid
3、帖子表 pre_forum_post :记录主题pid、fid、tid、title、content等主要信息
4、版块表 pre_forum_forum:这里主要更新版块的主题、帖子数量
5、帖子主题审核数据表 pre_forum_thread_moderate:这个可以根据自己状况决定,并不是必须的
6、用户统计表 pre_common_member_count:主要是更新用户的主题数量
自己处理发帖主要涉及到了上面6个数据库表,其中第5个不是必须的。想了解discuz 数据库相关信息可以查看:http://faq.comsenz.com/library/database/x25/x25_index.htm
大致流程是这样的:
第一步:向 主题表 pre_forum_thread 中插入版块ID、用户ID、用户名、帖子标题、发帖时间等信息。
第二步:获取第一步插入表 pre_forum_thread 的数据ID,作为主题ID,即 tid
第三步:向 post 分表协调表 pre_forum_post_tableid 插入一条数据,这张表中只有一个自增字段 pid
第四步:获取 第三步 插入表 pre_forum_post_tableid 的数据ID,作为 pid
第五部:向帖子表 pre_forum_post 中插入帖子相关信息,这里需要注意的是: pid为第四部的pid值,tid为第二步的tid值
第六部:更新版块 pre_forum_forum 相关主题、帖子数量信息
第七步:更新用户 pre_common_member_count 帖子数量信息
discuz发帖过程主要就是以上7个步骤,通过这几个步骤就可以完成对实现discuz的发帖流程,其中设计到一些积分等其他信息的可以自己加上。
<?php
date_default_timezone_set('PRC');
include 'db.class.php';

class post
{
        private $host = 'localhost';
        private $username = 'root';
        private $password = '123456';
        private $database_name = 'ultrax';
        private $_db = null;

        private $fid = null;
        private $title = null;
        private $content = null;
        private $author = null;
        private $author_id = null;
        private $current_time = null;
        private $displayorder = null; //0:正常,-2:待审核

        public function __construct($fid, $title, $content, $author, $author_id, $displayorder = -2)
        {
                $this->_db = db::get_instance($this->host, $this->username, $this->password, $this->database_name);

                $this->fid          = $fid;
                $this->title        = $title;
                $this->content      = $content;
                $this->author       = $author;
                $this->author_id    = $author_id;
                $this->current_time = time();
                $this->displayorder = $displayorder;
        }

        public function post_posts()
        {
                $tid = $this->do_pre_forum_thread();

                if(!$tid)
                {
                        return '更新表 pre_forum_thread 失败<br />';
                }

                $pid = $this->do_pre_forum_post_tableid();

                if(!$this->do_pre_forum_post($pid, $tid))
                {
                        return '更新表 pre_forum_post 失败<br />';
                }

                if(!$this->do_pre_forum_forum())
                {
                        return '更新表 pre_forum_forum 失败<br />';
                }

                if($this->displayorder == -2)
                {
                        if(!($this->do_pre_forum_thread_moderate($tid)))
                        {
                                return '更新表 pre_forum_thread_moderate 失败<br />';
                        }
                }

                return ($this->do_pre_common_member_count()) ? '发帖成功<br />' : '更新表 pre_pre_common_member_count 失败<br />';
        }

        private function do_pre_forum_thread()
        {
                $data                 = array();
                $data['fid']          = $this->fid;
                $data['author']       = $this->author;
                $data['authorid']     = $this->author_id;
                $data['subject']      = $this->title;
                $data['dateline']     = $this->current_time;
                $data['lastpost']     = $this->current_time;
                $data['lastposter']   = $this->author;
                $data['displayorder'] = $this->displayorder;

                $result = $this->_db->insert($data, 'pre_forum_thread');

                if($result == 1)
                {
                        $tid = $this->get_last_id();
                }

                return $tid;
        }

        private function do_pre_forum_post_tableid()
        {
                $sql    = "INSERT INTO `pre_forum_post_tableid`(`pid`) VALUES(NULL)";
                $result = $this->_db->query($sql);
                if($result == 1)
                {
                        $pid = $this->get_last_id();
                }

                return $pid;
        }

        private function do_pre_forum_post($pid, $tid)
        {
                $data             = array();
                $data['pid']      = $pid;
                $data['fid']      = $this->fid;
                $data['tid']      = $tid;
                $data['first']    = 1;
                $data['author']   = $this->author;
                $data['authorid'] = $this->author_id;
                $data['subject']  = $this->title;
                $data['dateline'] = $this->current_time;
                $data['message']  = $this->content;

                $result = $this->_db->insert($data, 'pre_forum_post');

                return ($result == 1) ? true : false;
        }

        private function do_pre_forum_forum()
        {
                $sql = "UPDATE `pre_forum_forum` SET `threads`=threads+1,`posts`=posts+1,`todayposts`=todayposts+1 WHERE `fid`={$this->fid}";

                $result = $this->_db->query($sql);

                return ($result == 1) ? true : false;
        }

        private function do_pre_forum_thread_moderate($tid)
        {
                $data             = array();
                $data['tid']      = $tid;
                $data['status']   = 0;
                $data['dateline'] = $this->current_time;

                $result = $this->_db->insert($data, 'pre_forum_thread_moderate');

                return ($result == 1) ? true : false;
        }

        private function do_pre_common_member_count()
        {
                $sql = "UPDATE `pre_common_member_count` SET `threads`=threads+1 WHERE `uid`={$this->author_id}";

                $result = $this->_db->query($sql);

                return ($result == 1) ? true : false;
        }

        private function get_last_id()
        {
                $sql    = "SELECT LAST_INSERT_ID()";
                $result = mysql_query($sql);

                while($row = mysql_fetch_assoc($result))
                {

                        $id = $row['LAST_INSERT_ID()'];
                }

                return $id;
        }
}
我是平方X~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|平方X ( 冀ICP备14018164号 )

GMT+8, 2024-4-27 02:12 , Processed in 0.118698 second(s), 21 queries .

技术支持:Powered by Discuz!X3.4  © 2001-2013 Comsenz Inc.

版权所有:Copyright © 2014-2018 平方X www.pingfangx.com All rights reserved.

快速回复 返回顶部 返回列表