/** * Creates a droplets from the given array * * @param array $droplet * @return array */ public static function create_from_array($droplets) { if (!count($droplets)) { return; } // Populate identities Model_Identity::get_identities($droplets); // Hash array with droplet_hash as key and index in droplets array that contain that hash $droplets_idx = array(); foreach ($droplets as $key => &$droplet) { if (!isset($droplet['id'])) { $hash = md5($droplet['identity_orig_id'] . $droplet['channel'] . $droplet['droplet_orig_id']); $droplet['droplet_hash'] = $hash; if (empty($droplets_idx[$hash])) { $droplets_idx[$hash] = array(); } $droplets_idx[$hash][] = $key; } } // Insert new drops $new_droplets = array(); if (!empty($droplets_idx)) { Swiftriver_Mutex::obtain(get_class(), 3600); // Find the drops that already exist by their droplet_hash $found_query = DB::select('droplet_hash', 'id')->from('droplets')->where('droplet_hash', 'IN', array_keys($droplets_idx)); $found = $found_query->execute()->as_array(); // Update the ids of existing drops found in the db and // remove them from droplets_idx to leave new drops $new_droplet_count = count($droplets_idx); foreach ($found as $hash) { foreach ($droplets_idx[$hash['droplet_hash']] as $key) { $droplets[$key]['id'] = $hash['id']; } $new_droplet_count--; unset($droplets_idx[$hash['droplet_hash']]); } if (!empty($droplets_idx)) { // Get a range of IDs to be used in inserting the new drops $base_id = Model_Droplet::get_ids($new_droplet_count); // Insert into the droplets table $query = DB::insert('droplets', array('id', 'channel', 'droplet_hash', 'droplet_orig_id', 'droplet_type', 'droplet_title', 'droplet_content', 'droplet_date_pub', 'droplet_date_add', 'identity_id', 'processing_status')); foreach ($droplets_idx as $hash => $keys) { foreach ($keys as $key) { $droplets[$key]['id'] = $base_id; } // PHP has reference issues with array so // we cannot copy the element we have but // refeference it in place as below $droplets[$keys[0]] // otherwise the element will be overwriten if we use a // copy. Sigh. $new_droplets[] = $droplets[$keys[0]]; $query->values(array('id' => $base_id++, 'channel' => $droplets[$keys[0]]['channel'], 'droplet_hash' => $droplets[$keys[0]]['droplet_hash'], 'droplet_orig_id' => $droplets[$keys[0]]['droplet_orig_id'], 'droplet_type' => $droplets[$keys[0]]['droplet_type'], 'droplet_title' => $droplets[$keys[0]]['droplet_title'], 'droplet_content' => $droplets[$keys[0]]['droplet_content'], 'droplet_date_pub' => $droplets[$keys[0]]['droplet_date_pub'], 'droplet_date_add' => gmdate("Y-m-d H:i:s", time()), 'identity_id' => $droplets[$keys[0]]['identity_id'], 'processing_status' => self::PROCESSING_STATUS_NEW)); } $query->execute(); } Swiftriver_Mutex::release(get_class()); } // Populate metadata IDs into the drops array Model_Tag::get_tags($droplets); Model_Link::get_links($droplets); Model_Place::get_places($droplets); Model_Media::get_media($droplets); // Populate the drop's metadata tables self::add_metadata($droplets); return array($droplets, $new_droplets); }