File: C:\phpStudy\bbs\xiunophp\misc.func.php, Line: 980, fopen(C:\phpStudy\bbs/./tmp/model_post.func.php , wb)
File: C:\phpStudy\bbs\model\plugin.func.php, Line: 30, file_put_contents_try(C:\phpStudy\bbs/./tmp/model_post.func.php , 最原生的 CURD,无关联其他数据。
// 只用传 message, message_fmt 自动生成
function post__create($arr, $gid) {
post_message_fmt($arr, $gid);
$r = db_insert('post', $arr);
return $r;
}
function post__update($pid, $arr) {
$r = db_update('post', array('pid'=>$pid), $arr);
return $r;
}
function post__read($pid) {
$post = db_find_one('post', array('pid'=>$pid));
return $post;
}
function post__delete($pid) {
$r = db_delete('post', array('pid'=>$pid));
return $r;
}
function post__find($cond = array(), $orderby = array(), $page = 1, $pagesize = 20) {
$postlist = db_find('post', $cond, $orderby, $page, $pagesize, 'pid');
return $postlist;
}
// ------------> 关联 CURD,主要是强相关的数据,比如缓存。弱相关的大量数据需要另外处理。
// 回帖
function post_create($arr, $fid, $gid) {
global $conf, $time;
$pid = post__create($arr, $gid);
if(!$pid) return $pid;
$tid = $arr['tid'];
$uid = $arr['uid'];
// 回帖
if($tid > 0) {
// todo: 如果是老帖,不更新 lastpid
thread__update($tid, array('posts+'=>1, 'lastpid'=>$pid, 'lastuid'=>$uid, 'last_date'=>$time));
$uid AND user__update($uid, array('posts+'=>1));
runtime_set('posts+', 1);
runtime_set('todayposts+', 1);
forum__update($fid, array('todayposts+'=>1));
}
//post_list_cache_delete($tid);
// 更新板块信息。
forum_list_cache_delete();
// 关联附件
$message = $arr['message'];
attach_assoc_post($pid);
// 更新用户的用户组
user_update_group($uid);
if(search_type() == 'fulltext') {
$s = strip_tags($message);
$words = search_cn_encode($s);
db_create('post_search', array('pid'=>$pid, 'message'=>$words));
}
return $pid;
}
// 编辑回帖
function post_update($pid, $arr, $tid = 0) {
global $conf, $user, $gid;
$post = post__read($pid);
if(empty($post)) return FALSE;
$tid = $post['tid'];
$uid = $post['uid'];
$isfirst = $post['isfirst'];
post_message_fmt($arr, $gid);
$r = post__update($pid, $arr);
attach_assoc_post($pid);
$message = $arr['message'];
if($isfirst) {
if(search_type() == 'fulltext') {
$thread = thread__read($tid);
$s = strip_tags($thread['subject'].' '.$message);
$words = search_cn_encode($s);
db_replace('thread_search', array('tid'=>$tid, 'message'=>$words));
}
} else {
if(search_type() == 'fulltext') {
$s = strip_tags($message);
$words = search_cn_encode($s);
db_replace('post_search', array('pid'=>$pid, 'message'=>$words));
}
}
return $r;
}
function post_read($pid) {
$post = post__read($pid);
post_format($post);
return $post;
}
// 从缓存中读取,避免重复从数据库取数据,主要用来前端显示,可能有延迟。重要业务逻辑不要调用此函数,数据可能不准确,因为并没有清理缓存,针对 request 生命周期有效。
function post_read_cache($pid) {
static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,要跨进程,可以再加一层缓存: memcached/xcache/apc/
if(isset($cache[$pid])) return $cache[$pid];
$cache[$pid] = post_read($pid);
return $cache[$pid];
}
// $tid 用来清理缓存
function post_delete($pid) {
global $conf;
$post = post_read_cache($pid);
if(empty($post)) return TRUE; // 已经不存在了。
$tid = $post['tid'];
$uid = $post['uid'];
$thread = thread_read_cache($tid);
$fid = $thread['fid'];
db_delete('post_search', array('pid'=>$pid));
if(!$post['isfirst']) {
thread__update($tid, array('posts-'=>1));
$uid AND user__update($uid, array('posts-'=>1));
runtime_set('posts-', 1);
} else {
//post_list_cache_delete($tid);
}
($post['images'] || $post['files']) AND attach_delete_by_pid($pid);
$r = post__delete($pid);
// 更新最后的 lastpid
if($r && !$post['isfirst'] && $pid == $thread['lastpid']) {
thread_update_last($tid);
}
return $r;
}
// 此处有可能会超时
function post_delete_by_tid($tid) {
$postlist = post_find_by_tid($tid);
foreach($postlist as $post) {
post_delete($post['pid']);
}
return count($postlist);
}
// 此处有可能会超时,并且导致统计不准确,需要重建统计数
function post_delete_by_uid($uid) {
$r = db_delete('post', array('uid'=>$uid));
return $r;
}
function post_find($cond = array(), $orderby = array(), $page = 1, $pagesize = 20) {
$postlist = post__find($cond, $orderby, $page, $pagesize);
$floor = 1;
if($postlist) foreach($postlist as &$post) {
$post['floor'] = $floor++;
post_format($post);
}
return $postlist;
}
// 此处有缓存,是否有必要?
function post_find_by_tid($tid, $page = 1, $pagesize = 50) {
global $conf;
$postlist = post__find(array('tid'=>$tid), array('pid'=>1), $page, $pagesize);
if($postlist) {
$floor = ($page - 1)* $pagesize + 1;
foreach($postlist as &$post) {
$post['floor'] = $floor++;
post_format($post);
}
}
return $postlist;
}
//

//
function user_post_message_format(&$s) {
if(xn_strlen($s) < 100) return;
$s = preg_replace('#.*?
#is', '', $s);
$s = str_ireplace(array('
', '
', '
', '', '', '
', '', ''. ''), "\r\n", $s);
$s = str_ireplace(array(' '), " ", $s);
$s = strip_tags($s);
$s = preg_replace('#[\r\n]+#', "\n", $s);
$s = xn_substr(trim($s), 0, 100);
$s = str_replace("\n", '