|
调用需求: 要实现论坛帖子点击和回复的日、周、月、总等四种数据排行. 目前版本里边有的包括: 1) 回复数总数排行 2) 回复数每日排行 3) 回复数每周排行(功能貌似版本没写好,补充了下) 4) 点击数总数排行 5) 点击数每日排行 先需要增加新的排序如下: 1) 回复数每月排行 (replysortmonth) 2) 点击数每周排行 (hitsortweek)(这个排序是有的,只是在筛选数据的时候没做而已,屏蔽了) 3) 点击数每月排行 (hitsortmonth) 方案如下: 向pw_elements表增加几种新的排序,replysortmonth、hitsortweek、hitsortmonth. 修改如下: 打开thread.php文件,找到: - if ($db_ifpwcache & 64 && $thread['postdate'] > $timestamp-7*24*3600) {
- if ($thread['hits'] > $hitsort_judge['hitsortweek'][$this->fid] && $thread['fid'] == $this->fid) {
- $updatelist[] = array('hitsortweek', $this->fid, $thread['tid'], $thread['hits'], $thread['postdate'], 0);
- $updatetype['hitsortweek'] = 1;
- }
- }
下下下方添加如下代码: - if ($thread['postdate'] > $timestamp-30*24*3600) {
- if ($thread['hits'] > $hitsort_judge['hitsortmonth'][$this->fid] && $thread['fid'] == $this->fid) {
- $updatelist[] = array('hitsortmonth', $this->fid, $thread['tid'], $thread['hits'], $thread['postdate'], 0);
- $updatetype['hitsortmonth'] = 1;
- }
- }
打开lib/elementupdate.class.php文件,找到: - if ($this->ifcache & 64 && $thread['postdate'] > 7 * 24 * 3600) {
- if ($thread['hits'] > $hitsort_judge['hitsortweek'][$fid]) {
- $this->updatelist[] = array(
- 'hitsortweek',
- $fid,
- $thread['tid'],
- $thread['hits'],
- $thread['postdate'],
- 0
- );
- $this->updatetype['hitsortweek'] = 1;
- }
- }
下下下方添加如下代码: - if ($thread['postdate'] > 30 * 24 * 3600) {
- if ($thread['hits'] > $hitsort_judge['hitsortmonth'][$fid]) {
- $this->updatelist[] = array(
- 'hitsortmonth',
- $fid,
- $thread['tid'],
- $thread['hits'],
- $thread['postdate'],
- 0
- );
- $this->updatetype['hitsortmonth'] = 1;
- }
- }
找到代码: - if ($this->ifcache & 8 && $postdate > $timestamp - 7 * 24 * 3600) {
- if ($replies > $this->judge['replysort']['replysortweek'][$fid]) {
- $this->updatelist[] = array(
- 'replysortweek',
- $fid,
- $tid,
- $replies,
- $postdate,
- $special
- );
- $this->updatetype['replysortweek'] = 1;
- }
- }
下下下方添加如下代码: - if ($postdate > $timestamp - 30 * 24 * 3600) {
- if ($replies > $this->judge['replysort']['replysortmonth'][$fid]) {
- $this->updatelist[] = array(
- 'replysortmonth',
- $fid,
- $tid,
- $replies,
- $postdate,
- $special
- );
- $this->updatetype['replysortmonth'] = 1;
- }
- }
找到: - function updateSQL() {
- …………
- }
替换成: - function updateSQL() {
- global $timestamp;
- if (!$this->updatelist || !$this->updatetype || !$this->mark) return false;
- $special = (int) $this->special;
- $judges = array();
- $todaytime = $weektime = '';
- foreach ($this->updatetype as $key => $val) {
- if (in_array($key, array(
- 'replysort',
- 'replysortday',
- 'replysortweek',
- 'replysortmonth',
- )) && $this->judge['replysort']) {
- $judges['replysort'] = $this->judge['replysort'];
- }
- if (in_array($key, array(
- 'hitsort',
- 'hitsortday',
- 'hitsortweek',
- 'hitsortmonth'
- )) && $this->judge['hitsort']) {
- $judges['hitsort'] = $this->judge['hitsort'];
- }
- if (strpos($key, 'day') && !$todaytime) {
- $todaytime = $timestamp - 24 * 3600;
- } elseif (strpos($key, 'week') && !$weektime) {
- $weektime = $timestamp - 7 * 24 * 3600;
- } elseif (strpos($key, 'month') && !$monthtime) {
- $monthtime = $timestamp - 30 * 24 * 3600;
- }
- }
- $this->db->update("REPLACE INTO pw_elements (type,mark,id,value,addition,special) VALUES " . S::sqlMulti($this->updatelist, false));
- $sortlist = array();
- $dellis = array();
- $query = $this->db->query("SELECT eid,type,value,addition FROM pw_elements WHERE type IN (" . S::sqlImplode(array_keys($this->updatetype)) . ") AND mark=" . S::sqlEscape($this->mark) . " AND special=" . S::sqlEscape($special) . " ORDER BY type,value DESC");
- while ($rt = $this->db->fetch_array($query)) {
- if (strpos($rt['type'], 'day') && $rt['addition'] && $rt['addition'] < $todaytime) {
- $dellist[] = $rt['eid'];
- } elseif (strpos($rt['type'], 'week') && $rt['addition'] && $rt['addition'] < $weektime) {
- $dellist[] = $rt['eid'];
- } elseif (strpos($rt['type'], 'month') && $rt['addition'] && $rt['addition'] < $monthtime) {
- $dellist[] = $rt['eid'];
- } else {
- $sortlist[$rt['type']][] = $rt;
- }
- }
-
- foreach ($sortlist as $key => $value) {
- if (count($value) > $this->cachenum) {
- $tem = array_slice($value, $this->cachenum);
- foreach ($tem as $val) {
- $dellist[] = $val['eid'];
- }
- }
- if (in_array($key, array(
- 'replysort',
- 'replysortday',
- 'replysortweek',
- 'replysortmonth'
- ))) {
- $judgetype = 'replysort';
- array_splice($value, $this->cachenum);
- } elseif (in_array($key, array(
- 'hitsort',
- 'hitsortday',
- 'hitsortweek',
- 'hitsortmonth'
- ))) {
- $judgetype = 'hitsort';
- array_splice($value, $this->cachenum);
- } else {
- $judgetype = '';
- }
- if ($judgetype && count($value) == $this->cachenum) {
- $tem = end($value);
- $judges[$judgetype][$key][$this->mark] = $tem['value'];
- } else {
- $judges[$judgetype][$key][$this->mark] = '0';
- }
- }
- if ($dellist) {
- $this->db->update("DELETE FROM pw_elements WHERE eid IN (" . S::sqlImplode($dellist) . ")");
- }
- if ($judges) {
- foreach ($judges as $key => $value) {
- if ($key == 'replysort') {
- if ($value != $this->judge['replysort']) {
- pwCache::setData(D_P . 'data/bbscache/replysort_judge_' . $special . '.php', "<?php\r\n\$replysort_judge=" . pw_var_export($value) . ";\r\n?>");
- }
- } elseif ($key == 'hitsort') {
- pwCache::setData(D_P . 'data/bbscache/hitsort_judge.php', "<?php\r\n\$hitsort_judge=" . pw_var_export($value) . ";\r\n?>");
- touch(D_P.'data/bbscache/hitsort_judge.php');
- }
- }
- }
- return true;
- }
这样修改以后,在帖子列表页的时候会触发更新hitsort相关的element项(定时更新600秒间隔,缓存见data/bbscache/hitsort_judge.php),点击回复的时候会更新replysort相关element项. 这样以后,pw_elements表里边就增加了新的几个排序类型了.接下来开始前台的数据调用,涉及到文件如下: 1、lib/area/source/subjectsource.class.php 2、lib/element.class.php 打开lib/area/source/subjectsource.class.php文件,找到: - 'replysortday' =>'今日回复',
- 'replysortweek' =>'近期回复',
- 'replysort' =>'回复排行',
- 'hitsortday' =>'今日点击',
- 'hitsort' =>'点击排行',
替换成: - 'replysortday' => '回复日排行',
- 'replysortweek' => '回复周排行',
- 'replysortmonth' => '回复月排行',
- 'replysort' => '回复总排行',
- 'hitsortday' => '点击日排行',
- 'hitsortweek' => '点击周排行',
- 'hitsortmonth' => '点击月排行',
- 'hitsort' => '点击总排行',
部分是重复的,只是在前台调用的时候显示更加清晰一些,所以这里放心替换。 找到: - function _getDataBySortType($sortType,$fid,$num) {
- $element = $this->_getElement();
- …………
- }
替换成: - function _getDataBySortType($sortType,$fid,$num) {
- $element = $this->_getElement();
- switch ($sortType) {
- case 'newsubject':
- return $element->newSubject($fid,$num);
- case 'newreply':
- return $element->newReply($fid,$num);
- case 'digestsubject':
- return $element->digestSubject($fid,$num);
- case 'topsubject':
- return $element->areaTopSubject($fid,$num);
- case 'highlightsubject':
- return $element->highLightSubject($fid,$num);
- case 'replysort':
- return $element->replySort($fid,$num);
- case 'replysortday':
- return $element->replySortDay($fid,$num);
- case 'replysortweek':
- return $element->replySortWeek($fid,$num);
- case 'replysortmonth':
- return $element->replySortMonth($fid,$num);
- case 'hitsort':
- return $element->hitSort($fid,$num);
- case 'hitsortday':
- return $element->hitSortDay($fid,$num);
- case 'hitsortweek':
- return $element->hitSortWeek($fid,$num);
- case 'hitsortmonth':
- return $element->hitSortMonth($fid,$num);
- default :
- return $element->newSubject($fid,$num);
- }
- }
打开lib/element.class.php文件,找到: - function replySortWeek($round=0,$num=0,$special=0){
- return $this->replySortInterface('replysortweek',$round,$num,$special);
- }
下下下方添加如下代码: - function replySortMonth($round=0,$num=0,$special=0){
- return $this->replySortInterface('replysortmonth',$round,$num,$special);
- }
找到: - function replySortInterface($type='replysort',$fid=0,$num=0,$special=0){
- …………
- }
替换成: - function replySortInterface($type='replysort',$fid=0,$num=0,$special=0){
- !$type && $type = 'replysort';
- !in_array($type,array('replysort','replysortday','replysortweek','replysortmonth')) && Showmsg('undefined_action');
- $num = intval($num) ? intval($num) : $this->defaultnum;
- $special = (int)$special;
- $fid = $this->_cookFid($fid);
-
- if (($type=='replysort' && ($this->ifpwcache & 2)) || ($type=='replysortday' && ($this->ifpwcache & 4)) || ($type=='replysortweek' && ($this->ifpwcache & 8)) || ($type=='replysortmonth')) {
- $sqladd = '';
- $sqladd .= ' AND e.special='.S::sqlEscape($special);
- $sort = array();
- $fid && $sqladd .= " AND e.mark IN ($fid) ";
- $dayTime = PwStrtoTime(get_date(time(),'Ymd'));
- if ($type == 'replysortday') {
- $sqladd .= " AND t.postdate >= $dayTime";
- }elseif ($type == 'replysortweek') {
- $weekTime = $dayTime - 7*24*3600;
- $sqladd .= " AND t.postdate >= $weekTime";
- }elseif ($type == 'replysortmonth') {
- $monthTime = $dayTime - 30*24*3600;
- $sqladd .= " AND t.postdate >= $monthTime";
- }
- if ($special == 2) {
- $sql = "SELECT a.*,e.mark as fid FROM pw_elements e LEFT JOIN pw_activity a ON e.id=a.tid WHERE e.type=".S::sqlEscape($type)." $sqladd ORDER BY e.value DESC".S::sqlLimit($num);
- } elseif ($special == 3) {
- global $db_moneyname,$db_rvrcname,$db_creditname,$db_currencyname,$_CREDITDB;
- $cType = array(
- 'money' => $db_moneyname,
- 'rvrc' => $db_rvrcname,
- 'credit' => $db_creditname,
- 'currency' => $db_currencyname
- );
- foreach ($_CREDITDB as $k => $v) {
- $cType[$k] = $v[0];
- }
- $sql = "SELECT r.tid,r.cbtype,r.catype,r.cbval,r.caval,r.timelimit,t.fid,t.author,t.authorid,t.subject,t.type,t.postdate,t.hits,t.replies FROM pw_elements e LEFT JOIN pw_reward r ON e.id=r.tid LEFT JOIN pw_threads t ON e.id=t.tid WHERE e.type=".S::sqlEscape($type)." $sqladd AND t.ifshield != 1 AND t.locked != 2 ORDER BY e.value DESC".S::sqlLimit($num);
- } elseif ($special == 4) {
- $sql = "SELECT t.tid,t.name,t.icon,t.price,e.mark as fid FROM pw_elements e LEFT JOIN pw_trade t ON e.id=t.tid WHERE e.type=".S::sqlEscape($type)." $sqladd ORDER BY e.value DESC".S::sqlLimit($num);
- } else {
- $sql = "SELECT t.tid,t.fid,t.author,t.authorid,t.subject,t.type,t.postdate,t.hits,t.replies FROM pw_elements e LEFT JOIN pw_threads t ON e.id=t.tid WHERE e.type=".S::sqlEscape($type)." $sqladd AND t.ifshield != 1 AND t.locked != 2 ORDER BY e.value DESC".S::sqlLimit($num);
- }
-
- $query = $this->db->query($sql);
- while ($rt = $this->db->fetch_array($query)) {
- $post = array();
- $post['url'] = 'read.php?tid='.$rt['tid'];
- $post['authorurl'] = 'u.php?uid=' . $rt['authorid'];
- if ($special == 2) {
- $post['title'] = $rt['subject'];
- $post['value'] = $rt['deadline'];
- $post['image'] = '';
- } elseif ($special == 3) {
- $post['title'] = $rt['subject'];
- $post['value'] = $cType[$rt['cbtype']].":".$rt['cbval'];
- $post['image'] = '';
- } elseif ($special == 4) {
- $post['title'] = $rt['name'];
- $post['value'] = $rt['price'];
- if ($rt['icon']) {
- $pic = geturl($rt['icon'],'show',1);
- if(is_array($pic)){
- $post['image'] = $pic[0];
- } else {
- $post['image'] = 'images/noproduct.gif';
- }
- } else {
- $post['image'] = 'images/noproduct.gif';
- }
- } else {
- $post['title'] = $rt['subject'];
- $post['value'] = $rt['replies'];
- $post['image'] = '';
- }
- $post['forumname'] = getForumName($rt['fid']);
- $post['forumurl'] = getForumUrl($rt['fid']);
- list($post['topictypename'],$post['topictypeurl']) = getTopicType($rt['type'],$rt['fid']);
- $post['addition'] = $rt;
- $sort[] = $post;
- }
- } else {
- $info = $this->singLeton(true,$num);
- switch ($type) {
- case 'replysort':
- $time = 0;
- break;
- case 'replysortday':
- $time = 24;
- break;
- case 'replysortweek':
- $time = 7*24;
- break;
- case 'replysortmonth':
- $time = 30*24;
- break;
- default:
- $time = 0;
- }
- $sort = $info->getPostList('replysort',$fid,$info->cachenum,$time,$special);
- }
- return $sort;
- }
找到: - function hitSortInterface($type='hitsort',$fid=0,$num=0,$special=0){
- …………
- }
替换成: - function hitSortInterface($type='hitsort',$fid=0,$num=0,$special=0){
- !$type && $type = 'hitsort';
- !in_array($type,array('hitsort','hitsortday','hitsortweek','hitsortmonth')) && Showmsg('undefined_action');
- $num = intval($num) ? intval($num) : $this->defaultnum;
- $fid = $this->_cookFid($fid);
- if (($type=='hitsort' && ($this->ifpwcache & 16)) || ($type=='hitsortday' && ($this->ifpwcache & 32)) || ($type=='hitsortweek' && ($this->ifpwcache & 64)) || ($type=='hitsortmonth')) {
- $sqladd = '';
- $sort = array();
- $fid && $sqladd .= " AND e.mark IN ($fid) ";
- $dayTime = PwStrtoTime(get_date(time(),'Ymd'));
- if ($type == 'hitsortday') {
- $sqladd .= " AND t.postdate >= $dayTime";
- }elseif ($type == 'hitsortweek') {
- $weekTime = $dayTime - 7*24*3600;
- $sqladd .= " AND t.postdate >= $weekTime";
- }elseif ($type == 'hitsortmonth') {
- $monthTime = $dayTime - 30*24*3600;
- $sqladd .= " AND t.postdate >= $monthTime";
- }
- $query = $this->db->query("SELECT t.tid,t.fid,t.author,t.authorid,t.subject,t.type,t.postdate,t.hits,t.replies FROM pw_elements e LEFT JOIN pw_threads t ON e.id=t.tid WHERE e.type=".S::sqlEscape($type)." $sqladd AND t.ifshield != 1 AND t.locked != 2 ORDER BY e.value DESC ".S::sqlLimit($num));
- while ($rt = $this->db->fetch_array($query)) {
- if (!$rt['tid']) continue;
- $post = array();
- $post['url'] = 'read.php?tid='.$rt['tid'];
- $post['title'] = $rt['subject'];
- $post['value'] = $rt['hits'];
- $post['image'] = '';
- $post['authorurl'] = 'u.php?uid=' . $rt['authorid'];
- $post['forumname'] = getForumName($rt['fid']);
- $post['forumurl'] = getForumUrl($rt['fid']);
- list($post['topictypename'],$post['topictypeurl']) = getTopicType($rt['type'],$rt['fid']);
- $post['addition'] = $rt;
- $sort[] = $post;
- }
- } else {
- $info = $this->singLeton(true,$num);
- switch ($type) {
- case 'hitsort':
- $time = 0;
- break;
- case 'hitsortday':
- $time = 24;
- break;
- case 'hitsortweek':
- $time = 7*24;
- break;
- case 'hitsortmonth':
- $time = 30*24;
- break;
- default:
- $time = 0;
- }
- $sort = $info->getPostList('hitsort',$fid,$info->cachenum,$time);
- }
- return $sort;
- }
找到: - function hitSortWeek($round=0,$num=0,$special=0){
- return $this->hitSortInterface('hitsortweek',$round,$num,$special);
- }
下下下方添加如下代码: - function hitSortMonth($round=0,$num=0,$special=0){
- return $this->hitSortInterface('hitsortmonth',$round,$num,$special);
- }
|