discuz 帖子模块用到的表及自动发帖函数

简介:

 最近在做一个discuz的插件,由于需要程序自动生成并调用discuz已经存在插件的帖子。然而这就相当于自动发帖的功能了。网上找了一下,大部分都是通过curl模拟登陆,模拟发帖的,这显然不满足我的要求。如果采用这种方式既笨重又麻烦。百度了一通,没发现好的结果。于是google了一番,最后找到一个类似的方法。经过一番整理,于是有了下面这个函数。

  discuz帖子模块用到的表:

    帖子表:pre_forum_post

    帖子表pid最大值设置表:pre_forum_post_tableid

    帖子列表表:pre_forum_thread

    帖子所在板块表:pre_forum_forum

  这几个表之间的关系是,帖子表pre_forum_post存放帖子的详细信息,其pid通过pre_forum_post_tableid表获得。帖子列表pre_forum_thread表决定了该条记录是否显示在列表中,如果此表中没有相应的记录帖子也就无法显示在列表中了。帖子所在板块表pre_forum_forum存放了对应板块的发帖数量,今日发帖数以及最近发帖的标题等信息。

  好了,了解了这几张表之间的关系后有了下面这个函数和测试例子。

  

复制代码
<?php
    if(!defined('IN_DISCUZ')) {
        exit('Access Denied');
    }
    echo "11111111111111111111111";
    require_once DISCUZ_ROOT . './source/class/class_core.php';
    $discuz = C::app();
    $discuz->cachelist = $cachelist;
    $discuz->init();
    
    $subject = '自行写入帖子';
    $message = '自行写入帖子的消息消息消息';
    $thread['fid'] = 86;
    $thread['subject'] = $subject;
    $thread['message'] = $message;
    $thread["authorid"]= 1;
    $thread["author"]=$_G['member'][username];
    
    $tid = addThread($thread);
    
    echo "*********** $tid **************";
    
    /*
    * 添加帖子
    * @data 帖子数组 
    *         array('fid' => '板块ID', 
                'subject' => '标题', 
                'message' => '具体内容',
                'authorid' => '用户ID',
                'author' => '用户名');
    */
    function addThread($data){
        $fid = $data['fid'];
        $subject = $data['subject'];
        $message = $data['message'];
        $authorid = $data['authorid'];
        $author = $data['author'];
        $proc_time = time();
        
        $thread['fid'] = $fid;//板块ID
        $thread['subject'] = $subject;//标题
        $thread["authorid"]= $authorid;
        $thread["author"]= $author;
        $thread["dateline"]= $proc_time;
        $thread["lastpost"]= $proc_time;
        $thread["lastposter"]= $author;
        //插件必须添加special参数,否则无法按照插件的样式进行显示
        $thread["special"]= 127;//特殊主题,1:投票;2:商品;3:悬赏;4:活动;5:辩论贴;127:插件相关
        
        $tid=C::t('forum_thread')->insert($thread, 1);//添加到帖子列表
        
        if($tid){
            $post["tid"] = $tid;
            $post['fid'] = $fid;
            $post["position"]= 1;
            $post["smileyoff"]= "-1";
            $post["bbcodeoff"]= "-1";
            $post["usesig"]= 1;
            $post["author"]= $author;
            $post["authorid"]= $authorid;
            $post["subject"]= $subject;
            $post["message"]= $message . chr(0).chr(0).chr(0) . "online_alipay";//结尾一定要加chr(0).chr(0).chr(0) . "online_alipay"结尾,否则无法调用插件的模板。
            $post["dateline"]= $proc_time;
            $post["first"]= 1;

            $pid = C::t('forum_post_tableid')->insert(array('pid' => null), true);//添加pre_forum_post表的pid最大值
            $post["pid"]= $pid;
            $okid=C::t('forum_post')->insert("0", $post, 1);//写入帖子
            
            $lastpost = $tid . "$subject" . $proc_time . $author;
            $sql = "update " . DB::table('forum_forum') . " set threads = threads + 1, todayposts = todayposts + 1, lastpost = '$lastpost' where fid= $fid";//修改今日主题和帖子数量
            DB::query($sql);
        }

        return $tid;
    }
?>
复制代码

  addThread参数需要提供几个必要的参数板块ID、标题、用户名、用户ID和消息内容。如果你想往哪个板块自动生成一个帖子,尽管调用addThread函数即可。

  如果是插件这里有个需要特别注意的地方

1、forum_thread表,必须将special字段的值设为127($thread["special"]= 127;)
2、forum_post表的message字段。如果你的是插件的话,最后面一定要加上
chr(0).chr(0).chr(0) . "插件名称"

  否则,插件的模板将无法调用。这是为什么呢?这涉及到discuz插件模板设计的问题。

  原因分析:

   ./source/module/forum/forum_viewthread.php,大概700行左右,有这么一段

复制代码
if($_G['forum_thread']['special'] > 0 && (empty($_GET['viewpid']) || $_GET['viewpid'] == $_G['forum_firstpid'])) {
    $_G['forum_thread']['starttime'] = gmdate($_G['forum_thread']['dateline']);
    $_G['forum_thread']['remaintime'] = '';
    switch($_G['forum_thread']['special']) {
        case 1: require_once libfile('thread/poll', 'include'); break;
        case 2: require_once libfile('thread/trade', 'include'); break;
        case 3: require_once libfile('thread/reward', 'include'); break;
        case 4: require_once libfile('thread/activity', 'include'); break;
        case 5: require_once libfile('thread/debate', 'include'); break;
        case 127:
            if($_G['forum_firstpid']) {
                $sppos = strpos($postlist[$_G['forum_firstpid']]['message'], chr(0).chr(0).chr(0));
                $specialextra = substr($postlist[$_G['forum_firstpid']]['message'], $sppos + 3);
                $postlist[$_G['forum_firstpid']]['message'] = substr($postlist[$_G['forum_firstpid']]['message'], 0, $sppos);
                if($specialextra) {
                    if(array_key_exists($specialextra, $_G['setting']['threadplugins'])) {
                        @include_once DISCUZ_ROOT.'./source/plugin/'.$_G['setting']['threadplugins'][$specialextra]['module'].'.class.php';
                        $classname = 'threadplugin_'.$specialextra;
                        if(class_exists($classname) && method_exists($threadpluginclass = new $classname, 'viewthread')) {
                            $threadplughtml = $threadpluginclass->viewthread($_G['tid']);
                            //var_dump($post['message']);
                        }
                    }
                }
            }
            break;
    }
}
复制代码

  原因就出在这。

  1、special为127是才执行插件的内容

  2、由于插件有很多,真么知道是哪个插件呢?因此,请看这行

    $sppos = strpos($postlist[$_G['forum_firstpid']]['message'], chr(0).chr(0).chr(0));

  discuz采用了chr(0).chr(0).chr(0)进行分割,获取插件名。如果无法获取插件名,则无法调用相应的模板,因而也就调用默认的系统模板了。






本文转自秋楓博客园博客,原文链接:http://www.cnblogs.com/rwxwsblog/p/4759775.html,如需转载请自行联系原作者
目录
相关文章
|
C#
WPF 左键单击弹出菜单 ContextMenu
原文:WPF 左键单击弹出菜单 ContextMenu WPF中的ContextMenu在XAML中可直接做出来,但是仅限于右键弹出菜单,如果需要添加左键弹出功能,只需要在事件中添加Click事件 XMAL代码如下 ...
3271 0
|
弹性计算 负载均衡 数据库
阿里云服务器带宽上传下载速度、出入网收费免费说明
用户花钱购买的阿里云服务器公网带宽是指公网出方向带宽,阿里云内网带宽和公网入方向带宽都是免费的。用户购买公网出方向带宽后,阿里云会提供免费的公网入方向带宽,公网入方向带宽10M起步
6592 0
阿里云服务器带宽上传下载速度、出入网收费免费说明
|
10月前
|
JavaScript 前端开发 Java
前端框架选择之争:jQuery与Vue在现代Web开发中的真实地位-优雅草卓伊凡
前端框架选择之争:jQuery与Vue在现代Web开发中的真实地位-优雅草卓伊凡
815 72
前端框架选择之争:jQuery与Vue在现代Web开发中的真实地位-优雅草卓伊凡
|
开发框架 安全 JavaScript
《Discuz! X3.5开发从入门到生态共建》第1章 Discuz! 的前世今生-优雅草卓伊凡
《Discuz! X3.5开发从入门到生态共建》第1章 Discuz! 的前世今生-优雅草卓伊凡
455 9
《Discuz! X3.5开发从入门到生态共建》第1章 Discuz! 的前世今生-优雅草卓伊凡
|
存储 JavaScript 搜索推荐
Node框架的安装和配置方法
安装 Node 框架是进行 Node 开发的第一步,通过正确的安装和配置,可以为后续的开发工作提供良好的基础。在安装过程中,需要仔细阅读相关文档和提示,遇到问题及时解决,以确保安装顺利完成。
1261 155
|
编解码 数据可视化 数据挖掘
Pygal库创建可缩放的矢量图表
【10月更文挑战第18天】Pygal 是一个 Python 库,专门用于创建可缩放的矢量图表。它基于 SVG 格式,支持多种图表类型,如线图、柱状图、饼图等,并提供丰富的自定义选项和交互功能。安装简单,使用 pip 即可安装。Pygal 不仅支持基本图表的创建,还允许添加数据标签、图例、注释、动画效果和交互功能,适用于数据分析、数据可视化和网站开发等多种场景。
|
Oracle 关系型数据库 MySQL
Centos7下图形化部署单点KFS同步工具并将Oracle增量同步到KES
Centos7下图形化部署单点KFS同步工具并将Oracle增量同步到KES
Centos7下图形化部署单点KFS同步工具并将Oracle增量同步到KES
|
缓存 UED 开发者
CDN的优缺点是什么呢
【4月更文挑战第21天】CDN提升网站访问速度和可用性,通过全球缓存服务器减轻源服务器负载,优化用户体验。然而,其成本较高,技术复杂,存在内容同步问题和对第三方服务的依赖。在使用CDN时需权衡利弊。
1574 4
|
缓存 中间件 API
从开发到部署微服务保姆级视频教程
从开发到部署微服务保姆级视频教程
|
应用服务中间件 API nginx

热门文章

最新文章