/** * Migrate data according to a scenario * @param $scenario */ private function _migrate_data($scenario) { $this->EE = get_instance(); $db = $this->EE->db; $this->EE->load->library('assets_lib'); clearstatcache(); switch ($scenario) { case '<2 -> 2.0': require_once PATH_THIRD . 'assets/sources/ee/source.ee.php'; $filedirs = array(); $folder_list = array(); // load upload preferences and store them in table for Assets $rows = $db->get('upload_prefs')->result(); foreach ($rows as $filedir) { $filedirs[$filedir->id] = Assets_ee_source::apply_filedir_overrides($filedir); } // load physical folder structure foreach ($filedirs as $id => $filedir) { $filedir->server_path = Assets_ee_source::resolve_server_path($filedir->server_path); $folder_list[$id][] = $filedir->server_path; $this->_load_folder_structure($filedir->server_path, $folder_list[$id]); } // store the folder structure in database $subfolders = array(); foreach ($folder_list as $filedir_id => $folders) { $filedir = $filedirs[$filedir_id]; foreach ($folders as $folder) { $subpath = substr($folder, strlen($filedir->server_path)); if (empty($subpath)) { $folder_name = $filedir->name; $parent_id = NULL; } else { $path_parts = explode('/', $subpath); $folder_name = array_pop($path_parts); $parent_key = $filedir_id . ':' . rtrim(join('/', $path_parts), '/'); $parent_id = isset($subfolders[$parent_key]) ? $subfolders[$parent_key] : 0; } // in case false was returned earlier $subpath = $subpath ? rtrim($subpath, '/') . '/' : ''; $folder_entry = array('source_type' => 'ee', 'filedir_id' => $filedir_id, 'folder_name' => $folder_name, 'full_path' => $subpath); if (!is_null($parent_id)) { $folder_entry['parent_id'] = $parent_id; } $this->EE->db->insert('assets_folders', $folder_entry); $subfolders[$filedir_id . ':' . rtrim($subpath, '/')] = $this->EE->db->insert_id(); } } // bring up the list of existing assets and update the entries $rows = $db->get('assets_files')->result(); $pattern = '/\\{filedir_(?P<filedir_id>[0-9]+)\\}(?P<path>.*)/'; foreach ($rows as $asset) { $asset->connector = 'ee'; if (preg_match($pattern, $asset->file_name, $matches)) { if (isset($filedirs[$matches['filedir_id']])) { $filedir = $filedirs[$matches['filedir_id']]; $full_path = str_replace('{filedir_' . $filedir->id . '}', $filedir->server_path, $asset->file_name); $subpath = substr($full_path, strlen($filedir->server_path)); $path_parts = explode('/', $subpath); $file = array_pop($path_parts); $subpath = join('/', $path_parts); $folder_key = $matches['filedir_id'] . ':' . $subpath; if (isset($subfolders[$folder_key])) { $folder_id = $subfolders[$folder_key]; } else { $folder_id = 0; } $kind = Assets_helper::get_kind($full_path); $data = array('source_type' => 'ee', 'filedir_id' => $filedir->id, 'folder_id' => $folder_id, 'file_name' => $file, 'kind' => $kind); if (file_exists($full_path)) { $data['size'] = filesize($full_path); $data['date_modified'] = filemtime($full_path); if ($kind == 'image') { list($width, $height) = getimagesize($full_path); $data['width'] = $width; $data['height'] = $height; } } $this->EE->db->update('assets_files', $data, array('file_id' => $asset->file_id)); $this->EE->assets_lib->update_file_search_keywords($asset->file_id); } } } // celebrate break; case '2.0b1 -> 2.0b2': // get S3 credentials if any $query = $this->EE->db->select('settings')->where('name', 'assets')->get('fieldtypes'); $settings = unserialize(base64_decode($query->row('settings'))); $settings = array_merge(array('license_key' => '', 's3_access_key_id' => '', 's3_secret_access_key' => ''), $settings); //if we have s3 settings, let's convert the "folder_prefs" way to "sources" way if (!empty($settings['s3_access_key_id']) && !empty($settings['s3_secret_access_key'])) { $old_sources = $this->EE->db->get('assets_sources')->result(); foreach ($old_sources as $source) { $previous_settings = json_decode($source->settings); $new_settings = (object) array('access_key_id' => $settings['s3_access_key_id'], 'secret_access_key' => $settings['s3_secret_access_key'], 'bucket' => $previous_settings->name, 'url_prefix' => $previous_settings->url_prefix, 'location' => $previous_settings->location); $data = array('name' => $previous_settings->name, 'settings' => Assets_helper::get_json($new_settings)); $this->EE->db->update('assets_sources', $data, array('source_id' => $source->source_id)); } } // modify folder data and also keep a list of who's who $folders = $this->EE->db->get('assets_folders')->result(); $folder_sources = array(); foreach ($folders as $row) { if ($row->source_type == 'ee') { $row->filedir_id = $row->source_id; $row->source_id = NULL; $this->EE->db->update('assets_folders', $row, array('folder_id' => $row->folder_id)); $folder_sources[$row->folder_id] = $row->filedir_id; } else { $folder_sources[$row->folder_id] = $row->source_id; } } // add some data for file entries and we're done! $files = $this->EE->db->get('assets_files')->result(); foreach ($files as $row) { if ($row->source_type == 'ee' && isset($folder_sources[$row->folder_id])) { $row->source_id = NULL; $row->filedir_id = $folder_sources[$row->folder_id]; $this->EE->db->update('assets_files', $row, array('file_id' => $row->file_id)); } else { if (isset($folder_sources[$row->folder_id])) { $row->source_id = $folder_sources[$row->folder_id]; $row->filedir_id = NULL; $this->EE->db->update('assets_files', $row, array('file_id' => $row->file_id)); } } } // party! break; } }
/** * Register a file in EE filedir * @param $filedir_id * @param $path * @return bool|mixed */ public function register_ee_file($filedir_id, $path) { $filedir = $this->EE->db->get_where('upload_prefs', array('id' => $filedir_id))->row(); if (empty($filedir)) { return FALSE; } require_once PATH_THIRD . 'assets/sources/ee/source.ee.php'; $filedir = Assets_ee_source::apply_filedir_overrides($filedir); $path_parts = explode("/", $path); $filename = array_pop($path_parts); $current_folder_id = $this->get_folder_id_by_parent_and_name(NULL, $filedir->name); if (empty($current_folder_id)) { $data = array('source_type' => 'ee', 'folder_name' => $filedir->name, 'full_path' => '', 'parent_id' => NULL, 'filedir_id' => $filedir_id); $this->EE->db->insert('assets_folders', $data); $current_folder_id = $this->EE->db->insert_id(); } $current_folder = $this->get_folder_row_by_id($current_folder_id); $full_path = ''; $next_folder_id = $current_folder_id; foreach ($path_parts as $path_part) { $full_path .= $path_part . '/'; $next_folder_id = $this->get_folder_id_by_parent_and_name($current_folder->folder_id, $path_part); if (empty($next_folder_id)) { $data = array('source_type' => 'ee', 'folder_name' => $path_part, 'full_path' => $full_path, 'parent_id' => $current_folder->folder_id, 'filedir_id' => $filedir_id); $this->EE->db->insert('assets_folders', $data); $next_folder_id = $this->EE->db->insert_id(); } $current_folder = $this->get_folder_row_by_id($next_folder_id); } return $this->register_file($next_folder_id, $filename); }