Anyone who uses the Discuz! forum knows that if the anti-spam mechanism is not well implemented, many bots will “visit” the forum. Although we can block and delete all posts by such users to remove this “spam,” the content of those “spam posts” will still remain in the “Last Post” section on the forum homepage. This is especially evident in forums with low traffic and few posts (forums with high post frequency experience this issue less often), where for a long time, the “Last Post” section is filled with “spam” content. Therefore, we need to rebuild the “Last Post” content.

Log into the Discuz! backend—Tools—Update Forum Statistics—Rebuild Forum Posts, and manually submit it.

However, this function needs to be executed manually. Now, the following scheduled task script has been created: Execution logic: Previously, it needed to be manually submitted. As long as we locate the script for submission (admincp_counter.php), we can modify it into a Discuz! scheduled task script. Following the Discuz! scheduled task configuration method, copy the code below into cron_update_lastpost.php and place it in the specified directory.

 

<?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;
}
}
?>