/** * Delete Attachments * * @param string $mode can be: post|message|topic|attach|user * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids * @param bool $resync set this to false if you are deleting posts or topics * * @return int|bool Number of deleted attachments or false if something * went wrong during attachment deletion */ public function delete($mode, $ids, $resync = true) { if (!$this->set_attachment_ids($ids)) { return false; } $this->set_sql_constraints($mode); /** * Perform additional actions before collecting data for attachment(s) deletion * * @event core.delete_attachments_collect_data_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 * @since 3.1.7-RC1 */ $vars = array('mode', 'ids', 'resync', 'sql_id'); 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) $this->collect_attachment_info($resync); // Delete attachments from database $this->delete_attachments_from_db(); /** * Perform additional actions after attachment(s) deletion from the database * * @event core.delete_attachments_from_database_after * @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 * @var int num_deleted The number of deleted attachment(s) from the database * @since 3.1.7-RC1 */ $vars = array('mode', 'ids', 'resync', 'sql_id', 'post_ids', 'topic_ids', 'message_ids', 'physical', 'num_deleted'); extract($this->dispatcher->trigger_event('core.delete_attachments_from_database_after', compact($vars))); if (!$this->num_deleted) { return 0; } // Delete attachments from filesystem $this->remove_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); // Update post indicators for posts now no longer having attachments $this->resync->resync('post', $this->post_ids); // Update message table if messages are affected $this->resync->resync('message', $this->message_ids); // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic $this->resync->resync('topic', $this->topic_ids); return $this->num_deleted; }
/** * @dataProvider data_resync */ public function test_resync($type, $ids, $sql_id, $exist_table, $exist_data, $resync_data) { $sql_prefix = $type ?: 'post'; $sql = 'SELECT ' . $sql_prefix . '_attachment FROM ' . $exist_table . ' WHERE ' . $sql_id . ' = ' . $ids[0]; $result = $this->db->sql_query($sql); $data = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); $this->assertEquals($exist_data, $data); $this->resync->resync($type, $ids); $sql = 'SELECT ' . $sql_prefix . '_attachment FROM ' . $exist_table . ' WHERE ' . $sql_id . ' = ' . $ids[0]; $result = $this->db->sql_query($sql); $data = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); $this->assertEquals($resync_data, $data); }