diff --git a/Solr_Base_Query.php b/Solr_Base_Query.php index 6671d5a..b2f472f 100644 --- a/Solr_Base_Query.php +++ b/Solr_Base_Query.php @@ -273,7 +273,7 @@ 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'); // A flag to allow the search to be aborted. @@ -300,11 +300,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(); @@ -335,6 +336,25 @@ class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterfa 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 'q.op' => TRUE, // http://wiki.apache.org/solr/SearchHandler#q.op diff --git a/apachesolr.interface.inc b/apachesolr.interface.inc index a72dd25..a296ed3 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 d0279b7..cee6cbb 100644 --- a/apachesolr.module +++ b/apachesolr.module @@ -1546,7 +1546,7 @@ function apachesolr_static_response_cache($searcher, $response = NULL) { * * @throws Exception */ -function apachesolr_drupal_query($name, array $params = array(), $solrsort = '', $base_path = '', DrupalApacheSolrServiceInterface $solr = NULL) { +function apachesolr_drupal_query($name, array $params = array(), $solrsort = '', $base_path = '', DrupalApacheSolrServiceInterface $solr = NULL, $context = array()) { if (!interface_exists('DrupalSolrQueryInterface')) { require_once(dirname(__FILE__) . '/apachesolr.interface.inc'); } @@ -1561,7 +1561,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 4d83b63..8f61225 100644 --- a/apachesolr_search.module +++ b/apachesolr_search.module @@ -62,7 +62,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) { @@ -507,7 +509,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'] = array( @@ -628,7 +632,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( @@ -649,7 +653,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])) { @@ -876,7 +880,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') { @@ -888,7 +894,9 @@ function apachesolr_search_search_results($keys = NULL, $conditions = NULL, $sea elseif (($keys || isset($conditions['f'])) || ($empty_search_behavior == 'results')) { $params['q'] = $keys; // Hardcoded apachesolr name since we rely on this for the facets - $results = apachesolr_search_run('apachesolr', $params, $solrsort, $search_page['search_path'], pager_find_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'], pager_find_page(), $solr, $context); } } catch (Exception $e) { @@ -1011,8 +1019,8 @@ function theme_apachesolr_search_browse_blocks($vars) { /** * 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); @@ -1038,11 +1046,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 cc2c782..08998b2 100644 --- a/tests/Dummy_Solr.php +++ b/tests/Dummy_Solr.php @@ -446,5 +446,29 @@ class DummySolr implements DrupalApacheSolrServiceInterface { */ function getSolrVersion() { } + + /** + * 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..7f92cc8 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 = '') { + 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); }