Esempio n. 1
0
 /**
  * Load a view file. The file should be located in WPSearch/Views.
  * @param string $file The filename of the view without the extenstion (assumed
  *  to be PHP)
  * @param array $data An associative array of data that be be extracted and
  *  available to the view
  */
 public static function load($file, $data = array())
 {
     $file = dirname(__FILE__) . '/Views/' . $file . '.php';
     if (!file_exists($file)) {
         WPSearch_Log::add('fatal', "View '{$file}' was not found");
         throw new Exception("View '{$file}' was not found");
     }
     # Extract the variables into the global scope so the views can use them
     extract($data);
     include $file;
 }
Esempio n. 2
0
 /**
  * The name of a timer that has already been started. Writes the stopping
  *  point and result to the log automatically
  * @param string $timer_description The description of a timer that has
  *  already been started
  * @return The number of seconds elapsed if the timer exists, false if it
  *  doesn't
  */
 public static function stop($timer_description)
 {
     if (array_key_exists($timer_description, self::$_timers)) {
         $start = self::$_timers[$timer_description];
         $stop = microtime(true);
         $seconds = round($stop - $start, 6);
         WPSearch_Log::add('info', "Stopped benchmark: {$seconds} seconds for '{$timer_description}'");
         return $seconds;
     } else {
         WPSearch_Log::add('warn', "Unknown benchmark 'stopped': {$timer_description}");
         return FALSE;
     }
 }
Esempio n. 3
0
 /**
  * Fetch a web resource by URL
  * @param string $url The HTTP URL that the request is being made to
  * @param array  $options Any PHP cURL options that are needed
  * @return object An object with properties of 'url', 'body', and 'status'
  */
 public static function fetch($url, $options = array())
 {
     if (!function_exists('curl_exec')) {
         WPSearch_Log::add('error', "cURL is not installed! Throwing an exception..");
         throw new WPSearch_Exception("The cURL module must be installed");
     }
     $curl_handle = curl_init($url);
     $options += array(CURLOPT_RETURNTRANSFER => true);
     curl_setopt_array($curl_handle, $options);
     $timer = "Call to {$url} via HTTP";
     WPSearch_Benchmark::start($timer);
     $body = curl_exec($curl_handle);
     $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
     WPSearch_Benchmark::stop($timer);
     return (object) array('url' => $url, 'body' => $body, 'status' => $status);
 }
Esempio n. 4
0
 /**
  * The Search library's constructor
  * @param string $cdriver Allow specification of the driver to be used. If
  *  this is omitted, the driver will be pulled from the config.
  */
 public function __construct($cdriver = FALSE)
 {
     if (!$cdriver) {
         $cdriver = WPSearch_Config::get('driver');
     }
     $driver = ucfirst(strtolower($cdriver));
     if (WPSearch_Config::get("driver-configs.{$cdriver}") === FALSE) {
         throw new WPSearch_Exception("Driver '{$cdriver}' chosen, but there is no config available.");
     }
     $driver_file = dirname(__FILE__) . '/Drivers/Search/' . $driver . '.php';
     if (!file_exists($driver_file)) {
         WPSearch_Log::add('fatal', "Cannot find search driver named '{$driver}'");
         throw new WPSearch_Exception("Driver '{$driver}' does not exist");
     }
     require_once $driver_file;
     $driver = 'WPSearch_Drivers_Search_' . $driver;
     if (!class_exists($driver)) {
         WPSearch_Log::add('fatal', "Driver file was loaded, but class '{$driver}' wasn't found");
         throw new WPSearch_Exception("Class '{$driver}' does not exist");
     }
     $this->_driver = new $driver();
     WPSearch_Log::add('info', "Driver '{$driver}' successfully loaded");
 }
Esempio n. 5
0
 public static function handleException(Exception $ex)
 {
     WPSearch_Log::add('error', "Exception: " . $ex->__toString());
 }
Esempio n. 6
0
 /**
  * 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)));
 }
Esempio n. 7
0
 /**
  * Set the last time the index was built and store it in a file
  * @param int $time UNIX timestamp
  */
 private function _setRebuildTime($time = FALSE)
 {
     if ($time === FALSE) {
         $time = time();
     }
     $file = WPSearch_Config::get('driver-configs.phplucene.tmp-path') . 'rebuild-time.nfo';
     $success = @file_put_contents($file, $time);
     if (!$success) {
         WPSearch_Log::add('error', "Could not write to temp file: {$file}");
     }
 }
Esempio n. 8
0
 /**
  * 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);
 }