Index: drupalorg_search/drupalorg_search.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalorg/drupalorg_search/drupalorg_search.module,v
retrieving revision 1.14
diff -u -r1.14 drupalorg_search.module
--- drupalorg_search/drupalorg_search.module	6 Jul 2010 23:45:27 -0000	1.14
+++ drupalorg_search/drupalorg_search.module	5 Aug 2010 01:28:01 -0000
@@ -24,14 +24,14 @@
     if (isset($node->taxonomy[DRUPALORG_MODULE_TID])) {
       return 'module';
     }
-    else if (isset($node->taxonomy[DRUPALORG_THEME_TID])) {
+    elseif (isset($node->taxonomy[DRUPALORG_THEME_TID])) {
       return 'theme';
     }
   }
-  else if ($node->type == 'book') {
+  elseif ($node->type == 'book') {
     return 'documentation';
   }
-  else if ($node->type == 'project_issue' || $node->type == 'forum') {
+  elseif ($node->type == 'project_issue' || $node->type == 'forum') {
     return 'forum-issues';
   }
 }
@@ -67,12 +67,17 @@
  * Returns an array keyed by block delta.
  */
 function drupalorg_search_apachesolr_facets() {
-  $facets = array();
-
-  $facets['meta_type'] = array(
-    'info' => t('Drupalorg Search: Filter by meta type'),
-    'facet_field' => '{!ex=meta_type}ss_meta_type',
-    'exclude_filter' => '',
+  $facets = array(
+    'meta_type' => array(
+      'info' => t('Drupalorg Search: Filter by meta type'),
+      'facet_field' => '{!ex=meta_type}ss_meta_type',
+      'exclude_filter' => '',
+    ),
+    'sort_title' => array(
+      'info' => t('New modules'),
+      'facet_field' => 'created',
+      'direction' => 'desc',
+    ),
   );
 
   return $facets;
@@ -120,7 +125,7 @@
     }
     return $blocks;
   }
-  else if ($op == 'view' && $delta == 'search_box') {
+  elseif ($op == 'view' && $delta == 'search_box') {
     if (apachesolr_has_searched() && ($response = apachesolr_static_response_cache()) && ($query = apachesolr_current_query())) {
       $querystring = $query->get_query_basic();
     }
@@ -137,7 +142,7 @@
       );
     }
   }
-  else if ($op == 'view' && $delta == 'meta_type') {
+  elseif ($op == 'view' && $delta == 'meta_type') {
     if (apachesolr_has_searched() && ($response = apachesolr_static_response_cache()) && ($query = apachesolr_current_query())) {
       $entries = array();
       $total = 0;
@@ -240,3 +245,97 @@
   unset($query['q']);
   $form_state['redirect'] = array('search/' . arg(1) . '/' . $form_state['values']['query'], $query);
 }
+
+/**
+ * Base query required for creating the download page context for solr-related
+ * blocks. Creates and caches a solr query for use by block elements.
+ */
+function drupalorg_search_execute_base_query() {
+  // Build a simple query.
+  $base_path = 'project/modules';
+  $query = apachesolr_drupal_query('', '', '', $base_path);
+
+  $params = array(
+    // The fields to return.
+    'fl' => 'id,nid,title',
+    'start' => 0,
+    // We don't actually need to return rows.
+    'rows' => 1,
+    // We need to be able to facet on the project vid value
+    // so we can pull project categories.
+    'facet.field' => array(
+      'im_vid_' . _project_get_vid(),
+    ),
+    // Additional criterias.
+    'fq' => array(
+      'type:project_project',
+      'im_vid_' . _project_get_vid() . ':' . DRUPALORG_MODULE_TID,
+    ),
+    'facet' => 'true',
+    'facet.mincount' => 1,
+    'facet.sort' => 'true',
+  );
+
+  apachesolr_search_add_facet_params($params, $query);
+
+  // Cache the built query. Since all the built queries go through
+  // this process, all the hook_invocations will happen later.
+  apachesolr_current_query($query);
+
+  // This hook allows modules to modify the query and params objects.
+  apachesolr_modify_query($query, $params, 'drupalorg_search_execute_base_query');
+
+  $solr = apachesolr_get_solr();
+  $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params);
+
+  apachesolr_static_response_cache($response);
+  apachesolr_has_searched(TRUE);
+}
+
+function drupalorg_search_load_facet_block($module, $delta, &$variables) {
+  // Load the title, since that can be modified administratively.  
+  $title = db_result(db_query("SELECT title FROM {blocks} WHERE module = '%s' AND delta = '%s')", $module, $delta));
+
+  $block_output = module_invoke($module, 'block', 'view', $delta);
+  foreach ($block_output as $k => $v) {
+    $block->$k = $v;
+  }
+  
+  // Set the module and delta so that the blocks contain
+  // at least the minimum of expected information.
+  $block->module = $module;
+  $block->delta = $delta;
+  
+  if (isset($block->content) && $block->content) {
+    // Override default block title if a custom display title is present.
+    if ($title) {
+      // Check plain here to allow module generated titles to keep any markup.
+      $block->subject = $title == '<none>' ? '' : check_plain($title);
+    }
+    if (!isset($block->subject)) {
+      $block->subject = '';
+    }    
+  }
+  return $block;
+}
+
+function drupalorg_search_preprocess_drupalorg_download(&$variables) {
+  drupalorg_search_execute_base_query();
+  
+  $facets = array(
+    'drupalorg_order_facet:sort_most_installed',
+    'project_solr:project_solr_categories',
+    'drupalorg_order_facet:sort_title',
+  );  
+  // Preprocess the facet blocks.  
+  $line = array();
+  foreach ($facets as $facet) {
+    list($module, $delta) = explode(':', $facet);
+    $block = drupalorg_search_load_facet_block($module, $delta, $variables);
+    $rendered_facet = theme('block', $block);
+    // Since we control what facets we're building, we can just utilize the
+    // delta for the sake of brevity.
+    $variables[$delta] = $rendered_facet;
+  }  
+  
+}
