tripal_chado.migrate.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /**
  3. * Implements hook_form()
  4. *
  5. * Provide a form to select Tripal v2 content types for migration
  6. *
  7. * @param $form
  8. * @param $form_state
  9. */
  10. function tripal_chado_migrate_form($form, &$form_state) {
  11. $form['overview_vert_tabs'] = array(
  12. '#type' => 'vertical_tabs'
  13. );
  14. $form['modes']['#group'] = 'overview_vert_tabs';
  15. $form['modes']['#weight'] = 1000;
  16. $form['step1'] = array(
  17. '#type' => 'fieldset',
  18. '#title' => 'Step1',
  19. '#description' => 'Enable Legacy Support',
  20. '#collapsible' => TRUE,
  21. '#collapsed' => TRUE,
  22. '#group' => 'overview_vert_tabs'
  23. );
  24. $form['step2'] = array(
  25. '#type' => 'fieldset',
  26. '#title' => 'Step2',
  27. '#description' => 'Migrate Content',
  28. '#collapsible' => TRUE,
  29. '#collapsed' => TRUE,
  30. '#group' => 'overview_vert_tabs'
  31. );
  32. $tv2_content_type = 'all';
  33. if (array_key_exists('values', $form_state)) {
  34. $tv2_content_type = $form_state['values']['tv2_content_type'];
  35. }
  36. $options = tripal_chado_get_tripal_v2_content_type_options(TRUE);
  37. $form['step2']['tv2_content_type'] = array(
  38. '#type' => 'select',
  39. '#title' => 'Tripal v2 Content Type',
  40. '#description' => t('Select the Tripal v2 content type to migrate.'),
  41. '#options' => $options,
  42. '#default_value' => $tv2_content_type,
  43. '#ajax' => array(
  44. 'callback' => "tripal_chado_migrate_form_ajax_callback",
  45. 'wrapper' => "tripal-chado-migrate-form",
  46. 'effect' => 'fade',
  47. 'method' => 'replace'
  48. ),
  49. );
  50. // Add a review button that allows reviewing migratable content types
  51. if ($tv2_content_type != 'all') {
  52. $table = str_replace('chado_', '', $tv2_content_type);
  53. $schema = chado_get_schema($table);
  54. $pkey = $schema['primary key'][0];
  55. $fkeys = $schema['foreign keys'];
  56. $form['step2']['tv3_content_type'] = array(
  57. '#type' => 'fieldset',
  58. '#title' => 'Tripal v3 Content Type',
  59. '#description' => "Click the 'Get Tripal v3 Types' button to retrieve a list of Tripal v3
  60. content types to which this Tripal v2 type can be converted. This may take a while
  61. depending on the size of your database. The number of items to be converted is
  62. shown beside the type."
  63. );
  64. $form['step2']['tv3_content_type']['get_v3_type_btn'] = array(
  65. '#type' => 'button',
  66. '#name' => 'get_v3_type_btn',
  67. '#value' => "Get Tripal v3 Types",
  68. '#ajax' => array(
  69. 'callback' => "tripal_chado_migrate_form_ajax_callback",
  70. 'wrapper' => "tripal-chado-migrate-form",
  71. 'effect' => 'fade',
  72. 'method' => 'replace'
  73. ),
  74. );
  75. $no_data = TRUE;
  76. if ($form_state['clicked_button']['#name'] == 'get_v3_type_btn') {
  77. // Migrate all
  78. $form['step2']['tv3_content_type']['tv3_migrate_all'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => 'Migrate All'
  81. );
  82. // Migrate selection only
  83. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  84. // Get all Tripal v2 node types from the chad_* linking table
  85. $sql =
  86. "SELECT V.name AS type, X.accession, db.name AS namespace , count(*) AS num
  87. FROM {" . $table . "} T
  88. INNER JOIN public.$tv2_content_type CT ON T.$pkey = CT.$pkey
  89. INNER JOIN {cvterm} V ON V.cvterm_id = T.type_id
  90. INNER JOIN {dbxref} X ON X.dbxref_id = V.dbxref_id
  91. INNER JOIN {db} ON db.db_id = X.db_id
  92. LEFT JOIN public.chado_entity CE ON CE.record_id = T.$pkey
  93. AND CE.data_table = '$table'
  94. WHERE CE.record_id IS NULL
  95. GROUP BY V.name, X.accession, db.name";
  96. $tv3_content_types = chado_query($sql);
  97. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  98. // We need to store namespace/accession/type for each checkbox in the key becuase
  99. // the value only allows 1 or 0
  100. $key = urlencode(
  101. 'tv3_content_type--' .
  102. $tv3_content_type->namespace . '--' .
  103. $tv3_content_type->accession . '--' .
  104. $tv3_content_type->type);
  105. $form['step2']['tv3_content_type'][$key] = array(
  106. '#type' => 'checkbox',
  107. '#title' => $tv3_content_type->type . ' (' . $tv3_content_type->num . ')',
  108. );
  109. $no_data = FALSE;
  110. }
  111. }
  112. else if ($table == 'organism') {
  113. $sql =
  114. "SELECT count(*)
  115. FROM {organism} O
  116. INNER JOIN public.chado_organism CO ON O.organism_id = CO.organism_id
  117. LEFT JOIN public.chado_entity CE ON CE.record_id = O.organism_id
  118. AND CE.data_table = 'organism'
  119. WHERE CE.record_id IS NULL";
  120. $org_count = chado_query($sql)->fetchField();
  121. if ($org_count > 0) {
  122. $key = urldecode('tv3_content_type--local--organism--organism');
  123. $form['step2']['tv3_content_type'][$key] = array(
  124. '#type' => 'checkbox',
  125. '#title' => 'Organism (' . $org_count . ')',
  126. );
  127. $no_data = FALSE;
  128. }
  129. }
  130. else if ($table == 'analysis') {
  131. $sql =
  132. "SELECT count(*)
  133. FROM {analysis} A
  134. INNER JOIN public.chado_analysis CA ON A.analysis_id = CA.analysis_id
  135. LEFT JOIN public.chado_entity CE ON CE.record_id = A.analysis_id
  136. AND CE.data_table = 'analysis'
  137. WHERE CE.record_id IS NULL";
  138. $ana_count = chado_query($sql)->fetchField();
  139. if ($ana_count > 0) {
  140. $key = urlencode('tv3_content_type--local--analysis--analysis');
  141. $form['step2']['tv3_content_type'][$key] = array(
  142. '#type' => 'checkbox',
  143. '#title' => 'Analysis (' . $ana_count . ')',
  144. );
  145. $no_data = FALSE;
  146. }
  147. }
  148. if ($no_data) {
  149. unset($form['step2']['tv3_content_type']['tv3_migrate_all']);
  150. drupal_set_message('No data for migration or all have been migrated.', 'warning');
  151. }
  152. }
  153. }
  154. // Submit button
  155. if ($tv2_content_type == 'all' || key_exists('tv3_migrate_all', $form['step2']['tv3_content_type'])) {
  156. $form['step2']['migrate_btn'] = array(
  157. '#type' => 'submit',
  158. '#name' => 'migrate_btn',
  159. '#value' => "Migrate $options[$tv2_content_type]",
  160. );
  161. }
  162. $form['#prefix'] = '<div id="tripal-chado-migrate-form">';
  163. $form['#suffix'] = '</div>';
  164. return $form;
  165. }
  166. /**
  167. * Implements hook_validate()
  168. *
  169. * @param $form
  170. * @param $form_state
  171. */
  172. function tripal_chado_migrate_form_validate($form, &$form_state) {
  173. }
  174. /**
  175. * Implements hook_submit()
  176. *
  177. * By submiting the form, a Tripal job to migrate Tripal v2 content is submitted
  178. *
  179. * @param $form
  180. * @param $form_state
  181. */
  182. function tripal_chado_migrate_form_submit($form, &$form_state) {
  183. if ($form_state['clicked_button']['#name'] == 'migrate_btn') {
  184. global $user;
  185. $values = $form_state['values'];
  186. $tv2_content_type = $form_state['values']['tv2_content_type'];
  187. $tv3_content_type = array();
  188. foreach ($values AS $key => $value) {
  189. if ($tv2_content_type != 'all') {
  190. $key = urldecode($key);
  191. if (preg_match('/^tv3_content_type--(.+)--(.+)--(.+)/', $key, $matches) &&
  192. ($value == 1 || $values['tv3_migrate_all'] == 1)) {
  193. $namespace = $matches[1];
  194. $accession = $matches[2];
  195. $type = $matches[3];
  196. $tv3_content_type [] = array(
  197. 'namespace' => $namespace,
  198. 'accession' => $accession,
  199. 'term_name' => $type
  200. );
  201. }
  202. }
  203. }
  204. // Submit a job to migrate content
  205. global $user;
  206. $args = array(
  207. array(
  208. 'tv2_content_type' => $tv2_content_type,
  209. 'tv3_content_type' => $tv3_content_type
  210. )
  211. );
  212. $includes = array(
  213. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  214. );
  215. if ($tv2_content_type == 'all' || count($tv3_content_type) != 0) {
  216. return tripal_add_job("Migrate $tv2_content_type Tripal v2 content.",
  217. 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
  218. }
  219. else {
  220. return drupal_set_message('Nothing to do. All data have been migrated or no data for migration.');
  221. }
  222. }
  223. }
  224. /**
  225. * Ajax call back that returns the entire form
  226. *
  227. * The callback is triggered by ajax elements on the form which leads to the update of
  228. * entire form according to the values set on the form
  229. *
  230. * @param $form
  231. * @param $form_state
  232. * @return $form
  233. */
  234. function tripal_chado_migrate_form_ajax_callback(&$form, &$form_state) {
  235. $active_tab = $form_state['values']['overview_vert_tabs__active_tab'];
  236. return $form;
  237. }
  238. /**
  239. * Get available Tripal v2 content types
  240. *
  241. * @param boolean $all_option
  242. * Include an 'all' option in the returned array
  243. * @return string[]
  244. * Return a string array keyed by the node type
  245. */
  246. function tripal_chado_get_tripal_v2_content_type_options($all_option = FALSE) {
  247. // Get all available Tripal v2 chado tables
  248. $sql =
  249. "SELECT table_name
  250. FROM information_schema.tables
  251. WHERE table_schema = 'public' AND table_name LIKE 'chado_%'";
  252. $result = db_query($sql);
  253. $tables = array();
  254. while ($field = $result->fetchField()) {
  255. $count = db_query("SELECT count(*) FROM $field")->fetchField();
  256. if ($count != 0) {
  257. array_push($tables, $field);
  258. }
  259. }
  260. // List all available Tripal v2 content types
  261. $result = db_select('node_type', 'nt')
  262. ->fields('nt', array('type', 'name', 'description'))
  263. ->condition('type', 'chado_%', 'LIKE')
  264. ->execute();
  265. $options = array();
  266. if ($all_option) {
  267. $options['all'] = 'All';
  268. }
  269. while ($obj = $result->fetchObject()) {
  270. if (in_array($obj->type, $tables)) {
  271. $options[$obj->type] = $obj->name;
  272. }
  273. }
  274. return $options;
  275. }
  276. /**
  277. * Tripal job callback to migrate Tripal v2 content into Tripal v3 content
  278. *
  279. * @param $migration
  280. * @param $job_id
  281. */
  282. function tripal_chado_migrate_records($migration, $job_id = NULL) {
  283. $tv2_content_type = $migration['tv2_content_type'];
  284. $tv3_content_type = $migration['tv3_content_type'];
  285. // If tv2_content_type is 'all', migrate all existing Tripal v2 content
  286. if ($tv2_content_type == 'all') {
  287. print "Migrating all Tripal v2 content...\n";
  288. tripal_chado_migrate_all_types();
  289. }
  290. // Otherwise, migrate only selected Tripal v2 content
  291. else {
  292. print "Migrating selected Tripal v2 content...\n";
  293. tripal_chado_migrate_selected_types($tv3_content_type);
  294. }
  295. }
  296. /**
  297. * Migrate all Tripal v2 content types
  298. */
  299. function tripal_chado_migrate_all_types() {
  300. // Get all available Tripal v2 content types
  301. $tv2_content_types = tripal_chado_get_tripal_v2_content_type_options();
  302. $types = array();
  303. foreach($tv2_content_types AS $tv2_content_type => $value) {
  304. $table = str_replace('chado_', '', $tv2_content_type);
  305. $schema = chado_get_schema($table);
  306. $pkey = $schema['primary key'][0];
  307. $fkeys = $schema['foreign keys'];
  308. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  309. // Get all Tripal v2 node types from the chad_* linking table
  310. $sql = "
  311. SELECT V.name AS type, X.accession, db.name AS namespace
  312. FROM {" . $table . "} T
  313. INNER JOIN public.$tv2_content_type CT ON T.$pkey = CT.$pkey
  314. INNER JOIN {cvterm} V ON V.cvterm_id = T.type_id
  315. INNER JOIN {dbxref} X ON X.dbxref_id = V.dbxref_id
  316. INNER JOIN {db} ON db.db_id = X.db_id
  317. GROUP BY V.name, X.accession, db.name
  318. ";
  319. $tv3_content_types = chado_query($sql);
  320. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  321. array_push($types, array(
  322. 'namespace' => $tv3_content_type->namespace,
  323. 'accession' => $tv3_content_type->accession,
  324. 'term_name' => $tv3_content_type->type
  325. ));
  326. }
  327. }
  328. else if ($table == 'organism') {
  329. array_push($types, array(
  330. 'namespace' => 'local',
  331. 'accession' => 'organism',
  332. 'term_name' => 'organism'
  333. ));
  334. }
  335. else if ($table == 'analysis') {
  336. array_push($types, array(
  337. 'namespace' => 'local',
  338. 'accession' => 'analysis',
  339. 'term_name' => 'analysis'
  340. ));
  341. }
  342. }
  343. tripal_chado_migrate_selected_types($types);
  344. }
  345. /**
  346. * Migrate only selected Tripal v2 content types
  347. *
  348. * @param unknown $tv3_content_type
  349. */
  350. function tripal_chado_migrate_selected_types($tv3_content_types) {
  351. foreach ($tv3_content_types AS $tv3_content_type) {
  352. // Check if the term already exists
  353. $term = tripal_load_term_entity($tv3_content_type);
  354. // If term doesn't exist, create a new bundle for this term
  355. if (!$term) {
  356. print("Creating bundle for term '" . $tv3_content_type['term_name'] . "'...\n");
  357. $success = tripal_create_bundle($tv3_content_type['namespace'], $tv3_content_type['accession'], $tv3_content_type['term_name']);
  358. $term = tripal_load_term_entity($tv3_content_type);
  359. }
  360. // Create bundle name
  361. $bundle_name = 'bio_data_' . $term->id;
  362. // Publish records for the bundle
  363. $value = array(
  364. 'sync_node' => 1,
  365. 'bundle_name' => $bundle_name
  366. );
  367. tripal_chado_publish_records ($value);
  368. }
  369. }