From 34029b43f7be06576985ef280fbe46f801832d86 Mon Sep 17 00:00:00 2001 From: Norv Date: Wed, 19 Jun 2013 20:51:49 +0300 Subject: [PATCH] Refactor out of controllers a few moderation related functions. Signed-off-by: Norv --- sources/admin/ManageCoreFeatures.php | 6 +- .../controllers/PostModeration.controller.php | 118 +----------------- sources/subs/Moderation.subs.php | 53 +++++++- sources/subs/Post.subs.php | 38 ------ sources/subs/Topic.subs.php | 103 +++++++++++++++ 5 files changed, 160 insertions(+), 158 deletions(-) diff --git a/sources/admin/ManageCoreFeatures.php b/sources/admin/ManageCoreFeatures.php index 36b1a8cf30..7c2025fde8 100644 --- a/sources/admin/ManageCoreFeatures.php +++ b/sources/admin/ManageCoreFeatures.php @@ -261,8 +261,8 @@ public function settings() // Cant use warning post moderation if disabled! if (!$value) { - require_once(CONTROLLERDIR . \'/PostModeration.controller.php\'); - approveAllData(); + require_once(SUBSDIR . \'/Moderation.subs.php\'); + approveAllUnapproved(); return array(\'warning_moderate\' => 0); } @@ -401,4 +401,4 @@ public function loadGeneralSettingParameters($subActions = array(), $defaultActi $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (!empty($defaultAction) ? $defaultAction : array_pop($temp = array_keys($subActions))); $context['sub_action'] = $_REQUEST['sa']; } -} \ No newline at end of file +} diff --git a/sources/controllers/PostModeration.controller.php b/sources/controllers/PostModeration.controller.php index 5410919abe..3f3cd52388 100644 --- a/sources/controllers/PostModeration.controller.php +++ b/sources/controllers/PostModeration.controller.php @@ -117,6 +117,8 @@ function action_unapproved() { checkSession('request'); + require_once(SUBSDIR . '/Topic.subs.php'); + // Handy shortcut. $any_array = $curAction == 'approve' ? $approve_boards : $delete_any_boards; @@ -532,119 +534,3 @@ function action_approve() redirectexit('topic=' . $topic . '.msg' . $current_msg. '#msg' . $current_msg); } } - -/** - * Approve a batch of posts (or topics in their own right) - * - * @param array $messages - * @param array $messageDetails - * @param (string) $current_view = replies - */ -function approveMessages($messages, $messageDetails, $current_view = 'replies') -{ - require_once(SUBSDIR . '/Post.subs.php'); - if ($current_view == 'topics') - { - approveTopics($messages); - - // and tell the world about it - foreach ($messages as $topic) - logAction('approve_topic', array('topic' => $topic, 'subject' => $messageDetails[$topic]['subject'], 'member' => $messageDetails[$topic]['member'], 'board' => $messageDetails[$topic]['board'])); - } - else - { - approvePosts($messages); - - // and tell the world about it again - foreach ($messages as $post) - logAction('approve', array('topic' => $messageDetails[$post]['topic'], 'subject' => $messageDetails[$post]['subject'], 'member' => $messageDetails[$post]['member'], 'board' => $messageDetails[$post]['board'])); - } -} - -/** - * This is a helper function - basically approve everything! - */ -function approveAllData() -{ - $db = database(); - - // Start with messages and topics. - $request = $db->query('', ' - SELECT id_msg - FROM {db_prefix}messages - WHERE approved = {int:not_approved}', - array( - 'not_approved' => 0, - ) - ); - $msgs = array(); - while ($row = $db->fetch_row($request)) - $msgs[] = $row[0]; - $db->free_result($request); - - if (!empty($msgs)) - { - require_once(SUBSDIR . '/Post.subs.php'); - approvePosts($msgs); - cache_put_data('num_menu_errors', null, 900); - } - - // Now do attachments - $request = $db->query('', ' - SELECT id_attach - FROM {db_prefix}attachments - WHERE approved = {int:not_approved}', - array( - 'not_approved' => 0, - ) - ); - $attaches = array(); - while ($row = $db->fetch_row($request)) - $attaches[] = $row[0]; - $db->free_result($request); - - if (!empty($attaches)) - { - require_once(SUBSDIR . '/Attachments.subs.php'); - approveAttachments($attaches); - cache_put_data('num_menu_errors', null, 900); - } -} - -/** - * Remove a batch of messages (or topics) - * - * @param array $messages - * @param array $messageDetails - * @param string $current_view = replies - */ -function removeMessages($messages, $messageDetails, $current_view = 'replies') -{ - global $modSettings; - - // @todo something's not right, removeMessage() does check permissions, - // removeTopics() doesn't - if ($current_view == 'topics') - { - require_once(SUBSDIR . '/Topic.subs.php'); - removeTopics($messages); - - // and tell the world about it - foreach ($messages as $topic) - { - // Note, only log topic ID in native form if it's not gone forever. - logAction('remove', array( - (empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $messageDetails[$topic]['board'] ? 'topic' : 'old_topic_id') => $topic, 'subject' => $messageDetails[$topic]['subject'], 'member' => $messageDetails[$topic]['member'], 'board' => $messageDetails[$topic]['board'])); - } - } - else - { - require_once(SUBSDIR . '/Messages.subs.php'); - foreach ($messages as $post) - { - removeMessage($post); - logAction('delete', array( - (empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $messageDetails[$post]['board'] ? 'topic' : 'old_topic_id') => $messageDetails[$post]['topic'], 'subject' => $messageDetails[$post]['subject'], 'member' => $messageDetails[$post]['member'], 'board' => $messageDetails[$post]['board'])); - } - } -} diff --git a/sources/subs/Moderation.subs.php b/sources/subs/Moderation.subs.php index 65af08692b..d119c5c5f4 100644 --- a/sources/subs/Moderation.subs.php +++ b/sources/subs/Moderation.subs.php @@ -600,4 +600,55 @@ function modReportDetails($id_report) $db->free_result($request); return $row; -} \ No newline at end of file +} + +/** + * This is a helper function: approve everything unapproved. + * Used from moderation panel. + */ +function approveAllUnapproved() +{ + $db = database(); + + // Start with messages and topics. + $request = $db->query('', ' + SELECT id_msg + FROM {db_prefix}messages + WHERE approved = {int:not_approved}', + array( + 'not_approved' => 0, + ) + ); + $msgs = array(); + while ($row = $db->fetch_row($request)) + $msgs[] = $row[0]; + $db->free_result($request); + + if (!empty($msgs)) + { + require_once(SUBSDIR . '/Post.subs.php'); + approvePosts($msgs); + cache_put_data('num_menu_errors', null, 900); + } + + // Now do attachments + $request = $db->query('', ' + SELECT id_attach + FROM {db_prefix}attachments + WHERE approved = {int:not_approved}', + array( + 'not_approved' => 0, + ) + ); + $attaches = array(); + while ($row = $db->fetch_row($request)) + $attaches[] = $row[0]; + $db->free_result($request); + + if (!empty($attaches)) + { + require_once(SUBSDIR . '/Attachments.subs.php'); + approveAttachments($attaches); + cache_put_data('num_menu_errors', null, 900); + } +} diff --git a/sources/subs/Post.subs.php b/sources/subs/Post.subs.php index 55aa1963fa..24b1c8c2b7 100644 --- a/sources/subs/Post.subs.php +++ b/sources/subs/Post.subs.php @@ -1691,44 +1691,6 @@ function approvePosts($msgs, $approve = true) return true; } -/** - * Approve topics? - * @todo shouldn't this be in topic - * - * @param array $topics array of topics ids - * @param bool $approve = true - */ -function approveTopics($topics, $approve = true) -{ - $db = database(); - - if (!is_array($topics)) - $topics = array($topics); - - if (empty($topics)) - return false; - - $approve_type = $approve ? 0 : 1; - - // Just get the messages to be approved and pass through... - $request = $db->query('', ' - SELECT id_msg - FROM {db_prefix}messages - WHERE id_topic IN ({array_int:topic_list}) - AND approved = {int:approve_type}', - array( - 'topic_list' => $topics, - 'approve_type' => $approve_type, - ) - ); - $msgs = array(); - while ($row = $db->fetch_assoc($request)) - $msgs[] = $row['id_msg']; - $db->free_result($request); - - return approvePosts($msgs, $approve); -} - /** * A special function for handling the hell which is sending approval notifications. * diff --git a/sources/subs/Topic.subs.php b/sources/subs/Topic.subs.php index 85737874ef..b986fa0a27 100644 --- a/sources/subs/Topic.subs.php +++ b/sources/subs/Topic.subs.php @@ -1789,3 +1789,106 @@ function getTopicsPostsAndPoster($topic, $limit, $sort) return $topic_details; } + +/** + * Remove a batch of messages (or topics) + * + * @param array $messages + * @param array $messageDetails + * @param string $type = replies + */ +function removeMessages($messages, $messageDetails, $type = 'replies') +{ + global $modSettings; + + // @todo something's not right, removeMessage() does check permissions, + // removeTopics() doesn't + if ($type == 'topics') + { + removeTopics($messages); + + // and tell the world about it + foreach ($messages as $topic) + { + // Note, only log topic ID in native form if it's not gone forever. + logAction('remove', array( + (empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $messageDetails[$topic]['board'] ? 'topic' : 'old_topic_id') => $topic, 'subject' => $messageDetails[$topic]['subject'], 'member' => $messageDetails[$topic]['member'], 'board' => $messageDetails[$topic]['board'])); + } + } + else + { + require_once(SUBSDIR . '/Messages.subs.php'); + foreach ($messages as $post) + { + removeMessage($post); + logAction('delete', array( + (empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $messageDetails[$post]['board'] ? 'topic' : 'old_topic_id') => $messageDetails[$post]['topic'], 'subject' => $messageDetails[$post]['subject'], 'member' => $messageDetails[$post]['member'], 'board' => $messageDetails[$post]['board'])); + } + } +} + +/** + * Approve a batch of posts (or topics in their own right) + * + * @param array $messages + * @param array $messageDetails + * @param (string) $type = replies + */ +function approveMessages($messages, $messageDetails, $type = 'replies') +{ + if ($type == 'topics') + { + approveTopics($messages); + + // and tell the world about it + foreach ($messages as $topic) + logAction('approve_topic', array('topic' => $topic, 'subject' => $messageDetails[$topic]['subject'], 'member' => $messageDetails[$topic]['member'], 'board' => $messageDetails[$topic]['board'])); + } + else + { + require_once(SUBSDIR . '/Post.subs.php'); + approvePosts($messages); + + // and tell the world about it again + foreach ($messages as $post) + logAction('approve', array('topic' => $messageDetails[$post]['topic'], 'subject' => $messageDetails[$post]['subject'], 'member' => $messageDetails[$post]['member'], 'board' => $messageDetails[$post]['board'])); + } +} + +/** + * Approve topics, all we got. + * + * @param array $topics array of topics ids + * @param bool $approve = true + */ +function approveTopics($topics, $approve = true) +{ + $db = database(); + + if (!is_array($topics)) + $topics = array($topics); + + if (empty($topics)) + return false; + + $approve_type = $approve ? 0 : 1; + + // Just get the messages to be approved and pass through... + $request = $db->query('', ' + SELECT id_msg + FROM {db_prefix}messages + WHERE id_topic IN ({array_int:topic_list}) + AND approved = {int:approve_type}', + array( + 'topic_list' => $topics, + 'approve_type' => $approve_type, + ) + ); + $msgs = array(); + while ($row = $db->fetch_assoc($request)) + $msgs[] = $row['id_msg']; + $db->free_result($request); + + require_once(SUBSDIR . '/Post.subs.php'); + return approvePosts($msgs, $approve); +}