Convert Tracker to DB:TNG.

From: Damien Tournoud <damien@tournoud.net>


---
 tracker/tracker.pages.inc |   48 ++++++++++++++++++++++++++++++---------------
 1 files changed, 32 insertions(+), 16 deletions(-)

diff --git modules/tracker/tracker.pages.inc modules/tracker/tracker.pages.inc
index 72b1251..a5e40ed 100644
--- modules/tracker/tracker.pages.inc
+++ modules/tracker/tracker.pages.inc
@@ -14,6 +14,24 @@ function tracker_page($account = NULL, $set_title = FALSE) {
   // Add CSS
   drupal_add_css(drupal_get_path('module', 'tracker') . '/tracker.css', array('preprocess' => FALSE));
 
+  $header = array(
+    t('Type'),
+    t('Post'),
+    t('Author'),
+    t('Replies'),
+    t('Last updated')
+  );
+
+  // TODO: These queries are very expensive, see http://drupal.org/node/105639
+  $query = db_select('node', 'n', array('target' => 'slave'))
+    ->fields('n', array('nid', 'title', 'type', 'changed', 'uid'));
+  $query->join('users', 'u', 'n.uid = u.uid');
+  $query->addField('u', 'name');
+  $query->join('node_comment_statistics', 'l', 'n.nid = l.nid');
+  $query->addExpression('GREATEST(n.changed, l.last_comment_timestamp)', 'last_updated');
+  $query->addField('l', 'comment_count');
+  $query->orderBy('last_updated', 'DESC');
+
   if ($account) {
     if ($set_title) {
       // When viewed from user/%user/track, display the name of the user
@@ -21,23 +39,23 @@ function tracker_page($account = NULL, $set_title = FALSE) {
       // here and not in the menu definition.
       drupal_set_title($account->name);
     }
-  // TODO: These queries are very expensive, see http://drupal.org/node/105639
-    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {comment} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d) ORDER BY last_updated DESC';
-    $sql = db_rewrite_sql($sql);
-    $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n LEFT JOIN {comment} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d)';
-    $sql_count = db_rewrite_sql($sql_count);
-    $result = pager_query($sql, 25, 0, $sql_count, COMMENT_PUBLISHED, $account->uid, $account->uid);
-  }
-  else {
-    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 ORDER BY last_updated DESC';
-    $sql = db_rewrite_sql($sql);
-    $sql_count = 'SELECT COUNT(n.nid) FROM {node} n WHERE n.status = 1';
-    $sql_count = db_rewrite_sql($sql_count);
-    $result = pager_query($sql, 25, 0, $sql_count);
+    $query->leftJoin('comment', 'c', 'n.nid = c.nid AND (c.status = :status OR c.status IS NULL)', array(':status' => COMMENT_PUBLISHED));
+    $query->condition(db_or()
+      ->condition('n.uid', $account->uid)
+      ->condition('c.uid', $account->uid)
+    );
   }
 
+  $query->condition('n.status', 1);
+  $query->distinct();
+  $query->extend('PagerDefault')->extend('TableSort')
+    ->limit(25, 0)
+    ->setHeader($header);
+
+  $result = $query->execute();
+
   $rows = array();
-  while ($node = db_fetch_object($result)) {
+  foreach ($result as $node) {
     // Determine the number of comments:
     $comments = 0;
     if ($node->comment_count) {
@@ -62,8 +80,6 @@ function tracker_page($account = NULL, $set_title = FALSE) {
     $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '5'));
   }
 
-  $header = array(t('Type'), t('Post'), t('Author'), t('Replies'), t('Last updated'));
-
   $output = '<div id="tracker">';
   $output .= theme('table', $header, $rows);
   $output .= theme('pager', NULL, 25, 0);
