最近在做一個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)進行分割,獲取插件名。如果無法獲取插件名,則無法調用相應的模板,因而也就調用默認的系統模板了。