diff -urpN --strip-trailing-cr ../flatcomments/flatcomments_flatten/flatcomments_flatten.inc ./flatcomments_flatten/flatcomments_flatten.inc
--- ../flatcomments/flatcomments_flatten/flatcomments_flatten.inc	1970-01-01 01:00:00.000000000 +0100
+++ ./flatcomments_flatten/flatcomments_flatten.inc	2009-05-16 15:12:49.000000000 +0200
@@ -0,0 +1,109 @@
+<?php
+// $Id:  $
+
+/**
+ * @file
+ *  Administrative callbacks to provide the functionality of flattening
+ *  previously existing comments.
+ */
+
+/**
+ * Form to select content types and confirm the operation.
+ */
+function flatcomments_flatten_form() {
+  $form['types'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Flatten existing comments for content types'),
+    '#options' => node_get_types('names'),
+    '#description' => t('To remove all threading information from already existing comments, select content types you wish to process, and click "Execute". <strong>Warning: This operation breaks any previously existing threads into separate comments permanently, so there\'s no way to revert existing discussions back to threads afterwards.</strong>'),
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Execute'),
+  );
+
+  return $form;
+}
+
+/**
+ * Form validation; checks that node type(s) are selected, and all valid
+ */
+function flatcomments_flatten_form_validate($form, &$form_state) {
+  $content_types = node_get_types('names');
+  $is_selected = FALSE;
+  $is_valid = TRUE;
+
+  foreach ($form_state['values']['types'] as $type => $value) {
+    if (!empty($value)) {
+      $is_selected = TRUE;
+      if (!isset($content_types[$type])) {
+        $is_valid = FALSE;
+      }
+    }
+  }
+
+  if (!$is_selected || !$is_valid) {
+    form_set_error('types', t('You must choose at least one valid content type.'));
+  }
+}
+
+/**
+ * Form submit handler; starts a batch of required operations.
+ */
+function flatcomments_flatten_form_submit($form, &$form_state) {
+  $content_types = node_get_types('names');
+  $processed_types = array();
+  $batch = array('file' => __FILE__);
+
+  // Add operations to the batch
+  foreach ($form_state['values']['types'] as $type => $value) {
+    if (!empty($value)) {
+      $processed_types[$type] = $content_types[$type];
+      $nodes = db_query("SELECT DISTINCT c.nid FROM {comments} c INNER JOIN {node} n ON c.nid = n.nid WHERE n.type = '%s'", $type);
+      while ($node = db_fetch_array($nodes)) {
+        $batch['operations'][] = array('flatcomments_flatten_one', array($node['nid']));
+      }
+    }
+  }
+
+  // This is a replacement for 'finished' callback, so that we can pass $processed_types
+  $batch['operations'][] = array('flatcomments_flatten_finished', array($processed_types));
+
+  // Execute the batch.
+  batch_set($batch);
+  // batch_process() is not needed here, because we're inside a form
+  // submit handler.
+}
+
+/**
+ * Flatten comments for single node. This is a batch operation.
+ */
+function flatcomments_flatten_one($nid, &$context) {
+  // Collect all comment ID's for the given node (published or not), ordering
+  // by cid to get the comments in the order they were created (the same order
+  // as flat display uses).
+  $cids = db_query('SELECT cid FROM {comments} WHERE nid = %d ORDER BY cid', $nid);
+
+  // Iterate through the comments, and set each one to be start of a new thread,
+  // by clearing the parent ID, and setting the 'thread' vancode equal to the
+  // order in flat list (with no descendants). This is done as php loop rather than
+  // database query, to ensure the vancodes are consistent with comment module,
+  // and to avoid possible MySQL/PgSQL/whatever compatibility issues.
+  $order = 1;
+  while ($cid = db_fetch_array($cids)) {
+    $cid = $cid['cid'];
+    $thread = int2vancode($order++) .'/';
+    db_query("UPDATE {comments} SET pid = 0, thread = '%s' WHERE cid = %d", $thread, $cid);
+  }
+}
+
+/**
+ * Print message when finished. This is a batch operation.
+ */
+function flatcomments_flatten_finished($processed_types, &$context) {
+  $types = implode(', ', $processed_types);
+
+  drupal_set_message(t('Threading information was successfully removed from comments on %types content type(s).', array('%types' => $types)));
+  watchdog('content', 'Removed threading information from comments on %types content type(s).', array('%types' => $types));
+
+}
diff -urpN --strip-trailing-cr ../flatcomments/flatcomments_flatten/flatcomments_flatten.info ./flatcomments_flatten/flatcomments_flatten.info
--- ../flatcomments/flatcomments_flatten/flatcomments_flatten.info	1970-01-01 01:00:00.000000000 +0100
+++ ./flatcomments_flatten/flatcomments_flatten.info	2009-05-16 11:30:42.000000000 +0200
@@ -0,0 +1,5 @@
+; $Id:  $
+name = Flatcomments Flatten
+description = Apply flatcomments behavior to already existing comments
+dependencies[] = comment
+core = 6.x
diff -urpN --strip-trailing-cr ../flatcomments/flatcomments_flatten/flatcomments_flatten.module ./flatcomments_flatten/flatcomments_flatten.module
--- ../flatcomments/flatcomments_flatten/flatcomments_flatten.module	1970-01-01 01:00:00.000000000 +0100
+++ ./flatcomments_flatten/flatcomments_flatten.module	2009-05-16 11:29:01.000000000 +0200
@@ -0,0 +1,38 @@
+<?php
+// $Id: $
+
+/**
+ * @file
+ * Administrative page allowing to remove threading from any existing comments
+ * in database, after the flatcomments module is installed and/or a content type
+ * is swithed to flat mode commenting.
+ */
+
+/**
+ * Implementation of hook_help().
+ */
+function flatcomments_flatten_help($path, $arg) {
+  switch ($path) {
+    case 'admin/help#flatcomments_flatten':
+    case 'admin/content/comment/flatten':
+      return '<p>'. t('The Flatcomments Flatten module allows you to remove threading information from comments already existing in the database. This is useful when the Flatcomments module gets installed to an already existing site, and/or if a content type is switched from threaded to flat commenting mode later, in order to apply the behavior to previously existing comments.') .'</p>';
+  }
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function flatcomments_flatten_menu() {
+  $items = array();
+
+  $items['admin/content/comment/flatten'] = array(
+    'title' => 'Flatten comments',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('flatcomments_flatten_form'),
+    'access arguments' => array('administer comments'),
+    'file' => 'flatcomments_flatten.inc',
+    'type' => MENU_LOCAL_TASK,
+  );
+
+  return $items;
+}
