1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-16 12:29:43 +02:00

[ticket/10899] Refactoring in \phpbb\log\log_interface

PHPBB3-10899
This commit is contained in:
Tristan Darricau 2014-05-10 18:24:07 +02:00
parent c5a4ad3d31
commit c6d7875b9b
5 changed files with 100 additions and 34 deletions

View File

@ -53,7 +53,7 @@ class acp_logs
{ {
if (confirm_box(true)) if (confirm_box(true))
{ {
$where_sql = ''; $conditions = array();
if ($deletemark && sizeof($marked)) if ($deletemark && sizeof($marked))
{ {
@ -62,27 +62,23 @@ class acp_logs
{ {
$sql_in[] = $mark; $sql_in[] = $mark;
} }
$where_sql = ' AND ' . $db->sql_in_set('log_id', $sql_in); $conditions['log_id'] = $sql_in;
unset($sql_in); unset($sql_in);
} }
if ($deleteall) if ($deleteall)
{ {
$where_sql = ($sort_days) ? 'AND log_time >= ' . (time() - ($sort_days * 86400)) : ''; if ($sort_days)
{
$conditions['log_time'] = array('>=', time() - ($sort_days * 86400));
}
$keywords = utf8_normalize_nfc(request_var('keywords', '', true)); $keywords = utf8_normalize_nfc(request_var('keywords', '', true));
$keywords_where = $phpbb_log->generate_sql_keyword($keywords, ''); $conditions['keywords'] = $keywords;
$where_sql .= ' ' . $keywords_where;
} }
if ($where_sql) $conditions['log_type'] = $this->log_type;
{ $phpbb_log->delete($mode, $conditions);
$sql = 'DELETE FROM ' . LOG_TABLE . "
WHERE log_type = {$this->log_type}
$where_sql";
$db->sql_query($sql);
add_log('admin', 'LOG_CLEAR_' . strtoupper($mode));
}
} }
else else
{ {

View File

@ -111,33 +111,35 @@ class mcp_logs
{ {
if ($deletemark && sizeof($marked)) if ($deletemark && sizeof($marked))
{ {
$sql = 'DELETE FROM ' . LOG_TABLE . ' $conditions = array(
WHERE log_type = ' . LOG_MOD . ' 'log_type' => LOG_MOD,
AND ' . $db->sql_in_set('forum_id', $forum_list) . ' 'forum_id' => $forum_list,
AND ' . $db->sql_in_set('log_id', $marked); 'log_id' => $marked,
$db->sql_query($sql); );
add_log('admin', 'LOG_CLEAR_MOD'); $phpbb_log->delete('mod', $conditions);
} }
else if ($deleteall) else if ($deleteall)
{ {
$where_sql = ($sort_days) ? 'AND log_time >= ' . (time() - ($sort_days * 86400)) : '';
$keywords = utf8_normalize_nfc(request_var('keywords', '', true)); $keywords = utf8_normalize_nfc(request_var('keywords', '', true));
$keywords_where = $phpbb_log->generate_sql_keyword($keywords, '');
$where_sql .= ' ' . $keywords_where;
$sql = 'DELETE FROM ' . LOG_TABLE . ' $conditions = array(
WHERE log_type = ' . LOG_MOD . ' 'log_type' => LOG_MOD,
AND ' . $db->sql_in_set('forum_id', $forum_list) . 'forum_id' => $forum_list,
$where_sql; 'keywords' => $keywords,
);
if ($sort_days)
{
$conditions['log_time'] = array('>=', time() - ($sort_days * 86400));
}
if ($mode == 'topic_logs') if ($mode == 'topic_logs')
{ {
$sql .= ' AND topic_id = ' . $topic_id; $conditions['topic_logs'] = $topic_id;
} }
$db->sql_query($sql);
add_log('admin', 'LOG_CLEAR_MOD'); $phpbb_log->delete('mod', $conditions);
} }
} }
else else

View File

@ -329,6 +329,54 @@ class log implements \phpbb\log\log_interface
return $this->db->sql_nextid(); return $this->db->sql_nextid();
} }
/**
* {@inheritDoc}
*/
public function delete($mode, $conditions = array())
{
$sql_where = '';
$started = false;
foreach ($conditions as $field => $field_value)
{
if ($started)
{
$sql_where .= ' AND ';
}
else
{
$sql_where = 'WHERE ';
$started = true;
}
if ($field == 'keywords')
{
$sql_where .= $this->generate_sql_keyword($field_value, '', '');
}
else
{
if (is_array($field_value) && sizeof($field_value) == 2)
{
$sql_where .= $field . ' ' . $field_value[0] . ' ' . $field_value[1];
}
else if (is_array($field_value) && sizeof($field_value) > 2)
{
$sql_where .= $this->db->sql_in_set($field, $field_value);;
}
else
{
$sql_where .= $field . ' = ' . $field_value;
}
}
}
$sql = 'DELETE FROM ' . LOG_TABLE . "
$sql_where";
$this->db->sql_query($sql);
$this->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_CLEAR_' . strtoupper($mode));
}
/** /**
* Grab the logs from the database * Grab the logs from the database
* *
@ -636,12 +684,13 @@ class log implements \phpbb\log\log_interface
/** /**
* Generates a sql condition for the specified keywords * Generates a sql condition for the specified keywords
* *
* @param string $keywords The keywords the user specified to search for * @param string $keywords The keywords the user specified to search for
* @param string $table_alias The alias of the logs' table ('l.' by default) * @param string $table_alias The alias of the logs' table ('l.' by default)
* @param string $statement_operator The operator used to prefix the statement ('AND' by default)
* *
* @return string Returns the SQL condition searching for the keywords * @return string Returns the SQL condition searching for the keywords
*/ */
public function generate_sql_keyword($keywords, $table_alias = 'l.') protected function generate_sql_keyword($keywords, $table_alias = 'l.', $statement_operator = 'AND')
{ {
// Use no preg_quote for $keywords because this would lead to sole // Use no preg_quote for $keywords because this would lead to sole
// backslashes being added. We also use an OR connection here for // backslashes being added. We also use an OR connection here for
@ -686,7 +735,7 @@ class log implements \phpbb\log\log_interface
} }
} }
$sql_keywords = 'AND ('; $sql_keywords = $statement_operator . ' (';
if (!empty($operations)) if (!empty($operations))
{ {
$sql_keywords .= $this->db->sql_in_set($table_alias . 'log_operation', $operations) . ' OR '; $sql_keywords .= $this->db->sql_in_set($table_alias . 'log_operation', $operations) . ' OR ';

View File

@ -66,6 +66,18 @@ interface log_interface
*/ */
public function add($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array()); public function add($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array());
/**
* Delete entries in the logs
*
* @param string $mode The mode defines which log_type is used and from which log the entries are deleted
* @param array $conditions An array of conditions, 3 different forms are accepted
* 1) <key> => 'value> transformed into 'AND <key> = <value>' (value should be an integer)
* 2) <key> => array(<operator>, <value>) transformed into 'AND <key> <operator> <value>' (value should be an integer)
* 3) <key> => array(<values>) transformed into 'AND <key> IN <values>'
* A special field, keywords, can also be defined. In this case only the log entries that have the keywords in log_operation or log_data will be deleted.
*/
public function delete($mode, $conditions = array());
/** /**
* Grab the logs from the database * Grab the logs from the database
* *

View File

@ -46,6 +46,13 @@ class null implements log_interface
return false; return false;
} }
/**
* {@inheritdoc}
*/
public function delete($mode, $conditions = array())
{
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */