tripal_chado.migrate.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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['overview_vert_tabs']['#default_tab'] = key_exists('tripal_migrate_current_tab', $_SESSION) ? $_SESSION['tripal_migrate_current_tab'] : 'edit-step1';
  15. $form['instructions'] = array(
  16. '#type' => 'item',
  17. '#markup' => t('Here you may migrate Tripal v2 content types to Tripal v3
  18. content types. The migration process is divided into four steps that
  19. allow you to migrate your site as your own pace. Please click each
  20. step below for details as to the purpose of the step. When all steps
  21. are completed your site will be fully Tripal v3 compatible.'),
  22. '#weight' => -100
  23. );
  24. // Step 1
  25. $form['step1'] = array(
  26. '#type' => 'fieldset',
  27. '#title' => 'Step1',
  28. '#description' => '<b>Enable Legacy Support</b>',
  29. '#collapsible' => TRUE,
  30. '#collapsed' => TRUE,
  31. '#group' => 'overview_vert_tabs'
  32. );
  33. global $base_url;
  34. $mod_url = '/admin/modules';
  35. $form['step1']['step1_content'] = array(
  36. '#markup' => 'Tripal legacy modules are needed to support the display of Tripal v2
  37. content types. Review and ' . l('enable modules', $mod_url) . ' in the \'Tripal v2 Legacy\' category
  38. for legacy content support. Only content types for enabled legacy
  39. moodules can be migrated'
  40. );
  41. // Step 2
  42. $form['step2'] = array(
  43. '#type' => 'fieldset',
  44. '#title' => 'Step2',
  45. '#description' => '<b>Migrate Content</b>',
  46. '#collapsible' => TRUE,
  47. '#collapsed' => TRUE,
  48. '#group' => 'overview_vert_tabs'
  49. );
  50. $form['step2']['step2_container'] = array(
  51. '#type' => 'container',
  52. '#collapsible' => FALSE,
  53. '#prefix' => '<div id="tripal-chado-migrate-form-step2">',
  54. '#suffix' => '</div>'
  55. );
  56. $form['step2']['step2_container']['instructions'] = array(
  57. '#type' => 'item',
  58. '#markup' => t('Here you can migrate Tripal v2 content types to Tripal v3
  59. content types. This will not destroy or remove existing Tripal v2 pages
  60. but will creatte new Tripal v3 pages. This allows you to keep existing
  61. pages while reviewing and customizing the Tripal v3 content types. Site
  62. visitors can continue to visit the Tripal v2 pages. Tripal
  63. v3 content types may remain private while customization is underway.
  64. Once customization is completed a subsequent step will allow you to
  65. swap out Tripal v2 pages for the newer Tripal v3 pages.') .
  66. t('If you would like to use Trial v3 web services you must migrate
  67. content types.'),
  68. );
  69. $tv2_content_type = 'all';
  70. if (array_key_exists('values', $form_state)) {
  71. $tv2_content_type = $form_state['values']['tv2_content_type'];
  72. }
  73. $tv2_options = tripal_chado_get_tripal_v2_content_type_options(TRUE);
  74. $form['step2']['step2_container']['tv2_content_type'] = array(
  75. '#type' => 'select',
  76. '#title' => 'Tripal v2 Content Type',
  77. '#description' => t('Select the Tripal v2 content type to migrate.'),
  78. '#options' => $tv2_options,
  79. '#default_value' => $tv2_content_type,
  80. '#ajax' => array(
  81. 'callback' => "tripal_chado_migrate_form_step2_ajax_callback",
  82. 'wrapper' => "tripal-chado-migrate-form-step2",
  83. 'effect' => 'fade',
  84. 'method' => 'replace'
  85. ),
  86. );
  87. // Add a review button that allows reviewing migratable content types
  88. if ($tv2_content_type != 'all') {
  89. $table = str_replace('chado_', '', $tv2_content_type);
  90. $schema = chado_get_schema($table);
  91. $pkey = $schema['primary key'][0];
  92. $fkeys = $schema['foreign keys'];
  93. $form['step2']['step2_container']['tv3_content_type'] = array(
  94. '#type' => 'fieldset',
  95. '#title' => 'Tripal v3 Content Type',
  96. '#description' => "Click the 'Get Tripal v3 Types' button to retrieve a list of Tripal v3
  97. content types to which this Tripal v2 type can be converted. This may take a while
  98. depending on the size of your database. The number of items to be converted is
  99. shown beside the type."
  100. );
  101. $form['step2']['step2_container']['tv3_content_type']['get_v3_type_btn'] = array(
  102. '#type' => 'button',
  103. '#name' => 'get_v3_type_btn',
  104. '#value' => "Get Tripal v3 Types",
  105. '#ajax' => array(
  106. 'callback' => "tripal_chado_migrate_form_step2_ajax_callback",
  107. 'wrapper' => "tripal-chado-migrate-form-step2",
  108. 'effect' => 'fade',
  109. 'method' => 'replace'
  110. ),
  111. );
  112. $no_data = TRUE;
  113. if ($form_state['clicked_button']['#name'] == 'get_v3_type_btn') {
  114. // Migrate all
  115. $form['step2']['step2_container']['tv3_content_type']['tv3_migrate_all'] = array(
  116. '#type' => 'checkbox',
  117. '#title' => 'Migrate All'
  118. );
  119. // Migrate selection only
  120. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  121. // Get all Tripal v2 node types from the chad_* linking table
  122. $sql =
  123. "SELECT V.name AS type, X.accession, db.name AS vocabulary , count(*) AS num
  124. FROM {" . $table . "} T
  125. INNER JOIN public.$tv2_content_type CT ON T.$pkey = CT.$pkey
  126. INNER JOIN {cvterm} V ON V.cvterm_id = T.type_id
  127. INNER JOIN {dbxref} X ON X.dbxref_id = V.dbxref_id
  128. INNER JOIN {db} ON db.db_id = X.db_id
  129. LEFT JOIN public.chado_entity CE ON CE.record_id = T.$pkey
  130. AND CE.data_table = '$table'
  131. WHERE CE.record_id IS NULL
  132. GROUP BY V.name, X.accession, db.name";
  133. $tv3_content_types = chado_query($sql);
  134. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  135. // We need to store vocabulary/accession/type for each checkbox in the key becuase
  136. // the value only allows 1 or 0
  137. $key = urlencode(
  138. 'tv3_content_type--' .
  139. $tv3_content_type->vocabulary . '--' .
  140. $tv3_content_type->accession . '--' .
  141. $tv3_content_type->type);
  142. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  143. '#type' => 'checkbox',
  144. '#title' => $tv3_content_type->type . ' (' . $tv3_content_type->num . ')',
  145. );
  146. $no_data = FALSE;
  147. }
  148. }
  149. else if ($table == 'organism') {
  150. $sql =
  151. "SELECT count(*)
  152. FROM {organism} O
  153. INNER JOIN public.chado_organism CO ON O.organism_id = CO.organism_id
  154. LEFT JOIN public.chado_entity CE ON CE.record_id = O.organism_id
  155. AND CE.data_table = 'organism'
  156. WHERE CE.record_id IS NULL";
  157. $org_count = chado_query($sql)->fetchField();
  158. if ($org_count > 0) {
  159. $key = urldecode('tv3_content_type--OBI--0100026--organism');
  160. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  161. '#type' => 'checkbox',
  162. '#title' => 'Organism (' . $org_count . ')',
  163. );
  164. $no_data = FALSE;
  165. }
  166. }
  167. else if ($table == 'analysis') {
  168. $sql =
  169. "SELECT count(*)
  170. FROM {analysis} A
  171. INNER JOIN public.chado_analysis CA ON A.analysis_id = CA.analysis_id
  172. LEFT JOIN public.chado_entity CE ON CE.record_id = A.analysis_id
  173. AND CE.data_table = 'analysis'
  174. WHERE CE.record_id IS NULL";
  175. $ana_count = chado_query($sql)->fetchField();
  176. if ($ana_count > 0) {
  177. $key = urlencode('tv3_content_type--local--analysis--analysis');
  178. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  179. '#type' => 'checkbox',
  180. '#title' => 'Analysis (' . $ana_count . ')',
  181. );
  182. $no_data = FALSE;
  183. }
  184. }
  185. if ($no_data) {
  186. unset($form['step2']['step2_container']['tv3_content_type']['tv3_migrate_all']);
  187. drupal_set_message('No data for migration or all have been migrated.', 'warning');
  188. }
  189. }
  190. }
  191. // Migrate button
  192. if ($tv2_content_type == 'all' || key_exists('tv3_migrate_all', $form['step2']['step2_container']['tv3_content_type'])) {
  193. $form['step2']['step2_container']['migrate_btn'] = array(
  194. '#type' => 'submit',
  195. '#name' => 'migrate_btn',
  196. '#value' => "Migrate $tv2_options[$tv2_content_type]",
  197. );
  198. }
  199. // Step 3
  200. $form['step3'] = array(
  201. '#type' => 'fieldset',
  202. '#title' => 'Step3',
  203. '#description' => '<b>Use Legacy Templates (optional)</b>',
  204. '#collapsible' => TRUE,
  205. '#collapsed' => TRUE,
  206. '#group' => 'overview_vert_tabs'
  207. );
  208. $form['step3']['instructions'] = array(
  209. '#type' => 'item',
  210. '#markup' => t('Once content types have been migrated you have the option
  211. to use the Tripal v2 display templates. Tripal v3 allows you to
  212. customize the page layout using a web interface whereas Tripal v2
  213. required programatic changes to template files. If your site has
  214. considerable Tripal v2 customizations that you do not
  215. want to lose you can use the legacy templates by checking the box
  216. for the desired content types below.'),
  217. );
  218. // Get a list of enabled legacy modules with tv2 templates
  219. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  220. $info = module_invoke_all('node_info');
  221. $options = tripal_chado_get_tripal_v2_content_type_options(FALSE, TRUE);
  222. foreach ($options AS $type_name => $type_label) {
  223. $title = $type_label;
  224. if ($type_name != 'all' and !key_exists($type_name, $info)) {
  225. $title .= " <strong>(please enable the $type_name legacy module).</strong>";
  226. }
  227. $form ['step3']['legacy_template--' . $type_name] = array (
  228. '#type' => 'checkbox',
  229. '#title' => $title,
  230. '#default_value' => key_exists('legacy_template--' . $type_name, $enabled_templates) ? $enabled_templates['legacy_template--' . $type_name] : 0,
  231. );
  232. }
  233. $form['step3']['save_btn'] = array(
  234. '#type' => 'submit',
  235. '#name' => 'save_enabled_template_btn',
  236. '#value' => "Save",
  237. );
  238. // Step 4
  239. $form['step4'] = array(
  240. '#type' => 'fieldset',
  241. '#title' => 'Step4',
  242. '#description' => '<b>Complete Migration</b>',
  243. '#collapsible' => TRUE,
  244. '#collapsed' => TRUE,
  245. '#group' => 'overview_vert_tabs'
  246. );
  247. $form['step4']['instructions'] = array(
  248. '#type' => 'item',
  249. '#markup' => t('This step allows you to fully switch to Tripal v3 pages.
  250. You can move URLs and titles from Tripal v2 pages to their
  251. corresponding Tripal v3 pages. This ensures user bookmarks and external
  252. links to your site are not broken. Here you can also unpublish Tripal
  253. v2 content types or completely remove them if desired. You can
  254. perform these action in stages such as first moving titles and URLs,
  255. then unpublishing Tripal v2 pages and once the migration has been
  256. verified you can finally delete the Tripal v2 pages to free space.
  257. Deleting the Tripal v2 content will not delete the data in Chado.
  258. The page is simply removed from the site.'),
  259. );
  260. $form['step4']['warning'] = array(
  261. '#type' => 'item',
  262. '#markup' => tripal_set_message('Any of the following options cannot be
  263. undone. Also, please be sure you have migrated all
  264. desired content types in Step 2 prior to deleting the Tripal v2
  265. contents.', TRIPAL_WARNING, array('return_html' => TRUE)),
  266. );
  267. $opt_complete_migration = array (
  268. 'cp_title' => 'Copy Title over to Tripal v3 Content',
  269. 'mv_url' => 'Migrate URL Alias to Tripal v3 Content',
  270. 'unpublish' => 'Unpublish Tripal v2 Content',
  271. 'delete' => 'Delete Tripal v2 Content',
  272. );
  273. if (count($tv2_options) == 1 && key_exists('all', $tv2_options)) {
  274. $form['step4']['complete'] = array(
  275. '#markup' => 'Migration completed. All content have been migrated.'
  276. );
  277. }
  278. else {
  279. foreach ($tv2_options AS $opt_key => $opt) {
  280. $form['step4'][$opt_key . '_title'] = array(
  281. '#markup' => "<b>$opt</b>"
  282. );
  283. $form['step4']['complete_migration--' . $opt_key] = array(
  284. '#type' => 'checkboxes',
  285. '#options' => $opt_complete_migration,
  286. );
  287. }
  288. $form['step4']['submit_btn'] = array(
  289. '#type' => 'submit',
  290. '#name' => 'complete_migration_btn',
  291. '#value' => "Submit",
  292. );
  293. }
  294. return $form;
  295. }
  296. /**
  297. * Implements hook_validate()
  298. *
  299. * @param $form
  300. * @param $form_state
  301. */
  302. function tripal_chado_migrate_form_validate($form, &$form_state) {
  303. }
  304. /**
  305. * Implements hook_submit()
  306. *
  307. * By submiting the form, a Tripal job to migrate Tripal v2 content is submitted
  308. *
  309. * @param $form
  310. * @param $form_state
  311. */
  312. function tripal_chado_migrate_form_submit($form, &$form_state) {
  313. // Store the legacy template setting in a Drupal variable
  314. if ($form_state['clicked_button']['#name'] == 'save_enabled_template_btn') {
  315. $values = $form_state['values'];
  316. $enabled_templates = array();
  317. foreach ($values AS $key => $value) {
  318. if (preg_match('/^legacy_template--/', $key)) {
  319. $enabled_templates[$key] = $value;
  320. }
  321. }
  322. variable_set('tripal_chado_enabled_legacy_templates', $enabled_templates);
  323. drupal_theme_rebuild();
  324. $_SESSION['tripal_migrate_current_tab'] = 'edit-step3';
  325. }
  326. // Complete migration
  327. else if ($form_state['clicked_button']['#name'] == 'complete_migration_btn') {
  328. $values = $form_state['values'];
  329. $config = array (
  330. 'delete' => array(),
  331. 'unpublish' => array(),
  332. 'cp_title' => array(),
  333. 'mv_url' => array()
  334. );
  335. $all = array();
  336. $all_types = array();
  337. // Gather checked checkboxes and store in the $config array
  338. foreach ($values AS $key => $value) {
  339. if (preg_match('/^complete_migration--/', $key)) {
  340. $type = str_replace('complete_migration--', '', $key);
  341. if ($type != 'all') {
  342. array_push($all_types, $type);
  343. }
  344. foreach ($value AS $key_op => $op) {
  345. if ($type == 'all') {
  346. if ($op) {
  347. array_push($all, $key_op);
  348. }
  349. }
  350. else {
  351. if ($op) {
  352. array_push($config[$key_op], $type);
  353. }
  354. }
  355. }
  356. }
  357. }
  358. foreach($all AS $a) {
  359. $config[$a] = $all_types;
  360. }
  361. // Submit jobs to complete the migration
  362. global $user;
  363. $includes = array(
  364. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  365. );
  366. if (count($config['cp_title']) > 0) {
  367. $args = array($config['cp_title']);
  368. tripal_add_job("Copy Title over to Tripal v3 Content", 'tripal_chado',
  369. 'tripal_chado_copy_title_for_selected_types', $args, $user->uid, 10, $includes);
  370. }
  371. if (count($config['mv_url']) > 0) {
  372. $args = array($config['mv_url']);
  373. tripal_add_job(" Migrate URL Alias to Tripal v3 Content ", 'tripal_chado',
  374. 'tripal_chado_migrate_url_alias_for_selected_types', $args, $user->uid, 10, $includes);
  375. }
  376. if (count($config['unpublish']) > 0) {
  377. $args = array($config['unpublish']);
  378. tripal_add_job(" Unpublish Tripal v2 Content ", 'tripal_chado',
  379. 'tripal_chado_unpublish_selected_types', $args, $user->uid, 10, $includes);
  380. }
  381. if (count($config['delete']) > 0) {
  382. $args = array($config['delete']);
  383. tripal_add_job("Delete Tripal v2 Content ", 'tripal_chado',
  384. 'tripal_chado_delete_selected_types', $args, $user->uid, 10, $includes);
  385. }
  386. $_SESSION['tripal_migrate_current_tab'] = 'edit-step4';
  387. return;
  388. }
  389. // Migrate tv2 content to tv3 content
  390. else if ($form_state['clicked_button']['#name'] == 'migrate_btn') {
  391. global $user;
  392. $values = $form_state['values'];
  393. $tv2_content_type = $form_state['values']['tv2_content_type'];
  394. $tv3_content_type = array();
  395. foreach ($values AS $key => $value) {
  396. if ($tv2_content_type != 'all') {
  397. $key = urldecode($key);
  398. if (preg_match('/^tv3_content_type--(.+)--(.+)--(.+)/', $key, $matches) &&
  399. ($value == 1 || $values['tv3_migrate_all'] == 1)) {
  400. $vocabulary = $matches[1];
  401. $accession = $matches[2];
  402. $type = $matches[3];
  403. $tv3_content_type [] = array(
  404. 'vocabulary' => $vocabulary,
  405. 'accession' => $accession,
  406. 'term_name' => $type
  407. );
  408. }
  409. }
  410. }
  411. // Submit a job to migrate content
  412. global $user;
  413. $args = array(
  414. array(
  415. 'tv2_content_type' => $tv2_content_type,
  416. 'tv3_content_type' => $tv3_content_type
  417. )
  418. );
  419. $includes = array(
  420. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  421. );
  422. if ($tv2_content_type == 'all' || count($tv3_content_type) != 0) {
  423. return tripal_add_job("Migrate $tv2_content_type Tripal v2 content.",
  424. 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
  425. }
  426. else {
  427. return drupal_set_message('Nothing to do. All data have been migrated or no data for migration.');
  428. }
  429. $_SESSION['tripal_migrate_current_tab'] = 'edit-step2';
  430. }
  431. }
  432. /**
  433. * Ajax call back that returns the entire form
  434. *
  435. * The callback is triggered by ajax elements on the form which leads to the update of
  436. * entire form according to the values set on the form
  437. *
  438. * @param $form
  439. * @param $form_state
  440. * @return $form
  441. */
  442. function tripal_chado_migrate_form_step2_ajax_callback(&$form, &$form_state) {
  443. return $form['step2']['step2_container'];
  444. }
  445. /**
  446. * Get available Tripal v2 content types
  447. *
  448. * @param boolean $all_option
  449. * Include an 'all' option in the returned array
  450. * @return string[]
  451. * Return a string array keyed by the node type
  452. */
  453. function tripal_chado_get_tripal_v2_content_type_options($all_option = FALSE, $has_template = FALSE) {
  454. // Get all available Tripal v2 chado tables
  455. $sql = "
  456. SELECT table_name
  457. FROM information_schema.tables
  458. WHERE table_schema = 'public' AND table_name LIKE 'chado_%'
  459. ";
  460. $result = db_query($sql);
  461. // Store 'chado_*' tables that has at least one node
  462. $tables = array();
  463. while ($field = $result->fetchField()) {
  464. $count = db_query("SELECT count(*) FROM $field LIMIT 1")->fetchField();
  465. if ($count != 0) {
  466. array_push($tables, $field);
  467. }
  468. }
  469. // List all available Tripal v2 content types
  470. $result = db_select('node_type', 'nt')
  471. ->fields('nt', array('type', 'name', 'description'))
  472. ->condition('type', 'chado_%', 'LIKE')
  473. ->execute();
  474. $options = array();
  475. if ($all_option) {
  476. $options['all'] = 'All';
  477. }
  478. while ($obj = $result->fetchObject()) {
  479. if ($has_template) {
  480. $mod = str_replace('chado_', 'tripal_', $obj->type);
  481. $mod_dir = drupal_get_path('module', $mod);
  482. if (file_exists($mod_dir . '/theme/templates/' . $mod . '_base.tpl.php')) {
  483. $options[$obj->type] = $obj->name;
  484. }
  485. }
  486. else {
  487. if (in_array($obj->type, $tables)) {
  488. $options[$obj->type] = $obj->name;
  489. }
  490. }
  491. }
  492. return $options;
  493. }
  494. /**
  495. * Tripal job callback to migrate Tripal v2 content into Tripal v3 content
  496. *
  497. * @param $migration
  498. * @param $job_id
  499. */
  500. function tripal_chado_migrate_records($migration, $job_id = NULL) {
  501. $tv2_content_type = $migration['tv2_content_type'];
  502. $tv3_content_type = $migration['tv3_content_type'];
  503. // If tv2_content_type is 'all', migrate all existing Tripal v2 content
  504. if ($tv2_content_type == 'all') {
  505. print "Migrating all Tripal v2 content...\n";
  506. tripal_chado_migrate_all_types();
  507. }
  508. // Otherwise, migrate only selected Tripal v2 content
  509. else {
  510. print "Migrating selected Tripal v2 content...\n";
  511. tripal_chado_migrate_selected_types($tv3_content_type);
  512. }
  513. }
  514. /**
  515. * Migrate all Tripal v2 content types
  516. *
  517. * Gather all available Tripal v2 content types and store the result in an
  518. * associated array with values of vocabulary, accession, term_name. The array
  519. * is then pass to the function tripal_chado_migrate_selected_types() that
  520. * handles the migration.
  521. */
  522. function tripal_chado_migrate_all_types() {
  523. // Get all available Tripal v2 content types
  524. $tv2_content_types = tripal_chado_get_tripal_v2_content_type_options();
  525. $types = tripal_chado_migrate_map_types($tv2_content_types);
  526. tripal_chado_migrate_selected_types($types);
  527. }
  528. /**
  529. * Map all tv2_content_type to tv3_content_type
  530. *
  531. * @param unknown $tv2_content_type
  532. * return $tv3_content_type
  533. */
  534. function tripal_chado_migrate_map_types($tv2_content_types) {
  535. $types = array();
  536. foreach($tv2_content_types AS $tv2_content_type => $value) {
  537. $table = str_replace('chado_', '', $tv2_content_type);
  538. $schema = chado_get_schema($table);
  539. $pkey = $schema['primary key'][0];
  540. $fkeys = $schema['foreign keys'];
  541. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  542. // Get all Tripal v2 node types from the chad_* linking table
  543. $sql = "
  544. SELECT V.name AS type, X.accession, db.name AS vocabulary
  545. FROM {" . $table . "} T
  546. INNER JOIN public.$tv2_content_type CT ON T.$pkey = CT.$pkey
  547. INNER JOIN {cvterm} V ON V.cvterm_id = T.type_id
  548. INNER JOIN {dbxref} X ON X.dbxref_id = V.dbxref_id
  549. INNER JOIN {db} ON db.db_id = X.db_id
  550. GROUP BY V.name, X.accession, db.name
  551. ";
  552. $tv3_content_types = chado_query($sql);
  553. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  554. array_push($types, array(
  555. 'vocabulary' => $tv3_content_type->vocabulary,
  556. 'accession' => $tv3_content_type->accession,
  557. 'term_name' => $tv3_content_type->type
  558. ));
  559. }
  560. }
  561. else if ($table == 'organism') {
  562. array_push($types, array(
  563. 'vocabulary' => 'OBI',
  564. 'accession' => '0100026',
  565. 'term_name' => 'organism'
  566. ));
  567. }
  568. else if ($table == 'analysis') {
  569. array_push($types, array(
  570. 'vocabulary' => 'local',
  571. 'accession' => 'analysis',
  572. 'term_name' => 'analysis'
  573. ));
  574. }
  575. }
  576. return $types;
  577. }
  578. /**
  579. * Migrate only selected Tripal v2 content types
  580. *
  581. * @param unknown $tv3_content_type
  582. */
  583. function tripal_chado_migrate_selected_types($tv3_content_types) {
  584. // Initialize the population of the chado_cvterm_mapping table before migration.
  585. tripal_chado_map_cvterms();
  586. foreach ($tv3_content_types AS $tv3_content_type) {
  587. // Check if the term already exists
  588. $term = tripal_load_term_entity($tv3_content_type);
  589. // If term doesn't exist, create a new bundle for this term
  590. if (!$term) {
  591. print("Creating bundle for term '" . $tv3_content_type['term_name'] . "'...\n");
  592. $success = tripal_create_bundle($tv3_content_type['vocabulary'],
  593. $tv3_content_type['accession'], $tv3_content_type['term_name']);
  594. $term = tripal_load_term_entity($tv3_content_type);
  595. }
  596. // Create bundle name
  597. $bundle_name = 'bio_data_' . $term->id;
  598. // Publish records for the bundle
  599. $value = array(
  600. 'sync_node' => 1,
  601. 'bundle_name' => $bundle_name
  602. );
  603. tripal_chado_publish_records($value);
  604. }
  605. }
  606. /**
  607. * Delete selected Tripal v2 content
  608. *
  609. * Delete all records from chado_* table then call the cleanup orphan nodes function
  610. *
  611. * @param unknown $tv2_content_types
  612. */
  613. function tripal_chado_delete_selected_types($tv2_content_types = array()) {
  614. foreach ($tv2_content_types AS $type) {
  615. $sql = "DELETE FROM $type";
  616. db_query($sql);
  617. chado_cleanup_orphaned_nodes(str_replace('chado_', '', $type));
  618. }
  619. }
  620. /**
  621. * Unpublish selected Tripal v2 content
  622. *
  623. * Set status = 0 (unpublished) for all nodes of selected Tripal v2 content types
  624. *
  625. * @param unknown $tv2_content_types
  626. */
  627. function tripal_chado_unpublish_selected_types($tv2_content_types = array()) {
  628. foreach ($tv2_content_types AS $type) {
  629. $sql = "UPDATE node SET status = 0 WHERE nid IN (SELECT nid FROM $type)";
  630. db_query($sql);
  631. $sql = "UPDATE node_revision SET status = 0 WHERE nid IN (SELECT nid FROM $type)";
  632. db_query($sql);
  633. }
  634. }
  635. /**
  636. * Copy titles for selected Tripal v2 content
  637. *
  638. * Copy tiltles for all nodes of selected Tripal v2 content types
  639. *
  640. * @param unknown $tv2_content_types
  641. */
  642. function tripal_chado_copy_title_for_selected_types($tv2_content_types = array()) {
  643. foreach ($tv2_content_types AS $type) {
  644. $sql = "SELECT nid, entity_id FROM chado_entity WHERE nid IN (SELECT nid FROM $type)";
  645. $result = db_query($sql);
  646. while ($entity = $result->fetchObject()) {
  647. $usql = "
  648. UPDATE tripal_entity
  649. SET title = (SELECT title FROM node WHERE nid = :nid)
  650. WHERE id = :entity_id";
  651. db_query($usql, array(
  652. ':nid' => $entity->nid,
  653. ':entity_id' => $entity->entity_id)
  654. );
  655. }
  656. }
  657. }
  658. /**
  659. * Migrate URL alias for selected Tripal v2 content
  660. *
  661. * Migrate URL alias for all nodes of selected Tripal v2 content types
  662. *
  663. * @param unknown $tv2_content_types
  664. */
  665. function tripal_chado_migrate_url_alias_for_selected_types($tv2_content_types = array()) {
  666. foreach ($tv2_content_types AS $type) {
  667. $sql = "SELECT nid, entity_id FROM chado_entity WHERE nid IN (SELECT nid FROM $type)";
  668. $result = db_query($sql);
  669. while ($entity = $result->fetchObject()) {
  670. $usql = "
  671. UPDATE url_alias
  672. SET source = 'bio_data/" . $entity->entity_id .
  673. "' WHERE source = 'node/" . $entity->nid . "'";
  674. db_query($usql);
  675. }
  676. }
  677. }