用Discuz!论坛的人都知道,如果论坛防灌水机制没有做好,就会有很多的发帖机来"光顾"论坛,虽然我们可以通过禁止并删除该用户所有发帖来清除这些"垃圾",但是在论坛首页的"最后发表"还是会留下那些"垃圾帖"的内容,尤其是对于流量少,发帖少的论坛(发帖频率高的论坛这种现象较少),很长一段时间内一眼看下去"最后发帖"全是"垃圾"内容,因此,我们需要重建"最后发表"的内容。
登陆到Discuz!的后台—-工具—-更新论坛统计—-重建论坛帖数 手动提交即可。
【注】:上面内容摘录自http://www.slyar.com/blog/discuz-last-publish.html
但是这个功能是需要手工执行的,现在实现定时任务脚本如下:
实现逻辑:之前是要手工提交执行的,只要我们找到提交执行的脚本(admincp_counter.php),改造为DZ定时任务执行脚本,并按照DZ的定时任务配置方法,将下面代码拷贝到 cron_update_lastpost.php,并放到指定路径即可。
<?php /** * @cron_update_lastpost.php * @重建(论坛/群组)帖数 的计划任务脚本 * @2013-03-25 by AndyZhang * */ if(!defined('IN_DISCUZ')) { exit('Access Denied'); } $pertaskinit = 15; //默认每个循环更新数量 $pertask = isset($_GET['pertask']) ? intval($_GET['pertask']) : $pertaskinit; $current = isset($_GET['current']) && $_GET['current'] > 0 ? intval($_GET['current']) : 0; $processed = 1; while ($processed) { $queryf = C::t('forum_forum')->fetch_all_fids(1, '', '', $current, $pertask); if($queryf[0]['fid']) { foreach($queryf as $forum) { $processed = 1; $threads = $posts = 0; $threadtables = array('0'); $archive = 0; foreach(C::t('forum_forum_threadtable')->fetch_all_by_fid($forum['fid']) as $data) { //板块存档表 if($data['threadtableid']) { $threadtables[] = $data['threadtableid']; } } $threadtables = array_unique($threadtables); foreach($threadtables as $tableid) { $data = C::t('forum_thread')->count_posts_by_fid($forum['fid'], $tableid); $threads += $data['threads']; $posts += $data['posts']; if($data['threads'] == 0 && $tableid != 0) { C::t('forum_forum_threadtable')->delete($forum['fid'], $tableid); } if($data['threads'] > 0 && $tableid != 0) { $archive = 1; } } C::t('forum_forum')->update($forum['fid'], array('archive' => $archive)); $thread = C::t('forum_thread')->fetch_by_fid_displayorder($forum['fid']); $lastpost = "$thread[tid]\t$thread[subject]\t$thread[lastpost]\t$thread[lastposter]"; C::t('forum_forum')->update($forum['fid'], array('threads' => $threads, 'posts' => $posts, 'lastpost' => $lastpost)); } $current += $pertask; } else { C::t('forum_forum')->clear_forum_counter_for_group(); $processed = 0; } } ?>