? .git
? .gitignore
? 616324-3_adopt.patch
? data_adopt_0.patch
Index: data_ui/data_ui.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/data/data_ui/data_ui.admin.inc,v
retrieving revision 1.25.2.13
diff -u -p -r1.25.2.13 data_ui.admin.inc
--- data_ui/data_ui.admin.inc	19 Oct 2009 19:30:34 -0000	1.25.2.13
+++ data_ui/data_ui.admin.inc	28 Oct 2009 14:19:06 -0000
@@ -226,6 +226,65 @@ function data_ui_adjust_form_submit_crea
 }
 
 /**
+ * Form callback for adopt table form.
+ *
+ * 'Orphaned' tables are database tables that don't have an active schema
+ * definition.
+ */
+function data_ui_adopt_form($form_state) {
+  $form = array();
+
+  // Compile a list of orphaned tables.
+  $drupal_schema = drupal_get_schema(NULL, TRUE);
+  $db_schema = schema_invoke('inspect');
+  $orphaned_tables = array();
+  foreach ($db_schema as $name => $table) {
+    if (!isset($drupal_schema[$name])) {
+      $orphaned_tables[$name] = $name;
+      $orphaned_schemas[$name] = $table;
+    }
+  }
+
+  $form['orphaned_tables'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Orphaned Tables'),
+    '#options' => $orphaned_tables,
+  );
+  if (count($orphaned_tables) < 1) {
+    $form['no_orphaned_tables'] = array(
+      '#type' => 'item',
+      '#value' => t('There are no orphaned tables in your database.'),
+    );
+  }
+  $form['schemas'] = array(
+    '#type' => 'value',
+    '#value' => $orphaned_schemas,
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Adopt',
+  );
+  return $form;
+}
+
+/**
+ * Submit handler for adopt table form.
+ */
+function data_ui_adopt_form_submit($form, &$form_state) {
+  data_include('DataTable');
+  $tables = array_keys(array_filter($form_state['values']['orphaned_tables']));
+  foreach ($tables as $table_name) {
+    $table = DataTable::instance($table_name);
+    $table->adopt($form_state['values']['schemas'][$table_name]);
+    unset($table);
+  }
+
+  DataTable::clearCaches();
+
+  $form_state['redirect'] = 'admin/build/data';
+}
+
+/**
  * Form callback for create table form.
  */
 function data_ui_create_form(&$form_state) {
Index: data_ui/data_ui.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/data/data_ui/data_ui.module,v
retrieving revision 1.15.2.6
diff -u -p -r1.15.2.6 data_ui.module
--- data_ui/data_ui.module	27 Oct 2009 22:49:50 -0000	1.15.2.6
+++ data_ui/data_ui.module	28 Oct 2009 14:19:06 -0000
@@ -13,6 +13,9 @@ function data_ui_help($path, $arg) {
     case 'admin/content/data':
       $output = '<p>'. t('View content in data tables. If you would like to edit these tables, visit the !data_manage_page.', array('!data_manage_page' => l(t('Data table management page'), 'admin/build/data'))) .'</p>';
       return $output;
+    case 'admin/build/data/adopt':
+      $output = '<p>' . t('Manage database tables that aren\'t claimed by other modules. Adopting tables listed here will add them to Data\'s list of tables.') . '</p>';
+      return $output;
     case 'admin/build/data':
       if (module_exists('views')) {
         $output = '<p>'. t('Manage data tables. If you would like to view the content of these tables, visit the !data_view_page.', array('!data_view_page' => l(t('Data table content page'), 'admin/content/data'))) .'</p>';
@@ -69,6 +72,16 @@ function data_ui_menu() {
       'access arguments' => array('administer data tables'),
       'type' => MENU_LOCAL_TASK,
     );
+    $items['admin/build/data/adopt'] = array(
+      'title' => 'Adopt tables',
+      'description' => 'Adopt data tables that aren\'t claimed by any module.',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('data_ui_adopt_form'),
+      'file' => 'data_ui.admin.inc',
+      'access arguments' => array('administer data tables'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 10,
+    );
   }
   $items['admin/build/data/create'] = array(
     'title' => 'Create a table',
@@ -233,4 +246,4 @@ function data_ui_get_default_path($name)
     return 'admin/content/data/view/'. $path . $name;
   }
   return '';
-}
\ No newline at end of file
+}
Index: includes/DataTable.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/data/includes/Attic/DataTable.inc,v
retrieving revision 1.1.2.7
diff -u -p -r1.1.2.7 DataTable.inc
--- includes/DataTable.inc	28 Oct 2009 13:37:43 -0000	1.1.2.7
+++ includes/DataTable.inc	28 Oct 2009 14:19:06 -0000
@@ -132,6 +132,41 @@ class DataTable {
   }
 
   /**
+   * Let Data manage a table that already exists in the database.
+   *
+   * @param $schema
+   *   The Schema API table definition of the table to adopt. If NULL adopt()
+   *   will inspect the table schema. If this value is defined, it should be the
+   *   result for a single table returned by schema_invoke('inspect').
+   *
+   * @return
+   *   TRUE if the table was successfully adopted; FALSE if the table was
+   *   already known to Data or if the query failed.
+   */
+  public function adopt($table_schema = NULL) {
+    if ($this->defined()) {
+      return FALSE;
+    }
+    if (empty($table_schema) && module_exists('schema')) {
+      $schema = schema_invoke('inspect');
+      if (isset($schema[$this->name])) {
+        $table_schema = $schema[$this->name];
+      }
+    }
+
+    $table = array(
+      'name' => $this->name,
+      'title' => data_natural_name($this->name),
+      'table_schema' => $table_schema,
+    );
+    if (drupal_write_record('data_tables', $table)) {
+      return TRUE;
+    }
+
+    return FALSE;
+  }
+
+  /**
    * Determine whether a table is defined.
    *
    * @return
@@ -207,8 +242,7 @@ class DataTable {
       $schema['fields'][$field] = $spec;
       $this->update(array('table_schema' => $schema));
 
-      // @todo: maybe move these cache clearing functions to their own method
-      // and let take API users take care of caching.
+      // @todo: use clearCaches().
       drupal_get_schema($this->name, TRUE);
       // Invalidate views caches to use new field immediately.
       if (function_exists('views_invalidate_cache')) {
@@ -473,4 +507,21 @@ class DataTable {
   public function handler() {
     return data_get_handler($this->name);
   }
+
+  /**
+   * Clear relevant caches. Call after operations that create, delete or modify
+   * tables.
+   */
+  public static function clearCaches() {
+    // Clear the schema cache.
+    drupal_get_schema(NULL, TRUE);
+    // Have views read new views information about table.
+    if (module_exists('views')) {
+      views_invalidate_cache();
+    }
+    // data ui exposes path to a new default view.
+    if (module_exists('data_ui')) {
+      menu_rebuild();
+    }
+  }
 }
