Skip to content

Commit

Permalink
Refactor out of controllers a few moderation related functions.
Browse files Browse the repository at this point in the history
Signed-off-by: Norv <a.w.norv@gmail.com>
  • Loading branch information
norv committed Jun 20, 2013
1 parent 5bc25c8 commit 34029b4
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 158 deletions.
6 changes: 3 additions & 3 deletions sources/admin/ManageCoreFeatures.php
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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'];
}
}
}
118 changes: 2 additions & 116 deletions sources/controllers/PostModeration.controller.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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']));
}
}
}
53 changes: 52 additions & 1 deletion sources/subs/Moderation.subs.php
Expand Up @@ -600,4 +600,55 @@ function modReportDetails($id_report)
$db->free_result($request);

return $row;
}
}

/**
* 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);
}
}
38 changes: 0 additions & 38 deletions sources/subs/Post.subs.php
Expand Up @@ -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.
*
Expand Down
103 changes: 103 additions & 0 deletions sources/subs/Topic.subs.php
Expand Up @@ -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);
}

0 comments on commit 34029b4

Please sign in to comment.