[feature/soft-delete] Update and simplify the logic on delete_post()

Todo: delete_topic case

PHPBB3-9567
This commit is contained in:
Joas Schilling 2012-10-06 03:59:49 +02:00
parent 3088855aa6
commit 009bd698fb
2 changed files with 49 additions and 58 deletions

View file

@ -359,28 +359,22 @@ class phpbb_content_visibility
} }
/** /**
* Do the required math to hide a single post (going from approved to deleted) * Remove post from topic and forum statistics
* 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 $forum_id int Forum where the topic is found
* @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 $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
* @param $sql_data - array - populated with the SQL changes, may be empty at call time
* @return void * @return void
*/ */
//static public function remove_post_from_postcount($forum_id, $current_time, $data, &$sql_data) static public function remove_post_from_postcount($forum_id, $data, &$sql_data)
static public function hide_post($forum_id, $current_time, $data, &$sql_data)
{ {
$sql_data[TOPICS_TABLE] = 'topic_last_view_time = ' . $current_time; $sql_data[TOPICS_TABLE] = ($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies - 1';
if ($data['topic_replies'] > 0)
{
$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']) 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); set_config_count('num_posts', -1, true);
@ -539,6 +533,7 @@ class phpbb_content_visibility
if ($total_topics) if ($total_topics)
{ {
//@todo: plurals!
$success_msg = ($total_topics == 1) ? 'TOPIC_APPROVED_SUCCESS' : 'TOPICS_APPROVED_SUCCESS'; $success_msg = ($total_topics == 1) ? 'TOPIC_APPROVED_SUCCESS' : 'TOPICS_APPROVED_SUCCESS';
} }
else else

View file

@ -1422,14 +1422,10 @@ function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $
{ {
$post_mode = 'delete_topic'; $post_mode = 'delete_topic';
} }
else if ($data['topic_first_post_id'] == $post_id && !$is_soft) else if ($data['topic_first_post_id'] == $post_id)
{ {
$post_mode = 'delete_first_post'; $post_mode = 'delete_first_post';
} }
else if ($data['topic_first_post_id'] == $post_id && $is_soft)
{
$post_mode = 'delete_topic';
}
else if ($data['topic_last_post_id'] == $post_id) else if ($data['topic_last_post_id'] == $post_id)
{ {
$post_mode = 'delete_last_post'; $post_mode = 'delete_last_post';
@ -1464,10 +1460,10 @@ function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $
$db->sql_freeresult($result); $db->sql_freeresult($result);
} }
// (Soft) delete the post
if ($is_soft) if ($is_soft)
{ {
phpbb_content_visibility::set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user->data['user_id'], time(), $softdelete_reason, ($data['topic_first_post_id'] == $post_id), ($data['topic_last_post_id'] == $post_id)); phpbb_content_visibility::set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user->data['user_id'], time(), $softdelete_reason, ($data['topic_first_post_id'] == $post_id), ($data['topic_last_post_id'] == $post_id));
phpbb_content_visibility::hide_post($forum_id, time(), $data, $sql_data);
} }
else else
{ {
@ -1535,53 +1531,45 @@ function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $
$row = $db->sql_fetchrow($result); $row = $db->sql_fetchrow($result);
$db->sql_freeresult($result); $db->sql_freeresult($result);
$sql_data[FORUMS_TABLE] = ($data['post_visibility'] == ITEM_APPROVED) ? 'forum_posts = forum_posts - 1' : '';
$sql_data[TOPICS_TABLE] = 'topic_poster = ' . intval($row['poster_id']) . ', topic_first_post_id = ' . intval($row['post_id']) . ", topic_first_poster_colour = '" . $db->sql_escape($row['user_colour']) . "', topic_first_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "', topic_time = " . (int) $row['post_time'];
// Decrementing topic_replies here is fine because this case only happens if there is more than one post within the topic - basically removing one "reply"
$sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_visibility'] == ITEM_APPROVED) ? ', topic_replies = topic_replies - 1' : '');
$next_post_id = (int) $row['post_id']; $next_post_id = (int) $row['post_id'];
$sql_data[TOPICS_TABLE] = $db->sql_build_array('UPDATE', array(
'topic_poster' => (int) $row['poster_id'],
'topic_first_post_id' => (int) $row['post_id']
'topic_first_poster_colour' => $row['user_colour'],
'topic_first_poster_name' => ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'],
'topic_time' => (int) $row['post_time'],
));
break; break;
case 'delete_last_post': case 'delete_last_post':
if ($is_soft) if (!$is_soft)
{ {
phpbb_content_visibility::set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user->data['user_id'], time(), $softdelete_reason, false, true);
phpbb_content_visibility::hide_post($forum_id, time(), $data, $sql_data);
}
else
{
$sql_data[FORUMS_TABLE] = ($data['post_visibility'] == ITEM_APPROVED) ? 'forum_posts = forum_posts - 1' : '';
$update_sql = update_post_information('forum', $forum_id, true); $update_sql = update_post_information('forum', $forum_id, true);
if (sizeof($update_sql)) if (sizeof($update_sql))
{ {
$sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : ''; $sql_data[FORUMS_TABLE] = ($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . implode(', ', $update_sql[$forum_id]);
$sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
} }
$sql_data[TOPICS_TABLE] = 'topic_bumped = 0, topic_bumper = 0, topic_replies_real = topic_replies_real - 1' . (($data['post_visibility'] == ITEM_APPROVED) ? ', topic_replies = topic_replies - 1' : ''); $sql_data[TOPICS_TABLE] = ($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_bumped = 0, topic_bumper = 0';
}
$update_sql = update_post_information('topic', $topic_id, true); $update_sql = update_post_information('topic', $topic_id, true);
if (sizeof($update_sql) && !$is_soft) if (!empty($update_sql))
{ {
$sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]); $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
$next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]); $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
} }
else }
if (!$next_post_id)
{ {
$sql = 'SELECT MAX(post_id) as last_post_id $sql = 'SELECT MAX(post_id) as last_post_id
FROM ' . POSTS_TABLE . " FROM ' . POSTS_TABLE . "
WHERE topic_id = $topic_id WHERE topic_id = $topic_id
AND " . phpbb_content_visibility::get_visibility_sql('post', $forum_id); AND " . phpbb_content_visibility::get_visibility_sql('post', $forum_id);
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result); $next_post_id = (int) $db->sql_fetchfield('last_post_id');
$db->sql_freeresult($result); $db->sql_freeresult($result);
$next_post_id = (int) $row['last_post_id'];
} }
break; break;
@ -1593,16 +1581,24 @@ function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $
AND post_time > ' . $data['post_time'] . ' AND post_time > ' . $data['post_time'] . '
ORDER BY post_time ASC'; ORDER BY post_time ASC';
$result = $db->sql_query_limit($sql, 1); $result = $db->sql_query_limit($sql, 1);
$row = $db->sql_fetchrow($result); $next_post_id = (int) $db->sql_fetchfield('post_id');
$db->sql_freeresult($result); $db->sql_freeresult($result);
$sql_data[FORUMS_TABLE] = ($data['post_visibility'] == ITEM_APPROVED) ? 'forum_posts = forum_posts - 1' : '';
$sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_visibility'] == ITEM_APPROVED) ? ', topic_replies = topic_replies - 1' : '');
$next_post_id = (int) $row['post_id'];
break; break;
} }
if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
{
if ($data['post_visibility'] == ITEM_APPROVED)
{
phpbb_content_visibility::remove_post_from_postcount($forum_id, $data, $sql_data);
}
if (!$is_soft)
{
$sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1';
}
}
if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post')) if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
{ {
$sql = 'SELECT 1 AS has_attachments $sql = 'SELECT 1 AS has_attachments
@ -1625,7 +1621,7 @@ function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $
$where_sql = array( $where_sql = array(
FORUMS_TABLE => "forum_id = $forum_id", FORUMS_TABLE => "forum_id = $forum_id",
TOPICS_TABLE => "topic_id = $topic_id", TOPICS_TABLE => "topic_id = $topic_id",
USERS_TABLE => 'user_id = ' . $data['poster_id'] USERS_TABLE => 'user_id = ' . $data['poster_id'],
); );
foreach ($sql_data as $table => $update_sql) foreach ($sql_data as $table => $update_sql)
@ -1960,7 +1956,7 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $u
// Correctly set back the topic replies and forum posts... but only if the post was approved before. // Correctly set back the topic replies and forum posts... but only if the post was approved before.
if (!$post_approval && $data['post_visibility'] == ITEM_APPROVED) if (!$post_approval && $data['post_visibility'] == ITEM_APPROVED)
{ {
//phpbb_content_visibility::hide_post($forum_id, $current_time, $sql_data); //phpbb_content_visibility::remove_post_from_postcount($forum_id, $current_time, $sql_data);
// ^^ hide_post SQL is identical, except that it does not include the ['stat'] sub-array // ^^ hide_post SQL is identical, except that it does not include the ['stat'] sub-array
$sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1, topic_last_view_time = ' . $current_time; $sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1, topic_last_view_time = ' . $current_time;
$sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1'; $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1';
@ -2302,7 +2298,7 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $u
} }
} }
} }
else if (!$data['post_visibility'] == ITEM_APPROVED && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))) else if ($data['post_visibility'] != ITEM_APPROVED && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies'])))
{ {
// like having the rug pulled from under us // like having the rug pulled from under us
$sql = 'SELECT MAX(post_id) as last_post_id $sql = 'SELECT MAX(post_id) as last_post_id