Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.631
diff -u -u -p -r1.631 comment.module
--- modules/comment/comment.module	6 May 2008 12:18:47 -0000	1.631
+++ modules/comment/comment.module	9 May 2008 14:48:28 -0000
@@ -1970,3 +1970,17 @@ function comment_unpublish_by_keyword_ac
     }
   }
 }
+
+/**
+ * Implementation of hook_ranking
+ */
+function comment_ranking() {
+  return array(
+    'comments' => array(
+      'title' => t('Number of comments'),
+      'join' => 'LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid',
+      'score' => '2.0 - 2.0 / (1.0 + c.comment_count * %f)',
+      'factors' => array(variable_get('node_cron_comments_scale', 0)),
+    ),
+  );
+}
Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.961
diff -u -u -p -r1.961 node.module
--- modules/node/node.module	6 May 2008 12:18:48 -0000	1.961
+++ modules/node/node.module	9 May 2008 14:48:28 -0000
@@ -1141,6 +1141,35 @@ function node_perm() {
   return $perms;
 }
 
+function _node_rankings() {
+  $rankings = array(
+    'total' => 0, 'join' => array(), 'score' => array(), 'terms' => array(),
+  );
+  if ($ranking = module_invoke_all('ranking')) {
+    foreach ($ranking as $rank => $values) {
+      // if the join doesn't already exist, add it
+      if (isset($values['join']) && !isset($rankings['join'][$values['join']])) {
+        $rankings['join'][$values['join']] = $values['join'];
+      }
+
+      // add the weighted score multiplier value, handle NULL gracefully
+      $rankings['score'][] = '%f * COALESCE(('. $values['score'] .'), 0)';
+
+      // add the the weighted score multiplier value
+      $node_rank = variable_get('node_rank_'. $rank, 5);
+      $rankings['total'] += $node_rank;
+      $rankings['factors'][] = $node_rank;
+
+      // add the other terms
+      if (isset($values['factors'])) {
+        $rankings['factors'] = array_merge($rankings['factors'], $values['factors']);
+      }
+    }
+  }
+  return $rankings;
+}
+
+
 /**
  * Implementation of hook_search().
  */
@@ -1170,23 +1199,14 @@ function node_search($op = 'search', $ke
         '#value' => '<em>' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '</em>'
       );
 
-      $ranking = array('node_rank_relevance' => t('Keyword relevance'),
-                       'node_rank_recent' => t('Recently posted'));
-      if (module_exists('comment')) {
-        $ranking['node_rank_comments'] = t('Number of comments');
-      }
-      if (module_exists('statistics') && variable_get('statistics_count_content_views', 0)) {
-        $ranking['node_rank_views'] = t('Number of views');
-      }
-
       // Note: reversed to reflect that higher number = higher ranking.
       $options = drupal_map_assoc(range(0, 10));
-      foreach ($ranking as $var => $title) {
-        $form['content_ranking']['factors'][$var] = array(
-          '#title' => $title,
+      foreach (module_invoke_all('ranking') as $var => $values) {
+        $form['content_ranking']['factors']['node_rank_'. $var] = array(
+          '#title' => $values['title'],
           '#type' => 'select',
           '#options' => $options,
-          '#default_value' => variable_get($var, 5),
+          '#default_value' => variable_get('node_rank_'. $var, 5),
         );
       }
       return $form;
@@ -1228,51 +1248,12 @@ function node_search($op = 'search', $ke
         $keys = search_query_insert($keys, 'language');
       }
 
-      // Build ranking expression (we try to map each parameter to a
-      // uniform distribution in the range 0..1).
-      $ranking = array();
-      $arguments2 = array();
-      $join2 = '';
-      // Used to avoid joining on node_comment_statistics twice
-      $stats_join = FALSE;
-      $total = 0;
-      if ($weight = (int)variable_get('node_rank_relevance', 5)) {
-        // Average relevance values hover around 0.15
-        $ranking[] = '%d * i.relevance';
-        $arguments2[] = $weight;
-        $total += $weight;
-      }
-      if ($weight = (int)variable_get('node_rank_recent', 5)) {
-        // Exponential decay with half-life of 6 months, starting at last indexed node
-        $ranking[] = '%d * POW(2, (GREATEST(MAX(n.created), MAX(n.changed), MAX(c.last_comment_timestamp)) - %d) * 6.43e-8)';
-        $arguments2[] = $weight;
-        $arguments2[] = (int)variable_get('node_cron_last', 0);
-        $join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
-        $stats_join = TRUE;
-        $total += $weight;
-      }
-      if (module_exists('comment') && $weight = (int)variable_get('node_rank_comments', 5)) {
-        // Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
-        $scale = variable_get('node_cron_comments_scale', 0.0);
-        $ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(c.comment_count) * %f))';
-        $arguments2[] = $weight;
-        $arguments2[] = $scale;
-        if (!$stats_join) {
-          $join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
-        }
-        $total += $weight;
-      }
-      if (module_exists('statistics') && variable_get('statistics_count_content_views', 0) &&
-          $weight = (int)variable_get('node_rank_views', 5)) {
-        // Inverse law that maps the highest view count on the site to 1 and 0 to 0.
-        $scale = variable_get('node_cron_views_scale', 0.0);
-        $ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(nc.totalcount) * %f))';
-        $arguments2[] = $weight;
-        $arguments2[] = $scale;
-        $join2 .= ' LEFT JOIN {node_counter} nc ON nc.nid = i.sid';
-        $total += $weight;
-      }
-      $select2 = (count($ranking) ? implode(' + ', $ranking) : 'i.relevance') . ' AS score';
+      // Build ranking expression
+      $rankings = _node_rankings();
+      $arguments2 = $rankings['terms'];
+      $join2 = implode(' ', $rankings['join']);
+      $select2 = '('. implode(' + ', $rankings['score']) .') AS score';
+      $total = $rankings['total'];
 
       // Do search
       $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid ' . $join1 . ' INNER JOIN {users} u ON n.uid = u.uid', $conditions1 . (empty($where1) ? '' : ' AND ' . $where1), $arguments1, $select2, $join2, $arguments2);
@@ -1309,6 +1290,28 @@ function node_search($op = 'search', $ke
 }
 
 /**
+ * Implementation of hook_ranking
+ */
+function node_ranking() {
+  // create the ranking array and add the keyword relevance scoring
+  $ranking = array(
+    'relevance' => array(
+      'title' => t('Keyword relevance'),
+      'score' => 'i.relevance',
+    ),
+  );
+  // Add relevance based on creation or changed date
+  if ($node_cron_last = variable_get('node_cron_last', 0)) {
+    $ranking['recent'] = array(
+      'title' => t('Recently posted'),
+      'score' => '(POW(2, GREATEST(n.created, n.changed) - %d) * 6.43e-8)',
+      'factors' => array($node_cron_last),
+    );
+  }
+  return $ranking;
+}
+
+/**
  * Implementation of hook_user().
  */
 function node_user($op, &$edit, &$user) {
Index: modules/statistics/statistics.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.module,v
retrieving revision 1.277
diff -u -u -p -r1.277 statistics.module
--- modules/statistics/statistics.module	6 May 2008 12:18:50 -0000	1.277
+++ modules/statistics/statistics.module	9 May 2008 14:48:29 -0000
@@ -314,3 +314,19 @@ function statistics_nodeapi(&$node, $op,
       db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
   }
 }
+
+/**
+ * Implementation of hook_ranking
+ */
+function statistics_ranking() {
+  if (variable_get('statistics_count_content_views', 0)) {
+    return array(
+      'views' => array(
+        'title' => t('Number of views'),
+        'join' => 'LEFT JOIN {node_counter} nc ON nc.nid = i.sid',
+        'score' => '2.0 - 2.0 / (1.0 + nc.totalcount * %f)',
+        'factors' => array(variable_get('node_cron_views_scale', 0)),
+      ),
+    );
+  }
+}
