1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-02-26 13:04:24 +01:00

[ticket/14168] Further split up attachment delete class

PHPBB3-14168
This commit is contained in:
Marc Alexander 2015-09-21 10:28:09 +02:00
parent 4709c8a128
commit ebfdd69525

View File

@ -35,6 +35,27 @@ class delete
/** @var array Attachement IDs */
protected $ids;
/** @var string SQL ID string */
private $sql_id;
/** @var string SQL where string */
private $sql_where = '';
/** @var int Number of deleted items */
private $num_deleted;
/** @var array Post IDs */
private $post_ids = array();
/** @var array Message IDs */
private $message_ids = array();
/** @var array Topic IDs */
private $topic_ids = array();
/** @var array Info of physical file */
private $physical = array();
/**
* Attachment delete class constructor
*
@ -66,31 +87,7 @@ class delete
return false;
}
$sql_where = '';
switch ($mode)
{
case 'post':
case 'message':
$sql_id = 'post_msg_id';
$sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
break;
case 'topic':
$sql_id = 'topic_id';
break;
case 'user':
$sql_id = 'poster_id';
break;
case 'attach':
default:
$sql_id = 'attach_id';
break;
}
$post_ids = $message_ids = $topic_ids = $physical = array();
$this->set_sql_constraints($mode);
/**
* Perform additional actions before collecting data for attachment(s) deletion
@ -111,68 +108,10 @@ class delete
extract($this->dispatcher->trigger_event('core.delete_attachments_collect_data_before', compact($vars)));
// Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
$sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set($sql_id, $ids);
$this->collect_attachment_info($resync);
$sql .= $sql_where;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
if ($resync && !$row['is_orphan'])
{
if (!$row['in_message'])
{
$post_ids[] = $row['post_msg_id'];
$topic_ids[] = $row['topic_id'];
}
else
{
$message_ids[] = $row['post_msg_id'];
}
}
$physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
}
$this->db->sql_freeresult($result);
/**
* Perform additional actions before attachment(s) deletion
*
* @event core.delete_attachments_before
* @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user
* @var mixed ids Array or comma separated list of ids corresponding to the mode
* @var bool resync Flag indicating if posts/messages/topics should be synchronized
* @var string sql_id The field name to collect/delete data for depending on the mode
* @var array post_ids Array with post ids for deleted attachment(s)
* @var array topic_ids Array with topic ids for deleted attachment(s)
* @var array message_ids Array with private message ids for deleted attachment(s)
* @var array physical Array with deleted attachment(s) physical file(s) data
* @since 3.1.7-RC1
*/
$vars = array(
'mode',
'ids',
'resync',
'sql_id',
'post_ids',
'topic_ids',
'message_ids',
'physical',
);
extract($this->dispatcher->trigger_event('core.delete_attachments_before', compact($vars)));
// Delete attachments
$sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set($sql_id, $ids);
$sql .= $sql_where;
$this->db->sql_query($sql);
$num_deleted = $this->db->sql_affectedrows();
// Delete attachments from database
$this->delete_attachments_from_db();
/**
* Perform additional actions after attachment(s) deletion from the database
@ -202,14 +141,265 @@ class delete
);
extract($this->dispatcher->trigger_event('core.delete_attachments_from_database_after', compact($vars)));
if (!$num_deleted)
if (!$this->num_deleted)
{
return 0;
}
// Delete attachments from filesystem
$this->delete_attachments_from_filesystem();
// If we do not resync, we do not need to adjust any message, post, topic or user entries
if (!$resync)
{
return $this->num_deleted;
}
// No more use for the original ids
unset($ids);
// Now, we need to resync posts, messages, topics. We go through every one of them
// Update post indicators for posts now no longer having attachments
if (sizeof($this->post_ids))
{
// Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
$sql = 'SELECT post_msg_id
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set('post_msg_id', $this->post_ids) . '
AND in_message = 0
AND is_orphan = 0';
$result = $this->db->sql_query($sql);
$remaining_ids = array();
while ($row = $this->db->sql_fetchrow($result))
{
$remaining_ids[] = $row['post_msg_id'];
}
$this->db->sql_freeresult($result);
// Now only unset those ids remaining
$this->post_ids = array_diff($this->post_ids, $remaining_ids);
if (sizeof($this->post_ids))
{
$sql = 'UPDATE ' . POSTS_TABLE . '
SET post_attachment = 0
WHERE ' . $this->db->sql_in_set('post_id', $this->post_ids);
$this->db->sql_query($sql);
}
}
// Update message table if messages are affected
if (sizeof($this->message_ids))
{
// Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
$sql = 'SELECT post_msg_id
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set('post_msg_id', $this->message_ids) . '
AND in_message = 1
AND is_orphan = 0';
$result = $this->db->sql_query($sql);
$remaining_ids = array();
while ($row = $this->db->sql_fetchrow($result))
{
$remaining_ids[] = $row['post_msg_id'];
}
$this->db->sql_freeresult($result);
// Now only unset those ids remaining
$this->message_ids = array_diff($this->message_ids, $remaining_ids);
if (sizeof($this->message_ids))
{
$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
SET message_attachment = 0
WHERE ' . $this->db->sql_in_set('msg_id', $this->message_ids);
$this->db->sql_query($sql);
}
}
// Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
if (sizeof($this->topic_ids))
{
// Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
$sql = 'SELECT topic_id
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set('topic_id', $this->topic_ids) . '
AND is_orphan = 0';
$result = $this->db->sql_query($sql);
$remaining_ids = array();
while ($row = $this->db->sql_fetchrow($result))
{
$remaining_ids[] = $row['topic_id'];
}
$this->db->sql_freeresult($result);
// Now only unset those ids remaining
$this->topic_ids = array_diff($this->topic_ids, $remaining_ids);
if (sizeof($this->topic_ids))
{
$sql = 'UPDATE ' . TOPICS_TABLE . '
SET topic_attachment = 0
WHERE ' . $this->db->sql_in_set('topic_id', $this->topic_ids);
$this->db->sql_query($sql);
}
}
return $this->num_deleted;
}
/**
* Set attachment IDs
*
* @param array $ids
*
* @return bool True if attachment IDs were set, false if not
*/
protected function set_attachment_ids($ids)
{
// 0 is as bad as an empty array
if (empty($ids))
{
return false;
}
if (is_array($ids))
{
$ids = array_unique($ids);
$this->ids = array_map('intval', $ids);
}
else
{
$this->ids = array((int) $ids);
}
return true;
}
/**
* Set SQL constraints based on mode
*
* @param string $mode Delete mode; can be: post|message|topic|attach|user
*/
private function set_sql_constraints($mode)
{
switch ($mode)
{
case 'post':
case 'message':
$this->sql_id = 'post_msg_id';
$this->sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
break;
case 'topic':
$this->sql_id = 'topic_id';
break;
case 'user':
$this->sql_id = 'poster_id';
break;
case 'attach':
default:
$this->sql_id = 'attach_id';
break;
}
}
/**
* Collect info about attachment IDs
*
* @param bool $resync Whether topics/posts should be resynced after delete
*/
protected function collect_attachment_info($resync)
{
// Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
$sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set($this->sql_id, $this->ids);
$sql .= $this->sql_where;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
if ($resync && !$row['is_orphan'])
{
if (!$row['in_message'])
{
$this->post_ids[] = $row['post_msg_id'];
$this->topic_ids[] = $row['topic_id'];
}
else
{
$this->message_ids[] = $row['post_msg_id'];
}
}
$this->physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
}
$this->db->sql_freeresult($result);
// IDs should be unique
$this->post_ids = array_unique($this->post_ids);
$this->message_ids = array_unique($this->message_ids);
$this->topic_ids = array_unique($this->topic_ids);
}
/**
* Delete attachments from database table
*/
protected function delete_attachments_from_db()
{
/**
* Perform additional actions before attachment(s) deletion
*
* @event core.delete_attachments_before
* @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user
* @var mixed ids Array or comma separated list of ids corresponding to the mode
* @var bool resync Flag indicating if posts/messages/topics should be synchronized
* @var string sql_id The field name to collect/delete data for depending on the mode
* @var array post_ids Array with post ids for deleted attachment(s)
* @var array topic_ids Array with topic ids for deleted attachment(s)
* @var array message_ids Array with private message ids for deleted attachment(s)
* @var array physical Array with deleted attachment(s) physical file(s) data
* @since 3.1.7-RC1
*/
$vars = array(
'mode',
'ids',
'resync',
'sql_id',
'post_ids',
'topic_ids',
'message_ids',
'physical',
);
extract($this->dispatcher->trigger_event('core.delete_attachments_before', compact($vars)));
// Delete attachments
$sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set($this->sql_id, $this->ids);
$sql .= $this->sql_where;
$this->db->sql_query($sql);
$this->num_deleted = $this->db->sql_affectedrows();
}
/**
* Delete attachments from filesystem
*/
protected function delete_attachments_from_filesystem()
{
$space_removed = $files_removed = 0;
foreach ($physical as $file_ary)
foreach ($this->physical as $file_ary)
{
if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
{
@ -261,138 +451,5 @@ class delete
$this->config->increment('upload_dir_size', $space_removed * (-1), false);
$this->config->increment('num_files', $files_removed * (-1), false);
}
// If we do not resync, we do not need to adjust any message, post, topic or user entries
if (!$resync)
{
return $num_deleted;
}
// No more use for the original ids
unset($ids);
// Now, we need to resync posts, messages, topics. We go through every one of them
$post_ids = array_unique($post_ids);
$message_ids = array_unique($message_ids);
$topic_ids = array_unique($topic_ids);
// Update post indicators for posts now no longer having attachments
if (sizeof($post_ids))
{
// Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
$sql = 'SELECT post_msg_id
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set('post_msg_id', $post_ids) . '
AND in_message = 0
AND is_orphan = 0';
$result = $this->db->sql_query($sql);
$remaining_ids = array();
while ($row = $this->db->sql_fetchrow($result))
{
$remaining_ids[] = $row['post_msg_id'];
}
$this->db->sql_freeresult($result);
// Now only unset those ids remaining
$post_ids = array_diff($post_ids, $remaining_ids);
if (sizeof($post_ids))
{
$sql = 'UPDATE ' . POSTS_TABLE . '
SET post_attachment = 0
WHERE ' . $this->db->sql_in_set('post_id', $post_ids);
$this->db->sql_query($sql);
}
}
// Update message table if messages are affected
if (sizeof($message_ids))
{
// Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
$sql = 'SELECT post_msg_id
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set('post_msg_id', $message_ids) . '
AND in_message = 1
AND is_orphan = 0';
$result = $this->db->sql_query($sql);
$remaining_ids = array();
while ($row = $this->db->sql_fetchrow($result))
{
$remaining_ids[] = $row['post_msg_id'];
}
$this->db->sql_freeresult($result);
// Now only unset those ids remaining
$message_ids = array_diff($message_ids, $remaining_ids);
if (sizeof($message_ids))
{
$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
SET message_attachment = 0
WHERE ' . $this->db->sql_in_set('msg_id', $message_ids);
$this->db->sql_query($sql);
}
}
// Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
if (sizeof($topic_ids))
{
// Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
$sql = 'SELECT topic_id
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $this->db->sql_in_set('topic_id', $topic_ids) . '
AND is_orphan = 0';
$result = $this->db->sql_query($sql);
$remaining_ids = array();
while ($row = $this->db->sql_fetchrow($result))
{
$remaining_ids[] = $row['topic_id'];
}
$this->db->sql_freeresult($result);
// Now only unset those ids remaining
$topic_ids = array_diff($topic_ids, $remaining_ids);
if (sizeof($topic_ids))
{
$sql = 'UPDATE ' . TOPICS_TABLE . '
SET topic_attachment = 0
WHERE ' . $this->db->sql_in_set('topic_id', $topic_ids);
$this->db->sql_query($sql);
}
}
return $num_deleted;
}
/**
* Set attachment IDs
*
* @param array $ids
*
* @return bool True if attachment IDs were set, false if not
*/
protected function set_attachment_ids($ids)
{
// 0 is as bad as an empty array
if (empty($ids))
{
return false;
}
if (is_array($ids))
{
$ids = array_unique($ids);
$this->ids = array_map('intval', $ids);
}
else
{
$this->ids = array((int) $ids);
}
return true;
}
}