diff --git a/phpBB/docs/CHANGELOG.html b/phpBB/docs/CHANGELOG.html
index a92ed8124d..9cef67cd72 100644
--- a/phpBB/docs/CHANGELOG.html
+++ b/phpBB/docs/CHANGELOG.html
@@ -219,6 +219,7 @@ p a {
[Fix] Consider viewonline permission when viewing friends/foes (Bug #12955)
[Fix] Added proper unicode support to ban reasons (Bug #12947)
[Fix] The forum/topic sync code now recognizes other ways that the latest post in a topic can be expressed (Bug #12947) - patch provided by APTX
+ [Fix] Topic deletion via the user post deletion mechanism did not take into account statistics for forums that hold shadow topics (Bug #12981)
diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php
index 0f15f5b604..21b2677483 100644
--- a/phpBB/includes/functions_posting.php
+++ b/phpBB/includes/functions_posting.php
@@ -1315,6 +1315,29 @@ function delete_post($forum_id, $topic_id, $post_id, &$data)
$db->sql_transaction('begin');
+ // we must make sure to update forums that contain the shadow'd topic
+ if ($post_mode == 'delete_topic')
+ {
+ $shadow_forum_ids = array();
+
+ $sql = 'SELECT forum_id
+ FROM ' . TOPICS_TABLE . '
+ WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
+ $result = $db->sql_query($sql);
+ while ($row = $db->sql_fetchrow($result))
+ {
+ if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
+ {
+ $shadow_forum_ids[(int) $row['forum_id']] = 1;
+ }
+ else
+ {
+ $shadow_forum_ids[(int) $row['forum_id']]++;
+ }
+ }
+ $db->sql_freeresult($result);
+ }
+
if (!delete_posts('post_id', array($post_id), false, false))
{
// Try to delete topic, we may had an previous error causing inconsistency
@@ -1332,6 +1355,15 @@ function delete_post($forum_id, $topic_id, $post_id, &$data)
switch ($post_mode)
{
case 'delete_topic':
+
+ foreach ($shadow_forum_ids as $updated_forum => $topic_count)
+ {
+ // counting is fun! we only have to do sizeof($forum_ids) number of queries,
+ // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
+ $db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET forum_topics_real = forum_topics_real - ' . $topic_count . ', forum_topics = forum_topics - ' . $topic_count . ' WHERE forum_id = ' . $updated_forum);
+ update_post_information('forum', $updated_forum);
+ }
+
delete_topics('topic_id', array($topic_id), false);
if ($data['topic_type'] != POST_GLOBAL)