From 5ff35ccf72042cadc7f0bddc540eb26f1d99e11b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 28 Aug 2012 21:54:33 +0200 Subject: [feature/soft-delete] Use autoloading for content_visibility class PHPBB3-9657 --- phpBB/includes/content_visibility.php | 461 ++++++++++++++++++++++++++++++++++ 1 file changed, 461 insertions(+) create mode 100644 phpBB/includes/content_visibility.php (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php new file mode 100644 index 0000000000..12b51df3bb --- /dev/null +++ b/phpBB/includes/content_visibility.php @@ -0,0 +1,461 @@ +acl_get('m_approve', $forum_id)) + { + $status_ary[] = ITEM_UNAPPROVED; + } + + if ($auth->acl_get('m_restore', $forum_id)) + { + $status_ary[] = ITEM_DELETED; + } + + $clause = $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); + + // only allow the user to view deleted posts he himself made + if ($auth->acl_get('f_restore', $forum_id) && !$auth->acl_get('m_restore', $forum_id)) + { + $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; + $clause = '(' . $clause . " + OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . " + AND $table_alias$poster_column = " . $user->data['user_id'] . '))'; + + } + + return $clause; + } + + /** + * Fetch visibility SQL for all forums on the board. + * @param $mode string - either "topic" or "post" + * @param $exclude_forum_ids - int array - + * @param $table_alias string - Table alias to prefix in SQL queries + * @return string with the appropriate combination SQL logic for topic/post_visibility + */ + static public function get_visibility_sql_global($mode, $exclude_forum_ids = array(), $table_alias = '') + { + global $auth, $db, $user; + + // users can always see approved posts + $where_sql = "($table_alias{$mode}_visibility = " . ITEM_APPROVED; + + // in set notation: {approve_forums} = {m_approve} - {exclude_forums} + $approve_forums = array_diff(array_keys($auth->acl_getf('m_approve', true)), $exclude_forum_ids); + if (sizeof($approve_forums)) + { + // users can view unapproved topics in certain forums. specify them. + $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_UNAPPROVED . ' + AND ' . $db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ')'; + } + + // this is exactly the same logic as for approve forums, above + $restore_forums = array_diff(array_keys($auth->acl_getf('m_restore', true)), $exclude_forum_ids); + if (sizeof($restore_forums)) + { + $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . ' + AND ' . $db->sql_in_set($table_alias . 'forum_id', $restore_forums) . ')'; + } + + // we also allow the user to view deleted posts he himself made + $user_restore_forums = array_diff(array_keys($auth->acl_getf('f_restore', true)), $exclude_forum_ids); + if (sizeof($user_restore_forums) && !sizeof($restore_forums)) + { + $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; + + // specify the poster ID, the visibility type, and the forums we're interested in + $where_sql .= " OR ($table_alias$poster_column = " . $user->data['user_id'] . " + AND $table_alias{$mode}_visibility = " . ITEM_DELETED . " + AND " . $db->sql_in_set($table_alias . 'forum_id', $user_restore_forums) . ')'; + } + + $where_sql .= ')'; + + return $where_sql; + } + + /** + * Description: Allows approving (which is akin to undeleting), unapproving (!) or soft deleting an entire topic. + * Calls set_post_visibility as needed. + * @param $visibility - int - element of {ITEM_UNAPPROVED, ITEM_APPROVED, ITEM_DELETED} + * @param $topic_id - int - topic ID to act on + * @param $forum_id - int - forum ID where $topic_id resides + * @return bool true = success, false = fail + */ + static public function set_topic_visibility($visibility, $topic_id, $forum_id) + { + global $db; + + $sql = 'UPDATE ' . TOPICS_TABLE . ' SET topic_visibility = ' . (int) $visibility . ' + WHERE topic_id = ' . (int) $topic_id; + $db->sql_query($sql); + + // if we're approving, disapproving, or deleteing a topic, assume that + // we are adjusting _all_ posts in that topic. + $status = self::set_post_visibility($visibility, false, $topic_id, $forum_id, true, true); + + return $status; + } + + /** + * @param $visibility - int - element of {ITEM_UNAPPROVED, ITEM_APPROVED, ITEM_DELETED} + * @param $post_id - int - the post ID to act on + * @param $topic_id - int - forum where $post_id is found + * @param $forum_id - int - forum ID where $topic_id resides + * @param $is_starter - bool - is this the first post of the topic + * @param $is_latest - bool - is this the last post of the topic + */ + static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $is_starter, $is_latest) + { + global $db; + + // if we're changing the starter, we need to change the rest of the topic + if ($is_starter && !$is_latest) + { + return self::set_topic_visibility($visibility, $topic_id, $forum_id); + } + + if ($post_id) + { + $where_sql = 'post_id = ' . (int) $post_id; + } + else if ($topic_id) + { + $where_sql = 'topic_id = ' . (int) $topic_id; + } + else + { + // throw new MissingArgumentsException(); <-- a nice idea + return false; + } + + $sql = 'UPDATE ' . POSTS_TABLE . ' SET post_visibility = ' . (int) $visibility . ' + WHERE ' . $where_sql; + $db->sql_query($sql); + + // Sync the first/last topic information if needed + if ($is_starter || $is_latest) + { + update_post_information('topic', $topic_id, false); + update_post_information('forum', $forum_id, false); + } + } + + /** + * Can the current logged-in user soft-delete posts? + * @param $forum_id - int - the forum ID whose permissions to check + * @param $poster_id - int - the poster ID of the post in question + * @param $post_locked - bool - is the post locked? + * @return bool + */ + static function can_soft_delete($forum_id, $poster_id, $post_locked) + { + global $auth, $user; + + if ($auth->acl_get('m_softdelete', $forum_id)) + { + return true; + } + else if ($auth->acl_get('f_softdelete', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked) + { + return true; + } + return false; + } + + /** + * Can the current logged-in user restore soft-deleted posts? + * @param $forum_id - int - the forum ID whose permissions to check + * @param $poster_id - int - the poster ID of the post in question + * @param $post_locked - bool - is the post locked? + * @return bool + */ + public function can_restore($forum_id, $poster_id, $post_locked) + { + global $auth, $user; + + if ($auth->acl_get('m_restore', $forum_id)) + { + return true; + } + else if ($auth->acl_get('f_restore', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked) + { + return true; + } + return false; + } + + /** + * Do the required math to hide a complete topic (going from approved to + * unapproved or from approved to deleted) + * @param $topic_id - int - the topic to act on + * @param $forum_id - int - the forum where the topic resides + * @param $topic_row - array - data about the topic, may be empty at call time + * @param $sql_data - array - populated with the SQL changes, may be empty at call time + * @return void + */ + static public function hide_topic($topic_id, $forum_id, &$topic_row, &$sql_data) + { + global $auth, $config, $db; + + // Do we need to grab some topic informations? + if (!sizeof($topic_row)) + { + $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_visibility + FROM ' . TOPICS_TABLE . ' + WHERE topic_id = ' . $topic_id; + $result = $db->sql_query($sql); + $topic_row = $db->sql_fetchrow($result); + $db->sql_freeresult($result); + } + + // If this is the only post remaining we do not need to decrement topic_replies. + // Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again. + + // If this is an edited topic or the first post the topic gets completely disapproved later on... + $sql_data[FORUMS_TABLE] = 'forum_topics = forum_topics - 1'; + $sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1); + + set_config_count('num_topics', -1, true); + set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true); + + // Only decrement this post, since this is the one non-approved now + if ($auth->acl_get('f_postcount', $forum_id)) + { + $sql_data[USERS_TABLE] = 'user_posts = user_posts - 1'; + } + } + + /** + * Do the required math to hide a single post (going from approved to + * unapproved or from approved to deleted) + * Notably, we do _not_ need the post ID to do this operation. We're only changing statistic caches + * @param $forum_id - int - the forum where the topic resides + * @param $current_time - int - passed for consistency instead of calling time() internally + * @param $topic_row - array - contains information from the topics table about given topic + * @param $sql_data - array - populated with the SQL changes, may be empty at call time + * @return void + */ + static public function hide_post($forum_id, $current_time, $topic_row, &$sql_data) + { + global $auth, $config, $db; + + // initialize the array if needed (php throws E_NOTICE when .= is used + // on a non-existing array element) + if (empty($sql_data[TOPICS_TABLE])) + { + $sql_data[TOPICS_TABLE] = ''; + } + + if ($topic_row['topic_replies'] > 0) + { + $sql_data[TOPICS_TABLE] = 'topic_replies = topic_replies - 1,'; + } + $sql_data[TOPICS_TABLE] .= ' topic_last_view_time = ' . $current_time; + + $sql_data[FORUMS_TABLE] = 'forum_posts = forum_posts - 1'; + + set_config_count('num_posts', -1, true); + + if ($auth->acl_get('f_postcount', $forum_id)) + { + $sql_data[USERS_TABLE] = 'user_posts = user_posts - 1'; + } + } + + /** + * One function to rule them all ... and unhide posts and topics. This could + * reasonably be broken up, I straight copied this code from the mcp_queue.php + * file here for global access. + * @param $mode - string - member of the set {'approve', 'restore'} + * @param $post_info - array - Contains info from post U topics table about + * the posts/topics in question + * @param $post_id_list - array of ints - the set of posts being worked on + */ + static public function unhide_posts_topics($mode, $post_info, $post_id_list) + { + global $db, $config; + + // If Topic -> total_topics = total_topics+1, total_posts = total_posts+1, forum_topics = forum_topics+1, forum_posts = forum_posts+1 + // If Post -> total_posts = total_posts+1, forum_posts = forum_posts+1, topic_replies = topic_replies+1 + + $total_topics = $total_posts = 0; + $topic_approve_sql = $post_approve_sql = $topic_id_list = $forum_id_list = $approve_log = array(); + $user_posts_sql = $post_approved_list = array(); + + foreach ($post_info as $post_id => $post_data) + { + if ($post_data['post_visibility'] == ITEM_APPROVED) + { + $post_approved_list[] = $post_id; + continue; + } + + $topic_id_list[$post_data['topic_id']] = 1; + + if ($post_data['forum_id']) + { + $forum_id_list[$post_data['forum_id']] = 1; + } + + // User post update (we do not care about topic or post, since user posts are strictly connected to posts) + // But we care about forums where post counts get not increased. ;) + if ($post_data['post_postcount']) + { + $user_posts_sql[$post_data['poster_id']] = (empty($user_posts_sql[$post_data['poster_id']])) ? 1 : $user_posts_sql[$post_data['poster_id']] + 1; + } + + // Topic or Post. ;) + if ($post_data['topic_first_post_id'] == $post_id) + { + if ($post_data['forum_id']) + { + $total_topics++; + } + $topic_approve_sql[] = $post_data['topic_id']; + + $approve_log[] = array( + 'type' => 'topic', + 'post_subject' => $post_data['post_subject'], + 'forum_id' => $post_data['forum_id'], + 'topic_id' => $post_data['topic_id'], + ); + } + else + { + $approve_log[] = array( + 'type' => 'post', + 'post_subject' => $post_data['post_subject'], + 'forum_id' => $post_data['forum_id'], + 'topic_id' => $post_data['topic_id'], + ); + } + + if ($post_data['forum_id']) + { + $total_posts++; + + // Increment by topic_replies if we approve a topic... + // This works because we do not adjust the topic_replies when re-approving a topic after an edit. + if ($post_data['topic_first_post_id'] == $post_id && $post_data['topic_replies']) + { + $total_posts += $post_data['topic_replies']; + } + } + + $post_approve_sql[] = $post_id; + } + + $post_id_list = array_values(array_diff($post_id_list, $post_approved_list)); + for ($i = 0, $size = sizeof($post_approved_list); $i < $size; $i++) + { + unset($post_info[$post_approved_list[$i]]); + } + + if (sizeof($topic_approve_sql)) + { + $sql = 'UPDATE ' . TOPICS_TABLE . ' + SET topic_visibility = ' . ITEM_APPROVED . ' + WHERE ' . $db->sql_in_set('topic_id', $topic_approve_sql); + $db->sql_query($sql); + } + + if (sizeof($post_approve_sql)) + { + $sql = 'UPDATE ' . POSTS_TABLE . ' + SET post_visibility = ' . ITEM_APPROVED . ' + WHERE ' . $db->sql_in_set('post_id', $post_approve_sql); + $db->sql_query($sql); + } + + unset($topic_approve_sql, $post_approve_sql); + + foreach ($approve_log as $log_data) + { + add_log('mod', $log_data['forum_id'], $log_data['topic_id'], ($log_data['type'] == 'topic') ? 'LOG_TOPIC_' . strtoupper($mode) . 'D' : 'LOG_POST_' . strtoupper($mode) . 'D', $log_data['post_subject']); + } + + if (sizeof($user_posts_sql)) + { + // Try to minimize the query count by merging users with the same post count additions + $user_posts_update = array(); + + foreach ($user_posts_sql as $user_id => $user_posts) + { + $user_posts_update[$user_posts][] = $user_id; + } + + foreach ($user_posts_update as $user_posts => $user_id_ary) + { + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_posts = user_posts + ' . $user_posts . ' + WHERE ' . $db->sql_in_set('user_id', $user_id_ary); + $db->sql_query($sql); + } + } + + if ($total_topics) + { + set_config_count('num_topics', $total_topics, true); + } + + if ($total_posts) + { + set_config_count('num_posts', $total_posts, true); + } + + if (!function_exists('sync')) + { + global $phpbb_root_path, $phpEx; + include ($phpbb_root_path . 'includes/functions_admin.'.$phpEx); + } + + sync('topic', 'topic_id', array_keys($topic_id_list), true); + sync('forum', 'forum_id', array_keys($forum_id_list), true, true); + unset($topic_id_list, $forum_id_list); + + if ($total_topics) + { + $success_msg = ($total_topics == 1) ? 'TOPIC_APPROVED_SUCCESS' : 'TOPICS_APPROVED_SUCCESS'; + } + else + { + $success_msg = (sizeof($post_id_list) + sizeof($post_approved_list) == 1) ? 'POST_APPROVED_SUCCESS' : 'POSTS_APPROVED_SUCCESS'; + } + + return $success_msg; + } +} -- cgit v1.2.1 From 44ed05f5678b36d907555ed040229564b336632f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 29 Aug 2012 19:30:33 +0200 Subject: [feature/soft-delete] Simplify the query output if the user has m_restore PHPBB3-9657 --- phpBB/includes/content_visibility.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 12b51df3bb..5c3e9d39dd 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -42,6 +42,11 @@ class phpbb_content_visibility if ($auth->acl_get('m_restore', $forum_id)) { $status_ary[] = ITEM_DELETED; + + // If the user has m_restore, the rest of the function will not + // make more content visible, so we can return the query here. + // This avoids one OR in all queries + return $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); } $clause = $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); @@ -52,8 +57,7 @@ class phpbb_content_visibility $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; $clause = '(' . $clause . " OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . " - AND $table_alias$poster_column = " . $user->data['user_id'] . '))'; - + AND $table_alias$poster_column = " . (int) $user->data['user_id'] . '))'; } return $clause; -- cgit v1.2.1 From a1e0690b6b25ffd64f6ae2fc2f7b17a04e931690 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 29 Aug 2012 22:12:33 +0200 Subject: [feature/soft-delete] Simplification part2: user can see all item visibilities If the user can see all visibilities, we can simply leave out the query part, instead of adding a bunch of ANDs. PHPBB3-9657 --- phpBB/includes/content_visibility.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 5c3e9d39dd..287c46a335 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -43,9 +43,15 @@ class phpbb_content_visibility { $status_ary[] = ITEM_DELETED; + if (sizeof($status_ary) == 3) + { + // The user can see all types, so we simplify this to an empty string, + // as we don't need to restrict anything on the query. + return ''; + } + // If the user has m_restore, the rest of the function will not // make more content visible, so we can return the query here. - // This avoids one OR in all queries return $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); } -- cgit v1.2.1 From 1c043254c00023a5fe17b1131fa605ca11d823a0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 30 Aug 2012 18:07:00 +0200 Subject: [feature/soft-delete] Add get_visibility_sql_forums based on global The resulting query is 4-times faster, as the forum_id IN () arrays are smaller and we need less AND/OR to build the hole query. The main difference between those two functions is, that this one takes an array of included ids and the _global one takes an array of excluded ids. PHPBB3-9657 --- phpBB/includes/content_visibility.php | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 287c46a335..2e91521c78 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -69,6 +69,54 @@ class phpbb_content_visibility return $clause; } + /** + * Fetch visibility SQL for a set of forums + * @param $mode string - either "topic" or "post" + * @param $forum_ids - int array - + * @param $table_alias string - Table alias to prefix in SQL queries + * @return string with the appropriate combination SQL logic for topic/post_visibility + */ + static public function get_visibility_sql_forums($mode, $forum_ids = array(), $table_alias = '') + { + global $auth, $db, $user; + + // users can always see approved posts + $where_sql = "($table_alias{$mode}_visibility = " . ITEM_APPROVED; + + // in set notation: {approve_forums} = {m_approve} - {exclude_forums} + $approve_forums = array_intersect($forum_ids, array_keys($auth->acl_getf('m_approve', true))); + if (sizeof($approve_forums)) + { + // users can view unapproved topics in certain forums. specify them. + $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_UNAPPROVED . ' + AND ' . $db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ')'; + } + + // this is exactly the same logic as for approve forums, above + $restore_forums = array_intersect($forum_ids, array_keys($auth->acl_getf('m_restore', true))); + if (sizeof($restore_forums)) + { + $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . ' + AND ' . $db->sql_in_set($table_alias . 'forum_id', $restore_forums) . ')'; + } + + // we also allow the user to view deleted posts he himself made + $user_restore_forums = array_diff(array_intersect($forum_ids, array_keys($auth->acl_getf('f_restore', true))), $restore_forums); + if (sizeof($user_restore_forums) && !sizeof($restore_forums)) + { + $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; + + // specify the poster ID, the visibility type, and the forums we're interested in + $where_sql .= " OR ($table_alias$poster_column = " . $user->data['user_id'] . " + AND $table_alias{$mode}_visibility = " . ITEM_DELETED . " + AND " . $db->sql_in_set($table_alias . 'forum_id', $user_restore_forums) . ')'; + } + + $where_sql .= ')'; + + return $where_sql; + } + /** * Fetch visibility SQL for all forums on the board. * @param $mode string - either "topic" or "post" -- cgit v1.2.1 From 8b2181eb851081a103802764517f7189ba8fc114 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 28 Sep 2012 16:14:44 +0200 Subject: [feature/soft-delete] Comment out stuff about f_restore for performance reason PHPBB3-9657 --- phpBB/includes/content_visibility.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 2e91521c78..55adf00e8c 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -47,9 +47,15 @@ class phpbb_content_visibility { // The user can see all types, so we simplify this to an empty string, // as we don't need to restrict anything on the query. - return ''; + return '1 = 1'; } + } + + return $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); + /** + * @todo: Commented out, because the performance is not the best + * // If the user has m_restore, the rest of the function will not // make more content visible, so we can return the query here. return $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); @@ -64,7 +70,7 @@ class phpbb_content_visibility $clause = '(' . $clause . " OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . " AND $table_alias$poster_column = " . (int) $user->data['user_id'] . '))'; - } + }*/ return $clause; } @@ -100,6 +106,9 @@ class phpbb_content_visibility AND ' . $db->sql_in_set($table_alias . 'forum_id', $restore_forums) . ')'; } + /* + * @todo: Commented out, because the performance is not the best + * // we also allow the user to view deleted posts he himself made $user_restore_forums = array_diff(array_intersect($forum_ids, array_keys($auth->acl_getf('f_restore', true))), $restore_forums); if (sizeof($user_restore_forums) && !sizeof($restore_forums)) @@ -111,6 +120,7 @@ class phpbb_content_visibility AND $table_alias{$mode}_visibility = " . ITEM_DELETED . " AND " . $db->sql_in_set($table_alias . 'forum_id', $user_restore_forums) . ')'; } + */ $where_sql .= ')'; @@ -148,6 +158,9 @@ class phpbb_content_visibility AND ' . $db->sql_in_set($table_alias . 'forum_id', $restore_forums) . ')'; } + /* + * @todo: Commented out, because the performance is not the best + * // we also allow the user to view deleted posts he himself made $user_restore_forums = array_diff(array_keys($auth->acl_getf('f_restore', true)), $exclude_forum_ids); if (sizeof($user_restore_forums) && !sizeof($restore_forums)) @@ -159,6 +172,7 @@ class phpbb_content_visibility AND $table_alias{$mode}_visibility = " . ITEM_DELETED . " AND " . $db->sql_in_set($table_alias . 'forum_id', $user_restore_forums) . ')'; } + */ $where_sql .= ')'; -- cgit v1.2.1 From e5377e98c7eca8754f423ef495e4b092590f4e03 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 28 Sep 2012 16:53:38 +0200 Subject: [feature/soft-delete] Allow setting the visibility change reason PHPBB3-9657 --- phpBB/includes/content_visibility.php | 49 +++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 55adf00e8c..165c28f1af 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -185,21 +185,28 @@ class phpbb_content_visibility * @param $visibility - int - element of {ITEM_UNAPPROVED, ITEM_APPROVED, ITEM_DELETED} * @param $topic_id - int - topic ID to act on * @param $forum_id - int - forum ID where $topic_id resides - * @return bool true = success, false = fail + * @return void */ - static public function set_topic_visibility($visibility, $topic_id, $forum_id) + static public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason) { global $db; - $sql = 'UPDATE ' . TOPICS_TABLE . ' SET topic_visibility = ' . (int) $visibility . ' + $data = array( + 'topic_visibility' => (int) $visibility, + 'topic_delete_user' => (int) $user_id, + 'topic_delete_time' => ((int) $time) ?: time(), + 'topic_delete_reason' => truncate_string($reason, 255, 255, false), + ); + + $sql = 'UPDATE ' . TOPICS_TABLE . ' + SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE topic_id = ' . (int) $topic_id; $db->sql_query($sql); - // if we're approving, disapproving, or deleteing a topic, assume that - // we are adjusting _all_ posts in that topic. - $status = self::set_post_visibility($visibility, false, $topic_id, $forum_id, true, true); - - return $status; + // If we're approving, disapproving, or deleteing a topic + // we also update all posts in that topic that need to be changed. + // However, we do not set the same reason for every post. + self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true); } /** @@ -209,8 +216,9 @@ class phpbb_content_visibility * @param $forum_id - int - forum ID where $topic_id resides * @param $is_starter - bool - is this the first post of the topic * @param $is_latest - bool - is this the last post of the topic + * @return void */ - static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $is_starter, $is_latest) + static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest) { global $db; @@ -230,19 +238,32 @@ class phpbb_content_visibility } else { - // throw new MissingArgumentsException(); <-- a nice idea - return false; + return; } - $sql = 'UPDATE ' . POSTS_TABLE . ' SET post_visibility = ' . (int) $visibility . ' + $data = array( + 'post_visibility' => (int) $visibility, + 'post_delete_user' => (int) $user_id, + 'post_delete_time' => ((int) $time) ?: time(), + 'post_delete_reason' => truncate_string($reason, 255, 255, false), + ); + + $sql = 'UPDATE ' . POSTS_TABLE . ' + SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE ' . $where_sql; $db->sql_query($sql); // Sync the first/last topic information if needed if ($is_starter || $is_latest) { - update_post_information('topic', $topic_id, false); - update_post_information('forum', $forum_id, false); + if ($topic_id) + { + update_post_information('topic', $topic_id, false); + } + if ($forum_id) + { + update_post_information('forum', $forum_id, false); + } } } -- cgit v1.2.1 From 1943de36f32609b37230da32aa679466043702fe Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 1 Oct 2012 16:25:05 +0200 Subject: [feature/soft-delete] Comment out user_posts update for the moment It should rely on the permissions of the post not the current user. PHPBB3-9657 --- phpBB/includes/content_visibility.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 165c28f1af..9d2bf34370 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -346,10 +346,16 @@ class phpbb_content_visibility set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true); // Only decrement this post, since this is the one non-approved now + // + /** + * @todo: this is wrong, it should rely on post_postcount + * also a user might have more than one post in the topic + * if ($auth->acl_get('f_postcount', $forum_id)) { $sql_data[USERS_TABLE] = 'user_posts = user_posts - 1'; } + */ } /** @@ -383,10 +389,14 @@ class phpbb_content_visibility set_config_count('num_posts', -1, true); + /** + * @todo: this is wrong, it should rely on post_postcount + * if ($auth->acl_get('f_postcount', $forum_id)) { $sql_data[USERS_TABLE] = 'user_posts = user_posts - 1'; } + */ } /** -- cgit v1.2.1 From 4a65940e6206aef6b85a0aacfb5324ecabf76e12 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 1 Oct 2012 22:14:04 +0200 Subject: [feature/soft-delete] Add unit tests for get_forums_visibility_sql() PHPBB3-9657 --- phpBB/includes/content_visibility.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 9d2bf34370..a598d863a4 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -82,12 +82,13 @@ class phpbb_content_visibility * @param $table_alias string - Table alias to prefix in SQL queries * @return string with the appropriate combination SQL logic for topic/post_visibility */ - static public function get_visibility_sql_forums($mode, $forum_ids = array(), $table_alias = '') + static public function get_forums_visibility_sql($mode, $forum_ids = array(), $table_alias = '') { global $auth, $db, $user; // users can always see approved posts - $where_sql = "($table_alias{$mode}_visibility = " . ITEM_APPROVED; + $where_sql = "(($table_alias{$mode}_visibility = " . ITEM_APPROVED . ' + AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids) . ')'; // in set notation: {approve_forums} = {m_approve} - {exclude_forums} $approve_forums = array_intersect($forum_ids, array_keys($auth->acl_getf('m_approve', true))); -- cgit v1.2.1 From b629b2cd95a9da67376f7a628c15199007e2ebd6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 1 Oct 2012 22:44:39 +0200 Subject: [feature/soft-delete] Add unit tests for get_global_visibility_sql() PHPBB3-9657 --- phpBB/includes/content_visibility.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index a598d863a4..e927460a49 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -135,12 +135,20 @@ class phpbb_content_visibility * @param $table_alias string - Table alias to prefix in SQL queries * @return string with the appropriate combination SQL logic for topic/post_visibility */ - static public function get_visibility_sql_global($mode, $exclude_forum_ids = array(), $table_alias = '') + static public function get_global_visibility_sql($mode, $exclude_forum_ids = array(), $table_alias = '') { global $auth, $db, $user; // users can always see approved posts - $where_sql = "($table_alias{$mode}_visibility = " . ITEM_APPROVED; + if (sizeof($exclude_forum_ids)) + { + $where_sql = '((' . $db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true, true) . " + AND $table_alias{$mode}_visibility = " . ITEM_APPROVED . ')'; + } + else + { + $where_sql = "($table_alias{$mode}_visibility = " . ITEM_APPROVED; + } // in set notation: {approve_forums} = {m_approve} - {exclude_forums} $approve_forums = array_diff(array_keys($auth->acl_getf('m_approve', true)), $exclude_forum_ids); -- cgit v1.2.1 From a84e4029e4ecde691a0d34ef72ddab4fc56a07ee Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 2 Oct 2012 12:46:44 +0200 Subject: [feature/soft-delete] Update doc blocks to proper format PHPBB3-9657 --- phpBB/includes/content_visibility.php | 39 ++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index e927460a49..1aa2ad1a29 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -24,10 +24,13 @@ class phpbb_content_visibility { /** * Create topic/post visibility SQL for a given forum ID - * @param $mode string - either "topic" or "post" - * @param $forum_id int - current forum ID - * @param $table_alias string - Table alias to prefix in SQL queries - * @return string with the appropriate combination SQL logic for topic/post_visibility + * + * Note: Read permissions are not checked. + * + * @param $mode string Either "topic" or "post" + * @param $forum_id int The forum id is used for permission checks + * @param $table_alias string Table alias to prefix in SQL queries + * @return string The appropriate combination SQL logic for topic/post_visibility */ static public function get_visibility_sql($mode, $forum_id, $table_alias = '') { @@ -76,11 +79,15 @@ class phpbb_content_visibility } /** - * Fetch visibility SQL for a set of forums - * @param $mode string - either "topic" or "post" - * @param $forum_ids - int array - - * @param $table_alias string - Table alias to prefix in SQL queries - * @return string with the appropriate combination SQL logic for topic/post_visibility + * Create topic/post visibility SQL for a set of forums + * + * Note: Read permissions are not checked. Forums without read permissions + * should not be in $forum_ids + * + * @param $mode string Either "topic" or "post" + * @param $forum_ids array Array of forum ids which the posts/topics are limited to + * @param $table_alias string Table alias to prefix in SQL queries + * @return string The appropriate combination SQL logic for topic/post_visibility */ static public function get_forums_visibility_sql($mode, $forum_ids = array(), $table_alias = '') { @@ -129,11 +136,15 @@ class phpbb_content_visibility } /** - * Fetch visibility SQL for all forums on the board. - * @param $mode string - either "topic" or "post" - * @param $exclude_forum_ids - int array - - * @param $table_alias string - Table alias to prefix in SQL queries - * @return string with the appropriate combination SQL logic for topic/post_visibility + * Create topic/post visibility SQL for all forums on the board + * + * Note: Read permissions are not checked. Forums without read permissions + * should be in $exclude_forum_ids + * + * @param $mode string Either "topic" or "post" + * @param $exclude_forum_ids array Array of forum ids which are excluded + * @param $table_alias string Table alias to prefix in SQL queries + * @return string The appropriate combination SQL logic for topic/post_visibility */ static public function get_global_visibility_sql($mode, $exclude_forum_ids = array(), $table_alias = '') { -- cgit v1.2.1 From 5b64ebc11d10b66212661282a4dcfd411d038211 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 2 Oct 2012 15:34:18 +0200 Subject: [feature/soft-delete] Fix a bug in sync() and set_post_visibility() PHPBB3-9657 --- phpBB/includes/content_visibility.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 1aa2ad1a29..8bc67a7fd2 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -242,12 +242,6 @@ class phpbb_content_visibility { global $db; - // if we're changing the starter, we need to change the rest of the topic - if ($is_starter && !$is_latest) - { - return self::set_topic_visibility($visibility, $topic_id, $forum_id); - } - if ($post_id) { $where_sql = 'post_id = ' . (int) $post_id; @@ -274,8 +268,9 @@ class phpbb_content_visibility $db->sql_query($sql); // Sync the first/last topic information if needed - if ($is_starter || $is_latest) + if (!$is_starter && $is_latest) { + // update_post_information can only update the last post info ... if ($topic_id) { update_post_information('topic', $topic_id, false); @@ -285,6 +280,12 @@ class phpbb_content_visibility update_post_information('forum', $forum_id, false); } } + else if (($is_starter || $is_latest) && $topic_id) + { + // ... so we need to use sync, if the first post is changed. + // The forum is resynced recursive by sync() itself. + sync('topic', 'topic_id', $topic_id, true); + } } /** -- cgit v1.2.1 From 2a81e4b48ee223d8538e960b6e8e6e1c3e1277b2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 12:06:36 +0200 Subject: [feature/soft-delete] Fix the get functions to match the new logic PHPBB3-9567 --- phpBB/includes/content_visibility.php | 138 +++++++++------------------------- 1 file changed, 35 insertions(+), 103 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 8bc67a7fd2..112780224f 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -34,48 +34,14 @@ class phpbb_content_visibility */ static public function get_visibility_sql($mode, $forum_id, $table_alias = '') { - global $auth, $db, $user; + global $auth; - $status_ary = array(ITEM_APPROVED); if ($auth->acl_get('m_approve', $forum_id)) { - $status_ary[] = ITEM_UNAPPROVED; + return '1 = 1'; } - if ($auth->acl_get('m_restore', $forum_id)) - { - $status_ary[] = ITEM_DELETED; - - if (sizeof($status_ary) == 3) - { - // The user can see all types, so we simplify this to an empty string, - // as we don't need to restrict anything on the query. - return '1 = 1'; - } - } - - return $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); - - /** - * @todo: Commented out, because the performance is not the best - * - // If the user has m_restore, the rest of the function will not - // make more content visible, so we can return the query here. - return $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); - } - - $clause = $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); - - // only allow the user to view deleted posts he himself made - if ($auth->acl_get('f_restore', $forum_id) && !$auth->acl_get('m_restore', $forum_id)) - { - $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; - $clause = '(' . $clause . " - OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . " - AND $table_alias$poster_column = " . (int) $user->data['user_id'] . '))'; - }*/ - - return $clause; + return $table_alias . $mode . '_visibility = ' . ITEM_APPROVED; } /** @@ -91,46 +57,37 @@ class phpbb_content_visibility */ static public function get_forums_visibility_sql($mode, $forum_ids = array(), $table_alias = '') { - global $auth, $db, $user; + global $auth, $db; - // users can always see approved posts - $where_sql = "(($table_alias{$mode}_visibility = " . ITEM_APPROVED . ' - AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids) . ')'; + $where_sql = '('; - // in set notation: {approve_forums} = {m_approve} - {exclude_forums} $approve_forums = array_intersect($forum_ids, array_keys($auth->acl_getf('m_approve', true))); + if (sizeof($approve_forums)) { - // users can view unapproved topics in certain forums. specify them. - $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_UNAPPROVED . ' - AND ' . $db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ')'; - } + // Remove moderator forums from the rest + $forum_ids = array_diff($forum_ids, $approve_forums); - // this is exactly the same logic as for approve forums, above - $restore_forums = array_intersect($forum_ids, array_keys($auth->acl_getf('m_restore', true))); - if (sizeof($restore_forums)) - { - $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . ' - AND ' . $db->sql_in_set($table_alias . 'forum_id', $restore_forums) . ')'; + if (!sizeof($forum_ids)) + { + // The user can see all posts/topics in all specified forums + return $db->sql_in_set($table_alias . 'forum_id', $approve_forums); + } + else + { + // Moderator can view all posts/topics in some forums + $where_sql .= $db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ' OR '; + } } - - /* - * @todo: Commented out, because the performance is not the best - * - // we also allow the user to view deleted posts he himself made - $user_restore_forums = array_diff(array_intersect($forum_ids, array_keys($auth->acl_getf('f_restore', true))), $restore_forums); - if (sizeof($user_restore_forums) && !sizeof($restore_forums)) + else { - $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; - - // specify the poster ID, the visibility type, and the forums we're interested in - $where_sql .= " OR ($table_alias$poster_column = " . $user->data['user_id'] . " - AND $table_alias{$mode}_visibility = " . ITEM_DELETED . " - AND " . $db->sql_in_set($table_alias . 'forum_id', $user_restore_forums) . ')'; + // The user is just a normal user + return "$table_alias{$mode}_visibility = " . ITEM_APPROVED . ' + AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids, false, true); } - */ - $where_sql .= ')'; + $where_sql .= "($table_alias{$mode}_visibility = " . ITEM_APPROVED . ' + AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids) . '))'; return $where_sql; } @@ -148,55 +105,30 @@ class phpbb_content_visibility */ static public function get_global_visibility_sql($mode, $exclude_forum_ids = array(), $table_alias = '') { - global $auth, $db, $user; + global $auth, $db; + + $where_sqls = array(); + + $approve_forums = array_diff(array_keys($auth->acl_getf('m_approve', true)), $exclude_forum_ids); - // users can always see approved posts if (sizeof($exclude_forum_ids)) { - $where_sql = '((' . $db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true, true) . " + $where_sqls[] = '(' . $db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true) . " AND $table_alias{$mode}_visibility = " . ITEM_APPROVED . ')'; } else { - $where_sql = "($table_alias{$mode}_visibility = " . ITEM_APPROVED; + $where_sqls[] = "$table_alias{$mode}_visibility = " . ITEM_APPROVED; } - // in set notation: {approve_forums} = {m_approve} - {exclude_forums} - $approve_forums = array_diff(array_keys($auth->acl_getf('m_approve', true)), $exclude_forum_ids); if (sizeof($approve_forums)) { - // users can view unapproved topics in certain forums. specify them. - $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_UNAPPROVED . ' - AND ' . $db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ')'; + $where_sqls[] = $db->sql_in_set($table_alias . 'forum_id', $approve_forums); + return '(' . implode(' OR ', $where_sqls) . ')'; } - // this is exactly the same logic as for approve forums, above - $restore_forums = array_diff(array_keys($auth->acl_getf('m_restore', true)), $exclude_forum_ids); - if (sizeof($restore_forums)) - { - $where_sql .= " OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . ' - AND ' . $db->sql_in_set($table_alias . 'forum_id', $restore_forums) . ')'; - } - - /* - * @todo: Commented out, because the performance is not the best - * - // we also allow the user to view deleted posts he himself made - $user_restore_forums = array_diff(array_keys($auth->acl_getf('f_restore', true)), $exclude_forum_ids); - if (sizeof($user_restore_forums) && !sizeof($restore_forums)) - { - $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; - - // specify the poster ID, the visibility type, and the forums we're interested in - $where_sql .= " OR ($table_alias$poster_column = " . $user->data['user_id'] . " - AND $table_alias{$mode}_visibility = " . ITEM_DELETED . " - AND " . $db->sql_in_set($table_alias . 'forum_id', $user_restore_forums) . ')'; - } - */ - - $where_sql .= ')'; - - return $where_sql; + // There is only one element, so we just return that one + return $where_sqls[0]; } /** -- cgit v1.2.1 From 63d11c976b0bcef68ce4809c8c76124451df88ea Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 12:37:01 +0200 Subject: [feature/soft-delete] Fix sync('topic') to match the new logic This also fixes set_post_visibility() PHPBB3-9567 --- phpBB/includes/content_visibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 112780224f..fbcdf27f08 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -212,7 +212,7 @@ class phpbb_content_visibility update_post_information('forum', $forum_id, false); } } - else if (($is_starter || $is_latest) && $topic_id) + else if ($is_starter && $topic_id) { // ... so we need to use sync, if the first post is changed. // The forum is resynced recursive by sync() itself. -- cgit v1.2.1 From bfa6a50a4ff2caf1589a1039c1037d4153223d63 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 13:12:36 +0200 Subject: [feature/soft-delete] Extend functionality for updating a hole topic Limit the posts to a certain visibility and deletion time This allows us to only restore posts, that were approved when the topic got soft deleted. So previous soft deleted and unapproved posts are still soft deleted/unapproved PHPBB3-9567 --- phpBB/includes/content_visibility.php | 37 ++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index fbcdf27f08..be08a5f536 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -162,18 +162,28 @@ class phpbb_content_visibility } /** - * @param $visibility - int - element of {ITEM_UNAPPROVED, ITEM_APPROVED, ITEM_DELETED} - * @param $post_id - int - the post ID to act on - * @param $topic_id - int - forum where $post_id is found - * @param $forum_id - int - forum ID where $topic_id resides - * @param $is_starter - bool - is this the first post of the topic - * @param $is_latest - bool - is this the last post of the topic + * Change visibility status of one post or a hole topic + * + * @param $visibility int Element of {ITEM_APPROVED, ITEM_DELETED} + * @param $post_id mixed Post ID to act on, if it is empty, + * all posts of topic_id will be modified + * @param $topic_id int Topic where $post_id is found + * @param $forum_id int Forum where $topic_id is found + * @param $is_starter bool Is this the first post of the topic changed? + * @param $is_latest bool Is this the last post of the topic changed? + * @param $limit_visibility mixed Limit updating per topic_id to a certain visibility + * @param $limit_delete_time mixed Limit updating per topic_id to a certain deletion time * @return void */ - static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest) + static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $limit_visibility = false, $limit_delete_time = false) { global $db; + if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) + { + return; + } + if ($post_id) { $where_sql = 'post_id = ' . (int) $post_id; @@ -181,6 +191,19 @@ class phpbb_content_visibility else if ($topic_id) { $where_sql = 'topic_id = ' . (int) $topic_id; + + // Limit the posts to a certain visibility and deletion time + // This allows us to only restore posts, that were approved + // when the topic got soft deleted. So previous soft deleted + // and unapproved posts are still soft deleted/unapproved + if ($limit_visibility !== false) + { + $where_sql .= ' AND post_visibility = ' . (int) $limit_visibility; + } + if ($limit_delete_time !== false) + { + $where_sql .= ' AND post_delete_time = ' . (int) $limit_delete_time; + } } else { -- cgit v1.2.1 From 42bb97a95cf57074edf2a30f1b4f417cfce81d13 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 13:15:55 +0200 Subject: [feature/soft-delete] Make use of set_post_visibility() limits when applicable PHPBB3-9567 --- phpBB/includes/content_visibility.php | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index be08a5f536..7bcabbdfd6 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -143,6 +143,24 @@ class phpbb_content_visibility { global $db; + if (in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) + { + return; + } + + $sql = 'SELECT topic_visibility, topic_delete_time + FROM ' . TOPICS_TABLE . ' + WHERE topic_id = ' . (int) $topic_id; + $result = $db->sql_query($sql); + $original_topic_data = $db->sql_fetchrow($result); + $db->sql_freeresult($result); + + if (!$original_topic_data) + { + // The topic does not exist... + return; + } + $data = array( 'topic_visibility' => (int) $visibility, 'topic_delete_user' => (int) $user_id, @@ -155,10 +173,16 @@ class phpbb_content_visibility WHERE topic_id = ' . (int) $topic_id; $db->sql_query($sql); - // If we're approving, disapproving, or deleteing a topic - // we also update all posts in that topic that need to be changed. - // However, we do not set the same reason for every post. - self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true); + // If we're restoring a topic we only restore posts, that were soft deleted through the topic soft deletion. + if ($original_topic_data['topic_delete_time'] && $original_topic_data['topic_visibility'] == ITEM_DELETED && $visibility == ITEM_APPROVED) + { + // Note, we do not set the same reason for every post. + self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility'], $original_topic_data['topic_delete_time']); + } + else + { + self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true); + } } /** -- cgit v1.2.1 From 92c5039af971722198ce634d719db6290d58c678 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 13:18:06 +0200 Subject: [feature/soft-delete] Allow forcing the set_visibility for all posts PHPBB3-9567 --- phpBB/includes/content_visibility.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 7bcabbdfd6..4f5da25919 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -139,7 +139,7 @@ class phpbb_content_visibility * @param $forum_id - int - forum ID where $topic_id resides * @return void */ - static public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason) + static public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason, $force_update_all = false) { global $db; @@ -148,17 +148,20 @@ class phpbb_content_visibility return; } - $sql = 'SELECT topic_visibility, topic_delete_time - FROM ' . TOPICS_TABLE . ' - WHERE topic_id = ' . (int) $topic_id; - $result = $db->sql_query($sql); - $original_topic_data = $db->sql_fetchrow($result); - $db->sql_freeresult($result); - - if (!$original_topic_data) + if (!$force_update_all && $visibility == ITEM_APPROVED) { - // The topic does not exist... - return; + $sql = 'SELECT topic_visibility, topic_delete_time + FROM ' . TOPICS_TABLE . ' + WHERE topic_id = ' . (int) $topic_id; + $result = $db->sql_query($sql); + $original_topic_data = $db->sql_fetchrow($result); + $db->sql_freeresult($result); + + if (!$original_topic_data) + { + // The topic does not exist... + return; + } } $data = array( @@ -174,7 +177,7 @@ class phpbb_content_visibility $db->sql_query($sql); // If we're restoring a topic we only restore posts, that were soft deleted through the topic soft deletion. - if ($original_topic_data['topic_delete_time'] && $original_topic_data['topic_visibility'] == ITEM_DELETED && $visibility == ITEM_APPROVED) + if (!$force_update_all && $original_topic_data['topic_delete_time'] && $original_topic_data['topic_visibility'] == ITEM_DELETED && $visibility == ITEM_APPROVED) { // Note, we do not set the same reason for every post. self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility'], $original_topic_data['topic_delete_time']); -- cgit v1.2.1 From 01a78907bd05fc51f62ebd169065277365c5d1f4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 13:30:59 +0200 Subject: [feature/soft-delete] Update set_topic_visibility() with some more logic By default, when a soft deleted topic is restored. Only posts that were approved at the time of soft deleting, are being restored. Same applies to soft deleting. Only approved posts will be marked as soft deleted. If you want to update all posts, use the force option. PHPBB3-9567 --- phpBB/includes/content_visibility.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 4f5da25919..238e131f1d 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -132,11 +132,24 @@ class phpbb_content_visibility } /** - * Description: Allows approving (which is akin to undeleting), unapproving (!) or soft deleting an entire topic. + * Set topic visibility + * + * Allows approving (which is akin to undeleting/restore) or soft deleting an entire topic. * Calls set_post_visibility as needed. - * @param $visibility - int - element of {ITEM_UNAPPROVED, ITEM_APPROVED, ITEM_DELETED} - * @param $topic_id - int - topic ID to act on - * @param $forum_id - int - forum ID where $topic_id resides + * + * Note: By default, when a soft deleted topic is restored. Only posts that + * were approved at the time of soft deleting, are being restored. + * Same applies to soft deleting. Only approved posts will be marked + * as soft deleted. + * If you want to update all posts, use the force option. + * + * @param $visibility int Element of {ITEM_APPROVED, ITEM_DELETED} + * @param $topic_id mixed Topic ID to act on + * @param $forum_id int Forum where $topic_id is found + * @param $user_id int User performing the action + * @param $time int Timestamp when the action is performed + * @param $reason string Reason why the visibilty was changed. + * @param $force_update_all bool Force to update all posts within the topic * @return void */ static public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason, $force_update_all = false) @@ -164,6 +177,7 @@ class phpbb_content_visibility } } + // Note, we do not set a reason for the posts, just for the topic $data = array( 'topic_visibility' => (int) $visibility, 'topic_delete_user' => (int) $user_id, @@ -176,12 +190,16 @@ class phpbb_content_visibility WHERE topic_id = ' . (int) $topic_id; $db->sql_query($sql); - // If we're restoring a topic we only restore posts, that were soft deleted through the topic soft deletion. if (!$force_update_all && $original_topic_data['topic_delete_time'] && $original_topic_data['topic_visibility'] == ITEM_DELETED && $visibility == ITEM_APPROVED) { - // Note, we do not set the same reason for every post. + // If we're restoring a topic we only restore posts, that were soft deleted through the topic soft deletion. self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility'], $original_topic_data['topic_delete_time']); } + else if (!$force_update_all && $original_topic_data['topic_visibility'] == ITEM_APPROVED && $visibility == ITEM_DELETED) + { + // If we're soft deleting a topic we only approved posts are soft deleted. + self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility']); + } else { self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true); -- cgit v1.2.1 From 7969cc7319002fdf24d041e8d298577cd04d9c4a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 13:31:47 +0200 Subject: [feature/soft-delete] Fix docs of set_post_visibility() PHPBB3-9567 --- phpBB/includes/content_visibility.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 238e131f1d..325993bd2a 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -214,6 +214,9 @@ class phpbb_content_visibility * all posts of topic_id will be modified * @param $topic_id int Topic where $post_id is found * @param $forum_id int Forum where $topic_id is found + * @param $user_id int User performing the action + * @param $time int Timestamp when the action is performed + * @param $reason string Reason why the visibilty was changed. * @param $is_starter bool Is this the first post of the topic changed? * @param $is_latest bool Is this the last post of the topic changed? * @param $limit_visibility mixed Limit updating per topic_id to a certain visibility -- cgit v1.2.1 From 526721c7db9ddc8fc39fd84409042deb470a1736 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 14:26:52 +0200 Subject: [feature/soft-delete] Fix set_topic_visibility() so it passes the tests PHPBB3-9567 --- phpBB/includes/content_visibility.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 325993bd2a..a0bb3e9fd4 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -156,12 +156,12 @@ class phpbb_content_visibility { global $db; - if (in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) + if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) { return; } - if (!$force_update_all && $visibility == ITEM_APPROVED) + if (!$force_update_all) { $sql = 'SELECT topic_visibility, topic_delete_time FROM ' . TOPICS_TABLE . ' -- cgit v1.2.1 From 05f236675528b5af68ba5f0ff140eb8c51ab92b1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 14:42:11 +0200 Subject: [feature/soft-delete] Update docs of can_soft_delete and remove can_restore PHPBB3-9567 --- phpBB/includes/content_visibility.php | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index a0bb3e9fd4..f2915a5143 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -293,9 +293,10 @@ class phpbb_content_visibility /** * Can the current logged-in user soft-delete posts? - * @param $forum_id - int - the forum ID whose permissions to check - * @param $poster_id - int - the poster ID of the post in question - * @param $post_locked - bool - is the post locked? + * + * @param $forum_id int Forum ID whose permissions to check + * @param $poster_id int Poster ID of the post in question + * @param $post_locked bool Is the post locked? * @return bool */ static function can_soft_delete($forum_id, $poster_id, $post_locked) @@ -310,28 +311,7 @@ class phpbb_content_visibility { return true; } - return false; - } - /** - * Can the current logged-in user restore soft-deleted posts? - * @param $forum_id - int - the forum ID whose permissions to check - * @param $poster_id - int - the poster ID of the post in question - * @param $post_locked - bool - is the post locked? - * @return bool - */ - public function can_restore($forum_id, $poster_id, $post_locked) - { - global $auth, $user; - - if ($auth->acl_get('m_restore', $forum_id)) - { - return true; - } - else if ($auth->acl_get('f_restore', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked) - { - return true; - } return false; } -- cgit v1.2.1 From c22d5bd37c26fb5e007519a0ea93001ecd060a1c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 5 Oct 2012 17:00:14 +0200 Subject: [feature/soft-delete] Clean the code of hide_post() and rely on postcount PHPBB3-9567 --- phpBB/includes/content_visibility.php | 41 ++++++++++------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index f2915a5143..2b79868930 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -316,8 +316,7 @@ class phpbb_content_visibility } /** - * Do the required math to hide a complete topic (going from approved to - * unapproved or from approved to deleted) + * Do the required math to hide a complete topic (going from approved to deleted) * @param $topic_id - int - the topic to act on * @param $forum_id - int - the forum where the topic resides * @param $topic_row - array - data about the topic, may be empty at call time @@ -326,7 +325,7 @@ class phpbb_content_visibility */ static public function hide_topic($topic_id, $forum_id, &$topic_row, &$sql_data) { - global $auth, $config, $db; + global $db; // Do we need to grab some topic informations? if (!sizeof($topic_row)) @@ -339,9 +338,6 @@ class phpbb_content_visibility $db->sql_freeresult($result); } - // If this is the only post remaining we do not need to decrement topic_replies. - // Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again. - // If this is an edited topic or the first post the topic gets completely disapproved later on... $sql_data[FORUMS_TABLE] = 'forum_topics = forum_topics - 1'; $sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1); @@ -363,44 +359,31 @@ class phpbb_content_visibility } /** - * Do the required math to hide a single post (going from approved to - * unapproved or from approved to deleted) + * Do the required math to hide a single post (going from approved to deleted) * Notably, we do _not_ need the post ID to do this operation. We're only changing statistic caches * @param $forum_id - int - the forum where the topic resides * @param $current_time - int - passed for consistency instead of calling time() internally - * @param $topic_row - array - contains information from the topics table about given topic + * @param $data - array - contains information from the topics table about given topic * @param $sql_data - array - populated with the SQL changes, may be empty at call time * @return void */ - static public function hide_post($forum_id, $current_time, $topic_row, &$sql_data) + //static public function remove_post_from_postcount($forum_id, $current_time, $data, &$sql_data) + static public function hide_post($forum_id, $current_time, $data, &$sql_data) { - global $auth, $config, $db; - - // initialize the array if needed (php throws E_NOTICE when .= is used - // on a non-existing array element) - if (empty($sql_data[TOPICS_TABLE])) - { - $sql_data[TOPICS_TABLE] = ''; - } - - if ($topic_row['topic_replies'] > 0) + $sql_data[TOPICS_TABLE] = 'topic_last_view_time = ' . $current_time; + if ($data['topic_replies'] > 0) { - $sql_data[TOPICS_TABLE] = 'topic_replies = topic_replies - 1,'; + $sql_data[TOPICS_TABLE] .= ', topic_replies = topic_replies - 1,'; } - $sql_data[TOPICS_TABLE] .= ' topic_last_view_time = ' . $current_time; $sql_data[FORUMS_TABLE] = 'forum_posts = forum_posts - 1'; - set_config_count('num_posts', -1, true); - - /** - * @todo: this is wrong, it should rely on post_postcount - * - if ($auth->acl_get('f_postcount', $forum_id)) + if ($data['post_postcount']) { $sql_data[USERS_TABLE] = 'user_posts = user_posts - 1'; } - */ + + set_config_count('num_posts', -1, true); } /** -- cgit v1.2.1 From 009bd698fb46a529f09d9d9a63748e63eec62895 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 6 Oct 2012 03:59:49 +0200 Subject: [feature/soft-delete] Update and simplify the logic on delete_post() Todo: delete_topic case PHPBB3-9567 --- phpBB/includes/content_visibility.php | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 2b79868930..f8b632bbe8 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -359,28 +359,22 @@ class phpbb_content_visibility } /** - * Do the required math to hide a single post (going from approved to deleted) - * Notably, we do _not_ need the post ID to do this operation. We're only changing statistic caches - * @param $forum_id - int - the forum where the topic resides - * @param $current_time - int - passed for consistency instead of calling time() internally - * @param $data - array - contains information from the topics table about given topic - * @param $sql_data - array - populated with the SQL changes, may be empty at call time + * Remove post from topic and forum statistics + * + * @param $forum_id int Forum where the topic is found + * @param $data array Contains information from the topics table about given topic + * @param $sql_data array Populated with the SQL changes, may be empty at call time * @return void */ - //static public function remove_post_from_postcount($forum_id, $current_time, $data, &$sql_data) - static public function hide_post($forum_id, $current_time, $data, &$sql_data) + static public function remove_post_from_postcount($forum_id, $data, &$sql_data) { - $sql_data[TOPICS_TABLE] = 'topic_last_view_time = ' . $current_time; - if ($data['topic_replies'] > 0) - { - $sql_data[TOPICS_TABLE] .= ', topic_replies = topic_replies - 1,'; - } + $sql_data[TOPICS_TABLE] = ($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies - 1'; - $sql_data[FORUMS_TABLE] = 'forum_posts = forum_posts - 1'; + $sql_data[FORUMS_TABLE] = ($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; if ($data['post_postcount']) { - $sql_data[USERS_TABLE] = 'user_posts = user_posts - 1'; + $sql_data[USERS_TABLE] = ($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; } set_config_count('num_posts', -1, true); @@ -539,6 +533,7 @@ class phpbb_content_visibility if ($total_topics) { + //@todo: plurals! $success_msg = ($total_topics == 1) ? 'TOPIC_APPROVED_SUCCESS' : 'TOPICS_APPROVED_SUCCESS'; } else -- cgit v1.2.1 From 44005f338e227c10a21270456d181d56749d3f29 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 6 Oct 2012 16:36:38 +0200 Subject: [feature/soft-delete] Fix delete_post() function PHPBB3-9567 --- phpBB/includes/content_visibility.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index f8b632bbe8..54c580cd40 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -316,14 +316,15 @@ class phpbb_content_visibility } /** - * Do the required math to hide a complete topic (going from approved to deleted) - * @param $topic_id - int - the topic to act on - * @param $forum_id - int - the forum where the topic resides - * @param $topic_row - array - data about the topic, may be empty at call time - * @param $sql_data - array - populated with the SQL changes, may be empty at call time + * Remove topic from forum statistics + * + * @param $topic_id int The topic to act on + * @param $forum_id int Forum where the topic is found + * @param $topic_row array Contains information from the topic, may be empty at call time + * @param $sql_data array Populated with the SQL changes, may be empty at call time * @return void */ - static public function hide_topic($topic_id, $forum_id, &$topic_row, &$sql_data) + static public function remove_topic_from_statistic($topic_id, $forum_id, &$topic_row, &$sql_data) { global $db; @@ -339,7 +340,7 @@ class phpbb_content_visibility } // If this is an edited topic or the first post the topic gets completely disapproved later on... - $sql_data[FORUMS_TABLE] = 'forum_topics = forum_topics - 1'; + $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_topics = forum_topics - 1'; $sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1); set_config_count('num_topics', -1, true); @@ -366,15 +367,15 @@ class phpbb_content_visibility * @param $sql_data array Populated with the SQL changes, may be empty at call time * @return void */ - static public function remove_post_from_postcount($forum_id, $data, &$sql_data) + static public function remove_post_from_statistic($forum_id, $data, &$sql_data) { - $sql_data[TOPICS_TABLE] = ($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies - 1'; + $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies - 1'; - $sql_data[FORUMS_TABLE] = ($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; + $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; if ($data['post_postcount']) { - $sql_data[USERS_TABLE] = ($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; + $sql_data[USERS_TABLE] = (($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; } set_config_count('num_posts', -1, true); -- cgit v1.2.1 From 25804eb8e8b17196116e233b2c8ad3b444cfb5ae Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 6 Oct 2012 19:56:52 +0200 Subject: [feature/soft-delete] Add test case for (soft)deleting the only post + fix PHPBB3-9567 --- phpBB/includes/content_visibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 54c580cd40..0d08cb83b6 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -416,7 +416,7 @@ class phpbb_content_visibility $forum_id_list[$post_data['forum_id']] = 1; } - // User post update (we do not care about topic or post, since user posts are strictly connected to posts) + // User post update (we do not care about topic or post, since user topics are strictly connected to posts) // But we care about forums where post counts get not increased. ;) if ($post_data['post_postcount']) { -- cgit v1.2.1 From c525e900d3b96b829c939010db343d8032698011 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 8 Oct 2012 15:01:20 +0200 Subject: [feature/soft-delete] Allow to update multiple posts with set_post_visibility PHPBB3-9567 --- phpBB/includes/content_visibility.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 0d08cb83b6..868400b5f6 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -210,8 +210,8 @@ class phpbb_content_visibility * Change visibility status of one post or a hole topic * * @param $visibility int Element of {ITEM_APPROVED, ITEM_DELETED} - * @param $post_id mixed Post ID to act on, if it is empty, - * all posts of topic_id will be modified + * @param $post_id mixed Post ID or array of post IDs to act on, + * if it is empty, all posts of topic_id will be modified * @param $topic_id int Topic where $post_id is found * @param $forum_id int Forum where $topic_id is found * @param $user_id int User performing the action @@ -234,7 +234,14 @@ class phpbb_content_visibility if ($post_id) { - $where_sql = 'post_id = ' . (int) $post_id; + if (is_array($post_id)) + { + $where_sql = $db->sql_in_set('post_id', array_map('intval', $post_id)); + } + else + { + $where_sql = 'post_id = ' . (int) $post_id; + } } else if ($topic_id) { @@ -382,7 +389,7 @@ class phpbb_content_visibility } /** - * One function to rule them all ... and unhide posts and topics. This could + * One function to rule them all... and unhide posts and topics. This could * reasonably be broken up, I straight copied this code from the mcp_queue.php * file here for global access. * @param $mode - string - member of the set {'approve', 'restore'} -- cgit v1.2.1 From 91398c9e48df7ce0da6763790d9ec233ab06e729 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 8 Oct 2012 15:03:54 +0200 Subject: [feature/soft-delete] Change order of functions PHPBB3-9567 --- phpBB/includes/content_visibility.php | 50 +++++++++++++++++------------------ 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 868400b5f6..182df69ec2 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -22,6 +22,30 @@ if (!defined('IN_PHPBB')) */ class phpbb_content_visibility { + /** + * Can the current logged-in user soft-delete posts? + * + * @param $forum_id int Forum ID whose permissions to check + * @param $poster_id int Poster ID of the post in question + * @param $post_locked bool Is the post locked? + * @return bool + */ + static function can_soft_delete($forum_id, $poster_id, $post_locked) + { + global $auth, $user; + + if ($auth->acl_get('m_softdelete', $forum_id)) + { + return true; + } + else if ($auth->acl_get('f_softdelete', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked) + { + return true; + } + + return false; + } + /** * Create topic/post visibility SQL for a given forum ID * @@ -207,7 +231,7 @@ class phpbb_content_visibility } /** - * Change visibility status of one post or a hole topic + * Change visibility status of one post or all posts of a topic * * @param $visibility int Element of {ITEM_APPROVED, ITEM_DELETED} * @param $post_id mixed Post ID or array of post IDs to act on, @@ -298,30 +322,6 @@ class phpbb_content_visibility } } - /** - * Can the current logged-in user soft-delete posts? - * - * @param $forum_id int Forum ID whose permissions to check - * @param $poster_id int Poster ID of the post in question - * @param $post_locked bool Is the post locked? - * @return bool - */ - static function can_soft_delete($forum_id, $poster_id, $post_locked) - { - global $auth, $user; - - if ($auth->acl_get('m_softdelete', $forum_id)) - { - return true; - } - else if ($auth->acl_get('f_softdelete', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked) - { - return true; - } - - return false; - } - /** * Remove topic from forum statistics * -- cgit v1.2.1 From 53e01bba19784b0fb36324c10c010f969f05d253 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 8 Oct 2012 22:47:50 +0200 Subject: [feature/soft-delete] Update post counts within set_post_visibility This is an additional query in some rare cases, but it makes it much easier to use and understand. This is mostly a preparation for the restore case. PHPBB3-9567 --- phpBB/includes/content_visibility.php | 337 ++++++++++++++++++++++++---------- 1 file changed, 237 insertions(+), 100 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 182df69ec2..7761587c53 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -155,6 +155,208 @@ class phpbb_content_visibility return $where_sqls[0]; } + /** + * Change visibility status of one post or all posts of a topic + * + * @param $visibility int Element of {ITEM_APPROVED, ITEM_DELETED} + * @param $post_id mixed Post ID or array of post IDs to act on, + * if it is empty, all posts of topic_id will be modified + * @param $topic_id int Topic where $post_id is found + * @param $forum_id int Forum where $topic_id is found + * @param $user_id int User performing the action + * @param $time int Timestamp when the action is performed + * @param $reason string Reason why the visibilty was changed. + * @param $is_starter bool Is this the first post of the topic changed? + * @param $is_latest bool Is this the last post of the topic changed? + * @param $limit_visibility mixed Limit updating per topic_id to a certain visibility + * @param $limit_delete_time mixed Limit updating per topic_id to a certain deletion time + * @return array Changed post data, empty array if an error occured. + */ + static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $limit_visibility = false, $limit_delete_time = false) + { + global $db; + + if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) + { + return array(); + } + + if ($post_id) + { + if (is_array($post_id)) + { + $where_sql = $db->sql_in_set('post_id', array_map('intval', $post_id)); + } + else + { + $where_sql = 'post_id = ' . (int) $post_id; + } + $where_sql .= ' AND topic_id = ' . (int) $topic_id; + } + else + { + $where_sql = 'topic_id = ' . (int) $topic_id; + + // Limit the posts to a certain visibility and deletion time + // This allows us to only restore posts, that were approved + // when the topic got soft deleted. So previous soft deleted + // and unapproved posts are still soft deleted/unapproved + if ($limit_visibility !== false) + { + $where_sql .= ' AND post_visibility = ' . (int) $limit_visibility; + } + if ($limit_delete_time !== false) + { + $where_sql .= ' AND post_delete_time = ' . (int) $limit_delete_time; + } + } + + $sql = 'SELECT poster_id, post_id, post_postcount, post_visibility + FROM ' . POSTS_TABLE . ' + WHERE ' . $where_sql; + $result = $db->sql_query($sql); + + $post_ids = $poster_postcounts = $postcounts = $postcount_visibility = array(); + while ($row = $db->sql_fetchrow($result)) + { + $post_ids[] = (int) $row['post_id']; + + if ($row['post_visibility'] != $visibility) + { + if ($row['post_postcount'] && !isset($poster_postcounts[$row['poster_id']])) + { + $poster_postcounts[$row['poster_id']] = 1; + } + else if ($row['post_postcount']) + { + $poster_postcounts[$row['poster_id']]++; + } + + if (!isset($postcount_visibility[$row['post_visibility']])) + { + $postcount_visibility[$row['post_visibility']] = 1; + } + else + { + $postcount_visibility[$row['post_visibility']]++; + } + } + } + $db->sql_freeresult($result); + + if (empty($post_ids)) + { + return array(); + } + + $data = array( + 'post_visibility' => (int) $visibility, + 'post_delete_user' => (int) $user_id, + 'post_delete_time' => ((int) $time) ?: time(), + 'post_delete_reason' => truncate_string($reason, 255, 255, false), + ); + + $sql = 'UPDATE ' . POSTS_TABLE . ' + SET ' . $db->sql_build_array('UPDATE', $data) . ' + WHERE ' . $db->sql_in_set('post_id', $post_ids); + $db->sql_query($sql); + + // Group the authors by post count, to reduce the number of queries + foreach ($poster_postcounts as $poster_id => $num_posts) + { + $postcounts[$num_posts][] = $poster_id; + } + + // Update users postcounts + foreach ($postcounts as $num_posts => $poster_ids) + { + if ($visibility == ITEM_DELETED) + { + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_posts = 0 + WHERE ' . $db->sql_in_set('user_id', $poster_ids) . ' + AND user_posts < ' . $num_posts; + $db->sql_query($sql); + + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_posts = user_posts - ' . $num_posts . ' + WHERE ' . $db->sql_in_set('user_id', $poster_ids) . ' + AND user_posts >= ' . $num_posts; + $db->sql_query($sql); + } + else + { + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_posts = user_posts + ' . $num_posts . ' + WHERE ' . $db->sql_in_set('user_id', $poster_ids) . ' + AND user_posts >= ' . $num_posts; + $db->sql_query($sql); + } + } + + $update_topic_postcount = true; + + // Sync the first/last topic information if needed + if (!$is_starter && $is_latest) + { + // update_post_information can only update the last post info ... + if ($topic_id) + { + update_post_information('topic', $topic_id, false); + } + if ($forum_id) + { + update_post_information('forum', $forum_id, false); + } + } + else if ($is_starter && $topic_id) + { + // ... so we need to use sync, if the first post is changed. + // The forum is resynced recursive by sync() itself. + sync('topic', 'topic_id', $topic_id, true); + + // sync recalculates the topic replies and forum posts by itself, so we don't do that. + $update_topic_postcount = false; + } + + // Update the topic's reply count and the forum's post count + if ($update_topic_postcount) + { + $num_posts = 0; + foreach ($postcount_visibility as $post_visibility => $visibility_posts) + { + // If we soft delete, we need to substract approved posts from the counters ... + if ($post_visibility == ITEM_APPROVED && $visibility == ITEM_DELETED) + { + $num_posts += $visibility_posts; + } + // ... and when we approve/restore, all others. + else if ($post_visibility != ITEM_APPROVED && $visibility == ITEM_APPROVED) + { + $num_posts += $visibility_posts; + } + } + + if ($num_posts) + { + $sql_num_posts = (($visibility == ITEM_DELETED) ? ' - ' : ' + ') . $num_posts; + + // Update the number for replies and posts + $sql = 'UPDATE ' . TOPICS_TABLE . " + SET topic_replies = topic_replies $sql_num_posts + WHERE topic_id = $topic_id"; + $db->sql_query($sql); + + $sql = 'UPDATE ' . FORUMS_TABLE . " + SET forum_posts = forum_posts $sql_num_posts + WHERE forum_id = $forum_id"; + $db->sql_query($sql); + } + } + + return $data; + } + /** * Set topic visibility * @@ -174,7 +376,7 @@ class phpbb_content_visibility * @param $time int Timestamp when the action is performed * @param $reason string Reason why the visibilty was changed. * @param $force_update_all bool Force to update all posts within the topic - * @return void + * @return array Changed topic data, empty array if an error occured. */ static public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason, $force_update_all = false) { @@ -182,7 +384,7 @@ class phpbb_content_visibility if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) { - return; + return array(); } if (!$force_update_all) @@ -197,7 +399,7 @@ class phpbb_content_visibility if (!$original_topic_data) { // The topic does not exist... - return; + return array(); } } @@ -214,6 +416,11 @@ class phpbb_content_visibility WHERE topic_id = ' . (int) $topic_id; $db->sql_query($sql); + if (!$db->sql_affectedrows()) + { + return array(); + } + if (!$force_update_all && $original_topic_data['topic_delete_time'] && $original_topic_data['topic_visibility'] == ITEM_DELETED && $visibility == ITEM_APPROVED) { // If we're restoring a topic we only restore posts, that were soft deleted through the topic soft deletion. @@ -228,98 +435,50 @@ class phpbb_content_visibility { self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true); } + + return $data; } /** - * Change visibility status of one post or all posts of a topic + * Add post to topic and forum statistics * - * @param $visibility int Element of {ITEM_APPROVED, ITEM_DELETED} - * @param $post_id mixed Post ID or array of post IDs to act on, - * if it is empty, all posts of topic_id will be modified - * @param $topic_id int Topic where $post_id is found - * @param $forum_id int Forum where $topic_id is found - * @param $user_id int User performing the action - * @param $time int Timestamp when the action is performed - * @param $reason string Reason why the visibilty was changed. - * @param $is_starter bool Is this the first post of the topic changed? - * @param $is_latest bool Is this the last post of the topic changed? - * @param $limit_visibility mixed Limit updating per topic_id to a certain visibility - * @param $limit_delete_time mixed Limit updating per topic_id to a certain deletion time + * @param $data array Contains information from the topics table about given topic + * @param $sql_data array Populated with the SQL changes, may be empty at call time * @return void */ - static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $limit_visibility = false, $limit_delete_time = false) + static public function add_post_to_statistic($data, &$sql_data) { - global $db; + $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies + 1'; - if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) - { - return; - } + $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts + 1'; - if ($post_id) + if ($data['post_postcount']) { - if (is_array($post_id)) - { - $where_sql = $db->sql_in_set('post_id', array_map('intval', $post_id)); - } - else - { - $where_sql = 'post_id = ' . (int) $post_id; - } + $sql_data[USERS_TABLE] = (($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts + 1'; } - else if ($topic_id) - { - $where_sql = 'topic_id = ' . (int) $topic_id; - // Limit the posts to a certain visibility and deletion time - // This allows us to only restore posts, that were approved - // when the topic got soft deleted. So previous soft deleted - // and unapproved posts are still soft deleted/unapproved - if ($limit_visibility !== false) - { - $where_sql .= ' AND post_visibility = ' . (int) $limit_visibility; - } - if ($limit_delete_time !== false) - { - $where_sql .= ' AND post_delete_time = ' . (int) $limit_delete_time; - } - } - else - { - return; - } + set_config_count('num_posts', 1, true); + } - $data = array( - 'post_visibility' => (int) $visibility, - 'post_delete_user' => (int) $user_id, - 'post_delete_time' => ((int) $time) ?: time(), - 'post_delete_reason' => truncate_string($reason, 255, 255, false), - ); + /** + * Remove post from topic and forum statistics + * + * @param $data array Contains information from the topics table about given topic + * @param $sql_data array Populated with the SQL changes, may be empty at call time + * @return void + */ + static public function remove_post_from_statistic($data, &$sql_data) + { + $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies - 1'; - $sql = 'UPDATE ' . POSTS_TABLE . ' - SET ' . $db->sql_build_array('UPDATE', $data) . ' - WHERE ' . $where_sql; - $db->sql_query($sql); + $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; - // Sync the first/last topic information if needed - if (!$is_starter && $is_latest) - { - // update_post_information can only update the last post info ... - if ($topic_id) - { - update_post_information('topic', $topic_id, false); - } - if ($forum_id) - { - update_post_information('forum', $forum_id, false); - } - } - else if ($is_starter && $topic_id) + if ($data['post_postcount']) { - // ... so we need to use sync, if the first post is changed. - // The forum is resynced recursive by sync() itself. - sync('topic', 'topic_id', $topic_id, true); + $sql_data[USERS_TABLE] = (($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; } + + set_config_count('num_posts', -1, true); } /** @@ -366,28 +525,6 @@ class phpbb_content_visibility */ } - /** - * Remove post from topic and forum statistics - * - * @param $forum_id int Forum where the topic is found - * @param $data array Contains information from the topics table about given topic - * @param $sql_data array Populated with the SQL changes, may be empty at call time - * @return void - */ - static public function remove_post_from_statistic($forum_id, $data, &$sql_data) - { - $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies - 1'; - - $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; - - if ($data['post_postcount']) - { - $sql_data[USERS_TABLE] = (($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; - } - - set_config_count('num_posts', -1, true); - } - /** * One function to rule them all... and unhide posts and topics. This could * reasonably be broken up, I straight copied this code from the mcp_queue.php -- cgit v1.2.1 From e447a0fa0797440688335bc0dc18c8a73b5586ec Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 8 Oct 2012 23:09:12 +0200 Subject: [feature/soft-delete] Fix restoring a post via editing PHPBB3-9567 --- phpBB/includes/content_visibility.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 7761587c53..dcb7f363f8 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -311,6 +311,12 @@ class phpbb_content_visibility } else if ($is_starter && $topic_id) { + if (!function_exists('sync')) + { + global $phpEx, $phpbb_root_path; + include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + } + // ... so we need to use sync, if the first post is changed. // The forum is resynced recursive by sync() itself. sync('topic', 'topic_id', $topic_id, true); -- cgit v1.2.1 From 7cc8b3eef82a70a1aa708614e37fd82482d9d7d2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 9 Oct 2012 12:23:15 +0200 Subject: [feature/soft-delete] Correctly update user_posts count Before soft delete this was much easier, as an unapproved topic could only have one post, because no one could reply to unapproved topics. Now we need to run multiple queries to correctly reduce the post counts. PHPBB3-9567 --- phpBB/includes/content_visibility.php | 38 ++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index dcb7f363f8..8854981303 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -518,17 +518,37 @@ class phpbb_content_visibility set_config_count('num_topics', -1, true); set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true); - // Only decrement this post, since this is the one non-approved now - // - /** - * @todo: this is wrong, it should rely on post_postcount - * also a user might have more than one post in the topic - * - if ($auth->acl_get('f_postcount', $forum_id)) + // Get user post count information + $sql = 'SELECT poster_id, COUNT(post_id) AS num_posts + FROM ' . POSTS_TABLE . ' + WHERE topic_id = ' . $topic_id . ' + AND post_postcount = 1 + AND post_visibility = ' . ITEM_APPROVED . ' + GROUP BY poster_id'; + $result = $db->sql_query($sql); + + $postcounts = array(); + while ($row = $db->sql_fetchrow($result)) + { + $postcounts[(int) $row['num_posts']][] = (int) $row['poster_id']; + } + $db->sql_freeresult($result); + + // Decrement users post count + foreach ($postcounts as $num_posts => $poster_ids) { - $sql_data[USERS_TABLE] = 'user_posts = user_posts - 1'; + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_posts = 0 + WHERE user_posts < ' . $num_posts . ' + AND ' . $db->sql_in_set('user_id', $poster_ids); + $db->sql_query($sql); + + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_posts = user_posts - ' . $num_posts . ' + WHERE user_posts >= ' . $num_posts . ' + AND ' . $db->sql_in_set('user_id', $poster_ids); + $db->sql_query($sql); } - */ } /** -- cgit v1.2.1 From 43d041bdecb5e564ad48efdf4702cad3714c7837 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 16 Oct 2012 14:20:23 +0200 Subject: [feature/soft-delete] Removed unused old functions PHPBB3-9567 --- phpBB/includes/content_visibility.php | 164 ---------------------------------- 1 file changed, 164 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 8854981303..8bfdfd2917 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -550,168 +550,4 @@ class phpbb_content_visibility $db->sql_query($sql); } } - - /** - * One function to rule them all... and unhide posts and topics. This could - * reasonably be broken up, I straight copied this code from the mcp_queue.php - * file here for global access. - * @param $mode - string - member of the set {'approve', 'restore'} - * @param $post_info - array - Contains info from post U topics table about - * the posts/topics in question - * @param $post_id_list - array of ints - the set of posts being worked on - */ - static public function unhide_posts_topics($mode, $post_info, $post_id_list) - { - global $db, $config; - - // If Topic -> total_topics = total_topics+1, total_posts = total_posts+1, forum_topics = forum_topics+1, forum_posts = forum_posts+1 - // If Post -> total_posts = total_posts+1, forum_posts = forum_posts+1, topic_replies = topic_replies+1 - - $total_topics = $total_posts = 0; - $topic_approve_sql = $post_approve_sql = $topic_id_list = $forum_id_list = $approve_log = array(); - $user_posts_sql = $post_approved_list = array(); - - foreach ($post_info as $post_id => $post_data) - { - if ($post_data['post_visibility'] == ITEM_APPROVED) - { - $post_approved_list[] = $post_id; - continue; - } - - $topic_id_list[$post_data['topic_id']] = 1; - - if ($post_data['forum_id']) - { - $forum_id_list[$post_data['forum_id']] = 1; - } - - // User post update (we do not care about topic or post, since user topics are strictly connected to posts) - // But we care about forums where post counts get not increased. ;) - if ($post_data['post_postcount']) - { - $user_posts_sql[$post_data['poster_id']] = (empty($user_posts_sql[$post_data['poster_id']])) ? 1 : $user_posts_sql[$post_data['poster_id']] + 1; - } - - // Topic or Post. ;) - if ($post_data['topic_first_post_id'] == $post_id) - { - if ($post_data['forum_id']) - { - $total_topics++; - } - $topic_approve_sql[] = $post_data['topic_id']; - - $approve_log[] = array( - 'type' => 'topic', - 'post_subject' => $post_data['post_subject'], - 'forum_id' => $post_data['forum_id'], - 'topic_id' => $post_data['topic_id'], - ); - } - else - { - $approve_log[] = array( - 'type' => 'post', - 'post_subject' => $post_data['post_subject'], - 'forum_id' => $post_data['forum_id'], - 'topic_id' => $post_data['topic_id'], - ); - } - - if ($post_data['forum_id']) - { - $total_posts++; - - // Increment by topic_replies if we approve a topic... - // This works because we do not adjust the topic_replies when re-approving a topic after an edit. - if ($post_data['topic_first_post_id'] == $post_id && $post_data['topic_replies']) - { - $total_posts += $post_data['topic_replies']; - } - } - - $post_approve_sql[] = $post_id; - } - - $post_id_list = array_values(array_diff($post_id_list, $post_approved_list)); - for ($i = 0, $size = sizeof($post_approved_list); $i < $size; $i++) - { - unset($post_info[$post_approved_list[$i]]); - } - - if (sizeof($topic_approve_sql)) - { - $sql = 'UPDATE ' . TOPICS_TABLE . ' - SET topic_visibility = ' . ITEM_APPROVED . ' - WHERE ' . $db->sql_in_set('topic_id', $topic_approve_sql); - $db->sql_query($sql); - } - - if (sizeof($post_approve_sql)) - { - $sql = 'UPDATE ' . POSTS_TABLE . ' - SET post_visibility = ' . ITEM_APPROVED . ' - WHERE ' . $db->sql_in_set('post_id', $post_approve_sql); - $db->sql_query($sql); - } - - unset($topic_approve_sql, $post_approve_sql); - - foreach ($approve_log as $log_data) - { - add_log('mod', $log_data['forum_id'], $log_data['topic_id'], ($log_data['type'] == 'topic') ? 'LOG_TOPIC_' . strtoupper($mode) . 'D' : 'LOG_POST_' . strtoupper($mode) . 'D', $log_data['post_subject']); - } - - if (sizeof($user_posts_sql)) - { - // Try to minimize the query count by merging users with the same post count additions - $user_posts_update = array(); - - foreach ($user_posts_sql as $user_id => $user_posts) - { - $user_posts_update[$user_posts][] = $user_id; - } - - foreach ($user_posts_update as $user_posts => $user_id_ary) - { - $sql = 'UPDATE ' . USERS_TABLE . ' - SET user_posts = user_posts + ' . $user_posts . ' - WHERE ' . $db->sql_in_set('user_id', $user_id_ary); - $db->sql_query($sql); - } - } - - if ($total_topics) - { - set_config_count('num_topics', $total_topics, true); - } - - if ($total_posts) - { - set_config_count('num_posts', $total_posts, true); - } - - if (!function_exists('sync')) - { - global $phpbb_root_path, $phpEx; - include ($phpbb_root_path . 'includes/functions_admin.'.$phpEx); - } - - sync('topic', 'topic_id', array_keys($topic_id_list), true); - sync('forum', 'forum_id', array_keys($forum_id_list), true, true); - unset($topic_id_list, $forum_id_list); - - if ($total_topics) - { - //@todo: plurals! - $success_msg = ($total_topics == 1) ? 'TOPIC_APPROVED_SUCCESS' : 'TOPICS_APPROVED_SUCCESS'; - } - else - { - $success_msg = (sizeof($post_id_list) + sizeof($post_approved_list) == 1) ? 'POST_APPROVED_SUCCESS' : 'POSTS_APPROVED_SUCCESS'; - } - - return $success_msg; - } } -- cgit v1.2.1 From 6c39563e9f7fad18f1425292dca652861f5e1cb6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 21 Oct 2012 23:38:55 +0200 Subject: [feature/soft-delete] Add a function to calculate the actual post/topic count PHPBB3-9567 --- phpBB/includes/content_visibility.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 8bfdfd2917..2a0cc3c850 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -46,6 +46,26 @@ class phpbb_content_visibility return false; } + /** + * Get the topics post count or the forums post/topic count based on permissions + * + * @param $mode string One of topic_posts, forum_posts or forum_topics + * @param $data array Array with the topic/forum data to calculate from + * @param $forum_id int The forum id is used for permission checks + * @return int Number of posts/topics the user can see in the topic/forum + */ + static public function get_count($mode, $data, $forum_id) + { + global $auth; + + if (!$auth->acl_get('m_approve', $forum_id)) + { + return (int) $data[$mode]; + } + + return (int) $data[$mode] + (int) $data[$mode . '_unapproved'] + (int) $data[$mode . '_softdeleted']; + } + /** * Create topic/post visibility SQL for a given forum ID * -- cgit v1.2.1 From 168dd29f24843e97182c894a5d43a7d38f8a195c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 22 Oct 2012 14:21:44 +0200 Subject: [feature/soft-delete] Fix sync() and some more functions to use the new fields PHPBB3-9567 --- phpBB/includes/content_visibility.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 2a0cc3c850..5596fe98df 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -523,7 +523,7 @@ class phpbb_content_visibility // Do we need to grab some topic informations? if (!sizeof($topic_row)) { - $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_visibility + $sql = 'SELECT topic_type, topic_posts, topic_posts_unapproved, topic_posts_softdeleted, topic_visibility FROM ' . TOPICS_TABLE . ' WHERE topic_id = ' . $topic_id; $result = $db->sql_query($sql); @@ -533,10 +533,12 @@ class phpbb_content_visibility // If this is an edited topic or the first post the topic gets completely disapproved later on... $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_topics = forum_topics - 1'; - $sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1); + $sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . $topic_row['topic_posts']; + $sql_data[FORUMS_TABLE] .= ', forum_posts_unapproved = forum_posts_unapproved - ' . $topic_row['topic_posts_unapproved']; + $sql_data[FORUMS_TABLE] .= ', forum_posts_softdeleted = forum_posts_softdeleted - ' . $topic_row['topic_posts_softdeleted']; set_config_count('num_topics', -1, true); - set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true); + set_config_count('num_posts', $topic_row['topic_posts'] * (-1), true); // Get user post count information $sql = 'SELECT poster_id, COUNT(post_id) AS num_posts -- cgit v1.2.1 From 90154db1dbe3cd24f5af82ad5f665d607aad97ed Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 22 Oct 2012 16:38:53 +0200 Subject: [feature/soft-delete] Fix submit_post() topic_replies* usage PHPBB3-9567 --- phpBB/includes/content_visibility.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 5596fe98df..20e743a4c4 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -495,8 +495,7 @@ class phpbb_content_visibility */ static public function remove_post_from_statistic($data, &$sql_data) { - $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies - 1'; - + $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts = topic_posts - 1'; $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; if ($data['post_postcount']) -- cgit v1.2.1 From b3d5f2b4e42404610f5db8dda25859db942fd60e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 23 Oct 2012 23:37:25 +0200 Subject: [feature/soft-delete] Fix unit tests for delete_posts() PHPBB3-9567 --- phpBB/includes/content_visibility.php | 71 +++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 15 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 20e743a4c4..da7e6b9c4f 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -348,34 +348,75 @@ class phpbb_content_visibility // Update the topic's reply count and the forum's post count if ($update_topic_postcount) { - $num_posts = 0; + $cur_posts = $cur_unapproved_posts = $cur_softdeleted_posts = 0; foreach ($postcount_visibility as $post_visibility => $visibility_posts) { - // If we soft delete, we need to substract approved posts from the counters ... - if ($post_visibility == ITEM_APPROVED && $visibility == ITEM_DELETED) + // We need to substract the posts from the counters ... + if ($post_visibility == ITEM_APPROVED) { - $num_posts += $visibility_posts; + $cur_posts += $visibility_posts; } - // ... and when we approve/restore, all others. - else if ($post_visibility != ITEM_APPROVED && $visibility == ITEM_APPROVED) + else if ($post_visibility == ITEM_UNAPPROVED) { - $num_posts += $visibility_posts; + $cur_unapproved_posts += $visibility_posts; + } + else if ($post_visibility == ITEM_DELETED) + { + $cur_softdeleted_posts += $visibility_posts; + } + } + + $sql_ary = array(); + if ($visibility == ITEM_DELETED) + { + if ($cur_posts) + { + $sql_ary['posts'] = ' - ' . $cur_posts; + } + if ($cur_unapproved_posts) + { + $sql_ary['posts_unapproved'] = ' - ' . $cur_unapproved_posts; + } + if ($cur_posts + $cur_unapproved_posts) + { + $sql_ary['posts_softdeleted'] = ' + ' . ($cur_posts + $cur_unapproved_posts); + } + } + else + { + if ($cur_unapproved_posts) + { + $sql_ary['posts_unapproved'] = ' - ' . $cur_unapproved_posts; + } + if ($cur_softdeleted_posts) + { + $sql_ary['posts_softdeleted'] = ' - ' . $cur_softdeleted_posts; + } + if ($cur_posts + $cur_unapproved_posts) + { + $sql_ary['posts'] = ' + ' . ($cur_softdeleted_posts + $cur_unapproved_posts); } } - if ($num_posts) + if (sizeof($sql_ary)) { - $sql_num_posts = (($visibility == ITEM_DELETED) ? ' - ' : ' + ') . $num_posts; + $topic_sql = $forum_sql = array(); + + foreach ($sql_ary as $field => $value_change) + { + $topic_sql[] = 'topic_' . $field . ' = topic_' . $field . $value_change; + $forum_sql[] = 'forum_' . $field . ' = forum_' . $field . $value_change; + } // Update the number for replies and posts - $sql = 'UPDATE ' . TOPICS_TABLE . " - SET topic_replies = topic_replies $sql_num_posts - WHERE topic_id = $topic_id"; + $sql = 'UPDATE ' . TOPICS_TABLE . ' + SET ' . implode(', ', $topic_sql) . ' + WHERE topic_id = ' . $topic_id; $db->sql_query($sql); - $sql = 'UPDATE ' . FORUMS_TABLE . " - SET forum_posts = forum_posts $sql_num_posts - WHERE forum_id = $forum_id"; + $sql = 'UPDATE ' . FORUMS_TABLE . ' + SET ' . implode(', ', $forum_sql) . ' + WHERE forum_id = ' . $forum_id; $db->sql_query($sql); } } -- cgit v1.2.1 From f21fd469bca3e5c3504a773a96d1a8fab6c374a7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 24 Oct 2012 19:52:16 +0200 Subject: [feature/soft-delete] Handle soft deleting via Delete Icon PHPBB3-9567 --- phpBB/includes/content_visibility.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index da7e6b9c4f..6dffe73114 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -225,6 +225,7 @@ class phpbb_content_visibility { $where_sql .= ' AND post_visibility = ' . (int) $limit_visibility; } + if ($limit_delete_time !== false) { $where_sql .= ' AND post_delete_time = ' . (int) $limit_delete_time; -- cgit v1.2.1 From 63e3baf0eb37d1d8f6f0b5b46df56a673eafa6fe Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 2 Nov 2012 12:40:10 +0100 Subject: [feature/soft-delete] Correctly manage softdeleting via posting.php PHPBB3-9567 --- phpBB/includes/content_visibility.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 6dffe73114..8f7e86cdf5 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -537,12 +537,12 @@ class phpbb_content_visibility */ static public function remove_post_from_statistic($data, &$sql_data) { - $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts = topic_posts - 1'; - $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; + $sql_data[TOPICS_TABLE] = ((!empty($sql_data[TOPICS_TABLE])) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts = topic_posts - 1'; + $sql_data[FORUMS_TABLE] = ((!empty($sql_data[FORUMS_TABLE])) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; if ($data['post_postcount']) { - $sql_data[USERS_TABLE] = (($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; + $sql_data[USERS_TABLE] = ((!empty($sql_data[USERS_TABLE])) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; } set_config_count('num_posts', -1, true); -- cgit v1.2.1 From bb173afe3fee816dab9d11764a8b9ff53dc5a1df Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 8 Nov 2012 22:17:59 +0100 Subject: [feature/soft-delete] Fix adding posts to *_posts when restoring soft deleted PHPBB3-9567 --- phpBB/includes/content_visibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 8f7e86cdf5..a1b7c16c2d 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -393,7 +393,7 @@ class phpbb_content_visibility { $sql_ary['posts_softdeleted'] = ' - ' . $cur_softdeleted_posts; } - if ($cur_posts + $cur_unapproved_posts) + if ($cur_softdeleted_posts + $cur_unapproved_posts) { $sql_ary['posts'] = ' + ' . ($cur_softdeleted_posts + $cur_unapproved_posts); } -- cgit v1.2.1 From 9c2a58eff4c2bd164ee3bdb2ec66729d4562963d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 9 Nov 2012 13:37:53 +0100 Subject: [feature/soft-delete] Append _approved to *_posts and *_topics column names PHPBB3-9567 --- phpBB/includes/content_visibility.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index a1b7c16c2d..bea70571f9 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -60,10 +60,10 @@ class phpbb_content_visibility if (!$auth->acl_get('m_approve', $forum_id)) { - return (int) $data[$mode]; + return (int) $data[$mode . '_approved']; } - return (int) $data[$mode] + (int) $data[$mode . '_unapproved'] + (int) $data[$mode . '_softdeleted']; + return (int) $data[$mode . '_approved'] + (int) $data[$mode . '_unapproved'] + (int) $data[$mode . '_softdeleted']; } /** @@ -372,7 +372,7 @@ class phpbb_content_visibility { if ($cur_posts) { - $sql_ary['posts'] = ' - ' . $cur_posts; + $sql_ary['posts_approved'] = ' - ' . $cur_posts; } if ($cur_unapproved_posts) { @@ -395,7 +395,7 @@ class phpbb_content_visibility } if ($cur_softdeleted_posts + $cur_unapproved_posts) { - $sql_ary['posts'] = ' + ' . ($cur_softdeleted_posts + $cur_unapproved_posts); + $sql_ary['posts_approved'] = ' + ' . ($cur_softdeleted_posts + $cur_unapproved_posts); } } @@ -564,7 +564,7 @@ class phpbb_content_visibility // Do we need to grab some topic informations? if (!sizeof($topic_row)) { - $sql = 'SELECT topic_type, topic_posts, topic_posts_unapproved, topic_posts_softdeleted, topic_visibility + $sql = 'SELECT topic_type, topic_posts_approved, topic_posts_unapproved, topic_posts_softdeleted, topic_visibility FROM ' . TOPICS_TABLE . ' WHERE topic_id = ' . $topic_id; $result = $db->sql_query($sql); @@ -573,8 +573,8 @@ class phpbb_content_visibility } // If this is an edited topic or the first post the topic gets completely disapproved later on... - $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_topics = forum_topics - 1'; - $sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . $topic_row['topic_posts']; + $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_topics_approved = forum_topics_approved - 1'; + $sql_data[FORUMS_TABLE] .= ', forum_posts_approved = forum_posts_approved - ' . $topic_row['topic_posts_approved']; $sql_data[FORUMS_TABLE] .= ', forum_posts_unapproved = forum_posts_unapproved - ' . $topic_row['topic_posts_unapproved']; $sql_data[FORUMS_TABLE] .= ', forum_posts_softdeleted = forum_posts_softdeleted - ' . $topic_row['topic_posts_softdeleted']; -- cgit v1.2.1 From f77a6eaab5485329a3b13922649fb8902e6e397f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 10 Nov 2012 11:24:52 +0100 Subject: [feature/soft-delete] Fix the rest of *_approved and the delete_post unit test PHPBB3-9567 --- phpBB/includes/content_visibility.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index bea70571f9..3118be6574 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -516,9 +516,9 @@ class phpbb_content_visibility */ static public function add_post_to_statistic($data, &$sql_data) { - $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies + 1'; + $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts_approved = topic_posts_approved + 1'; - $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts + 1'; + $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts_approved = forum_posts_approved + 1'; if ($data['post_postcount']) { @@ -537,8 +537,8 @@ class phpbb_content_visibility */ static public function remove_post_from_statistic($data, &$sql_data) { - $sql_data[TOPICS_TABLE] = ((!empty($sql_data[TOPICS_TABLE])) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts = topic_posts - 1'; - $sql_data[FORUMS_TABLE] = ((!empty($sql_data[FORUMS_TABLE])) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1'; + $sql_data[TOPICS_TABLE] = ((!empty($sql_data[TOPICS_TABLE])) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts_approved = topic_posts_approved - 1'; + $sql_data[FORUMS_TABLE] = ((!empty($sql_data[FORUMS_TABLE])) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts_approved = forum_posts_approved - 1'; if ($data['post_postcount']) { @@ -579,7 +579,7 @@ class phpbb_content_visibility $sql_data[FORUMS_TABLE] .= ', forum_posts_softdeleted = forum_posts_softdeleted - ' . $topic_row['topic_posts_softdeleted']; set_config_count('num_topics', -1, true); - set_config_count('num_posts', $topic_row['topic_posts'] * (-1), true); + set_config_count('num_posts', $topic_row['topic_posts_approved'] * (-1), true); // Get user post count information $sql = 'SELECT poster_id, COUNT(post_id) AS num_posts -- cgit v1.2.1 From 7db5eec5d28449af2d313f52b4d4c1c4534ba870 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 10 Mar 2013 22:15:29 +0100 Subject: [ticket/9657] Correctly increase users post count when approving posts PHPBB3-9657 --- phpBB/includes/content_visibility.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 3118be6574..6ca1f8b25c 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -309,8 +309,7 @@ class phpbb_content_visibility { $sql = 'UPDATE ' . USERS_TABLE . ' SET user_posts = user_posts + ' . $num_posts . ' - WHERE ' . $db->sql_in_set('user_id', $poster_ids) . ' - AND user_posts >= ' . $num_posts; + WHERE ' . $db->sql_in_set('user_id', $poster_ids); $db->sql_query($sql); } } -- cgit v1.2.1 From cc5ba36a061215e0d96f32b67e9f1391e699ac63 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 10 Mar 2013 22:18:37 +0100 Subject: [ticket/9657] Cast IDs to integer PHPBB3-9657 --- phpBB/includes/content_visibility.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 6ca1f8b25c..ac827bd822 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -244,13 +244,13 @@ class phpbb_content_visibility if ($row['post_visibility'] != $visibility) { - if ($row['post_postcount'] && !isset($poster_postcounts[$row['poster_id']])) + if ($row['post_postcount'] && !isset($poster_postcounts[(int) $row['poster_id']])) { - $poster_postcounts[$row['poster_id']] = 1; + $poster_postcounts[(int) $row['poster_id']] = 1; } else if ($row['post_postcount']) { - $poster_postcounts[$row['poster_id']]++; + $poster_postcounts[(int) $row['poster_id']]++; } if (!isset($postcount_visibility[$row['post_visibility']])) @@ -411,12 +411,12 @@ class phpbb_content_visibility // Update the number for replies and posts $sql = 'UPDATE ' . TOPICS_TABLE . ' SET ' . implode(', ', $topic_sql) . ' - WHERE topic_id = ' . $topic_id; + WHERE topic_id = ' . (int) $topic_id; $db->sql_query($sql); $sql = 'UPDATE ' . FORUMS_TABLE . ' SET ' . implode(', ', $forum_sql) . ' - WHERE forum_id = ' . $forum_id; + WHERE forum_id = ' . (int) $forum_id; $db->sql_query($sql); } } -- cgit v1.2.1 From cdb13fc7be4771bc89da624fe0b5f38b0cc3ccd1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 11 Jul 2013 11:52:12 +0200 Subject: [ticket/9657] Correctly use " vs ' and variables in queries PHPBB3-9657 --- phpBB/includes/content_visibility.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index ac827bd822..88f57a0020 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -126,11 +126,11 @@ class phpbb_content_visibility else { // The user is just a normal user - return "$table_alias{$mode}_visibility = " . ITEM_APPROVED . ' + return $table_alias . $mode . '_visibility = ' . ITEM_APPROVED . ' AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids, false, true); } - $where_sql .= "($table_alias{$mode}_visibility = " . ITEM_APPROVED . ' + $where_sql .= '(' . $table_alias . $mode . '_visibility = ' . ITEM_APPROVED . ' AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids) . '))'; return $where_sql; @@ -157,12 +157,12 @@ class phpbb_content_visibility if (sizeof($exclude_forum_ids)) { - $where_sqls[] = '(' . $db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true) . " - AND $table_alias{$mode}_visibility = " . ITEM_APPROVED . ')'; + $where_sqls[] = '(' . $db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true) . ' + AND ' . $table_alias . $mode . '_visibility = ' . ITEM_APPROVED . ')'; } else { - $where_sqls[] = "$table_alias{$mode}_visibility = " . ITEM_APPROVED; + $where_sqls[] = $table_alias . $mode . '_visibility = ' . ITEM_APPROVED; } if (sizeof($approve_forums)) -- cgit v1.2.1 From dc2f13d55d181c1a927b6daa8652f68ee87b4a90 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 11 Jul 2013 11:56:05 +0200 Subject: [ticket/9657] Cast topic_id to integer PHPBB3-9657 --- phpBB/includes/content_visibility.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 88f57a0020..f4126d0fff 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -565,7 +565,7 @@ class phpbb_content_visibility { $sql = 'SELECT topic_type, topic_posts_approved, topic_posts_unapproved, topic_posts_softdeleted, topic_visibility FROM ' . TOPICS_TABLE . ' - WHERE topic_id = ' . $topic_id; + WHERE topic_id = ' . (int) $topic_id; $result = $db->sql_query($sql); $topic_row = $db->sql_fetchrow($result); $db->sql_freeresult($result); @@ -583,7 +583,7 @@ class phpbb_content_visibility // Get user post count information $sql = 'SELECT poster_id, COUNT(post_id) AS num_posts FROM ' . POSTS_TABLE . ' - WHERE topic_id = ' . $topic_id . ' + WHERE topic_id = ' . (int) $topic_id . ' AND post_postcount = 1 AND post_visibility = ' . ITEM_APPROVED . ' GROUP BY poster_id'; -- cgit v1.2.1 From 9f89cb4cfbbd46ad45a9c7942fc2233b489bb076 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 11 Jul 2013 13:31:39 +0200 Subject: [ticket/9657] Make content visibility a service and inject everything PHPBB3-9657 --- phpBB/includes/content_visibility.php | 224 ++++++++++++++++++++-------------- 1 file changed, 130 insertions(+), 94 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index f4126d0fff..edf6aa3b31 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -22,6 +22,59 @@ if (!defined('IN_PHPBB')) */ class phpbb_content_visibility { + /** + * Database object + * @var phpbb_db_driver + */ + protected $this->db; + + /** + * User object + * @var phpbb_user + */ + protected $this->user; + + /** + * Auth object + * @var phpbb_auth + */ + protected $this->auth; + + /** + * phpBB root path + * @var string + */ + protected $phpbb_root_path; + + /** + * PHP Extension + * @var string + */ + protected $php_ext; + + /** + * Constructor + * + * @param phpbb_auth $this->auth Auth object + * @param phpbb_db_driver $this->db Database object + * @param phpbb_user $this->user User object + * @param string $phpbb_root_path Root path + * @param string $php_ext PHP Extension + * @return null + */ + public function __construct($this->auth, phpbb_db_driver $this->db, $this->user, $phpbb_root_path, $phpEx, $forums_table, $posts_table, $topics_table, $users_table) + { + $this->auth = $this->auth; + $this->db = $this->db; + $this->user = $this->user; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + $this->forums_table = $forums_table; + $this->posts_table = $posts_table; + $this->topics_table = $topics_table; + $this->users_table = $users_table; + } + /** * Can the current logged-in user soft-delete posts? * @@ -30,15 +83,13 @@ class phpbb_content_visibility * @param $post_locked bool Is the post locked? * @return bool */ - static function can_soft_delete($forum_id, $poster_id, $post_locked) + public function can_soft_delete($forum_id, $poster_id, $post_locked) { - global $auth, $user; - - if ($auth->acl_get('m_softdelete', $forum_id)) + if ($this->auth->acl_get('m_softdelete', $forum_id)) { return true; } - else if ($auth->acl_get('f_softdelete', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked) + else if ($this->auth->acl_get('f_softdelete', $forum_id) && $poster_id == $this->user->data['user_id'] && !$post_locked) { return true; } @@ -54,11 +105,9 @@ class phpbb_content_visibility * @param $forum_id int The forum id is used for permission checks * @return int Number of posts/topics the user can see in the topic/forum */ - static public function get_count($mode, $data, $forum_id) + public function get_count($mode, $data, $forum_id) { - global $auth; - - if (!$auth->acl_get('m_approve', $forum_id)) + if (!$this->auth->acl_get('m_approve', $forum_id)) { return (int) $data[$mode . '_approved']; } @@ -76,11 +125,9 @@ class phpbb_content_visibility * @param $table_alias string Table alias to prefix in SQL queries * @return string The appropriate combination SQL logic for topic/post_visibility */ - static public function get_visibility_sql($mode, $forum_id, $table_alias = '') + public function get_visibility_sql($mode, $forum_id, $table_alias = '') { - global $auth; - - if ($auth->acl_get('m_approve', $forum_id)) + if ($this->auth->acl_get('m_approve', $forum_id)) { return '1 = 1'; } @@ -99,13 +146,11 @@ class phpbb_content_visibility * @param $table_alias string Table alias to prefix in SQL queries * @return string The appropriate combination SQL logic for topic/post_visibility */ - static public function get_forums_visibility_sql($mode, $forum_ids = array(), $table_alias = '') + public function get_forums_visibility_sql($mode, $forum_ids = array(), $table_alias = '') { - global $auth, $db; - $where_sql = '('; - $approve_forums = array_intersect($forum_ids, array_keys($auth->acl_getf('m_approve', true))); + $approve_forums = array_intersect($forum_ids, array_keys($this->auth->acl_getf('m_approve', true))); if (sizeof($approve_forums)) { @@ -115,23 +160,23 @@ class phpbb_content_visibility if (!sizeof($forum_ids)) { // The user can see all posts/topics in all specified forums - return $db->sql_in_set($table_alias . 'forum_id', $approve_forums); + return $this->db->sql_in_set($table_alias . 'forum_id', $approve_forums); } else { // Moderator can view all posts/topics in some forums - $where_sql .= $db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ' OR '; + $where_sql .= $this->db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ' OR '; } } else { // The user is just a normal user return $table_alias . $mode . '_visibility = ' . ITEM_APPROVED . ' - AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids, false, true); + AND ' . $this->db->sql_in_set($table_alias . 'forum_id', $forum_ids, false, true); } $where_sql .= '(' . $table_alias . $mode . '_visibility = ' . ITEM_APPROVED . ' - AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids) . '))'; + AND ' . $this->db->sql_in_set($table_alias . 'forum_id', $forum_ids) . '))'; return $where_sql; } @@ -147,17 +192,15 @@ class phpbb_content_visibility * @param $table_alias string Table alias to prefix in SQL queries * @return string The appropriate combination SQL logic for topic/post_visibility */ - static public function get_global_visibility_sql($mode, $exclude_forum_ids = array(), $table_alias = '') + public function get_global_visibility_sql($mode, $exclude_forum_ids = array(), $table_alias = '') { - global $auth, $db; - $where_sqls = array(); - $approve_forums = array_diff(array_keys($auth->acl_getf('m_approve', true)), $exclude_forum_ids); + $approve_forums = array_diff(array_keys($this->auth->acl_getf('m_approve', true)), $exclude_forum_ids); if (sizeof($exclude_forum_ids)) { - $where_sqls[] = '(' . $db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true) . ' + $where_sqls[] = '(' . $this->db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true) . ' AND ' . $table_alias . $mode . '_visibility = ' . ITEM_APPROVED . ')'; } else @@ -167,7 +210,7 @@ class phpbb_content_visibility if (sizeof($approve_forums)) { - $where_sqls[] = $db->sql_in_set($table_alias . 'forum_id', $approve_forums); + $where_sqls[] = $this->db->sql_in_set($table_alias . 'forum_id', $approve_forums); return '(' . implode(' OR ', $where_sqls) . ')'; } @@ -192,10 +235,8 @@ class phpbb_content_visibility * @param $limit_delete_time mixed Limit updating per topic_id to a certain deletion time * @return array Changed post data, empty array if an error occured. */ - static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $limit_visibility = false, $limit_delete_time = false) + public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $limit_visibility = false, $limit_delete_time = false) { - global $db; - if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) { return array(); @@ -205,7 +246,7 @@ class phpbb_content_visibility { if (is_array($post_id)) { - $where_sql = $db->sql_in_set('post_id', array_map('intval', $post_id)); + $where_sql = $this->db->sql_in_set('post_id', array_map('intval', $post_id)); } else { @@ -233,12 +274,12 @@ class phpbb_content_visibility } $sql = 'SELECT poster_id, post_id, post_postcount, post_visibility - FROM ' . POSTS_TABLE . ' + FROM ' . $this->posts_table . ' WHERE ' . $where_sql; - $result = $db->sql_query($sql); + $result = $this->db->sql_query($sql); $post_ids = $poster_postcounts = $postcounts = $postcount_visibility = array(); - while ($row = $db->sql_fetchrow($result)) + while ($row = $this->db->sql_fetchrow($result)) { $post_ids[] = (int) $row['post_id']; @@ -263,7 +304,7 @@ class phpbb_content_visibility } } } - $db->sql_freeresult($result); + $this->db->sql_freeresult($result); if (empty($post_ids)) { @@ -277,10 +318,10 @@ class phpbb_content_visibility 'post_delete_reason' => truncate_string($reason, 255, 255, false), ); - $sql = 'UPDATE ' . POSTS_TABLE . ' - SET ' . $db->sql_build_array('UPDATE', $data) . ' - WHERE ' . $db->sql_in_set('post_id', $post_ids); - $db->sql_query($sql); + $sql = 'UPDATE ' . $this->posts_table . ' + SET ' . $this->db->sql_build_array('UPDATE', $data) . ' + WHERE ' . $this->db->sql_in_set('post_id', $post_ids); + $this->db->sql_query($sql); // Group the authors by post count, to reduce the number of queries foreach ($poster_postcounts as $poster_id => $num_posts) @@ -293,24 +334,24 @@ class phpbb_content_visibility { if ($visibility == ITEM_DELETED) { - $sql = 'UPDATE ' . USERS_TABLE . ' + $sql = 'UPDATE ' . $this->users_table . ' SET user_posts = 0 - WHERE ' . $db->sql_in_set('user_id', $poster_ids) . ' + WHERE ' . $this->db->sql_in_set('user_id', $poster_ids) . ' AND user_posts < ' . $num_posts; - $db->sql_query($sql); + $this->db->sql_query($sql); - $sql = 'UPDATE ' . USERS_TABLE . ' + $sql = 'UPDATE ' . $this->users_table . ' SET user_posts = user_posts - ' . $num_posts . ' - WHERE ' . $db->sql_in_set('user_id', $poster_ids) . ' + WHERE ' . $this->db->sql_in_set('user_id', $poster_ids) . ' AND user_posts >= ' . $num_posts; - $db->sql_query($sql); + $this->db->sql_query($sql); } else { - $sql = 'UPDATE ' . USERS_TABLE . ' + $sql = 'UPDATE ' . $this->users_table . ' SET user_posts = user_posts + ' . $num_posts . ' - WHERE ' . $db->sql_in_set('user_id', $poster_ids); - $db->sql_query($sql); + WHERE ' . $this->db->sql_in_set('user_id', $poster_ids); + $this->db->sql_query($sql); } } @@ -333,8 +374,7 @@ class phpbb_content_visibility { if (!function_exists('sync')) { - global $phpEx, $phpbb_root_path; - include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + include($this->phpbb_root_path . 'includes/functions_admin.' . $this->php_ext); } // ... so we need to use sync, if the first post is changed. @@ -409,15 +449,15 @@ class phpbb_content_visibility } // Update the number for replies and posts - $sql = 'UPDATE ' . TOPICS_TABLE . ' + $sql = 'UPDATE ' . $this->topics_table . ' SET ' . implode(', ', $topic_sql) . ' WHERE topic_id = ' . (int) $topic_id; - $db->sql_query($sql); + $this->db->sql_query($sql); - $sql = 'UPDATE ' . FORUMS_TABLE . ' + $sql = 'UPDATE ' . $this->forums_table . ' SET ' . implode(', ', $forum_sql) . ' WHERE forum_id = ' . (int) $forum_id; - $db->sql_query($sql); + $this->db->sql_query($sql); } } @@ -445,10 +485,8 @@ class phpbb_content_visibility * @param $force_update_all bool Force to update all posts within the topic * @return array Changed topic data, empty array if an error occured. */ - static public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason, $force_update_all = false) + public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason, $force_update_all = false) { - global $db; - if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED))) { return array(); @@ -457,11 +495,11 @@ class phpbb_content_visibility if (!$force_update_all) { $sql = 'SELECT topic_visibility, topic_delete_time - FROM ' . TOPICS_TABLE . ' + FROM ' . $this->topics_table . ' WHERE topic_id = ' . (int) $topic_id; - $result = $db->sql_query($sql); - $original_topic_data = $db->sql_fetchrow($result); - $db->sql_freeresult($result); + $result = $this->db->sql_query($sql); + $original_topic_data = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); if (!$original_topic_data) { @@ -478,12 +516,12 @@ class phpbb_content_visibility 'topic_delete_reason' => truncate_string($reason, 255, 255, false), ); - $sql = 'UPDATE ' . TOPICS_TABLE . ' - SET ' . $db->sql_build_array('UPDATE', $data) . ' + $sql = 'UPDATE ' . $this->topics_table . ' + SET ' . $this->db->sql_build_array('UPDATE', $data) . ' WHERE topic_id = ' . (int) $topic_id; - $db->sql_query($sql); + $this->db->sql_query($sql); - if (!$db->sql_affectedrows()) + if (!$this->db->sql_affectedrows()) { return array(); } @@ -513,15 +551,15 @@ class phpbb_content_visibility * @param $sql_data array Populated with the SQL changes, may be empty at call time * @return void */ - static public function add_post_to_statistic($data, &$sql_data) + public function add_post_to_statistic($data, &$sql_data) { - $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts_approved = topic_posts_approved + 1'; + $sql_data[$this->topics_table] = (($sql_data[$this->topics_table]) ? $sql_data[$this->topics_table] . ', ' : '') . 'topic_posts_approved = topic_posts_approved + 1'; - $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts_approved = forum_posts_approved + 1'; + $sql_data[$this->forums_table] = (($sql_data[$this->forums_table]) ? $sql_data[$this->forums_table] . ', ' : '') . 'forum_posts_approved = forum_posts_approved + 1'; if ($data['post_postcount']) { - $sql_data[USERS_TABLE] = (($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts + 1'; + $sql_data[$this->users_table] = (($sql_data[$this->users_table]) ? $sql_data[$this->users_table] . ', ' : '') . 'user_posts = user_posts + 1'; } set_config_count('num_posts', 1, true); @@ -534,14 +572,14 @@ class phpbb_content_visibility * @param $sql_data array Populated with the SQL changes, may be empty at call time * @return void */ - static public function remove_post_from_statistic($data, &$sql_data) + public function remove_post_from_statistic($data, &$sql_data) { - $sql_data[TOPICS_TABLE] = ((!empty($sql_data[TOPICS_TABLE])) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts_approved = topic_posts_approved - 1'; - $sql_data[FORUMS_TABLE] = ((!empty($sql_data[FORUMS_TABLE])) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts_approved = forum_posts_approved - 1'; + $sql_data[$this->topics_table] = ((!empty($sql_data[$this->topics_table])) ? $sql_data[$this->topics_table] . ', ' : '') . 'topic_posts_approved = topic_posts_approved - 1'; + $sql_data[$this->forums_table] = ((!empty($sql_data[$this->forums_table])) ? $sql_data[$this->forums_table] . ', ' : '') . 'forum_posts_approved = forum_posts_approved - 1'; if ($data['post_postcount']) { - $sql_data[USERS_TABLE] = ((!empty($sql_data[USERS_TABLE])) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1'; + $sql_data[$this->users_table] = ((!empty($sql_data[$this->users_table])) ? $sql_data[$this->users_table] . ', ' : '') . 'user_posts = user_posts - 1'; } set_config_count('num_posts', -1, true); @@ -556,60 +594,58 @@ class phpbb_content_visibility * @param $sql_data array Populated with the SQL changes, may be empty at call time * @return void */ - static public function remove_topic_from_statistic($topic_id, $forum_id, &$topic_row, &$sql_data) + public function remove_topic_from_statistic($topic_id, $forum_id, &$topic_row, &$sql_data) { - global $db; - // Do we need to grab some topic informations? if (!sizeof($topic_row)) { $sql = 'SELECT topic_type, topic_posts_approved, topic_posts_unapproved, topic_posts_softdeleted, topic_visibility - FROM ' . TOPICS_TABLE . ' + FROM ' . $this->topics_table . ' WHERE topic_id = ' . (int) $topic_id; - $result = $db->sql_query($sql); - $topic_row = $db->sql_fetchrow($result); - $db->sql_freeresult($result); + $result = $this->db->sql_query($sql); + $topic_row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); } // If this is an edited topic or the first post the topic gets completely disapproved later on... - $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_topics_approved = forum_topics_approved - 1'; - $sql_data[FORUMS_TABLE] .= ', forum_posts_approved = forum_posts_approved - ' . $topic_row['topic_posts_approved']; - $sql_data[FORUMS_TABLE] .= ', forum_posts_unapproved = forum_posts_unapproved - ' . $topic_row['topic_posts_unapproved']; - $sql_data[FORUMS_TABLE] .= ', forum_posts_softdeleted = forum_posts_softdeleted - ' . $topic_row['topic_posts_softdeleted']; + $sql_data[$this->forums_table] = (($sql_data[$this->forums_table]) ? $sql_data[$this->forums_table] . ', ' : '') . 'forum_topics_approved = forum_topics_approved - 1'; + $sql_data[$this->forums_table] .= ', forum_posts_approved = forum_posts_approved - ' . $topic_row['topic_posts_approved']; + $sql_data[$this->forums_table] .= ', forum_posts_unapproved = forum_posts_unapproved - ' . $topic_row['topic_posts_unapproved']; + $sql_data[$this->forums_table] .= ', forum_posts_softdeleted = forum_posts_softdeleted - ' . $topic_row['topic_posts_softdeleted']; set_config_count('num_topics', -1, true); set_config_count('num_posts', $topic_row['topic_posts_approved'] * (-1), true); // Get user post count information $sql = 'SELECT poster_id, COUNT(post_id) AS num_posts - FROM ' . POSTS_TABLE . ' + FROM ' . $this->posts_table . ' WHERE topic_id = ' . (int) $topic_id . ' AND post_postcount = 1 AND post_visibility = ' . ITEM_APPROVED . ' GROUP BY poster_id'; - $result = $db->sql_query($sql); + $result = $this->db->sql_query($sql); $postcounts = array(); - while ($row = $db->sql_fetchrow($result)) + while ($row = $this->db->sql_fetchrow($result)) { $postcounts[(int) $row['num_posts']][] = (int) $row['poster_id']; } - $db->sql_freeresult($result); + $this->db->sql_freeresult($result); // Decrement users post count foreach ($postcounts as $num_posts => $poster_ids) { - $sql = 'UPDATE ' . USERS_TABLE . ' + $sql = 'UPDATE ' . $this->users_table . ' SET user_posts = 0 WHERE user_posts < ' . $num_posts . ' - AND ' . $db->sql_in_set('user_id', $poster_ids); - $db->sql_query($sql); + AND ' . $this->db->sql_in_set('user_id', $poster_ids); + $this->db->sql_query($sql); - $sql = 'UPDATE ' . USERS_TABLE . ' + $sql = 'UPDATE ' . $this->users_table . ' SET user_posts = user_posts - ' . $num_posts . ' WHERE user_posts >= ' . $num_posts . ' - AND ' . $db->sql_in_set('user_id', $poster_ids); - $db->sql_query($sql); + AND ' . $this->db->sql_in_set('user_id', $poster_ids); + $this->db->sql_query($sql); } } } -- cgit v1.2.1 From 753dc62267118e44b16653113521fe6d0a360720 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 11 Jul 2013 15:02:07 +0200 Subject: [ticket/9657] Fix unit tests PHPBB3-9657 --- phpBB/includes/content_visibility.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index edf6aa3b31..43efef5d7f 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -26,19 +26,19 @@ class phpbb_content_visibility * Database object * @var phpbb_db_driver */ - protected $this->db; + protected $db; /** * User object * @var phpbb_user */ - protected $this->user; + protected $user; /** * Auth object * @var phpbb_auth */ - protected $this->auth; + protected $auth; /** * phpBB root path @@ -55,18 +55,18 @@ class phpbb_content_visibility /** * Constructor * - * @param phpbb_auth $this->auth Auth object - * @param phpbb_db_driver $this->db Database object - * @param phpbb_user $this->user User object + * @param phpbb_auth $auth Auth object + * @param phpbb_db_driver $db Database object + * @param phpbb_user $user User object * @param string $phpbb_root_path Root path * @param string $php_ext PHP Extension * @return null */ - public function __construct($this->auth, phpbb_db_driver $this->db, $this->user, $phpbb_root_path, $phpEx, $forums_table, $posts_table, $topics_table, $users_table) + public function __construct($auth, phpbb_db_driver $db, $user, $phpbb_root_path, $phpEx, $forums_table, $posts_table, $topics_table, $users_table) { - $this->auth = $this->auth; - $this->db = $this->db; - $this->user = $this->user; + $this->auth = $auth; + $this->db = $db; + $this->user = $user; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; $this->forums_table = $forums_table; -- cgit v1.2.1 From 0e5e128174f862f16069b56ef6f5b9803fe5cb9b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 11 Jul 2013 18:02:45 -0400 Subject: [ticket/9657] Fix variable naming of phpEx to php_ext PHPBB3-9657 --- phpBB/includes/content_visibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 43efef5d7f..01e688d106 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -62,7 +62,7 @@ class phpbb_content_visibility * @param string $php_ext PHP Extension * @return null */ - public function __construct($auth, phpbb_db_driver $db, $user, $phpbb_root_path, $phpEx, $forums_table, $posts_table, $topics_table, $users_table) + public function __construct($auth, phpbb_db_driver $db, $user, $phpbb_root_path, $php_ext, $forums_table, $posts_table, $topics_table, $users_table) { $this->auth = $auth; $this->db = $db; -- cgit v1.2.1 From 003a104f931201704998f5c00c5cf982896b470b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 11 Jul 2013 22:41:24 -0400 Subject: [ticket/9657] Add type hints for classes PHPBB3-9657 --- phpBB/includes/content_visibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/content_visibility.php') diff --git a/phpBB/includes/content_visibility.php b/phpBB/includes/content_visibility.php index 01e688d106..4ad5f6793e 100644 --- a/phpBB/includes/content_visibility.php +++ b/phpBB/includes/content_visibility.php @@ -62,7 +62,7 @@ class phpbb_content_visibility * @param string $php_ext PHP Extension * @return null */ - public function __construct($auth, phpbb_db_driver $db, $user, $phpbb_root_path, $php_ext, $forums_table, $posts_table, $topics_table, $users_table) + public function __construct(phpbb_auth $auth, phpbb_db_driver $db, phpbb_user $user, $phpbb_root_path, $php_ext, $forums_table, $posts_table, $topics_table, $users_table) { $this->auth = $auth; $this->db = $db; -- cgit v1.2.1