public static function debug($action, $value = null) { if (is_array($value) || is_object($value)) { $value = json_encode($value); } elseif (is_bool($value)) { $value = true === $value ? '(bool) true' : '(bool) false'; } elseif (is_null($value)) { $value = '(null)'; } SP_Sync_Meta()->log(new WP_Error('debug', "SP_Debug.{$action} (@" . self::split() . ") : {$value}")); }
/** * Cancel the indexing process. */ public function cancel_reindex() { wp_clear_scheduled_hook('sp_reindex'); SP_Sync_Meta()->stop('save'); }
function test_sync_meta_error_notice() { SP_Sync_Meta()->running = false; $message = rand_str(); SP_Sync_Meta()->log(new WP_Error('error', $message)); $this->assertEquals($message, SP_Sync_Meta()->messages['error'][0]); $this->assertTrue(SP_Sync_Meta()->has_errors()); SP_Sync_Meta()->clear_error_notice(); $this->assertFalse(SP_Sync_Meta()->has_errors()); }
function test_singular_index_non_200() { // This domain is used in unit tests, and we'll get a 404 from trying to use it with ES SP_Config()->update_settings(array('host' => 'http://asdftestblog1.files.wordpress.com', 'active' => true)); // Because we changed the host, we have to re-init SP_API SP_API()->setup(); $posts = array($this->factory->post->create(array('post_title' => 'searchpress'))); $this->assertNotEmpty(SP_Sync_Meta()->messages['error']); $this->assertTrue(SP_Sync_Meta()->has_errors()); }
public function admin_notices() { if (isset($_GET['page']) && 'searchpress' == $_GET['page']) { return; } elseif (SP_Sync_Meta()->running) { printf('<div class="updated"><p>%s <a href="%s">%s</a></p></div>', __('SearchPress sync is currently running.', 'searchpress'), admin_url('tools.php?page=searchpress'), __('View status', 'searchpress')); } elseif (SP_Config()->must_init()) { printf('<div class="updated error"><p>%s <a href="%s">%s</a></p></div>', __('SearchPress needs to be configured and synced before you can use it.', 'searchpress'), admin_url('tools.php?page=searchpress'), __('Go to SearchPress Settings', 'searchpress')); } elseif (SP_Sync_Meta()->has_errors()) { printf('<div class="updated error"><p>%s <a href="%s">%s</a></p></div>', __('SearchPress encountered an error while syncing.', 'searchpress'), admin_url('tools.php?page=searchpress#sp-log'), __('Go to Log', 'searchpress')); } }
/** * Index the current site or individual posts in elasticsearch, optionally flushing any existing data and adding the document mapping. * * ## OPTIONS * * [--flush] * : Flushes out the current data * * [--put-mapping] * : Adds the document mapping in SP_Config() * * [--bulk=<num>] * : Process this many posts as a time. Defaults to 2,000, which seems to * be the fastest on average. * * [--limit=<num>] * : How many posts to process. Defaults to all posts. * * [--page=<num>] * : Which page to start on. This is helpful if you encountered an error on * page 145/150 or if you want to have multiple processes running at once * * [--after-date=<date>] * : Index posts published on or after this date. Use YYYY-MM-DD. * * [--before-date=<date>] * : Index posts published on or before this date. Use YYYY-MM-DD. * * [<post-id>] * : By default, this subcommand will query posts based on ID and pagination. * Instead, you can specify one or more individual post IDs to process. Multiple * post IDs should be space-delimited (see examples) * If present, the --bulk, --limit, and --page arguments are ignored. * * ## EXAMPLES * * # Flush the current document index, add the mapping, and index the whole site * wp searchpress index --flush --put-mapping * * # Index the first 10 posts in the database * wp searchpress index --bulk=10 --limit=10 * * # Index the whole site starting on page 145 * wp searchpress index --page=145 * * # Index a single post (post ID 12345) * wp searchpress index 12345 * * # Index six specific posts * wp searchpress index 12340 12341 12342 12343 12344 12345 * * # Index posts published between 11-1-2015 and 12-30-2015 (inclusive) * wp searchpress index --after-date=2015-11-01 --before-date=2015-12-30 * * # Index posts published after 11-1-2015 (inclusive) * wp searchpress index --after-date=2015-11-01 * * @synopsis [--flush] [--put-mapping] [--bulk=<num>] [--limit=<num>] [--page=<num>] [--after-date=<date>] [--before-date=<date>] [<post-id>] */ public function index($args, $assoc_args) { ob_end_clean(); $timestamp_start = microtime(true); if ($assoc_args['flush']) { $this->flush(); } if ($assoc_args['put-mapping']) { $this->put_mapping(); } if (!empty($args)) { # Individual post indexing $num_posts = count($args); WP_CLI::line(sprintf(_n("Indexing %d post", "Indexing %d posts", $num_posts), $num_posts)); foreach ($args as $post_id) { $post_id = intval($post_id); if (!$post_id) { continue; } WP_CLI::line("Indexing post {$post_id}"); SP_Sync_Manager()->sync_post($post_id); } WP_CLI::success("Index complete!"); } else { # Bulk indexing $assoc_args = array_merge(array('bulk' => 2000, 'limit' => 0, 'page' => 1), $assoc_args); if ($assoc_args['limit'] && $assoc_args['limit'] < $assoc_args['bulk']) { $assoc_args['bulk'] = $assoc_args['limit']; } if (isset($assoc_args['after-date']) || isset($assoc_args['before-date'])) { $this->date_range = array(); if (isset($assoc_args['after-date'])) { $this->date_range['after'] = $assoc_args['after-date']; } if (isset($assoc_args['before-date'])) { $this->date_range['before'] = $assoc_args['before-date']; } add_filter('searchpress_index_loop_args', array($this, '__apply_date_range')); add_filter('searchpress_index_count_args', array($this, '__apply_date_range')); } $limit_number = $assoc_args['limit'] > 0 ? $assoc_args['limit'] : SP_Sync_Manager()->count_posts(); $limit_text = sprintf(_n('%s post', '%s posts', $limit_number), number_format($limit_number)); WP_CLI::line("Indexing {$limit_text}, " . number_format($assoc_args['bulk']) . " at a time, starting on page {$assoc_args['page']}"); # Keep tabs on where we are and what we've done $sync_meta = SP_Sync_Meta(); $sync_meta->page = intval($assoc_args['page']) - 1; $sync_meta->bulk = $assoc_args['bulk']; $sync_meta->running = true; $total_pages = $limit_number / $sync_meta->bulk; $total_pages_ceil = ceil($total_pages); $start_page = $sync_meta->page; do { $lap = microtime(true); SP_Sync_Manager()->do_index_loop(); if (0 < $sync_meta->page - $start_page) { $seconds_per_page = (microtime(true) - $timestamp_start) / ($sync_meta->page - $start_page); WP_CLI::line("Completed page {$sync_meta->page}/{$total_pages_ceil} (" . number_format(microtime(true) - $lap, 2) . 's / ' . round(memory_get_usage() / 1024 / 1024, 2) . 'M current / ' . round(memory_get_peak_usage() / 1024 / 1024, 2) . 'M max), ' . $this->time_format(($total_pages - $sync_meta->page) * $seconds_per_page) . ' remaining'); } $this->contain_memory_leaks(); if ($assoc_args['limit'] > 0 && $sync_meta->processed >= $assoc_args['limit']) { break; } } while ($sync_meta->page < $total_pages_ceil); $errors = !empty($sync_meta->messages['error']) ? count($sync_meta->messages['error']) : 0; $errors += !empty($sync_meta->messages['warning']) ? count($sync_meta->messages['warning']) : 0; WP_CLI::success(sprintf(__("Index Complete!\n%d\tposts processed\n%d\tposts indexed\n%d\terrors/warnings", 'searchpress'), $sync_meta->processed, $sync_meta->success, $errors)); $this->activate(); } $this->finish($timestamp_start); }
/** * Initialize a cron reindexing. */ public function do_cron_reindex() { SP_Sync_Meta()->start(); SP_Sync_Meta()->total = $this->count_posts(); SP_Sync_Meta()->save(); SP_Cron()->schedule_reindex(); }