? SolrPhpClient
Index: Solr_Base_Query.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/Solr_Base_Query.php,v
retrieving revision 1.1.4.24
diff -u -p -r1.1.4.24 Solr_Base_Query.php
--- Solr_Base_Query.php	3 Apr 2009 14:37:32 -0000	1.1.4.24
+++ Solr_Base_Query.php	6 Apr 2009 22:09:10 -0000
@@ -1,12 +1,12 @@
 <?php
 // $Id: Solr_Base_Query.php,v 1.1.4.24 2009/04/03 14:37:32 pwolanin Exp $
 
-class Solr_Base_Query {
+class Solr_Base_Query implements Drupal_Solr_Query_Interface {
 
   /**
    * Extract all uses of one named field from a filter string e.g. 'type:book'
    */
-  static function field_extract(&$filters, $name) {
+  static function filter_extract(&$filters, $name) {
     $queries = array();
     $values = array();
     // Range queries.  The "TO" is case-sensitive.
@@ -30,13 +30,13 @@ class Solr_Base_Query {
    * Takes an array $field and combines the #name and #value in a way
    * suitable for use in a Solr query.
    */
-  static function make_field(array $field) {
+  static function make_filter(array $filter) {
     // If the field value has spaces, or : in it, wrap it in double quotes.
     // unless it is a range query.
-    if (preg_match('/[ :]/', $field['#value']) && !preg_match('/[\[\{]\S+ TO \S+[\]\}]/', $field['#value'])) {
-      $field['#value'] = '"'. $field['#value']. '"';
+    if (preg_match('/[ :]/', $filter['#value']) && !preg_match('/[\[\{]\S+ TO \S+[\]\}]/', $filter['#value'])) {
+      $filter['#value'] = '"'. $filter['#value']. '"';
     }
-    return $field['#name'] . ':' . $field['#value'];
+    return $filter['#name'] . ':' . $filter['#value'];
   }
 
   /**
@@ -54,7 +54,6 @@ class Solr_Base_Query {
    * is an array with #name and #value properties.  Each value is a
    * used for filter queries, e.g. array('#name' => 'uid', '#value' => 0)
    * for anonymous content.
-
    */
   protected $fields;
 
@@ -113,15 +112,15 @@ class Solr_Base_Query {
     $this->id = ++self::$idCount;
   }
 
-  function add_field($field, $value) {
+  function add_filter($field, $value) {
     $this->fields[] = array('#name' => $field, '#value' => trim($value));
   }
 
-  public function get_fields() {
+  public function get_filters() {
     return $this->fields;
   }
 
-  public function remove_field($name, $value = NULL) {
+  public function remove_filter($name, $value = NULL) {
     // We can only remove named fields.
     if (empty($name)) {
       return;
@@ -142,7 +141,7 @@ class Solr_Base_Query {
     }
   }
 
-  public function has_field($name, $value) {
+  public function has_filter($name, $value) {
     foreach ($this->fields as $pos => $values) {
       if (!empty($values['#name']) && !empty($values['#value']) && $values['#name'] == $name && $values['#value'] == $value) {
         return TRUE;
@@ -179,12 +178,12 @@ class Solr_Base_Query {
    * OR.
    *
    * @param $query
-   *   An instance of Solr_Base_Query.
+   *   An instance of Drupal_Solr_Query_Interface.
    *
    * @param $operator
    *   'AND' or 'OR'
    */
-  function add_subquery(Solr_Base_Query $query, $fq_operator = 'OR', $q_operator = 'AND') {
+  function add_subquery(Drupal_Solr_Query_Interface $query, $fq_operator = 'OR', $q_operator = 'AND') {
     $this->subqueries[$query->id] = array('#query' => $query, '#fq_operator' => $fq_operator, '#q_operator' => $q_operator);
   }
 
@@ -227,6 +226,20 @@ class Solr_Base_Query {
   public function get_query_basic() {
     return $this->rebuild_query();
   }
+  
+  /**
+   * return the search path
+   * this class assumes its always through the search api
+   *
+   * @param string $new_keywords
+   * if we are using new keywords as our query string
+   */
+  public function get_path($new_keywords = NULL) {
+    if ($new_keywords) {
+      return 'search/' . arg(1) . '/' . $new_keywords;
+    }
+    return 'search/' . arg(1) . '/' . $this->get_query_basic();
+  }
 
   /**
    * Build additional breadcrumb elements relative to $base.
@@ -245,7 +258,7 @@ class Solr_Base_Query {
       if (isset($this->field_map[$name])) {
         $field['#name'] = $this->field_map[$name];
       }
-      $progressive_crumb[] = Solr_Base_Query::make_field($field);
+      $progressive_crumb[] = Solr_Base_Query::make_filter($field);
       $options = array('query' => 'filters=' . implode(' ', $progressive_crumb));
       if ($themed = theme("apachesolr_breadcrumb_{$name}", $field['#value'])) {
         $breadcrumb[] = l($themed, $base, $options);
@@ -277,7 +290,7 @@ class Solr_Base_Query {
       // Look for a field alias.
       $alias = isset($this->field_map[$name]) ? $this->field_map[$name] : $name;
       // Get the values for $name
-      $extracted = Solr_Base_Query::field_extract($filters, $alias);
+      $extracted = Solr_Base_Query::filter_extract($filters, $alias);
       if (count($extracted['values'])) {
         foreach ($extracted['values'] as $index => $value) {
           $pos = strpos($this->filters, $extracted['queries'][$index]);
@@ -306,7 +319,7 @@ class Solr_Base_Query {
       if ($aliases && isset($this->field_map[$field['#name']])) {
         $field['#name'] = $this->field_map[$field['#name']];
       }
-      $fq[] = Solr_Base_Query::make_field($field);
+      $fq[] = Solr_Base_Query::make_filter($field);
     }
     foreach ($this->subqueries as $id => $data) {
       $subfq = $data['#query']->rebuild_fq($aliases);
Index: apachesolr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v
retrieving revision 1.1.2.12.2.123
diff -u -p -r1.1.2.12.2.123 apachesolr.module
--- apachesolr.module	3 Apr 2009 14:37:32 -0000	1.1.2.12.2.123
+++ apachesolr.module	6 Apr 2009 22:09:10 -0000
@@ -695,7 +695,7 @@ function apachesolr_block($op = 'list', 
             }
 
             $sort_links = array();
-            $path = 'search/' . arg(1) . '/' . $query->get_query_basic();
+            $path = $query->get_path();
             $new_query = clone $query;
             foreach ($sorts as $type => $sort) {
               $new_sort = isset($solrsorts[$type]) ? $solrsorts[$type] == 'asc' ? 'desc' : 'asc' : $sort['default'];
@@ -747,17 +747,17 @@ function apachesolr_facet_block($respons
         $facet_text = $facet;
       }
       $new_query = clone $query;
-      if ($active = $query->has_field($facet_field, $facet)) {
+      if ($active = $query->has_filter($facet_field, $facet)) {
         $contains_active = TRUE;
-        $new_query->remove_field($facet_field, $facet);
+        $new_query->remove_filter($facet_field, $facet);
         // TODO: don't assume 'search' - find the real path.
-        $path = 'search/'. arg(1) .'/'. $new_query->get_query_basic();
+        $path = $new_query->get_path();
         $querystring = $new_query->get_url_querystring();
         $unclick_link = theme('apachesolr_unclick_link', $path, $querystring);
       }
       else {
-        $new_query->add_field($facet_field, $facet);
-        $path = 'search/'. arg(1) .'/'. $new_query->get_query_basic();
+        $new_query->add_filter($facet_field, $facet);
+        $path = $new_query->get_path();
         $querystring = $new_query->get_url_querystring();
       }
       $countsort = $count == 0 ? '' : 1 / $count;
@@ -823,7 +823,7 @@ function apachesolr_date_facet_block($re
         $facet_text = apachesolr_date_format_iso_by_gap(substr($gap, 2), $facet);
       }
       $new_query = clone $query;
-      $new_query->add_field($facet_field, "[$facet TO {$range_end[$facet]}]");
+      $new_query->add_filter($facet_field, "[$facet TO {$range_end[$facet]}]");
       $path = 'search/'. arg(1) .'/'. $new_query->get_query_basic();
       $querystring = $new_query->get_url_querystring();
 
@@ -1057,7 +1057,7 @@ function apachesolr_facetcount_save($edi
  *
  * function my_module_apachesolr_modify_query(&$query, &$params) {
  *   // I only want to see articles by the admin!
- *   $query->add_field("uid", 1);
+ *   $query->add_filter("uid", 1);
  *
  * }
  */
@@ -1100,7 +1100,7 @@ function apachesolr_has_searched($search
  */
 function apachesolr_get_solr($host = NULL, $port = NULL, $path = NULL) {
   static $solr_cache;
-
+  
   if (empty($host)) {
     $host = variable_get('apachesolr_host', 'localhost');
   }
@@ -1144,7 +1144,6 @@ function apachesolr_static_response_cach
  * The query object is built from the keys, filters, and sort.
  */
 function apachesolr_drupal_query($keys = '', $filters = '', $solrsort = '') {
-
   list($module, $class) = variable_get('apachesolr_query_class', array('apachesolr', 'Solr_Base_Query'));
   include_once drupal_get_path('module', $module) .'/'. $class .'.php';
 
@@ -1155,37 +1154,20 @@ function apachesolr_drupal_query($keys =
     watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR);
     $query = NULL;
   }
+  
   return $query;
 }
 
 /**
- * Factory function for query objects representing the current search URL.
- *
- * The query object is built from the keys in the URL, but these may be
- * overridden by passing in parameters.
+ * Static getter/setter for the current query
  */
-function apachesolr_current_query($keys = '', $filters = '', $solrsort = '', $reset = FALSE) {
-  static $_queries = array();
-
-  if ($reset) {
-    $_queries = array();
-  }
-
-  if (empty($keys)) {
-    $keys = search_get_keys();
-  }
-  if (empty($filters) && !empty($_GET['filters'])) {
-    $filters = $_GET['filters'];
+function apachesolr_current_query($query = NULL) {
+  static $query_obj = NULL;
+  if (!empty($query)) {
+    $query_obj = $query;
   }
-  if (empty($solrsort) && !empty($_GET['solrsort'])) {
-    $solrsort = $_GET['solrsort'];
-  }
-  $index = $keys . '&filters=' . $filters;
-
-  if (empty($_queries) || !array_key_exists($index, $_queries)) {
-    $_queries[$index] = apachesolr_drupal_query($keys, $filters, $solrsort);
-  }
-  return is_object($_queries[$index]) ? clone $_queries[$index] : $_queries[$index];
+  
+  return $query_obj;
 }
 
 /**
@@ -1343,3 +1325,76 @@ function theme_apachesolr_sort_list($ite
   return theme('item_list', $items);
 }
 
+/**
+ * The interface for which all 'query' objects talk
+ */
+interface Drupal_Solr_Query_Interface {
+  /**
+   * Checks to see if a specific filter is already present.
+   *
+   * @param string $field
+   * the facet field to check
+   *
+   * @param string $value
+   * The facet value to check against
+   */
+  function has_filter($field, $value);
+  
+  /**
+   * Remove a filter from the query
+   *
+   * @param string $field
+   * the facet field to remove
+   *
+   * @param string $value
+   * The facet value to remove
+   * This value can be NULL
+   */
+  function remove_filter($field, $value = NULL);
+  
+  /**
+   * Add a filter to a query
+   *
+   * @param string $field
+   * the facet field to apply to this query
+   *
+   * @param string value
+   * the value of the facet to apply
+   */
+  function add_filter($field, $value);
+  
+  /**
+   * return the search path
+   */
+  function get_path();
+  
+  /**
+   * Return any query string for use in the l function.
+   *
+   * @see l()
+   */
+  function get_url_querystring();
+  
+  /**
+   * return the basic string query
+   */
+  function get_query_basic();
+  
+  /**
+   * Set the solrsort.
+   *
+   * @param string raw string to set the sort to
+   */
+  function set_solrsort($sortstring);
+  
+  /**
+   * Return an array of all filters.
+   */
+  function get_filters();
+  
+  /**
+   * Add a subquery to the query.
+   */
+  function add_subquery(Drupal_Solr_Query_Interface $query, $fq_operator = 'OR', $q_operator = 'AND');
+}
+
Index: apachesolr_search.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr_search.module,v
retrieving revision 1.1.2.6.2.80
diff -u -p -r1.1.2.6.2.80 apachesolr_search.module
--- apachesolr_search.module	3 Apr 2009 14:37:32 -0000	1.1.2.6.2.80
+++ apachesolr_search.module	6 Apr 2009 22:09:10 -0000
@@ -90,7 +90,13 @@ function apachesolr_search_search($op = 
 
       try {
         // This is the object that knows about the query coming from the user.
-        $query = apachesolr_current_query($keys);
+        if (empty($filters) && !empty($_GET['filters'])) {
+          $filters = $_GET['filters'];
+        }
+        if (empty($solrsort) && !empty($_GET['solrsort'])) {
+          $solrsort = $_GET['solrsort'];
+        }
+        $query = apachesolr_drupal_query($keys, $filters, $solrsort);
         if (is_null($query)) {
           throw new Exception(t('Could not construct a Solr query in function apachesolr_search_search()'));
         }
@@ -246,6 +252,7 @@ function apachesolr_search_search($op = 
         if (!$query) {
           return array();
         }
+        apachesolr_current_query($query);
 
         $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params);
         // The response is cached so that it is accessible to the blocks and anything
@@ -310,7 +317,7 @@ function apachesolr_search_date_range($q
   // Find the smallest filter range for $delta in the query.  $query
   // has no method to retrieve filters for $delta, so we iterate
   // through them all.
-  $fields = $query->get_fields();
+  $fields = $query->get_filters();
   foreach ($fields as $info) {
     if ($info['#name'] == $delta) {
       // $info['#value'] is the filter value.  Ideally it would also
@@ -469,16 +476,16 @@ function apachesolr_search_block($op = '
               unset($active);
               $term = taxonomy_get_term($tid);
               $new_query = clone $query;
-              if ($active = $query->has_field('tid', $tid)) {
+              if ($active = $query->has_filter('tid', $tid)) {
                 $contains_active = TRUE;
-                $new_query->remove_field('tid', $term->tid);
-                $path = 'search/' . arg(1) . '/' . $new_query->get_query_basic();
+                $new_query->remove_filter('tid', $term->tid);
+                $path = $new_query->get_path();
                 $querystring = $new_query->get_url_querystring();
                 $unclick_link = theme('apachesolr_unclick_link', $path, $querystring);
               }
               else {
-                $new_query->add_field('tid', $term->tid);
-                $path = 'search/' . arg(1) . '/' . $new_query->get_query_basic();
+                $new_query->add_filter('tid', $term->tid);
+                $path = $new_query->get_path();
                 $querystring = $new_query->get_url_querystring();
               }
               $countsort = $count == 0 ? '' : 1 / $count;
@@ -506,9 +513,8 @@ function apachesolr_search_block($op = '
 
         switch ($delta) {
           case 'currentsearch':
-            $fields = $query->get_fields();
-            $search_keys = $query->get_query_basic();
-            $path = 'search/' . arg(1) . '/' . $search_keys;
+            $fields = $query->get_filters();
+            $path = $query->get_path();
             $options = array();
             if (!$fields) {
               $options['attributes']['class'] = 'active';
@@ -517,8 +523,8 @@ function apachesolr_search_block($op = '
             foreach($fields as $field) {
               if ($field['#name']) {
                 $new_query = clone $query;
-                $new_query->remove_field($field['#name'], $field['#value']);
-                $path = 'search/'. arg(1) .'/'. $new_query->get_query_basic();
+                $new_query->remove_filter($field['#name'], $field['#value']);
+                $path = $new_query->get_path();
                 $querystring = $new_query->get_url_querystring();
                 $unclick_link = theme('apachesolr_unclick_link', $path, $querystring);
                 if (! $fielddisplay = theme("apachesolr_breadcrumb_". $field['#name'], $field['#value'])) {
@@ -615,7 +621,7 @@ function apachesolr_search_form_search_f
         '#suffix' => '</div>',
         '#type' => 'item',
         '#title' => t('Did you mean'),
-        '#value' => l($new_keywords, 'search/'. arg(1) .'/'. $new_keywords),
+        '#value' => l($new_keywords, $query->get_path($new_keywords)),
       );
     }
   }
Index: contrib/apachesolr_nodeaccess/apachesolr_nodeaccess.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/contrib/apachesolr_nodeaccess/Attic/apachesolr_nodeaccess.module,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 apachesolr_nodeaccess.module
--- contrib/apachesolr_nodeaccess/apachesolr_nodeaccess.module	20 Feb 2009 19:31:16 -0000	1.1.2.6
+++ contrib/apachesolr_nodeaccess/apachesolr_nodeaccess.module	6 Apr 2009 22:09:10 -0000
@@ -32,7 +32,7 @@ function _apachesolr_nodeaccess_build_su
     $node_access_query = apachesolr_drupal_query();
     foreach ($grants as $realm => $gids) {
       foreach ($gids as $gid) {
-        $node_access_query->add_field('nodeaccess_' . $realm, $gid);
+        $node_access_query->add_filter('nodeaccess_' . $realm, $gid);
       }
     }
     return $node_access_query;
Index: contrib/apachesolr_nodeaccess/tests/apachesolr_nodeaccess.test
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/contrib/apachesolr_nodeaccess/tests/Attic/apachesolr_nodeaccess.test,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 apachesolr_nodeaccess.test
--- contrib/apachesolr_nodeaccess/tests/apachesolr_nodeaccess.test	22 Nov 2008 10:28:17 -0000	1.1.2.1
+++ contrib/apachesolr_nodeaccess/tests/apachesolr_nodeaccess.test	6 Apr 2009 22:09:10 -0000
@@ -84,7 +84,7 @@ class DrupalApacheSolrNodeAccess extends
     $this->drupalLogin($basic_user);
 
     include_once drupal_get_path('module', 'apachesolr') .'/Solr_Base_Query.php';
-    $query = apachesolr_drupal_query();
+    $query = apachesolr_current_query();
     $params = array();
 
     $subquery = _apachesolr_nodeaccess_build_subquery($basic_user);
@@ -99,7 +99,7 @@ class DrupalApacheSolrNodeAccess extends
       "nodeaccess_nodeaccess_author" => $basic_user->uid,
     );
 
-    $fields = $subquery->get_fields();
+    $fields = $subquery->get_filters();
 
     foreach ($fields as $field) {
       if (is_array($expected_criterion[$field['#name']])) {
Index: tests/solr_base_query.test
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/tests/solr_base_query.test,v
retrieving revision 1.1.4.9
diff -u -p -r1.1.4.9 solr_base_query.test
--- tests/solr_base_query.test	21 Nov 2008 21:23:36 -0000	1.1.4.9
+++ tests/solr_base_query.test	6 Apr 2009 22:09:10 -0000
@@ -41,7 +41,7 @@ class DrupalSolrQueryTests extends Drupa
     foreach ($this->queries as $string) {
       $query = apachesolr_drupal_query($string, TRUE);
       // force the query to be rebuilt without removing any fields.
-      $query->remove_field('fake-field-name');
+      $query->remove_filter('fake-field-name');
       $this->assertEqual($string, $query->get_query());
     }
   }
@@ -49,7 +49,7 @@ class DrupalSolrQueryTests extends Drupa
   function testAddTerm() {
     foreach ($this->queries as $string) {
       $query = apachesolr_drupal_query($string, TRUE);
-      $query->add_field('wham', '1');
+      $query->add_filter('wham', '1');
       $this->assertEqual($string . ' wham:1', $query->get_query());
     }
   }
@@ -57,27 +57,27 @@ class DrupalSolrQueryTests extends Drupa
   function testRemoveTerm() {
     $string = 'foo';
     $query = apachesolr_drupal_query($string, TRUE);
-    $query->remove_field('', 'foo');
+    $query->remove_filter('', 'foo');
     $this->assertEqual('foo', $query->get_query());
 
     $string = 'foo bar';
     $query = apachesolr_drupal_query($string, TRUE);
-    $query->remove_field('', 'foo');
+    $query->remove_filter('', 'foo');
     $this->assertEqual('foo bar', $query->get_query());
 
     $string = 'foo uid:1 bar';
     $query = apachesolr_drupal_query($string, TRUE);
-    $query->remove_field('uid', '1');
+    $query->remove_filter('uid', '1');
     $this->assertEqual('foo bar', $query->get_query());
 
     $string = 'foo uid:1 bar';
     $query = apachesolr_drupal_query($string, TRUE);
-    $query->remove_field('uid');
+    $query->remove_filter('uid');
     $this->assertEqual('foo bar', $query->get_query());
 
     $string = 'foo uid:1 bar uid:2 tid:3';
     $query = apachesolr_drupal_query($string, TRUE);
-    $query->remove_field('uid', '1');
+    $query->remove_filter('uid', '1');
 
     // Not very beautiful, but probably best way there is:
     $pass = TRUE;
@@ -103,7 +103,7 @@ class DrupalSolrQueryTests extends Drupa
 
     $string = 'foo uid:1 bar uid:2 tid:3';
     $query = apachesolr_drupal_query($string, TRUE);
-    $query->remove_field('uid');
+    $query->remove_filter('uid');
     $this->assertEqual('foo bar tid:3', $query->get_query());
   }
 }
