Index: solr/project_solr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project/solr/project_solr.module,v
retrieving revision 1.67
diff -u -p -r1.67 project_solr.module
--- solr/project_solr.module	23 Jul 2010 18:33:19 -0000	1.67
+++ solr/project_solr.module	26 Jul 2010 05:16:58 -0000
@@ -741,3 +741,90 @@ function theme_project_solr_no_count_fac
   $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
   return apachesolr_l($facet_text,  $path, $options);
 }
+
+/**
+ * Query Solr for projects matching given filters
+ *
+ * @param $type
+ *   The type of projects to query, from the Project categories vocabulary.
+ * @param $api_compatability
+ *   Optional: TID from the API Compatability vocabulary to filter by.
+ * @param $sort
+ *   Optional: An array with the keys 'field' and 'direction'.
+ * @param $num_results
+ *   Optional: Number of results to return. Defaults to 10.
+ *
+ * @return
+ *   An array of matching node IDs.
+ */
+function project_solr_query_projects($type = NULL, $api_compatability = NULL, $sort = NULL, $num_results = 10) {
+  try {
+    $results = array();
+
+    include_once drupal_get_path('module', 'project_solr') .'/ProjectSolrQuery.php';
+
+    $query = new ProjectSolrQuery(apachesolr_get_solr());
+    if (is_null($query)) {
+      throw new Exception(t('Could not construct a Solr query.'));
+    }
+    
+    $params = array(
+      'fl' => 'nid',
+      'rows' => $num_results,
+    );
+
+    // This is the object that does the communication with the solr server.
+    $solr = apachesolr_get_solr();
+
+    // This hook allows modules to modify the query and params objects.
+    apachesolr_modify_query($query, $params, 'project_solr');
+    if (!$query) {
+      return array();
+    }
+
+    /*********** Filters ***********/
+
+    // Project nodes only.
+    $params['fq'][] = 'type:project_project';
+
+    // Select the correct type of project.
+    if (!empty($type)) {
+      $parent_term = db_fetch_object(db_query("SELECT t.tid, t.name, t.description FROM {term_data} t WHERE t.vid = %d AND LOWER(t.name) = LOWER('%s')", _project_get_vid(), $type));
+      $params['fq'][] = 'im_vid_'. _project_get_vid() .':'. $parent_term->tid;
+    }
+
+    // Add API compatability
+    if (!empty($api_compatability)) {
+      $params['fq'][] = 'im_project_release_api_tids:'. $api_compatability;
+    }
+
+    // Select only projects that have a release
+    if (module_exists('project_release')) {
+      $params['fq'][] = 'is_project_has_releases:1';
+    }
+
+    /********** Sorts ***********/
+
+    if (!empty($sort)) {
+      $params['sort'] = $sort['field'] .' '. $sort['direction'];
+    }
+    else {
+      // Provide default sort.
+      $params['sort'] = 'sis_project_release_usage desc';
+    }
+
+    $response = $solr->search($query->get_query_basic(), 0, $params['rows'], $params);
+
+    if ($response->response->numFound > 0) {
+      foreach ($response->response->docs as $doc) {
+        $results[] = $doc->nid;
+      }
+    }
+  }
+  catch (Exception $e) {
+    watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR);
+    apachesolr_failure(t('Solr search'), is_null($query) ? $keys : $query->get_query_basic());
+  }
+
+  return $results;
+}
