? dbtuner-761852.patch
? dbtuner-762186.patch
Index: dbtuner.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/dbtuner/dbtuner.admin.inc,v
retrieving revision 1.1
diff -u -p -r1.1 dbtuner.admin.inc
--- dbtuner.admin.inc	3 Apr 2010 20:09:21 -0000	1.1
+++ dbtuner.admin.inc	5 Apr 2010 07:00:00 -0000
@@ -19,56 +19,64 @@ function dbtuner_page() {
  */
 function dbtuner_configuration() {
   $form = array();
-  $indexes = dbtuner_get_checkboxes(dbtuner_views_filters_relationshps());
-  $defaults = $indexes['defaults'];
-  $options = $indexes['options'];
-
-  $form['views'] = array(
-    '#type'           => 'fieldset',
-    '#title'          => t('Indexes for Views'),
-    '#description'    => t('If a CCK field is used in a view filter it can be quite slow due to the database not using an index. This will add an index to these fields. Checked means its indexed.'),
-  );
-  $form['views']['view-indexes'] = array(
-    '#type'           => 'checkboxes',
-    '#title'          => t('Select the fields you whish to be indexed by the database.'),
-    '#description'    => t('DB fields that could use an index detected by loading all views and looking at what filters and relationships are used.'),
-    '#default_value'  => $defaults,
-    '#options'        => $options,
-  );
+  if (module_exists('views') && module_exists('cck')) {
+    $indexes = dbtuner_get_checkboxes(dbtuner_views_filters_relationshps());
+    if ($indexes) {
+      $defaults = $indexes['defaults'];
+      $options = $indexes['options'];
+
+      $form['views'] = array(
+        '#type'           => 'fieldset',
+        '#title'          => t('Indexes for Views'),
+        '#description'    => t('If a CCK field is used in a view filter it can be quite slow due to the database not using an index. This will add an index to these fields. Checked means its indexed.'),
+      );
+      $form['views']['view-indexes'] = array(
+        '#type'           => 'checkboxes',
+        '#title'          => t('Select the fields you whish to be indexed by the database.'),
+        '#description'    => t('DB fields that could use an index detected by loading all views and looking at what filters and relationships are used.'),
+        '#default_value'  => $defaults,
+        '#options'        => $options,
+      );
+    }
+  }
 
   $indexes = dbtuner_get_checkboxes(dbtuner_core_indexes());
-  $defaults = $indexes['defaults'];
-  $options = $indexes['options'];
-  $form['core'] = array(
-    '#type'           => 'fieldset',
-    '#title'          => t('Indexes for Core modules'),
-    '#description'    => t('Core could really use indexes on these fields.'),
-  );
-  $form['core']['core-indexes'] = array(
-    '#type'           => 'checkboxes',
-    '#title'          => t('Select the fields you whish to be indexed by the database.'),
-    '#description'    => t('DB fields that could use an index.'),
-    '#default_value'  => $defaults,
-    '#options'        => $options,
-  );
+  if ($indexes) {
+    $defaults = $indexes['defaults'];
+    $options = $indexes['options'];
+    $form['core'] = array(
+      '#type'           => 'fieldset',
+      '#title'          => t('Indexes for Core modules'),
+      '#description'    => t('Core could really use indexes on these fields.'),
+    );
+    $form['core']['core-indexes'] = array(
+      '#type'           => 'checkboxes',
+      '#title'          => t('Select the fields you whish to be indexed by the database.'),
+      '#description'    => t('DB fields that could use an index.'),
+      '#default_value'  => $defaults,
+      '#options'        => $options,
+    );
+  }
 
   $indexes = dbtuner_get_checkboxes(dbtuner_expermental_core_indexes());
-  $defaults = $indexes['defaults'];
-  $options = $indexes['options'];
-  $form['experimental'] = array(
-    '#type'           => 'fieldset',
-    '#collapsible'   => TRUE,
-    '#collapsed'     => TRUE,
-    '#title'          => t('Experimental Indexes for Core modules'),
-    '#description'    => t('Unproven indexes; may hurt or help performance.'),
-  );
-  $form['experimental']['experimental-indexes'] = array(
-    '#type'           => 'checkboxes',
-    '#title'          => t('Select the fields you whish to be indexed by the database.'),
-    '#description'    => t('DB fields that could use an index.'),
-    '#default_value'  => $defaults,
-    '#options'        => $options,
-  );
+  if ($indexes) {
+    $defaults = $indexes['defaults'];
+    $options = $indexes['options'];
+    $form['experimental'] = array(
+      '#type'           => 'fieldset',
+      '#collapsible'   => TRUE,
+      '#collapsed'     => TRUE,
+      '#title'          => t('Experimental Indexes for Core modules'),
+      '#description'    => t('Unproven indexes; may hurt or help performance.'),
+    );
+    $form['experimental']['experimental-indexes'] = array(
+      '#type'           => 'checkboxes',
+      '#title'          => t('Select the fields you whish to be indexed by the database.'),
+      '#description'    => t('DB fields that could use an index.'),
+      '#default_value'  => $defaults,
+      '#options'        => $options,
+    );
+  }
 
   $form['submit'] = array('#type' => 'submit', '#value' => t('Set Index Configuration'));
   $form['#validate'][] = 'dbtuner_configuration_validate';
@@ -99,31 +107,60 @@ function dbtuner_configuration_validate(
 
   // Change Indexes
   $ret = array();
-  foreach ($add as $value) {
-    db_add_index($ret, $value[0], $value[1], array($value[1]));
+  if (!empty($add)) {
+    foreach ($add as $value) {
+      db_add_index($ret, $value[0], $value[1], array($value[1]));
+    }
   }
-  foreach ($del as $value) {
-    db_drop_index($ret, $value[0], $value[1]);
+  if (!empty($del)) {
+    foreach ($del as $value) {
+      db_drop_index($ret, $value[0], $value[1]);
+    }
   }
   if (!empty($ret)) {
     drupal_set_message(str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($ret, TRUE)))));
   }
 }
 
+/**
+ * Form API Checkboxes handler. Only get what has changed.
+ *
+ * @param $old
+ *   Default values
+ * @param $new
+ *   New values from user
+ * @param $add
+ *   added by selection
+ * @param $del
+ *   removed by selection
+ */
 function dbtuner_get_add_del($old, $new, &$add, &$del) {
-  foreach ($new as $key => $value) {
-    if (!in_array($value, $old)) {
-      $add[] = dbtuner_extract_table_index($key);
-    }
-    if (is_numeric($value) && in_array($key, $old)) {
-      $del[] = dbtuner_extract_table_index($key);
+  if (!empty($new) && !empty($old)) {
+    foreach ($new as $key => $value) {
+      if (!in_array($value, $old)) {
+        $add[] = dbtuner_extract_table_index($key);
+      }
+      if (is_numeric($value) && in_array($key, $old)) {
+        $del[] = dbtuner_extract_table_index($key);
+      }
     }
   }
 }
 
+/**
+ * Process the indexes array and return options and defaults.
+ *
+ * @param $indexes
+ *   Report from database
+ * @return
+ *   array('options' => $options, 'defaults' => $defaults)
+ */
 function dbtuner_get_checkboxes($indexes) {
   $options = array();
   $defaults = array();
+  if (empty($indexes)) {
+    return FALSE;
+  }
   foreach ($indexes as $key => $value) {
     extract($value);
     $index = trim($index);
@@ -143,6 +180,14 @@ function dbtuner_get_checkboxes($indexes
   return array('options' => $options, 'defaults' => $defaults);
 }
 
+/**
+ * Get table & index info out of array key
+ *
+ * @param $value
+ *   key value from array
+ * @return
+ *   array($table, $index)
+ */
 function dbtuner_extract_table_index($value) {
   return explode(' ', $value);
 }
@@ -234,18 +279,22 @@ function dbtuner_views_filters_relations
     $relationships = $view->get_items('relationship', $row['id']);
 
     // Build data structure
-    foreach ($filters as $name => $filter) {
-      if (!empty($filters['type']['value'])) {
-        foreach ($filters['type']['value'] as $key => $value)
-        $indexes[$filter['table']][$filter['id']]['node-type'][$key] = $value;
-      }
-      else {
-        $indexes[$filter['table']][$filter['id']]['node-type']['-1'] = 'all';
+    if (!empty($filters)) {
+      foreach ($filters as $name => $filter) {
+        if (!empty($filters['type']['value'])) {
+          foreach ($filters['type']['value'] as $key => $value)
+          $indexes[$filter['table']][$filter['id']]['node-type'][$key] = $value;
+        }
+        else {
+          $indexes[$filter['table']][$filter['id']]['node-type']['-1'] = 'all';
+        }
+        $indexes[$filter['table']][$filter['id']]['relationship'] = $filter['relationship'];
       }
-      $indexes[$filter['table']][$filter['id']]['relationship'] = $filter['relationship'];
     }
-    foreach ($relationships as $name => $relationship) {
-      $indexes[$relationship['table']][$relationship['id']] = $relationship['relationship'];
+    if (!empty($relationships)) {
+      foreach ($relationships as $name => $relationship) {
+        $indexes[$relationship['table']][$relationship['id']] = $relationship['relationship'];
+      }
     }
   }
 
@@ -258,6 +307,9 @@ function dbtuner_views_filters_relations
 
   // Relate indexes to tables.
   $return = array();
+  if (empty($indexes)) {
+    return $return;
+  }
   foreach ($indexes as $table => $values) {
     foreach ($values as $field => $extra) {
       $cck_table = str_replace('node_data', 'content', $table);
@@ -279,7 +331,7 @@ function dbtuner_views_filters_relations
       }
 
       // CCK field in content type table
-      else {
+      elseif (!empty($cck_content_types) && is_array($cck_content_types)) {
         foreach ($cck_content_types as $cck_content_table) {
           // Place index on all content_type tables
           if (!empty($extra['node-type']['-1']) && $extra['node-type']['-1'] == 'all') {
@@ -290,7 +342,7 @@ function dbtuner_views_filters_relations
           }
 
           // Place index only where needed
-          else {
+          elseif (!empty($extra['node-type']) && is_array($extra['node-type'])) {
             foreach ($extra['node-type'] as $nodetype) {
               $return['content_type_' . $nodetype . ' ' . $field] = array('table' => 'content_type_' . $nodetype, 'index' => $field);
               if ($extra['relationship'] != 'none') {
@@ -306,7 +358,6 @@ function dbtuner_views_filters_relations
   return $return;
 }
 
-
 /**
  * Build core index
  *
