Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1005
diff -u -p -r1.1005 common.inc
--- includes/common.inc	3 Oct 2009 19:16:03 -0000	1.1005
+++ includes/common.inc	4 Oct 2009 06:15:25 -0000
@@ -3964,7 +3964,6 @@ function _drupal_bootstrap_full() {
   require_once DRUPAL_ROOT . '/includes/theme.inc';
   require_once DRUPAL_ROOT . '/includes/pager.inc';
   require_once DRUPAL_ROOT . '/includes/menu.inc';
-  require_once DRUPAL_ROOT . '/includes/tablesort.inc';
   require_once DRUPAL_ROOT . '/includes/file.inc';
   require_once DRUPAL_ROOT . '/includes/unicode.inc';
   require_once DRUPAL_ROOT . '/includes/image.inc';
Index: includes/tablesort.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/tablesort.inc,v
retrieving revision 1.55
diff -u -p -r1.55 tablesort.inc
--- includes/tablesort.inc	29 Sep 2009 15:31:13 -0000	1.55
+++ includes/tablesort.inc	4 Oct 2009 06:48:47 -0000
@@ -16,7 +16,7 @@
 class TableSort extends SelectQueryExtender {
 
   /**
-   * The array of fields that can be sorted by.
+   * An array of table header columns that can be sorted by, as described in theme_table().
    *
    * @var array
    */
@@ -34,16 +34,18 @@ class TableSort extends SelectQueryExten
   /**
    * Order the query based on a header array.
    *
-   * @see theme_table()
    * @param $header
-   *   Table header array.
+   *   An array of table header columns as described in theme_table().
+   *
+   * @see theme_table()
    */
-  public function orderByHeader(Array $header) {
+  public function orderByHeader(array $header) {
     $this->header = $header;
     $ts = $this->init();
-    if (!empty($ts['sql'])) {
+    if (!empty($ts['field'])) {
+      // Remove table prefix from sorting field.
       // Based on code from db_escape_table(), but this can also contain a dot.
-      $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
+      $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['field']);
 
       // Sort order can only be ASC or DESC.
       $sort = drupal_strtoupper($ts['sort']);
@@ -67,23 +69,12 @@ class TableSort extends SelectQueryExten
    * Determine the current sort direction.
    *
    * @param $headers
-   *   An array of column headers in the format described in theme_table().
+   *   An array of table header columns as described in theme_table().
    * @return
    *   The current sort direction ("asc" or "desc").
    */
   protected function getSort() {
-    if (isset($_GET['sort'])) {
-      return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
-    }
-    // User has not specified a sort. Use default if specified; otherwise use "asc".
-    else {
-      foreach ($this->header as $header) {
-        if (is_array($header) && array_key_exists('sort', $header)) {
-          return $header['sort'];
-        }
-      }
-    }
-    return 'asc';
+    return tablesort_get_sort($this->header);
   }
 
   /**
@@ -102,38 +93,13 @@ class TableSort extends SelectQueryExten
   /**
    * Determine the current sort criterion.
    *
-   * @param $headers
-   *   An array of column headers in the format described in theme_table().
    * @return
    *   An associative array describing the criterion, containing the keys:
-   *   - "name": The localized title of the table column.
-   *   - "sql": The name of the database field to sort on.
+   *   - name: The localized title of the table column.
+   *   - field: The name of the database field to sort on.
    */
   protected function order() {
-    $order = isset($_GET['order']) ? $_GET['order'] : '';
-    foreach ($this->header as $header) {
-      if (isset($header['data']) && $order == $header['data']) {
-        return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
-      }
-
-      if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
-        $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
-      }
-    }
-
-    if (isset($default)) {
-      return $default;
-    }
-    else {
-      // The first column specified is initial 'order by' field unless otherwise specified
-      if (is_array($this->header[0])) {
-        $this->header[0] += array('data' => NULL, 'field' => NULL);
-        return array('name' => $this->header[0]['data'], 'sql' => $this->header[0]['field']);
-      }
-      else {
-        return array('name' => $this->header[0]);
-      }
-    }
+    return tablesort_get_order($this->header);
   }
 }
 
@@ -156,7 +122,7 @@ function tablesort_init($header) {
  * @param $cell
  *   The cell to format.
  * @param $header
- *   An array of column headers in the format described in theme_table().
+ *   An array of table header columns as described in theme_table().
  * @param $ts
  *   The current table sort context as returned from tablesort_init().
  * @return
@@ -166,7 +132,7 @@ function tablesort_header($cell, $header
   // Special formatting for the currently sorted column header.
   if (is_array($cell) && isset($cell['field'])) {
     $title = t('sort by @s', array('@s' => $cell['data']));
-    if ($cell['data'] == $ts['name']) {
+    if ($cell['field'] == $ts['field']) {
       $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
       $cell['class'][] = 'active';
       $image = theme('tablesort_indicator', $ts['sort']);
@@ -176,7 +142,12 @@ function tablesort_header($cell, $header
       $ts['sort'] = 'asc';
       $image = '';
     }
-    $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => array_merge($ts['query'], array('sort' => $ts['sort'], 'order' => $cell['data'])), 'html' => TRUE));
+    $options = array(
+      'attributes' => array('title' => $title),
+      'query' => array_merge($ts['query'], array('sort' => $ts['sort'], 'order' => $cell['field'])),
+      'html' => TRUE,
+    );
+    $cell['data'] = l($cell['data'] . $image, $_GET['q'], $options);
 
     unset($cell['field'], $cell['sort']);
   }
@@ -191,7 +162,7 @@ function tablesort_header($cell, $header
  * @param $cell
  *   The cell to format.
  * @param $header
- *   An array of column headers in the format described in theme_table().
+ *   An array of table header columns as described in theme_table().
  * @param $ts
  *   The current table sort context as returned from tablesort_init().
  * @param $i
@@ -200,7 +171,7 @@ function tablesort_header($cell, $header
  *   A properly formatted cell, ready for _theme_table_cell().
  */
 function tablesort_cell($cell, $header, $ts, $i) {
-  if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
+  if (is_array($header[$i]) && isset($header[$i]['field']) && $header[$i]['field'] == $ts['field']) {
     if (is_array($cell)) {
       $cell['class'][] = 'active';
     }
@@ -225,58 +196,66 @@ function tablesort_get_query_parameters(
 /**
  * Determine the current sort criterion.
  *
- * @param $headers
- *   An array of column headers in the format described in theme_table().
+ * @param $header
+ *   An array of table header columns as described in theme_table().
  * @return
  *   An associative array describing the criterion, containing the keys:
- *   - "name": The localized title of the table column.
- *   - "sql": The name of the database field to sort on.
+ *   - name: The localized title of the table column.
+ *   - field: The name of the database field to sort on.
  */
-function tablesort_get_order($headers) {
+function tablesort_get_order($header) {
   $order = isset($_GET['order']) ? $_GET['order'] : '';
-  foreach ($headers as $header) {
-    if (isset($header['data']) && $order == $header['data']) {
-      return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
-    }
-
-    if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
-      $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
+  foreach ($header as $column) {
+    if (!is_array($column) || !isset($column['field'])) {
+      continue;
+    }
+    // Use the header column matching the URL parameter.
+    if ($order == $column['field']) {
+      return array('name' => $column['data'], 'field' => $column['field']);
+    }
+    // In case no header column will match the URL parameter, and this column
+    // defines 'sort', it is supposed to be the default sorting column.
+    if (isset($column['sort']) && ($column['sort'] == 'asc' || $column['sort'] == 'desc')) {
+      $default_column = array('name' => $column['data'], 'field' => $column['field']);
+    }
+    // In case there is no default sorting header column, store the first that
+    // defines a field.
+    elseif (!isset($first_column)) {
+      $first_column = array('name' => $column['data'], 'field' => $column['field']);
     }
   }
 
-  if (isset($default)) {
-    return $default;
-  }
-  else {
-    // The first column specified is the initial 'order by' field unless otherwise specified.
-    $first = current($headers);
-    if (is_array($first)) {
-      $first += array('data' => NULL, 'field' => NULL);
-      return array('name' => $first['data'], 'sql' => $first['field']);
-    }
-    else {
-      return array('name' => $first, 'sql' => '');
-    }
-  }
+  // If there was a default sorting column, return that.
+  if (isset($default_column)) {
+    return $default_column;
+  }
+  // Otherwise, use the first header column that defines a field.
+  if (isset($first_column)) {
+    return $first_column;
+  }
+  // If we end up here, then a table header did not define any valid tablesort
+  // data.
+  throw new Exception(t('Invalid TableSort data; header needs to define at least one sorting field.'));
+  return array('name' => '', 'field' => '');
 }
 
 /**
  * Determine the current sort direction.
  *
- * @param $headers
- *   An array of column headers in the format described in theme_table().
+ * @param $header
+ *   An array of table header columns as described in theme_table().
  * @return
  *   The current sort direction ("asc" or "desc").
  */
-function tablesort_get_sort($headers) {
+function tablesort_get_sort($header) {
   if (isset($_GET['sort'])) {
     return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
   }
   // User has not specified a sort. Use default if specified; otherwise use "asc".
   else {
-    foreach ($headers as $header) {
-      if (is_array($header) && array_key_exists('sort', $header)) {
-        return $header['sort'];
+    foreach ($header as $column) {
+      if (is_array($column) && isset($column['sort'])) {
+        return $column['sort'];
       }
     }
   }
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.529
diff -u -p -r1.529 theme.inc
--- includes/theme.inc	3 Oct 2009 19:27:44 -0000	1.529
+++ includes/theme.inc	4 Oct 2009 06:25:54 -0000
@@ -1674,22 +1674,28 @@ function theme_table($header, $rows, $at
     }
   }
 
-  // Format the table header:
+  // Format the table header.
   if (count($header)) {
-    $ts = tablesort_init($header);
+    // Determine whether we need to load tablesort.
+    foreach ($header as $column) {
+      if (is_array($column) && isset($column['field'])) {
+        require_once DRUPAL_ROOT . '/includes/tablesort.inc';
+        $ts = tablesort_init($header);
+        break;
+      }
+    }
     // HTML requires that the thead tag has tr tags in it followed by tbody
     // tags. Using ternary operator to check and see if we have any rows.
     $output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
     foreach ($header as $cell) {
-      $cell = tablesort_header($cell, $header, $ts);
+      if (isset($ts)) {
+        $cell = tablesort_header($cell, $header, $ts);
+      }
       $output .= _theme_table_cell($cell, TRUE);
     }
     // Using ternary operator to close the tags based on whether or not there are rows
     $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
   }
-  else {
-    $ts = array();
-  }
 
   // Format the table rows:
   if (count($rows)) {
@@ -1722,7 +1728,9 @@ function theme_table($header, $rows, $at
         $output .= ' <tr' . drupal_attributes($attributes) . '>';
         $i = 0;
         foreach ($cells as $cell) {
-          $cell = tablesort_cell($cell, $header, $ts, $i++);
+          if (isset($ts)) {
+            $cell = tablesort_cell($cell, $header, $ts, $i++);
+          }
           $output .= _theme_table_cell($cell);
         }
         $output .= " </tr>\n";
Index: modules/forum/forum.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/forum/forum.module,v
retrieving revision 1.519
diff -u -p -r1.519 forum.module
--- modules/forum/forum.module	18 Sep 2009 00:04:22 -0000	1.519
+++ modules/forum/forum.module	4 Oct 2009 06:57:19 -0000
@@ -39,7 +39,7 @@ function forum_theme() {
   return array(
     'forums' => array(
       'template' => 'forums',
-      'arguments' => array('forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
+      'arguments' => array('forums' => NULL, 'topics' => array(), 'parents' => NULL, 'tid' => NULL, 'header' => array(), 'forum_per_page' => NULL),
     ),
     'forum_list' => array(
       'template' => 'forum-list',
@@ -47,7 +47,7 @@ function forum_theme() {
     ),
     'forum_topic_list' => array(
       'template' => 'forum-topic-list',
-      'arguments' => array('tid' => NULL, 'topics' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
+      'arguments' => array('tid' => NULL, 'topics' => array(), 'header' => array(), 'forum_per_page' => NULL),
     ),
     'forum_icon' => array(
       'template' => 'forum-icon',
@@ -697,22 +697,8 @@ function _forum_topics_unread($term, $ui
     ->fetchField();
 }
 
-function forum_get_topics($tid, $sortby, $forum_per_page) {
-  global $user, $forum_topic_list_header;
-
-  $forum_topic_list_header = array(
-    NULL,
-    array('data' => t('Topic'), 'field' => 'n.title'),
-    array('data' => t('Replies'), 'field' => 'ncs.comment_count'),
-    array('data' => t('Last reply'), 'field' => 'ncs.last_comment_timestamp'),
-  );
-
-  $order = _forum_get_topic_order($sortby);
-  for ($i = 0; $i < count($forum_topic_list_header); $i++) {
-    if ($forum_topic_list_header[$i]['field'] == $order['field']) {
-      $forum_topic_list_header[$i]['sort'] = $order['sort'];
-    }
-  }
+function forum_get_topics($tid, $header, $forum_per_page) {
+  global $user;
 
   $query = db_select('node_comment_statistics', 'ncs')->extend('PagerDefault')->extend('TableSort');
   $query->join('node', 'n', 'n.nid = ncs.nid');
@@ -732,7 +718,7 @@ function forum_get_topics($tid, $sortby,
     ->fields('ncs', array('last_comment_timestamp', 'last_comment_uid'))
     ->condition('n.status', 1)
     ->orderBy('n.sticky', 'DESC')
-    ->orderByHeader($forum_topic_list_header)
+    ->orderByHeader($header)
     ->orderBy('n.created', 'DESC')
     ->limit($forum_per_page);
 
@@ -785,7 +771,7 @@ function forum_get_topics($tid, $sortby,
  * - $topics
  * - $parents
  * - $tid
- * - $sortby
+ * - $header
  * - $forum_per_page
  *
  * @see forums.tpl.php
@@ -850,7 +836,7 @@ function template_preprocess_forums(&$va
     }
 
     if ($variables['tid'] && !in_array($variables['tid'], variable_get('forum_containers', array()))) {
-      $variables['topics'] = theme('forum_topic_list', $variables['tid'], $variables['topics'], $variables['sortby'], $variables['forum_per_page']);
+      $variables['topics'] = theme('forum_topic_list', $variables['tid'], $variables['topics'], $variables['header'], $variables['forum_per_page']);
       drupal_add_feed(url('taxonomy/term/' . $variables['tid'] . '/0/feed'), 'RSS - ' . $title);
     }
     else {
@@ -929,21 +915,21 @@ function template_preprocess_forum_list(
  *
  * $variables contains the following data:
  * - $tid
+ * - $header
  * - $topics
- * - $sortby
  * - $forum_per_page
  *
  * @see forum-topic-list.tpl.php
  * @see theme_forum_topic_list()
  */
 function template_preprocess_forum_topic_list(&$variables) {
-  global $forum_topic_list_header;
-
-  // Create the tablesorting header.
-  $ts = tablesort_init($forum_topic_list_header);
+  // Render the table header; this is required, because the forum topic list
+  // table does not use theme_table().
+  require_once DRUPAL_ROOT . '/includes/tablesort.inc';
+  $ts = tablesort_init($variables['header']);
   $header = '';
-  foreach ($forum_topic_list_header as $cell) {
-    $cell = tablesort_header($cell, $forum_topic_list_header, $ts);
+  foreach ($variables['header'] as $cell) {
+    $cell = tablesort_header($cell, $variables['header'], $ts);
     $header .= _theme_table_cell($cell, TRUE);
   }
   $variables['header'] = $header;
@@ -980,10 +966,6 @@ function template_preprocess_forum_topic
 
     }
   }
-  else {
-    // Make this safe for the template
-    $variables['topics'] = array();
-  }
   // Give meaning to $tid for themers. $tid actually stands for term id.
   $variables['topic_id'] = $variables['tid'];
   unset($variables['tid']);
@@ -1051,15 +1033,14 @@ function _forum_get_topic_order($sortby)
   switch ($sortby) {
     case 1:
       return array('field' => 'ncs.last_comment_timestamp', 'sort' => 'desc');
-      break;
+
     case 2:
       return array('field' => 'ncs.last_comment_timestamp', 'sort' => 'asc');
-      break;
+
     case 3:
       return array('field' => 'ncs.comment_count', 'sort' => 'desc');
-      break;
+
     case 4:
       return array('field' => 'ncs.comment_count', 'sort' => 'asc');
-      break;
   }
 }
Index: modules/forum/forum.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/forum/forum.pages.inc,v
retrieving revision 1.2
diff -u -p -r1.2 forum.pages.inc
--- modules/forum/forum.pages.inc	26 Jul 2007 06:48:03 -0000	1.2
+++ modules/forum/forum.pages.inc	4 Oct 2009 06:40:31 -0000
@@ -10,15 +10,27 @@
  * Menu callback; prints a forum listing.
  */
 function forum_page($tid = 0) {
-  $topics = '';
   $forum_per_page = variable_get('forum_per_page', 25);
-  $sortby = variable_get('forum_order', 1);
+
+  $header = array(
+    NULL,
+    array('data' => t('Topic'), 'field' => 'n.title'),
+    array('data' => t('Replies'), 'field' => 'ncs.comment_count'),
+    array('data' => t('Last reply'), 'field' => 'ncs.last_comment_timestamp'),
+  );
+  $order = _forum_get_topic_order(variable_get('forum_order', 1));
+  for ($i = 0; $i < count($header); $i++) {
+    if ($header[$i]['field'] == $order['field']) {
+      $header[$i]['sort'] = $order['sort'];
+    }
+  }
 
   $forums = forum_get_forums($tid);
   $parents = taxonomy_get_parents_all($tid);
+  $topics = array();
   if ($tid && !in_array($tid, variable_get('forum_containers', array()))) {
-    $topics = forum_get_topics($tid, $sortby, $forum_per_page);
+    $topics = forum_get_topics($tid, $header, $forum_per_page);
   }
 
-  return theme('forums', $forums, $topics, $parents, $tid, $sortby, $forum_per_page);
+  return theme('forums', $forums, $topics, $parents, $tid, $header, $forum_per_page);
 }
