diff --git a/Solr_Base_Query.php b/Solr_Base_Query.php index 54a835d..d2a9287 100644 --- a/Solr_Base_Query.php +++ b/Solr_Base_Query.php @@ -273,6 +273,8 @@ class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterfa * environment id */ protected $name; + + protected $context = array(); // Makes sure we always have a valid sort. protected $solrsort = array('#name' => 'score', '#direction' => 'desc'); @@ -300,11 +302,12 @@ class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterfa * @param $base_path * The search base path (without the keywords) for this query, without trailing slash. */ - function __construct($name, $solr, array $params = array(), $sortstring = '', $base_path = '') { + function __construct($name, $solr, array $params = array(), $sortstring = '', $base_path = '', $context = array()) { parent::__construct(); $this->name = $name; $this->solr = $solr; - $this->addParams($params); + $this->addContext((array) $context); + $this->addParams((array) $params); $this->available_sorts = $this->defaultSorts(); $this->sortstring = trim($sortstring); $this->parseSortString(); @@ -334,6 +337,25 @@ class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterfa public function getSearcher() { return $this->name . '@' . $this->solr->getId(); } + + /** + * Get context values. + */ + public function getContext() { + return $this->context; + } + + /** + * Set context value. + */ + public function addContext(array $context) { + foreach ($context as $k => $v) { + $this->context[$k] = $v; + } + // The env_id must match that of the actual $solr object + $this->context['env_id'] = $this->solr->getId(); + return $this->context; + } protected $single_value_params = array( 'q' => TRUE, // http://wiki.apache.org/solr/SearchHandler#q diff --git a/apachesolr.interface.inc b/apachesolr.interface.inc index eb5df65..0e4657f 100644 --- a/apachesolr.interface.inc +++ b/apachesolr.interface.inc @@ -4,6 +4,27 @@ * The interface for all 'query' objects. */ interface DrupalSolrQueryInterface { + + /** + * Get query name. + */ + function getName(); + + /** + * Get query searcher name (for facetapi, views, pages, etc). + */ + function getSearcher(); + + /** + * Get context values. + */ + function getContext(); + + /** + * Set context value. + */ + function addContext(array $context); + /** * Returns all filters matching $name, if set; otherwise, returns all filters. * diff --git a/apachesolr.module b/apachesolr.module index 9c1445e..9a2c13e 100644 --- a/apachesolr.module +++ b/apachesolr.module @@ -1679,7 +1679,7 @@ function apachesolr_static_response_cache($searcher, $response = NULL) { * * @throws Exception */ -function apachesolr_drupal_query($name, array $params = array(), $solrsort = '', $base_path = '', $solr = NULL) { +function apachesolr_drupal_query($name, array $params = array(), $solrsort = '', $base_path = '', $solr = NULL, $context = array()) { if (!interface_exists('DrupalSolrQueryInterface')) { require_once(dirname(__FILE__) . '/apachesolr.interface.inc'); } @@ -1694,7 +1694,7 @@ function apachesolr_drupal_query($name, array $params = array(), $solrsort = '', if (empty($solr)) { $solr = apachesolr_get_solr(); } - return new $class($name, $solr, $params, $solrsort, $base_path); + return new $class($name, $solr, $params, $solrsort, $base_path, $context); } /** diff --git a/apachesolr_search.module b/apachesolr_search.module index 575fa40..1cca333 100644 --- a/apachesolr_search.module +++ b/apachesolr_search.module @@ -61,7 +61,9 @@ function apachesolr_search_init() { 'fq' => array(), 'rows' => 1, ); - apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr); + $context['page_id'] = $search_page_id; + $context['search_type'] = 'apachesolr_search_show_facets'; + apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr, $context); } } catch (Exception $e) { @@ -470,7 +472,9 @@ function apachesolr_search_block_view($delta = '') { $env_id = (!empty($block['mlt_env_id'])) ? $block['mlt_env_id'] : ''; try { $solr = apachesolr_get_solr($env_id); - $docs = apachesolr_search_mlt_suggestions($block, apachesolr_document_id($node->nid), $solr); + $context['search_type'] = 'apachesolr_search_mlt'; + $context['block_id'] = $delta; + $docs = apachesolr_search_mlt_suggestions($block, apachesolr_document_id($node->nid), $solr, $context); if (!empty($docs)) { $suggestions['subject'] = check_plain($block['name']); $suggestions['content'] = theme('apachesolr_search_mlt_recommendation_block', $docs, $delta); @@ -657,7 +661,7 @@ function apachesolr_search_mlt_block_load($block_id) { * * @return An array of response documents, or NULL */ -function apachesolr_search_mlt_suggestions($settings, $id, $solr = NULL) { +function apachesolr_search_mlt_suggestions($settings, $id, $solr = NULL, $context = array()) { try { $fields = array( @@ -678,7 +682,7 @@ function apachesolr_search_mlt_suggestions($settings, $id, $solr = NULL) { 'rows' => $settings['num_results'], ); // We can optionally specify a Solr object. - $query = apachesolr_drupal_query('apachesolr_mlt', $params, '', '', $solr); + $query = apachesolr_drupal_query('apachesolr_mlt', $params, '', '', $solr, $context); foreach ($fields as $form_key => $name) { if (!empty($settings[$form_key])) { @@ -965,7 +969,9 @@ function apachesolr_search_search_results($keys = NULL, $conditions = NULL, $sea if (!$keys && !isset($conditions['f']) && ($empty_search_behavior == 'browse' || $empty_search_behavior == 'blocks')) { // Pass empty search behavior as string on to apachesolr_search_search_page() // Hardcoded apachesolr name since we rely on this for the facets - apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr); + $context['page_id'] = $search_page['page_id']; + $context['search_type'] = 'apachesolr_search_browse'; + apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr, $context); $results['apachesolr_search_browse'] = $empty_search_behavior; if ($empty_search_behavior == 'browse') { @@ -983,7 +989,9 @@ function apachesolr_search_search_results($keys = NULL, $conditions = NULL, $sea // Hardcoded apachesolr name since we rely on this for the facets $page = isset($_GET['page']) ? $_GET['page'] : ''; $page = check_plain($page); - $results = apachesolr_search_run('apachesolr', $params, $solrsort, $search_page['search_path'], $page, $solr); + $context['page_id'] = $search_page['page_id']; + $context['search_type'] = 'apachesolr_search_results'; + $results = apachesolr_search_run('apachesolr', $params, $solrsort, $search_page['search_path'], $page, $solr, $context); } } catch (Exception $e) { @@ -1105,8 +1113,8 @@ function theme_apachesolr_search_browse_blocks($blocks) { /** * Execute a search with zero results rows so as to populate facets. */ -function apachesolr_search_run_empty($name, array $params = array(), $base_path = '', $solr) { - $query = apachesolr_drupal_query($name, $params, '', $base_path, $solr); +function apachesolr_search_run_empty($name, array $params = array(), $base_path = '', $solr = NULL, $context = array()) { + $query = apachesolr_drupal_query($name, $params, '', $base_path, $solr, $context); $query->addParam('rows', '0'); $solr_id = $query->solr('getId'); list($final_query, $response) = apachesolr_do_query($query); @@ -1132,11 +1140,11 @@ function apachesolr_search_run_empty($name, array $params = array(), $base_path * * @throws Exception */ -function apachesolr_search_run($name, array $params = array(), $solrsort = '', $base_path = '', $page = 0, DrupalApacheSolrServiceInterface $solr = NULL) { +function apachesolr_search_run($name, array $params = array(), $solrsort = '', $base_path = '', $page = 0, DrupalApacheSolrServiceInterface $solr = NULL, $context = array()) { // Merge the default params into the params sent in. $params += apachesolr_search_basic_params(); // This is the object that knows about the query coming from the user. - $query = apachesolr_drupal_query($name, $params, $solrsort, $base_path, $solr); + $query = apachesolr_drupal_query($name, $params, $solrsort, $base_path, $solr, $context); if ($query->getParam('q')) { apachesolr_search_add_spellcheck_params($query); diff --git a/tests/Dummy_Solr.php b/tests/Dummy_Solr.php index 84d3ca1..5e93d62 100644 --- a/tests/Dummy_Solr.php +++ b/tests/Dummy_Solr.php @@ -434,5 +434,28 @@ class DummySolr implements DrupalApacheSolrServiceInterface { */ function optimize($waitFlush = true, $waitSearcher = true, $timeout = 3600, $softCommit = false) { } -} + + /** + * Get query name. + */ + function getName() { + } + + /** + * Get query searcher name (for facetapi, views, pages, etc). + */ + function getSearcher() { + } + /** + * Get context values. + */ + function getContext() { + } + + /** + * Set context value. + */ + function addContext(array $context) { + } +} diff --git a/tests/solr_base_query.test b/tests/solr_base_query.test index 4cf550d..fec3521 100644 --- a/tests/solr_base_query.test +++ b/tests/solr_base_query.test @@ -18,6 +18,7 @@ class SolrBaseQueryTests extends DrupalUnitTestCase { require_once dirname(dirname(realpath(__FILE__))) . '/apachesolr.interface.inc'; require_once dirname(dirname(realpath(__FILE__))) . '/Solr_Base_Query.php'; require_once dirname(dirname(realpath(__FILE__))) . '/Drupal_Apache_Solr_Service.php'; + require_once dirname(dirname(realpath(__FILE__))) . '/tests/Dummy_Solr.php'; } /** @@ -26,7 +27,10 @@ class SolrBaseQueryTests extends DrupalUnitTestCase { * @see apachesolr_drupal_query(). * @return SolrBaseQuery */ - private function _apachesolr_drupal_query($name, $params = array(), $solrsort = '', $base_path = '', $solr = 'DrupalApacheSolrService') { + private function _apachesolr_drupal_query($name, $params = array(), $solrsort = '', $base_path = '', $solr = NULL) { + if (empty($solr)) { + $solr = new DummySolr(NULL); + } return new SolrBaseQuery($name, $solr, $params, $solrsort, $base_path); } diff --git a/tests/solr_base_subquery.test b/tests/solr_base_subquery.test index e7d916a..b49800c 100644 --- a/tests/solr_base_subquery.test +++ b/tests/solr_base_subquery.test @@ -20,6 +20,7 @@ class DrupalSolrFilterSubQueryTests extends DrupalUnitTestCase { require_once dirname(dirname(realpath(__FILE__))) . '/apachesolr.interface.inc'; require_once dirname(dirname(realpath(__FILE__))) . '/Solr_Base_Query.php'; require_once dirname(dirname(realpath(__FILE__))) . '/Drupal_Apache_Solr_Service.php'; + require_once dirname(dirname(realpath(__FILE__))) . '/tests/Dummy_Solr.php'; } /** @@ -28,7 +29,10 @@ class DrupalSolrFilterSubQueryTests extends DrupalUnitTestCase { * @see apachesolr_drupal_query(). * @return SolrBaseQuery */ - private function _apachesolr_drupal_query($name, $params = array(), $solrsort = '', $base_path = '', $solr = 'DrupalApacheSolrService') { + private function _apachesolr_drupal_query($name, $params = array(), $solrsort = '', $base_path = '', $solr = NULL) { + if (empty($solr)) { + $solr = new DummySolr(NULL); + } return new SolrBaseQuery($name, $solr, $params, $solrsort, $base_path); }