/** * Helper method to make a Configuration API request, stores error if encountered * * @param string $method * @param array $payload * @return array [response string, Cloud_Config_Request object used for request] */ protected function _make_request($method, $payload = array(), $flatten_keys = true, $region = false) { if ($payload && $flatten_keys) { $payload = $this->_flatten_keys($payload); } $credentials['access-key-id'] = $this->access_key; $credentials['secret-access-key'] = $this->secret_key; if (!($region && Lift_Search::is_valid_region($region))) { $region = Lift_Search::get_domain_region(); } $config = new Cloud_Config_Request($credentials, $this->http_api, $region); $r = $config->send_request($method, $payload); $this->set_last_status_code($config->status_code); if ($r && false !== ($r_json = json_decode($r))) { $r_json = json_decode($r); if (isset($r_json->Error) || $this->last_status_code != '200') { $this->set_last_error(array('code' => $r_json->Error->Code, 'message' => $r_json->Error->Message)); return false; } $response_name = $method . 'Response'; $result_name = $method . 'Result'; return $r_json->{$response_name}->{$result_name}; } else { $this->set_last_error(array('code' => 'invalid_response', 'message' => $config->error)); } return $r; }
/** * * @param string $search_domain_uri * @param iLift_HTTP $http_interface */ public function __construct($http_interface, $document_endpoint, $search_endpoint, $version = null) { if (is_null($version)) { $version = Lift_Search::api_version(); } $this->http_interface = $http_interface; $this->submission_uri = sprintf('http://%s/%s/documents/batch', $document_endpoint, $version); $this->search_uri = sprintf('http://%s/%s/search?', $search_endpoint, $version); }
/** * use the config API to get the CloudSearch domain/index status * * @return array */ public static function get_remote_status() { $domain = Lift_Search::get_domain_manager()->get_domain(Lift_Search::get_search_domain_name()); if (!$domain) { return array('errors' => true, 'reason' => 'Domain has been deleted or the CloudSearch API request failed.', 'severity' => 2, 'status' => array('fatal' => true, 'text' => 'Domain has been deleted or the CloudSearch API request failed')); } $errors = false; $severity = 0; $reason = ''; $text = 'active'; $pending = !$domain->Created && !$domain->Deleted; $deleting = $domain->Deleted; $processing = $domain->Processing; $num_searchable_docs = $domain->NumSearchableDocs; $needs_indexing = $domain->RequiresIndexDocuments; $search_instance_count = $domain->SearchInstanceCount; $search_instance_type = $domain->SearchInstanceType; $search_partition_count = $domain->SearchPartitionCount; if ($deleting) { $severity = 2; $reason = 'CloudSearch domain being deleted'; $text = 'being deleted'; } else { if ($needs_indexing || $processing) { if (0 == $search_instance_count) { $severity = 1; $reason = 'CloudSearch domain loading'; $text = 'loading'; } else { if ($needs_indexing) { $severity = 1; $reason = 'CloudSearch domain needs indexing'; $text = 'needs indexing'; } else { $severity = 1; $reason = 'CloudSearch domain processing'; $text = 'processing'; } } } else { if ($pending) { $severity = 1; $reason = 'CloudSearch domain pending'; $text = 'pending'; } } } if (0 != $severity) { $errors = true; } $status = compact('text', 'pending', 'deleting', 'processing', 'num_searchable_docs', 'needs_indexing', 'search_instance_count', 'search_instance_type', 'search_partition_count'); $status['api_error'] = false; $status['text'] = $text; $results = array('errors' => $errors, 'severity' => $severity, 'reason' => $reason, 'status' => $status); return $results; }
function _lift_deactivate() { if (class_exists('Left_Search')) { $domain_manager = Lift_Search::get_domain_manager(); if ($domain_name = Lift_Search::get_search_domain_name()) { TAE_Async_Event::Unwatch('lift_domain_created_' . $domain_name); TAE_Async_Event::Unwatch('lift_needs_indexing_' . $domain_name); } //clean up options delete_option(Lift_Search::INITIAL_SETUP_COMPLETE_OPTION); delete_option(Lift_Search::SETTINGS_OPTION); delete_option('lift_db_version'); delete_option(Lift_Document_Update_Queue::QUEUE_IDS_OPTION); if (class_exists('Voce_Error_Logging')) { Voce_Error_Logging::delete_logs(array('lift-search')); } Lift_Batch_Handler::_deactivation_cleanup(); Lift_Document_Update_Queue::_deactivation_cleanup(); } }
/** * Pulls the next set of items from the queue and sends a batch from it * Callback for Batch Submission Cron * * @todo Add locking */ public static function send_next_batch() { if (!self::ready_for_batch(Lift_Search::get_search_domain_name())) { delete_transient(self::BATCH_LOCK); Lift_Search::event_log('CloudSearch Not Ready for Batch ' . time(), 'The batch is locked or the search domain is either currently processing, needs indexing, or your domain does not have indexes set up.', array('send-queue', 'response-false', 'notice')); return; } $lock_key = md5(uniqid(microtime() . mt_rand(), true)); if (!get_transient(self::BATCH_LOCK)) { set_transient(self::BATCH_LOCK, $lock_key, 300); } if (get_transient(self::BATCH_LOCK) !== $lock_key) { //another cron has this lock return; } update_option(self::LAST_CRON_TIME_OPTION, time()); $closed_queue_id = Lift_Document_Update_Queue::get_closed_queue_id(); $update_query = Lift_Document_Update_Queue::query_updates(array('per_page' => self::get_queue_all_set_size(), 'queue_ids' => array($closed_queue_id))); if (!count($update_query->meta_rows)) { //no documents queued up Lift_Document_Update_Queue::close_active_queue(); delete_transient(self::BATCH_LOCK); return; } $batch = new Lift_Batch(); $batched_meta_keys = array(); $blog_id = get_current_blog_id(); $site_id = lift_get_current_site_id(); foreach ($update_query->meta_rows as $meta_row) { $update_data = get_post_meta($meta_row->post_id, $meta_row->meta_key, true); if ($update_data['document_type'] == 'post') { $action = $update_data['action']; if ($action == 'add') { $post = get_post($update_data['document_id'], ARRAY_A); $post_data = array('ID' => $update_data['document_id'], 'blog_id' => $blog_id, 'site_id' => $site_id); foreach (Lift_Search::get_indexed_post_fields($post['post_type']) as $field) { $post_data[$field] = isset($post[$field]) ? $post[$field] : null; } $sdf_field_data = apply_filters('lift_post_changes_to_data', $post_data, $update_data['fields'], $update_data['document_id']); } else { $sdf_field_data = array('ID' => intval($update_data['document_id'])); } $sdf_doc = Lift_Posts_To_SDF::format_post((object) $sdf_field_data, array('action' => $action, 'time' => time())); try { $batch->add_document((object) $sdf_doc); $batched_meta_keys[] = $meta_row->meta_key; } catch (Lift_Batch_Exception $e) { if (isset($e->errors[0]['code']) && 500 == $e->errors[0]['code']) { break; } Lift_Search::event_log('Batch Add Error ' . time(), json_encode($e), array('batch-add', 'error')); //@todo log error, stop cron? --- update_option( self::$search_semaphore, 1 ); continue; } } } //send the batch $cloud_api = Lift_Search::get_search_api(); if ($r = $cloud_api->sendBatch($batch)) { if ($r->status === "success") { $log_title = "Post Queue Sent "; $tag = 'success'; foreach ($batched_meta_keys as $meta_key) { delete_post_meta($closed_queue_id, $meta_key); } } else { $log_title = "Post Queue Send Error "; $tag = 'error'; } Lift_Search::event_log($log_title . time(), json_encode($r), array('send-queue', 'response-true', $tag)); //@todo delete sent queued items } else { $messages = $cloud_api->getErrorMessages(); Lift_Search::event_log('Post Queue Error ' . time(), $messages, array('send-queue', 'response-false', 'error')); } delete_transient(self::BATCH_LOCK); }
public static function get_watched_post_fields($post_type) { return apply_filters('lift_watch_post_fields', Lift_Search::get_indexed_post_fields($post_type), $post_type); }
public function apply_access_policy($domain_name, $policies = false, $region = false) { if (!$policies) { $policies = $this->get_default_access_policies($domain_name, $region); if (!$policies) { return false; } } if (!$this->config_api->UpdateServiceAccessPolicies($domain_name, $policies, $region)) { Lift_Search::event_log('There was an error while applying the default access policy to the domain.', $this->config_api->get_last_error(), array('access policy', 'error')); return new WP_Error('There was an error while applying the default access policy to the domain.'); } return true; }
/** * semi-factory method for simplify getting an api instance */ public static function get_search_api() { $lift_http = self::get_http_api(); return new CloudSearch_API($lift_http, Lift_Search::get_document_endpoint(), Lift_Search::get_search_endpoint(), '2011-02-01'); }
/** * Filters the sql for a WP_Query before it's sent to the DB. * @param string $request * @param WP_Query $wp_query * @return string|bool false if the request is overwritten */ public static function _filter_posts_request($request, $wp_query) { $lift_query = Lift_WP_Query::GetInstance($wp_query); if (!apply_filters('lift_override_post_results', true) || !$lift_query->wp_query->is_search()) { return $request; } // filter the lift query $cs_query = apply_filters('lift_filter_query', $lift_query->get_cs_query()); $lift_api = Lift_Search::get_search_api(); $lift_results = $lift_api->sendSearch($cs_query); do_action_ref_array('lift_search_results', array($lift_results, $lift_api, $wp_query)); if (false !== $lift_results && is_object($lift_results)) { $lift_query->set_results($lift_results); // Since lift results exists, we no longer need to run the wp search return ''; } return $request; }
/** * Filters the sql for a WP_Query before it's sent to the DB. * @param string $request * @param WP_Query $wp_query * @return string|bool false if the request is overwritten */ public static function _filter_posts_request($request, $wp_query) { $lift_query = Lift_WP_Query::GetInstance($wp_query); if (!apply_filters('lift_override_post_results', true) || !$lift_query->wp_query->is_search()) { return $request; } // filter the lift query $cs_query = apply_filters('lift_filter_query', $lift_query->get_cs_query()); $lift_api = Lift_Search::get_search_api(); $lift_results = $lift_api->sendSearch($cs_query); if (false !== $lift_results && is_object($lift_results)) { $lift_query->set_results($lift_results); } return $request; }
public function action__wp_ajax_lift_error_log() { $response = (object) array('error' => false, 'nonce' => wp_create_nonce('lift_error_log')); if (Lift_Search::error_logging_enabled()) { $response->view_all_url = esc_url(admin_url(sprintf('edit.php?post_type=%s', Voce_Error_Logging::POST_TYPE))); if ($_SERVER['REQUEST_METHOD'] === 'POST') { check_ajax_referer('lift_error_log', 'nonce'); $response = Voce_Error_Logging::delete_logs(array('lift-search')); } else { $args = array('post_type' => Voce_Error_Logging::POST_TYPE, 'posts_per_page' => 5, 'post_status' => 'any', 'orderby' => 'date', 'order' => 'DESC', 'tax_query' => array(array('taxonomy' => Voce_Error_Logging::TAXONOMY, 'field' => 'slug', 'terms' => array('error', 'lift-search'), 'operator' => 'AND'))); $query = new WP_Query($args); $response->current_page = $query->get('paged'); $response->per_page = $query->get('posts_per_page'); $response->found_rows = $query->found_posts; $response->errors = array(); foreach ($query->posts as $post) { $response->errors[] = array('error_html' => sprintf('<strong>%s</strong><pre>%s</pre>', esc_html($post->post_title), wpautop($post->post_content)), 'date' => mysql2date('D. M d Y g:ia', $post->post_date)); } } } else { status_header(400); $response->view_all_url = ''; $response->error = array('code' => 'logging_disabled', 'Error Logging is Disabled'); } header('Content-Type: application/json'); die(json_encode($response)); }
/** * semi-factory method for simplify getting an api instance */ public static function get_search_api() { $lift_http = self::get_http_api(); return new CloudSearch_API($lift_http, Lift_Search::get_document_endpoint(), Lift_Search::get_search_endpoint(), self::api_version()); }
public static function _print_api_nag() { $api_version = Lift_Search::api_version(); $search_domain = Lift_Search::get_search_domain_name(); if (!$search_domain) { return false; } elseif (!strtotime($api_version) || date('Y', strtotime($api_version)) <= 2011) { ?> <div class="error"><p>Your search domain is using a deprecated version of the API. Please consider updating to the new version by following these <a target="_BLANK" href="http://docs.aws.amazon.com/cloudsearch/latest/developerguide/migrating.html"><strong>instructions</strong></strong></a>.</p></div> <?php } }
public function __construct($credentials, $http_interface, $region = false) { $this->key = $credentials['access-key-id']; $this->secret_key = $credentials['secret-access-key']; $this->http_interface = $http_interface; $this->api_version = Lift_Search::api_version(); if ($region) { $this->endpoint = str_replace('us-east-1', $region, $this->endpoint); $this->region = $region; } }