Skip to content

Commit

Permalink
Merge pull request #690 from eurich/merge_topics_controller
Browse files Browse the repository at this point in the history
Merge topics controller
  • Loading branch information
norv committed Jul 18, 2013
2 parents b83023f + fd4de26 commit 0d31015
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 77 deletions.
71 changes: 8 additions & 63 deletions sources/controllers/MergeTopics.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public function action_mergeIndex()
global $txt, $board, $context;
global $scripturl, $user_info, $modSettings;

$db = database();

if (!isset($_GET['from']))
fatal_lang_error('no_access', false);
$_GET['from'] = (int) $_GET['from'];
Expand All @@ -82,24 +80,13 @@ public function action_mergeIndex()
$onlyApproved = false;

// How many topics are on this board? (used for paging.)
$request = $db->query('', '
SELECT COUNT(*)
FROM {db_prefix}topics AS t
WHERE t.id_board = {int:id_board}' . ($onlyApproved ? '
AND t.approved = {int:is_approved}' : ''),
array(
'id_board' => $_REQUEST['targetboard'],
'is_approved' => 1,
)
);
list ($topiccount) = $db->fetch_row($request);
$db->free_result($request);
require_once(SOURCEDIR . '/Topic.subs.php');
$topiccount = countTopicsByBoard($_REQUEST['targetboard'], $onlyApproved);

// Make the page list.
$context['page_index'] = constructPageIndex($scripturl . '?action=mergetopics;from=' . $_GET['from'] . ';targetboard=' . $_REQUEST['targetboard'] . ';board=' . $board . '.%1$d', $_REQUEST['start'], $topiccount, $modSettings['defaultMaxTopics'], true);

// Get the topic's subject.
require_once(SUBSDIR . '/Topic.subs.php');
$topic_info = getTopicInfo($_GET['from'], 'message');

// @todo review: double check the logic
Expand Down Expand Up @@ -138,43 +125,7 @@ public function action_mergeIndex()
}

// Get some topics to merge it with.
$request = $db->query('', '
SELECT t.id_topic, m.subject, m.id_member, IFNULL(mem.real_name, m.poster_name) AS poster_name
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE t.id_board = {int:id_board}
AND t.id_topic != {int:id_topic}' . ($onlyApproved ? '
AND t.approved = {int:is_approved}' : '') . '
ORDER BY {raw:sort}
LIMIT {int:offset}, {int:limit}',
array(
'id_board' => $_REQUEST['targetboard'],
'id_topic' => $_GET['from'],
'sort' => (!empty($modSettings['enableStickyTopics']) ? 't.is_sticky DESC, ' : '') . 't.id_last_msg DESC',
'offset' => $_REQUEST['start'],
'limit' => $modSettings['defaultMaxTopics'],
'is_approved' => 1,
)
);
$context['topics'] = array();
while ($row = $db->fetch_assoc($request))
{
censorText($row['subject']);

$context['topics'][] = array(
'id' => $row['id_topic'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" target="_blank" class="new_win">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'js_subject' => addcslashes(addslashes($row['subject']), '/')
);
}
$db->free_result($request);
$context['topics'] = mergeableTopics($_REQUEST['targetboard'], $_GET['from'], $onlyApproved, $_REQUEST['start']);

if (empty($context['topics']) && count($context['boards']) <= 1)
fatal_lang_error('merge_need_more_topics');
Expand Down Expand Up @@ -208,6 +159,8 @@ public function action_mergeExecute($topics = array())
// Check the session.
checkSession('request');

require_once(SUBSDIR . '/Topic.subs.php');

// Handle URLs from action_mergeIndex.
if (!empty($_GET['from']) && !empty($_GET['to']))
$topics = array((int) $_GET['from'], (int) $_GET['to']);
Expand Down Expand Up @@ -512,17 +465,7 @@ public function action_mergeExecute($topics = array())
$db->free_result($request);

// Obtain all the message ids we are going to affect.
$affected_msgs = array();
$request = $db->query('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic IN ({array_int:topic_list})',
array(
'topic_list' => $topics,
));
while ($row = $db->fetch_row($request))
$affected_msgs[] = $row[0];
$db->free_result($request);
$affected_msgs = messagesInTopics($topics);

// Assign the first topic ID to be the merged topic.
$id_topic = min($topics);
Expand Down Expand Up @@ -591,6 +534,8 @@ public function action_mergeExecute($topics = array())
cache_put_data('response_prefix', $context['response_prefix'], 600);
}

$_POST['enforce_subject'] = isset($_POST['enforce_subject']) ? Util::htmlspecialchars(trim($_POST['enforce_subject'])): '';

// Change the topic IDs of all messages that will be merged. Also adjust subjects if 'enforce subject' was checked.
$db->query('', '
UPDATE {db_prefix}messages
Expand Down
126 changes: 112 additions & 14 deletions sources/subs/Topic.subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,7 @@ function removeTopics($topics, $decreasePostCount = true, $ignoreRecycling = fal

// Reuse the message array if available
if (empty($messages))
{
$messages = array();
$request = $db->query('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic IN ({array_int:topics})',
array(
'topics' => $topics,
)
);
while ($row = $db->fetch_row($request))
$messages[] = $row[0];
$db->free_result($request);
}
$messages = messagesInTopics($topics);

// Remove all likes now that the topic is gone
$db->query('', '
Expand Down Expand Up @@ -2488,3 +2475,114 @@ function postersCount($id_topic)

return $posters;
}

/**
* Counts topics from the given id_board.
*
* @param int $board
* @param bool $approved
* @return int
*/
function countTopicsByBoard($board, $approved = false)
{
$db = database();

// How many topics are on this board? (used for paging.)
$request = $db->query('', '
SELECT COUNT(*)
FROM {db_prefix}topics AS t
WHERE t.id_board = {int:id_board}' . (empty($approved) ? '
AND t.approved = {int:is_approved}' : ''),
array(
'id_board' => $board,
'is_approved' => 1,
)
);
list ($topics) = $db->fetch_row($request);
$db->free_result($request);

return $topics;
}

/**
* Determines topics which can be merged from a specific board.
*
* @param int $id_board
* @param int $id_topic
* @param bool $approved
* @param int $offset
* @return array
*/
function mergeableTopics($id_board, $id_topic, $approved, $offset)
{
global $modSettings, $scripturl;

$db = database();

// Get some topics to merge it with.
$request = $db->query('', '
SELECT t.id_topic, m.subject, m.id_member, IFNULL(mem.real_name, m.poster_name) AS poster_name
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE t.id_board = {int:id_board}
AND t.id_topic != {int:id_topic}' . (empty($approved) ? '
AND t.approved = {int:is_approved}' : '') . '
ORDER BY {raw:sort}
LIMIT {int:offset}, {int:limit}',
array(
'id_board' => $id_board,
'id_topic' => $id_topic,
'sort' => (!empty($modSettings['enableStickyTopics']) ? 't.is_sticky DESC, ' : '') . 't.id_last_msg DESC',
'offset' => $offset,
'limit' => $modSettings['defaultMaxTopics'],
'is_approved' => 1,
)
);
$topics = array();
while ($row = $db->fetch_assoc($request))
{
censorText($row['subject']);

$topics[] = array(
'id' => $row['id_topic'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" target="_blank" class="new_win">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'js_subject' => addcslashes(addslashes($row['subject']), '/')
);
}
$db->free_result($request);

return $topics;
}

/**
* Determines all messages from a given array of topics.
*
* @param array int $topics
* @return array
*/
function messagesInTopics($topics)
{
$db = database();

// Obtain all the message ids we are going to affect.
$messages = array();
$request = $db->query('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic IN ({array_int:topic_list})',
array(
'topic_list' => $topics,
));
while ($row = $db->fetch_row($request))
$messages[] = $row['id_msg'];
$db->free_result($request);

return $messages;
}

0 comments on commit 0d31015

Please sign in to comment.