/** * An ajax method that will search the index (you can specify the driver) * and return results in JSON format. */ public static function search() { $driver = WPSearch_Utility::arrayGet($_POST, 'driver'); $term = WPSearch_Utility::arrayGet($_POST, 'q'); $page = WPSearch_Utility::arrayGet($_POST, 'p', 0); $per_page = WPSearch_Utility::arrayGet($_POST, 'n', 5); if (!$term) { die(json_encode(array('success' => false))); } try { $search = new WPSearch_Search($driver); $start = microtime(true); $results = $search->search($term, $page, $per_page); $search_time = round(microtime(true) - $start, 3); # Return seconds without the leading 0 if (substr($search_time, 0, 1) == '0') { $search_time = substr($search_time, 1); } } catch (WPSearch_Exception $ex) { WPSearch_Log::add('error', "Exception during AJAX search call: " . $ex->__toString()); die(json_encode(array('success' => false))); } die(json_encode(array('success' => true, 'search_time' => $search_time, 'result' => $results))); }
/** * Get information about when the last index was built, whether it is * currently running a full build, and it's progress * @return object An object with properties: * 'last_index_build' * 'is_building' * 'total_to_index' * 'total_in_index' */ public function getStats($cache = TRUE) { if (self::$_stats === NULL || $cache === FALSE) { self::$_stats = $this->_driver->getStats(); } return self::$_stats; }
/** * The callback that is executed when the user is loading the admin page. * Basically, output the page content for the admin page. The function * acts just like a controller method for and MVC app. That is, it loads * a view. */ public function adminMenuCallback() { WPSearch_Log::add('debug', "Admin page callback executed"); WPSearch_Utility::sendInstallReportIfNew(); $stats = WPSearch_Search::instance()->getStats(); $about = WPSearch_Search::instance()->getAbout(); $errors = WPSearch_Search::instance()->runTests(); $search_types = unserialize(WPSearch_Utility::getOption(self::KEY_SEARCH_TYPES, FALSE)); $search_types = $search_types !== FALSE ? $search_types : self::$_defaultSearchTypes; $data = array(); if ($about) { $data['is_alive'] = true; $data['total_docs'] = $stats->total; $data['indexed_docs'] = $stats->current; $data['is_building'] = $stats->reindexing; $data['last_reindex'] = $stats->last_rebuild; $data['about'] = $about; $data['errors'] = $errors; $data['searched_types'] = $search_types; $data['post_types'] = get_post_types(); $data['index_comments'] = WPSearch_Utility::getOption(self::KEY_INDEX_COMMENTS) == 'true'; $data['index_categories'] = WPSearch_Utility::getOption(self::KEY_INDEX_CATEGORIES) == 'true'; $data['indexed_comments'] = WPSearch_Utility::getOption(self::KEY_INDEXED_COMMENTS) == 'true'; $data['indexed_categories'] = WPSearch_Utility::getOption(self::KEY_INDEXED_CATEGORIES) == 'true'; $data['title_boost'] = WPSearch_Utility::getOption(self::KEY_TITLE_BOOST, self::DEFAULT_TITLE_BOOST); $data['content_boost'] = WPSearch_Utility::getOption(self::KEY_CONTENT_BOOST, self::DEFAULT_CONTENT_BOOST); $data['service_tag'] = WPSearch_Utility::getServiceTag(); } else { $data['is_alive'] = false; $data['error_message'] = 'The backend WPSearch Driver is not responding.'; } WPSearch_View::load('admin', $data); }