diff --git a/phpBB/docs/CHANGELOG.html b/phpBB/docs/CHANGELOG.html
index 8185eba3a0..d0f72a9784 100644
--- a/phpBB/docs/CHANGELOG.html
+++ b/phpBB/docs/CHANGELOG.html
@@ -100,6 +100,7 @@
[Fix] Do not increment users post count after post approval if post had been posted in a forum with no post count increasing set (Bug #37865)
[Fix] Extend vertical line for last post column if no posts in forum (Bug #37125)
[Fix] correctly update last topic/forum information if changing guest usernames through editing posts (Bug #38095)
+ [Fix] fix postcount resync for situations where low and high post ids are higher than step value, resulting in users having 0 posts. (Bug #38195)
[Change] Alllow applications to set custom module inclusion path (idea by HoL)
[Change] Handle checking for duplicate usernames in chunks (Bug #17285 - Patch by A_Jelly_Doughnut)
[Change] Better handling and finer control for custom profile fields visibility options. (Patch by Highway of Life)
diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php
index d100c49381..a558fe6712 100644
--- a/phpBB/includes/acp/acp_main.php
+++ b/phpBB/includes/acp/acp_main.php
@@ -185,12 +185,36 @@ class acp_main
}
// Resync post counts
- $start = 0;
- $step = ($config['num_posts']) ? (max((int) ($config['num_posts'] / 5), 20000)) : 20000;
+ $start = $max_post_id = 0;
+ // Find the maximum post ID, we can only stop the cycle when we've reached it
+ $sql = 'SELECT MAX(forum_last_post_id) as max_post_id
+ FROM ' . FORUMS_TABLE;
+ $result = $db->sql_query($sql);
+ $max_post_id = (int) $db->sql_fetchfield('max_post_id');
+ $db->sql_freeresult($result);
+
+ // No maximum post id? :o
+ if (!$max_post_id)
+ {
+ $sql = 'SELECT MAX(post_id)
+ FROM ' . POSTS_TABLE;
+ $result = $db->sql_query($sql);
+ $max_post_id = (int) $db->sql_fetchfield('max_post_id');
+ $db->sql_freeresult($result);
+ }
+
+ // Still no maximum post id? Then we are finished
+ if (!$max_post_id)
+ {
+ add_log('admin', 'LOG_RESYNC_POSTCOUNTS');
+ break;
+ }
+
+ $step = ($config['num_posts']) ? (max((int) ($config['num_posts'] / 5), 20000)) : 20000;
$db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_posts = 0');
- do
+ while ($start < $max_post_id)
{
$sql = 'SELECT COUNT(post_id) AS num_posts, poster_id
FROM ' . POSTS_TABLE . '
@@ -207,16 +231,11 @@ class acp_main
$db->sql_query($sql);
}
while ($row = $db->sql_fetchrow($result));
-
- $start += $step;
- }
- else
- {
- $start = 0;
}
$db->sql_freeresult($result);
+
+ $start += $step;
}
- while ($start);
add_log('admin', 'LOG_RESYNC_POSTCOUNTS');