Переглянути джерело

Finished admin form for Tripal v2 content migration. Added nid column to chado_entity table in tripal_chado.install

ccheng 9 роки тому
батько
коміт
9452ef31a3

+ 2 - 0
tripal/api/tripal.jobs.api.inc

@@ -34,6 +34,7 @@ class TripalEntityController extends EntityAPIController {
     $values['changed'] = time();
     $values['title'] = '';
     $values['type'] = 'TripalEntity';
+    $values['nid'] = '';
 
     // Call the parent constructor.
     $entity = parent::create($values);
@@ -282,6 +283,7 @@ class TripalEntityController extends EntityAPIController {
         'uid'       => $user->uid,
         'created'   => $entity->created,
         'changed'   => time(),
+        'nid' => $entity->nid,
       );
       if ($invocation == 'entity_update') {
         $record['id'] = $entity->id;

+ 246 - 85
tripal_chado/includes/tripal_chado.migrate.inc

@@ -1,38 +1,27 @@
 <?php
 
+/**
+ * Implements hook_form()
+ * 
+ * Provide a form to select Tripal v2 content types for migration
+ * 
+ * @param $form
+ * @param $form_state
+ */
 function tripal_chado_migrate_form($form, &$form_state) {
 
-  $chado_content = 'all';
+  $tv2_content_type = 'all';
   if (array_key_exists('values', $form_state)) {
-    $chado_content = $form_state['values']['chado_content'];
-  }
-    
-  // Get all available Tripal v2 chado tables
-  $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE 'chado_%'";
-  $result = db_query($sql);
-  $tables = array();
-  while ($field = $result->fetchField()) {
-    array_push($tables, $field);
+    $tv2_content_type = $form_state['values']['tv2_content_type'];
   }
   
-  // List all available Tripal v2 content types
-  $result = db_select('node_type', 'nt')
-    ->fields('nt', array('type', 'name', 'description'))
-    ->condition('type', 'chado_%', 'LIKE')
-    ->execute();
-  $options = array ('all' => 'All');
-  while ($obj = $result->fetchObject()) {
-    if (in_array($obj->type, $tables)) {
-      $options[$obj->type] = $obj->name;
-    }
-  }
-  
-  $form['chado_content'] = array(
+  $options = tripal_chado_get_tripal_v2_content_type_options(TRUE);
+  $form['tv2_content_type'] = array(
     '#type' => 'select',
-    '#title' => 'Chado Content',
-    '#description' => t('Select the chado content to migrate.'),
+    '#title' => 'Tripal v2 Content Type',
+    '#description' => t('Select the Tripal v2 content type to migrate.'),
     '#options' => $options,
-    '#default_value' => $chado_content,
+    '#default_value' => $tv2_content_type,
     '#ajax' => array(
       'callback' => "tripal_chado_migrate_form_ajax_callback",
       'wrapper' => "tripal-chado-migrate-form",
@@ -40,92 +29,135 @@ function tripal_chado_migrate_form($form, &$form_state) {
       'method' => 'replace'
     ),
   );
-  
+
   // Add a review button that allows reviewing migratable content types
-  if ($chado_content != 'all') {
+  if ($tv2_content_type != 'all') {
     
-    $table = str_replace('chado_', '', $chado_content);
+    $table = str_replace('chado_', '', $tv2_content_type);
     $schema = chado_get_schema($table);
     $pkey = $schema['primary key'][0];
     $fkeys = $schema['foreign keys'];
-    
-    if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
-      $form['review'] = array(
-        '#type' => 'fieldset',
-        '#title' => 'Review Content Type',
-        '#description' => 'By clicking on the Review button, Tripal will gather information from
-        existing database and list content types which can be mapped to a Tripal v3 content 
-        type. (may take a while depending on the size of your database.). This allows you 
-        to migrate only the types of your selection.'
-      );
-      $form['review']['review_btn'] = array(
-        '#type' => 'button',
-        '#name' => 'review_btn',
-        '#value' => "Review",
-        '#ajax' => array(
-          'callback' => "tripal_chado_migrate_form_ajax_callback",
-          'wrapper' => "tripal-chado-migrate-form",
-          'effect' => 'fade',
-          'method' => 'replace'
-        ),
+
+    $form['tv3_content_type'] = array(
+      '#type' => 'fieldset',
+      '#title' => 'Tripal v3 Content Type',
+      '#description' => "Click the 'Get Tripal v3 Types' button to retrieve a list of Tripal v3 
+        content types to which this Tripal v2 type can be converted. This may take a while 
+        depending on the size of your database.  The number of items to be converted is 
+        shown beside the type."
+    );
+    $form['tv3_content_type']['get_v3_type_btn'] = array(
+      '#type' => 'button',
+      '#name' => 'get_v3_type_btn',
+      '#value' => "Get Tripal v3 Types",
+      '#ajax' => array(
+        'callback' => "tripal_chado_migrate_form_ajax_callback",
+        'wrapper' => "tripal-chado-migrate-form",
+        'effect' => 'fade',
+        'method' => 'replace'
+      ),
+    );
+
+    if ($form_state['clicked_button']['#name'] == 'get_v3_type_btn') {
+
+      // Migrate all
+      $form['tv3_content_type']['tv3_migrate_all'] = array(
+        '#type' => 'checkbox',
+        '#title' => 'Migrate All'
       );
-      $counter = 0;
-      if ($form_state['clicked_button']['#name'] == 'review_btn') {
+      // Migrate selection only
+      if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
         // Get all Tripal v2 node types from the chad_* linking table
         $sql = 
-          "SELECT DISTINCT V.name AS type, X.accession, db.name AS namespace 
+          "SELECT V.name AS type, X.accession, db.name AS namespace , count(*) AS num
               FROM chado.$table T
-              INNER JOIN $chado_content CT ON T.$pkey = CT.$pkey
+              INNER JOIN $tv2_content_type CT ON T.$pkey = CT.$pkey
               INNER JOIN chado.cvterm V ON V.cvterm_id = T.type_id
               INNER JOIN chado.dbxref X ON X.dbxref_id = V.dbxref_id
-              INNER JOIN chado.db ON db.db_id = X.db_id";
-        $subtypes = db_query($sql);
-        while($subtype = $subtypes->fetchObject()) {
-          $form['review']['chado_content_type--' . $subtype->namespace . '--' . $subtype->type] = array(
+              INNER JOIN chado.db ON db.db_id = X.db_id
+              GROUP BY V.name, X.accession, db.name";
+        $tv3_content_types = db_query($sql);
+        while($tv3_content_type = $tv3_content_types->fetchObject()) {
+          $form['tv3_content_type']['tv3_content_type--' . $tv3_content_type->namespace . 
+              '--' . $tv3_content_type->accession] = array(
             '#type' => 'checkbox',
-            '#title' => $subtype->type,
-          );
-            $counter ++;
-        }
-      
-        // No subtype exists, migrate all
-        if ($counter == 0) {
-          $form['review']['nosubtype'] = array(
-            '#markup' => t("<br>Type not found. Migrate all $options[$chado_content]."),
+            '#title' => $tv3_content_type->type . ' (' . $tv3_content_type->num . ')',
           );
         }
       }
+      else if ($table == 'organism') {
+        $sql =
+          "SELECT count(*) 
+              FROM chado.organism O
+              INNER JOIN chado_organism CO ON O.organism_id = CO.organism_id";
+        $org_count = db_query($sql)->fetchField();
+        $form['tv3_content_type']['tv3_content_type--local--organism'] = array(
+          '#type' => 'checkbox',
+          '#title' => 'Organism (' . $org_count . ')',
+        );
+      }
+      else if ($table == 'analysis') {
+        $sql =
+        "SELECT count(*)
+              FROM chado.analysis A
+              INNER JOIN chado_analysis CA ON A.analysis_id = CA.analysis_id";
+        $ana_count = db_query($sql)->fetchField();
+        $form['tv3_content_type']['tv3_content_type--local--analysis'] = array(
+          '#type' => 'checkbox',
+          '#title' => 'Analysis (' . $ana_count . ')',
+        );
+      }
     }
   }
   
   // Submit button
-  $form['migrate_btn'] = array(
-    '#type' => 'submit',
-    '#name' => 'migrate_btn',
-    '#value' => "Migrate $options[$chado_content]",
-  );
+  if ($tv2_content_type == 'all' || key_exists('tv3_migrate_all', $form['tv3_content_type'])) {
+    $form['migrate_btn'] = array(
+      '#type' => 'submit',
+      '#name' => 'migrate_btn',
+      '#value' => "Migrate $options[$tv2_content_type]",
+    );
+  }
 
   $form['#prefix'] = '<div id="tripal-chado-migrate-form">';
   $form['#suffix'] = '</div>';
   return $form;
 }
 
+/**
+ * Implements hook_validate()
+ * 
+ * @param $form
+ * @param $form_state
+ */
 function tripal_chado_migrate_form_validate($form, &$form_state) {
 
 }
 
+/**
+ * Implements hook_submit()
+ * 
+ * By submiting the form, a Tripal job to migrate Tripal v2 content is submitted
+ * 
+ * @param $form
+ * @param $form_state
+ */
 function tripal_chado_migrate_form_submit($form, &$form_state) {
   if ($form_state['clicked_button']['#name'] == 'migrate_btn') {
     global $user;
     $values = $form_state['values'];
-    $chado_contents = $form_state['values']['chado_content'];
-    $subtypes = array();
+    $tv2_content_type = $form_state['values']['tv2_content_type'];
+    $tv3_content_type = array();
     foreach ($values AS $key => $value) {
-      if ($chado_contents != 'all') {
-        if (preg_match('/^chado_content_type--(.+)--(.+)/', $key, $matches) && $value == 1) {
+      if ($tv2_content_type != 'all') {
+        if (preg_match('/^tv3_content_type--(.+)--(.+)/', $key, $matches) &&
+            ($value == 1 || $values['tv3_migrate_all'] == 1)) {
           $namespace = $matches[1];
           $accession = $matches[2];          
-          $subtypes [$namespace] = $accession;
+          $tv3_content_type [] = array(
+            'namespace' => $namespace,
+            'accession' => $accession
+          );
         }
       }
     }
@@ -134,24 +166,153 @@ function tripal_chado_migrate_form_submit($form, &$form_state) {
     global $user;
     $args = array(
       array(
-        'chado_content' => $chado_contents,
-        'subtypes' => $subtypes
-      ),
+        'tv2_content_type' => $tv2_content_type,
+        'tv3_content_type' => $tv3_content_type
+      )
     );
-    $includes = array();
-    return tripal_add_job("Migrate $chado_contents.", 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
+    $includes = array(
+      module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
+    );
+    if ($tv2_content_type == 'all' || count($tv3_content_type) != 0) {
+      return tripal_add_job("Migrate $tv2_content_type Tripal v2 content.", 
+        'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
+    }
+    else {
+      return drupal_set_message('Nothing to do. No data for migration.');
+    }
   }
 }
+
 /**
- *
- */
+ * Ajax call back that returns the entire form
+ * 
+ * The callback is triggered by ajax elements on the form which leads to the update of 
+ * entire form according to the values set on the form
+ * 
+ * @param $form
+ * @param $form_state
+ * @return $form
+ */ 
 function tripal_chado_migrate_form_ajax_callback($form, $form_state) {
   return $form;
 }
 
 /**
- *
+ * Get available Tripal v2 content types
+ * 
+ * @param boolean $all_option
+ *   Include an 'all' option in the returned array
+ * @return string[]
+ *  Return a string array keyed by the node type
+ */
+function tripal_chado_get_tripal_v2_content_type_options ($all_option = FALSE) {
+  // Get all available Tripal v2 chado tables
+  $sql =
+  "SELECT table_name
+      FROM information_schema.tables
+      WHERE table_schema = 'public' AND table_name LIKE 'chado_%'";
+  $result = db_query($sql);
+  $tables = array();
+  while ($field = $result->fetchField()) {
+    $count = db_query("SELECT count(*) FROM $field")->fetchField();
+    if ($count != 0) {
+      array_push($tables, $field);
+    }
+  }
+  
+  // List all available Tripal v2 content types
+  $result = db_select('node_type', 'nt')
+  ->fields('nt', array('type', 'name', 'description'))
+  ->condition('type', 'chado_%', 'LIKE')
+  ->execute();
+  $options = array();
+  if ($all_option) {
+    $options['all'] = 'All';
+  }
+  while ($obj = $result->fetchObject()) {
+    if (in_array($obj->type, $tables)) {
+      $options[$obj->type] = $obj->name;
+    }
+  }
+  return $options;
+}
+
+/**
+ * Tripal job callback to migrate Tripal v2 content into Tripal v3 content
+ * 
+ * @param $migration
+ * @param $job_id
+ */
+ function tripal_chado_migrate_records($migration, $job_id = NULL) {
+  $tv2_content_type = $migration['tv2_content_type'];
+  $tv3_content_type = $migration['tv3_content_type'];
+  
+  // If tv2_content_type is 'all', migrate all existing Tripal v2 content
+  if ($tv2_content_type == 'all') {
+    print "Migrating all Tripal v2 content...\n";
+    tripal_chado_migrate_all_types ();
+  }
+  // Otherwise, migrate only selected Tripal v2 content
+  else {
+    print "Migrating selected Tripal v2 content...\n";
+    tripal_chado_migrate_selected_types ($tv3_content_type);
+  }
+}
+
+/**
+ * Migrate all Tripal v2 content types
  */
-function tripal_chado_migrate_records($bundle_name, $job_id = NULL) {
+function tripal_chado_migrate_all_types () {
+  // Get all available Tripal v2 content types
+  $tv2_content_types = tripal_chado_get_tripal_v2_content_type_options();
+  
+  $types = array();
+  foreach($tv2_content_types AS $tv2_content_type => $value) {
+    $table = str_replace('chado_', '', $tv2_content_type);
+    $schema = chado_get_schema($table);
+    $pkey = $schema['primary key'][0];
+    $fkeys = $schema['foreign keys'];
+    
+    if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
+      // Get all Tripal v2 node types from the chad_* linking table
+      $sql =
+      "SELECT X.accession, db.name AS namespace
+      FROM chado.$table T
+      INNER JOIN $tv2_content_type CT ON T.$pkey = CT.$pkey
+      INNER JOIN chado.cvterm V ON V.cvterm_id = T.type_id
+      INNER JOIN chado.dbxref X ON X.dbxref_id = V.dbxref_id
+      INNER JOIN chado.db ON db.db_id = X.db_id
+      GROUP BY V.name, X.accession, db.name";
+      $tv3_content_types = db_query($sql);
+      while($tv3_content_type = $tv3_content_types->fetchObject()) {
+        array_push($types, array(
+          'namespace' => $tv3_content_type->namespace,
+          'accession' => $tv3_content_type->accession
+        ));
+      }
+    }
+    else if ($table == 'organism') {
+      array_push($types, array(
+        'namespace' => 'local',
+        'accession' => 'organism'
+      ));
+    }
+    else if ($table == 'analysis') {
+      array_push($types, array(
+        'namespace' => 'local',
+        'accession' => 'analysis'
+      ));
+    }
+  }
+  
+  tripal_chado_migrate_selected_types($types);
+}
 
+/**
+ * Migrate only selected Tripal v2 content types
+ *
+ * @param unknown $tv3_content_type
+ */
+function tripal_chado_migrate_selected_types ($tv3_content_types) {
+  print_r($tv3_content_types);
 }

+ 5 - 0
tripal_chado/tripal_chado.install

@@ -480,11 +480,16 @@ function tripal_chado_chado_entity_schema() {
         'not null' => FALSE,
         'default' => ''
       ),
+      'nid' => array(
+        'description' => 'Optional. For linking nid to the entity when migrating Tripal v2 content',
+        'type' => 'int',
+      )
     ),
     'indexes' => array(
       'record_id' => array('record_id'),
       'entity_id' => array('entity_id'),
       'data_table' => array('data_table'),
+      'nid' => array('nid'),
     ),
     'unique keys' => array(
       'record' => array('data_table', 'record_id'),