[feature/soft-delete] I told you I was going to rename the class!

Rename topic_visibility class to phpbb_visibility. Also a bit of work to the class itself, mostly cleanup and adding the comments that I'd previously written.

PHPBB3-9657
This commit is contained in:
Josh Woody 2010-06-20 15:01:26 -05:00 committed by Joas Schilling
parent 244f6e2ddc
commit c32d760806
9 changed files with 82 additions and 43 deletions

View file

@ -760,7 +760,7 @@ class phpbb_feed_overall extends phpbb_feed_post_base
FROM ' . TOPICS_TABLE . ' FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . ' WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
AND topic_moved_id = 0 AND topic_moved_id = 0
AND ' . topic_visibility::get_visibility_sql_global('topic') . ' AND ' . phpbb_visibility::get_visibility_sql_global('topic') . '
ORDER BY topic_last_post_time DESC'; ORDER BY topic_last_post_time DESC';
$result = $db->sql_query_limit($sql, $this->num_items); $result = $db->sql_query_limit($sql, $this->num_items);
@ -795,7 +795,7 @@ class phpbb_feed_overall extends phpbb_feed_post_base
), ),
), ),
'WHERE' => $db->sql_in_set('p.topic_id', $topic_ids) . ' 'WHERE' => $db->sql_in_set('p.topic_id', $topic_ids) . '
AND ' . topic_visibility::get_visibility_sql('post', array(), 'p.') . ' AND ' . phpbb_visibility::get_visibility_sql('post', array(), 'p.') . '
AND p.post_time >= ' . $min_post_time . ' AND p.post_time >= ' . $min_post_time . '
AND u.user_id = p.poster_id', AND u.user_id = p.poster_id',
'ORDER_BY' => 'p.post_time DESC', 'ORDER_BY' => 'p.post_time DESC',
@ -892,7 +892,7 @@ class phpbb_feed_forum extends phpbb_feed_post_base
FROM ' . TOPICS_TABLE . ' FROM ' . TOPICS_TABLE . '
WHERE forum_id = ' . $this->forum_id . ' WHERE forum_id = ' . $this->forum_id . '
AND topic_moved_id = 0 AND topic_moved_id = 0
AND ' . topic_visibility::get_visibility_sql('topic', $this->forum_id) . ' AND ' . phpbb_visibility::get_visibility_sql('topic', $this->forum_id) . '
ORDER BY topic_last_post_time DESC'; ORDER BY topic_last_post_time DESC';
$result = $db->sql_query_limit($sql, $this->num_items); $result = $db->sql_query_limit($sql, $this->num_items);
@ -919,7 +919,7 @@ class phpbb_feed_forum extends phpbb_feed_post_base
USERS_TABLE => 'u', USERS_TABLE => 'u',
), ),
'WHERE' => $db->sql_in_set('p.topic_id', $topic_ids) . ' 'WHERE' => $db->sql_in_set('p.topic_id', $topic_ids) . '
AND ' . topic_visibility::get_visibility_sql('post', $this->forum_id, 'p.') . ' AND ' . phpbb_visibility::get_visibility_sql('post', $this->forum_id, 'p.') . '
AND p.post_time >= ' . $min_post_time . ' AND p.post_time >= ' . $min_post_time . '
AND p.poster_id = u.user_id', AND p.poster_id = u.user_id',
'ORDER_BY' => 'p.post_time DESC', 'ORDER_BY' => 'p.post_time DESC',
@ -1025,7 +1025,7 @@ class phpbb_feed_topic extends phpbb_feed_post_base
USERS_TABLE => 'u', USERS_TABLE => 'u',
), ),
'WHERE' => 'p.topic_id = ' . $this->topic_id . ' 'WHERE' => 'p.topic_id = ' . $this->topic_id . '
AND ' . topic_visibility::get_visibility_sql('post', $this->forum_id, 'p.') . ' AND ' . phpbb_visibility::get_visibility_sql('post', $this->forum_id, 'p.') . '
AND p.poster_id = u.user_id', AND p.poster_id = u.user_id',
'ORDER_BY' => 'p.post_time DESC', 'ORDER_BY' => 'p.post_time DESC',
); );

View file

@ -1,7 +1,35 @@
<?php <?php
/**
*
* @package phpbb
* @version $Id$
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class topic_visibility /**
* @ignore
*/
if (!defined('IN_PHPBB'))
{ {
exit;
}
/**
* phpbb_visibility
* Handle fetching and setting the visibility for topics and posts
* @package phpbb
*/
class phpbb_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
*/
public function get_visibility_sql($mode, $forum_id, $table_alias = '') public function get_visibility_sql($mode, $forum_id, $table_alias = '')
{ {
global $auth, $db, $user; global $auth, $db, $user;
@ -31,6 +59,13 @@ class topic_visibility
return $clause; 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
*/
public function get_visibility_sql_global($mode, $exclude_forum_ids = array(), $table_alias = '') public function get_visibility_sql_global($mode, $exclude_forum_ids = array(), $table_alias = '')
{ {
global $auth, $db, $user; global $auth, $db, $user;
@ -70,6 +105,14 @@ class topic_visibility
return $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
*/
public function set_topic_visibility($visibility, $topic_id, $forum_id) public function set_topic_visibility($visibility, $topic_id, $forum_id)
{ {
global $db; global $db;
@ -78,31 +121,32 @@ class topic_visibility
WHERE topic_id = ' . (int) $topic_id; WHERE topic_id = ' . (int) $topic_id;
$db->sql_query($sql); $db->sql_query($sql);
if ($visibility != ITEM_APPROVED) // if we're approving, disapproving, or deleteing a topic, assume that
{ // we are adjusting _all_ posts in that topic.
$sql = 'SELECT post_id FROM ' . POSTS_TABLE . ' $status = self::set_post_visibility($visibility, false, $topic_id, $forum_id, true, true);
WHERE topic_id = ' . (int) $topic_id;
$result = $db->sql_query($sql);
$status = true;
while ($row = $db->sql_fetchrow($result))
{
$status = min($status, self::set_post_visibility($visibility, false, $topic_id, $forum_id, true, true));
}
}
else
{
// TOOD: figure out which posts we actually care about
$status = self::set_post_visibility($visibility, 0, false, $forum_id, true, true);
}
return $status; 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
*/
public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $is_starter, $is_latest) public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $is_starter, $is_latest)
{ {
global $db; 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) if ($post_id)
{ {
$where_sql = 'post_id = ' . (int) $post_id; $where_sql = 'post_id = ' . (int) $post_id;
@ -121,17 +165,12 @@ class topic_visibility
WHERE ' . $where_sql; WHERE ' . $where_sql;
$db->sql_query($sql); $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('topic', $topic_id, false); update_post_information('topic', $topic_id, false);
update_post_information('forum', $forum_id, false); update_post_information('forum', $forum_id, false);
} }
// if we're changing the starter, we need to change the rest of the topic
if ($is_starter && !$is_latest)
{
self::set_topic_visibility($visibility, $topic_id, $forum_id);
}
} }
} }
?> ?>

View file

@ -993,7 +993,7 @@ function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id
$sql = 'SELECT p.post_id $sql = 'SELECT p.post_id
FROM ' . POSTS_TABLE . ' p' . " FROM ' . POSTS_TABLE . ' p' . "
WHERE p.topic_id = $topic_id WHERE p.topic_id = $topic_id
AND " . topic_visibility::get_visibility_sql('post', $forum_id, 'p.') . ' AND " . phpbb_visibility::get_visibility_sql('post', $forum_id, 'p.') . '
' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . ' ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . ' ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
ORDER BY p.post_time '; ORDER BY p.post_time ';
@ -1542,7 +1542,7 @@ function delete_post($forum_id, $topic_id, $post_id, &$data)
$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 " . topic_visibility::get_visibility_sql('post', $forum_id); AND " . phpbb_visibility::get_visibility_sql('post', $forum_id);
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result); $row = $db->sql_fetchrow($result);
$db->sql_freeresult($result); $db->sql_freeresult($result);
@ -1555,7 +1555,7 @@ function delete_post($forum_id, $topic_id, $post_id, &$data)
$sql = 'SELECT post_id $sql = 'SELECT post_id
FROM ' . POSTS_TABLE . " FROM ' . POSTS_TABLE . "
WHERE topic_id = $topic_id WHERE topic_id = $topic_id
AND " . topic_visibility::get_visibility_sql('post', $forum_id) . ' AND " . phpbb_visibility::get_visibility_sql('post', $forum_id) . '
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);

View file

@ -154,7 +154,7 @@ function mcp_forum_view($id, $mode, $action, $forum_info)
$sql = 'SELECT t.topic_id $sql = 'SELECT t.topic_id
FROM ' . TOPICS_TABLE . ' t FROM ' . TOPICS_TABLE . ' t
WHERE t.forum_id = ' . $forum_id . ' WHERE t.forum_id = ' . $forum_id . '
' . topic_visibility::get_visibility_sql('topic', $forum_id, 't.') . " ' . phpbb_visibility::get_visibility_sql('topic', $forum_id, 't.') . "
$limit_time_sql $limit_time_sql
ORDER BY t.topic_type DESC, $sort_order_sql"; ORDER BY t.topic_type DESC, $sort_order_sql";
$result = $db->sql_query_limit($sql, $topics_per_page, $start); $result = $db->sql_query_limit($sql, $topics_per_page, $start);

View file

@ -146,7 +146,7 @@ function mcp_topic_view($id, $mode, $action)
FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
WHERE ' . (($action == 'reports') ? 'p.post_reported = 1 AND ' : '') . ' WHERE ' . (($action == 'reports') ? 'p.post_reported = 1 AND ' : '') . '
p.topic_id = ' . $topic_id . ' p.topic_id = ' . $topic_id . '
AND ' . topic_visibility::get_visibility_sql('post', $topic_info['forum_id'], 'p.') . ' AND ' . phpbb_visibility::get_visibility_sql('post', $topic_info['forum_id'], 'p.') . '
AND p.poster_id = u.user_id ' . AND p.poster_id = u.user_id ' .
$limit_time_sql . ' $limit_time_sql . '
ORDER BY ' . $sort_order_sql; ORDER BY ' . $sort_order_sql;

View file

@ -87,7 +87,7 @@ switch ($mode)
FROM ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f FROM ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f
WHERE t.topic_id = $topic_id WHERE t.topic_id = $topic_id
AND f.forum_id = t.forum_id AND f.forum_id = t.forum_id
AND " . topic_visibility::get_visibility_sql('topic', $forum_id, 't.'); AND " . phpbb_visibility::get_visibility_sql('topic', $forum_id, 't.');
break; break;
case 'quote': case 'quote':
@ -115,7 +115,7 @@ switch ($mode)
AND t.topic_id = p.topic_id AND t.topic_id = p.topic_id
AND u.user_id = p.poster_id AND u.user_id = p.poster_id
AND f.forum_id = t.forum_id AND f.forum_id = t.forum_id
AND " . topic_visibility::get_visibility_sql('topic', $forum_id, 't.'); AND " . phpbb_visibility::get_visibility_sql('topic', $forum_id, 't.');
break; break;
case 'smilies': case 'smilies':

View file

@ -266,7 +266,7 @@ if ($keywords || $author || $author_id || $search_id || $submit)
$m_approve_fid_sql = ' AND p.post_approved = 1'; $m_approve_fid_sql = ' AND p.post_approved = 1';
} }
*/ */
$m_approve_fid_sql = ' AND ' . topic_visibility::get_visibility_sql_global('post', $ex_fid_ary, 'p.'); $m_approve_fid_sql = ' AND ' . phpbb_visibility::get_visibility_sql_global('post', $ex_fid_ary, 'p.');
if ($reset_search_forum) if ($reset_search_forum)
{ {

View file

@ -241,7 +241,7 @@ if ($sort_days)
AND (topic_last_post_time >= $min_post_time AND (topic_last_post_time >= $min_post_time
OR topic_type = " . POST_ANNOUNCE . ' OR topic_type = " . POST_ANNOUNCE . '
OR topic_type = ' . POST_GLOBAL . ') OR topic_type = ' . POST_GLOBAL . ')
AND ' . topic_visibility::get_visibility_sql('topic', $forum_id); AND ' . phpbb_visibility::get_visibility_sql('topic', $forum_id);
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$topics_count = (int) $db->sql_fetchfield('num_topics'); $topics_count = (int) $db->sql_fetchfield('num_topics');
$db->sql_freeresult($result); $db->sql_freeresult($result);
@ -353,7 +353,7 @@ $sql_array = array(
'LEFT_JOIN' => array(), 'LEFT_JOIN' => array(),
); );
$sql_approved = 'AND ' . topic_visibility::get_visibility_sql('topic', $forum_id, 't.'); $sql_approved = 'AND ' . phpbb_visibility::get_visibility_sql('topic', $forum_id, 't.');
if ($user->data['is_registered']) if ($user->data['is_registered'])
{ {

View file

@ -83,7 +83,7 @@ if ($view && !$post_id)
$sql = 'SELECT post_id, topic_id, forum_id $sql = 'SELECT post_id, topic_id, forum_id
FROM ' . POSTS_TABLE . " FROM ' . POSTS_TABLE . "
WHERE topic_id = $topic_id WHERE topic_id = $topic_id
AND " . topic_visibility::get_visibility_sql('post', $forum_id) . " AND " . phpbb_visibility::get_visibility_sql('post', $forum_id) . "
AND post_time > $topic_last_read AND post_time > $topic_last_read
AND forum_id = $forum_id AND forum_id = $forum_id
ORDER BY post_time ASC"; ORDER BY post_time ASC";
@ -137,7 +137,7 @@ if ($view && !$post_id)
WHERE forum_id = ' . $row['forum_id'] . " WHERE forum_id = ' . $row['forum_id'] . "
AND topic_moved_id = 0 AND topic_moved_id = 0
AND topic_last_post_time $sql_condition {$row['topic_last_post_time']} AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
AND" . topic_visibility::get_visibility_sql('topic', $row['forum_id']) . " AND" . phpbb_visibility::get_visibility_sql('topic', $row['forum_id']) . "
ORDER BY topic_last_post_time $sql_ordering"; ORDER BY topic_last_post_time $sql_ordering";
$result = $db->sql_query_limit($sql, 1); $result = $db->sql_query_limit($sql, 1);
$row = $db->sql_fetchrow($result); $row = $db->sql_fetchrow($result);
@ -277,7 +277,7 @@ if ($post_id)
$sql = 'SELECT COUNT(p.post_id) AS prev_posts $sql = 'SELECT COUNT(p.post_id) AS prev_posts
FROM ' . POSTS_TABLE . " p FROM ' . POSTS_TABLE . " p
WHERE p.topic_id = {$topic_data['topic_id']} WHERE p.topic_id = {$topic_data['topic_id']}
" . topic_visibility::get_visibility_sql('post', $forum_id, 'p'); " . phpbb_visibility::get_visibility_sql('post', $forum_id, 'p');
if ($sort_dir == 'd') if ($sort_dir == 'd')
{ {
@ -408,7 +408,7 @@ if ($sort_days)
FROM ' . POSTS_TABLE . " FROM ' . POSTS_TABLE . "
WHERE topic_id = $topic_id WHERE topic_id = $topic_id
AND post_time >= $min_post_time AND post_time >= $min_post_time
AND " . topic_visibility::get_visibility_sql('post', $forum_id); AND " . phpbb_visibility::get_visibility_sql('post', $forum_id);
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$total_posts = (int) $db->sql_fetchfield('num_posts'); $total_posts = (int) $db->sql_fetchfield('num_posts');
$db->sql_freeresult($result); $db->sql_freeresult($result);
@ -944,7 +944,7 @@ $i = $i_total = 0;
$sql = 'SELECT p.post_id $sql = 'SELECT p.post_id
FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . " FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . "
WHERE p.topic_id = $topic_id WHERE p.topic_id = $topic_id
AND " . topic_visibility::get_visibility_sql('post', $forum_id, 'p.') . " AND " . phpbb_visibility::get_visibility_sql('post', $forum_id, 'p.') . "
" . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . " " . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
$limit_posts_time $limit_posts_time
ORDER BY $sql_sort_order"; ORDER BY $sql_sort_order";