From 79353883066f1a9d5d7b0c40cbd96ce759e94382 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 8 May 2014 01:37:53 +0200 Subject: [PATCH 01/10] [ticket/12174] Update topic_attachment flag when a post is soft-deleted https://tracker.phpbb.com/browse/PHPBB3-12174 PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 58 +++++++++++++++++++++++++----- phpBB/viewtopic.php | 15 ++++++-- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index 881a8f2c54..e19393d3c0 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -384,6 +384,7 @@ class content_visibility $update_topic_postcount = false; } + $topic_update_array = array(); // Update the topic's reply count and the forum's post count if ($update_topic_postcount) { @@ -421,20 +422,14 @@ class content_visibility if (sizeof($sql_ary)) { - $topic_sql = $forum_sql = array(); + $forum_sql = array(); foreach ($sql_ary as $field => $value_change) { - $topic_sql[] = 'topic_' . $field . ' = topic_' . $field . $value_change; + $topic_update_array[] = 'topic_' . $field . ' = topic_' . $field . $value_change; $forum_sql[] = 'forum_' . $field . ' = forum_' . $field . $value_change; } - // Update the number for replies and posts - $sql = 'UPDATE ' . $this->topics_table . ' - SET ' . implode(', ', $topic_sql) . ' - WHERE topic_id = ' . (int) $topic_id; - $this->db->sql_query($sql); - $sql = 'UPDATE ' . $this->forums_table . ' SET ' . implode(', ', $forum_sql) . ' WHERE forum_id = ' . (int) $forum_id; @@ -442,6 +437,53 @@ class content_visibility } } + $update_topic_attachments_flag = false; + if ($post_id) + { + if (is_array($post_id)) + { + $where_clause = $this->db->sql_in_set('post_id', array_map('intval', $post_id), true); + } + else + { + $where_clause = 'post_id <> ' . (int) $post_id; + } + + $sql = 'SELECT count(*) as nb_attachments + FROM ' . POSTS_TABLE . ' + WHERE topic_id = ' . (int) $topic_id . ' + AND post_attachment = 1 + AND post_visibility = ' . ITEM_APPROVED . ' + AND ' . $where_clause; + $result = $this->db->sql_query($sql); + + if ($row = $this->db->sql_fetchrow($result)) + { + if ($row['nb_attachments'] == 0) + { + $update_topic_attachments_flag = true; + $topic_update_array[] = 'topic_attachment = 0'; + } + else + { + if ($visibility == ITEM_APPROVED) + { + $update_topic_attachments_flag = true; + $topic_update_array[] = 'topic_attachment = 1'; + } + } + } + } + + if ($update_topic_postcount || $update_topic_attachments_flag) + { + // Update the number for replies and posts, and update the attachments flag + $sql = 'UPDATE ' . $this->topics_table . ' + SET ' . implode(', ', $topic_update_array) . ' + WHERE topic_id = ' . (int) $topic_id; + $this->db->sql_query($sql); + } + return $data; } diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php index 84040af2bb..a6c20ddc8a 100644 --- a/phpBB/viewtopic.php +++ b/phpBB/viewtopic.php @@ -930,7 +930,7 @@ else // Container for user details, only process once $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = $post_delete_list = array(); -$has_attachments = $display_notice = false; +$has_unapproved_attachments = $has_approved_attachments = $display_notice = false; $bbcode_bitfield = ''; $i = $i_total = 0; @@ -1041,7 +1041,11 @@ while ($row = $db->sql_fetchrow($result)) if ($row['post_visibility'] == ITEM_UNAPPROVED || $row['post_visibility'] == ITEM_REAPPROVE) { - $has_attachments = true; + $has_unapproved_attachments = true; + } + else if ($row['post_visibility'] == ITEM_APPROVED) + { + $has_approved_attachments = true; } } @@ -1345,7 +1349,7 @@ if (sizeof($attach_list)) $db->sql_query($sql); } } - else if ($has_attachments && !$topic_data['topic_attachment']) + else if ($has_approved_attachments && !$topic_data['topic_attachment']) { // Topic has approved attachments but its flag is wrong $sql = 'UPDATE ' . TOPICS_TABLE . " @@ -1355,6 +1359,11 @@ if (sizeof($attach_list)) $topic_data['topic_attachment'] = 1; } + else if ($has_unapproved_attachments && !$topic_data['topic_attachment']) + { + // Topic has only unapproved attachments but we have the right to see and download them + $topic_data['topic_attachment'] = 1; + } } else { From 112e55e8ab6909f5d6fee0b566aba834c9db0c51 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 8 May 2014 19:07:18 +0200 Subject: [PATCH 02/10] [ticket/12174] Corrections PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 31 ++++++++++-------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index e19393d3c0..ff28f2a7d6 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -426,7 +426,7 @@ class content_visibility foreach ($sql_ary as $field => $value_change) { - $topic_update_array[] = 'topic_' . $field . ' = topic_' . $field . $value_change; + $topic_update_array['topic_' . $field] = 'topic_' . $field . $value_change; $forum_sql[] = 'forum_' . $field . ' = forum_' . $field . $value_change; } @@ -440,46 +440,35 @@ class content_visibility $update_topic_attachments_flag = false; if ($post_id) { - if (is_array($post_id)) - { - $where_clause = $this->db->sql_in_set('post_id', array_map('intval', $post_id), true); - } - else - { - $where_clause = 'post_id <> ' . (int) $post_id; - } - - $sql = 'SELECT count(*) as nb_attachments + $sql = 'SELECT 1 as nb_attachments FROM ' . POSTS_TABLE . ' WHERE topic_id = ' . (int) $topic_id . ' AND post_attachment = 1 AND post_visibility = ' . ITEM_APPROVED . ' - AND ' . $where_clause; - $result = $this->db->sql_query($sql); + AND ' . $this->db->sql_in_set('post_id', $post_id, true); + $result = $this->db->sql_query_limit($sql, 1); if ($row = $this->db->sql_fetchrow($result)) { if ($row['nb_attachments'] == 0) { $update_topic_attachments_flag = true; - $topic_update_array[] = 'topic_attachment = 0'; + $topic_update_array['topic_attachment'] = 0; } - else + else if ($visibility == ITEM_APPROVED) { - if ($visibility == ITEM_APPROVED) - { - $update_topic_attachments_flag = true; - $topic_update_array[] = 'topic_attachment = 1'; - } + $update_topic_attachments_flag = true; + $topic_update_array['topic_attachment'] = 1; } } + $this->db->sql_freeresult($result); } if ($update_topic_postcount || $update_topic_attachments_flag) { // Update the number for replies and posts, and update the attachments flag $sql = 'UPDATE ' . $this->topics_table . ' - SET ' . implode(', ', $topic_update_array) . ' + SET ' . $this->db->sql_build_array('UPDATE', $topic_update_array) . ' WHERE topic_id = ' . (int) $topic_id; $this->db->sql_query($sql); } From 51c93aeb025a11930611a792b364502824314e03 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 8 May 2014 19:48:00 +0200 Subject: [PATCH 03/10] [ticket/12174] Revert the changes on $topic_update_array PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index ff28f2a7d6..420a43ec98 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -426,7 +426,7 @@ class content_visibility foreach ($sql_ary as $field => $value_change) { - $topic_update_array['topic_' . $field] = 'topic_' . $field . $value_change; + $topic_update_array[] = 'topic_' . $field . ' = topic_' . $field . $value_change; $forum_sql[] = 'forum_' . $field . ' = forum_' . $field . $value_change; } @@ -453,12 +453,12 @@ class content_visibility if ($row['nb_attachments'] == 0) { $update_topic_attachments_flag = true; - $topic_update_array['topic_attachment'] = 0; + $topic_update_array[] = 'topic_attachment = 0'; } else if ($visibility == ITEM_APPROVED) { $update_topic_attachments_flag = true; - $topic_update_array['topic_attachment'] = 1; + $topic_update_array[] = 'topic_attachment = 1'; } } $this->db->sql_freeresult($result); @@ -468,7 +468,7 @@ class content_visibility { // Update the number for replies and posts, and update the attachments flag $sql = 'UPDATE ' . $this->topics_table . ' - SET ' . $this->db->sql_build_array('UPDATE', $topic_update_array) . ' + SET ' . implode(', ', $topic_update_array) . ' WHERE topic_id = ' . (int) $topic_id; $this->db->sql_query($sql); } From f755f696069c48e40cba8163b04ea8537354283f Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 9 May 2014 00:18:38 +0200 Subject: [PATCH 04/10] [ticket/12174] Update sql query PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index 420a43ec98..7cb7bd6922 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -440,7 +440,7 @@ class content_visibility $update_topic_attachments_flag = false; if ($post_id) { - $sql = 'SELECT 1 as nb_attachments + $sql = 'SELECT 1 as has_attachments FROM ' . POSTS_TABLE . ' WHERE topic_id = ' . (int) $topic_id . ' AND post_attachment = 1 @@ -450,17 +450,18 @@ class content_visibility if ($row = $this->db->sql_fetchrow($result)) { - if ($row['nb_attachments'] == 0) - { - $update_topic_attachments_flag = true; - $topic_update_array[] = 'topic_attachment = 0'; - } - else if ($visibility == ITEM_APPROVED) + if ($visibility == ITEM_APPROVED) { $update_topic_attachments_flag = true; $topic_update_array[] = 'topic_attachment = 1'; } } + else + { + $update_topic_attachments_flag = true; + $topic_update_array[] = 'topic_attachment = 0'; + } + $this->db->sql_freeresult($result); } @@ -468,8 +469,8 @@ class content_visibility { // Update the number for replies and posts, and update the attachments flag $sql = 'UPDATE ' . $this->topics_table . ' - SET ' . implode(', ', $topic_update_array) . ' - WHERE topic_id = ' . (int) $topic_id; + SET ' . implode(', ', $topic_update_array) . ' + WHERE topic_id = ' . (int) $topic_id; $this->db->sql_query($sql); } From a80ad5d00f66d5195cdfe1c953079b54d27063dc Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 9 May 2014 00:31:53 +0200 Subject: [PATCH 05/10] [ticket/12174] Remove inline assignment PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index 7cb7bd6922..f532d7e61e 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -448,7 +448,8 @@ class content_visibility AND ' . $this->db->sql_in_set('post_id', $post_id, true); $result = $this->db->sql_query_limit($sql, 1); - if ($row = $this->db->sql_fetchrow($result)) + $row = $this->db->sql_fetchrow($result); + if ($row != false) { if ($visibility == ITEM_APPROVED) { From 712491697e79e5af80df6ad8a563ea3a1601a1a4 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 9 May 2014 00:46:56 +0200 Subject: [PATCH 06/10] [ticket/12174] Update the conditions PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index f532d7e61e..c587c2b07b 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -448,22 +448,19 @@ class content_visibility AND ' . $this->db->sql_in_set('post_id', $post_id, true); $result = $this->db->sql_query_limit($sql, 1); - $row = $this->db->sql_fetchrow($result); - if ($row != false) + $has_attachment = (bool) $this->db->sql_fetchfield('has_attachments'); + $this->db->sql_freeresult($result); + + if ($has_attachment && $visibility == ITEM_APPROVED) { - if ($visibility == ITEM_APPROVED) - { - $update_topic_attachments_flag = true; - $topic_update_array[] = 'topic_attachment = 1'; - } + $update_topic_attachments_flag = true; + $topic_update_array[] = 'topic_attachment = 1'; } - else + else if (!$has_attachment) { $update_topic_attachments_flag = true; $topic_update_array[] = 'topic_attachment = 0'; } - - $this->db->sql_freeresult($result); } if ($update_topic_postcount || $update_topic_attachments_flag) From 93f901d078a706368925e50b60d9b086d7ce01e3 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 9 May 2014 00:50:34 +0200 Subject: [PATCH 07/10] [ticket/12174] Don't update the flag for a post without attachment PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index c587c2b07b..42bf8cf44b 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -456,7 +456,7 @@ class content_visibility $update_topic_attachments_flag = true; $topic_update_array[] = 'topic_attachment = 1'; } - else if (!$has_attachment) + else if (!$has_attachment && $visibility != ITEM_APPROVED) { $update_topic_attachments_flag = true; $topic_update_array[] = 'topic_attachment = 0'; From 27cb84d3d483ecac19ba51be17d97306ae436ba8 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 9 May 2014 09:48:09 +0200 Subject: [PATCH 08/10] [ticket/12174] Remove $update_topic_attachments_flag PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index 42bf8cf44b..bced0a3247 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -437,7 +437,6 @@ class content_visibility } } - $update_topic_attachments_flag = false; if ($post_id) { $sql = 'SELECT 1 as has_attachments @@ -453,17 +452,15 @@ class content_visibility if ($has_attachment && $visibility == ITEM_APPROVED) { - $update_topic_attachments_flag = true; $topic_update_array[] = 'topic_attachment = 1'; } else if (!$has_attachment && $visibility != ITEM_APPROVED) { - $update_topic_attachments_flag = true; $topic_update_array[] = 'topic_attachment = 0'; } } - if ($update_topic_postcount || $update_topic_attachments_flag) + if (!empty($topic_update_array)) { // Update the number for replies and posts, and update the attachments flag $sql = 'UPDATE ' . $this->topics_table . ' From ca5987ceb863d557bd3509e164f9fe982aecb00d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 10 May 2014 23:50:42 +0200 Subject: [PATCH 09/10] [ticket/12174] Coding style PHPBB3-12174 --- phpBB/phpbb/content_visibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php index bced0a3247..c186e7f765 100644 --- a/phpBB/phpbb/content_visibility.php +++ b/phpBB/phpbb/content_visibility.php @@ -439,7 +439,7 @@ class content_visibility if ($post_id) { - $sql = 'SELECT 1 as has_attachments + $sql = 'SELECT 1 AS has_attachments FROM ' . POSTS_TABLE . ' WHERE topic_id = ' . (int) $topic_id . ' AND post_attachment = 1 From ad4a373557adc93c0490471b472312483ac0b1a0 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 28 May 2014 14:55:55 +0200 Subject: [PATCH 10/10] [ticket/12174] Add tests PHPBB3-12174 --- .../fixtures/set_post_visibility.xml | 84 +++++++++++++++++++ .../set_post_visibility_test.php | 41 +++++++++ 2 files changed, 125 insertions(+) diff --git a/tests/content_visibility/fixtures/set_post_visibility.xml b/tests/content_visibility/fixtures/set_post_visibility.xml index 5f792d0f05..1b8dac2670 100644 --- a/tests/content_visibility/fixtures/set_post_visibility.xml +++ b/tests/content_visibility/fixtures/set_post_visibility.xml @@ -10,6 +10,7 @@ topic_posts_approved topic_posts_softdeleted topic_posts_unapproved + topic_attachment 1 1 @@ -20,6 +21,7 @@ 1 1 1 + 0 @@ -32,6 +34,7 @@ 1 1 1 + 0 @@ -44,6 +47,33 @@ 1 0 0 + 0 + + + + 10 + 10 + 1 + Only 3 posts (2 with attachments) + 10 + 12 + 3 + 0 + 0 + 1 + + + + 11 + 10 + 1 + Only 2 posts (1 with attachments) + 13 + 14 + 3 + 0 + 0 + 1 @@ -53,6 +83,7 @@ forum_idpost_visibilitypost_text + post_attachment 1 1 @@ -60,6 +91,7 @@ 1 0 Unapproved + 0 2 @@ -68,6 +100,7 @@ 1 1 Approved + 0 3 @@ -76,6 +109,7 @@ 1 2 Softdeleted + 0 @@ -85,6 +119,7 @@ 1 0 Unapproved + 0 5 @@ -93,6 +128,7 @@ 1 1 Approved + 0 6 @@ -101,6 +137,7 @@ 1 1 Approved 2 + 0 7 @@ -109,6 +146,7 @@ 1 2 Softdeleted + 0 8 @@ -117,6 +155,52 @@ 1 1 Approved + 0 + + + 10 + 1 + 10 + 10 + 1 + Softdeleted + 1 + + + 11 + 1 + 10 + 10 + 1 + Softdeleted + 1 + + + 12 + 1 + 10 + 10 + 1 + Approved + 0 + + + 13 + 1 + 11 + 10 + 1 + Approved + 1 + + + 14 + 1 + 11 + 10 + 1 + Approved + 0
diff --git a/tests/content_visibility/set_post_visibility_test.php b/tests/content_visibility/set_post_visibility_test.php index f81b83ff86..be4571bdfe 100644 --- a/tests/content_visibility/set_post_visibility_test.php +++ b/tests/content_visibility/set_post_visibility_test.php @@ -140,4 +140,45 @@ class phpbb_content_visibility_set_post_visibility_test extends phpbb_database_t $this->assertEquals($expected_topic, $db->sql_fetchrowset($result)); $db->sql_freeresult($result); } + + public function set_post_soft_deleted_data() + { + return array( + array( + 10, 10, 10, + 1, time(), 'soft-deleted', + true, false, + array(array('topic_attachment' => 1)), + ), + array( + 13, 11, 10, + 1, time(), 'soft-deleted', + true, false, + array(array('topic_attachment' => 0)), + ), + ); + } + + /** + * @dataProvider set_post_soft_deleted_data + */ + public function test_set_post_soft_deleted($post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $expected) + { + global $cache, $db, $auth, $phpbb_root_path, $phpEx; + + $cache = new phpbb_mock_cache; + $db = $this->new_dbal(); + $auth = $this->getMock('\phpbb\auth\auth'); + $user = $this->getMock('\phpbb\user'); + $content_visibility = new \phpbb\content_visibility($auth, $db, $user, $phpbb_root_path, $phpEx, FORUMS_TABLE, POSTS_TABLE, TOPICS_TABLE, USERS_TABLE); + + $content_visibility->set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest); + + $result = $db->sql_query('SELECT topic_attachment + FROM phpbb_topics + WHERE topic_id = ' . $topic_id); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + $db->sql_freeresult($result); + } }