tripal_chado.migrate.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. else if ($table == 'project') {
  186. $sql =
  187. "SELECT count(*)
  188. FROM {project} P
  189. INNER JOIN public.chado_project CP ON P.project_id = CP.project_id
  190. LEFT JOIN public.chado_entity CE ON CE.record_id = P.project_id
  191. AND CE.data_table = 'project'
  192. WHERE CE.record_id IS NULL";
  193. $proj_count = chado_query($sql)->fetchField();
  194. if ($proj_count > 0) {
  195. $key = urlencode('tv3_content_type--local--project--project');
  196. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  197. '#type' => 'checkbox',
  198. '#title' => 'Project (' . $proj_count . ')',
  199. );
  200. $no_data = FALSE;
  201. }
  202. }
  203. else if ($table == 'featuremap') {
  204. $sql =
  205. "SELECT count(*)
  206. FROM {featuremap} M
  207. INNER JOIN public.chado_featuremap CM ON M.featuremap_id = CM.featuremap_id
  208. LEFT JOIN public.chado_entity CE ON CE.record_id = M.featuremap_id
  209. AND CE.data_table = 'featuremap'
  210. WHERE CE.record_id IS NULL";
  211. $map_count = chado_query($sql)->fetchField();
  212. if ($map_count > 0) {
  213. $key = urlencode('tv3_content_type--data--1274--map');
  214. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  215. '#type' => 'checkbox',
  216. '#title' => 'Map (' . $map_count . ')',
  217. );
  218. $no_data = FALSE;
  219. }
  220. }
  221. if ($no_data) {
  222. unset($form['step2']['step2_container']['tv3_content_type']['tv3_migrate_all']);
  223. drupal_set_message('No data for migration or all have been migrated.', 'warning');
  224. }
  225. }
  226. }
  227. // Migrate button
  228. if ($tv2_content_type == 'all' || key_exists('tv3_migrate_all', $form['step2']['step2_container']['tv3_content_type'])) {
  229. $form['step2']['step2_container']['migrate_btn'] = array(
  230. '#type' => 'submit',
  231. '#name' => 'migrate_btn',
  232. '#value' => "Migrate $tv2_options[$tv2_content_type]",
  233. );
  234. }
  235. // Step 3
  236. $form['step3'] = array(
  237. '#type' => 'fieldset',
  238. '#title' => 'Step3',
  239. '#description' => '<b>Use Legacy Templates (optional)</b>',
  240. '#collapsible' => TRUE,
  241. '#collapsed' => TRUE,
  242. '#group' => 'overview_vert_tabs'
  243. );
  244. $form['step3']['instructions'] = array(
  245. '#type' => 'item',
  246. '#markup' => t('Once content types have been migrated you have the option
  247. to use the Tripal v2 display templates. Tripal v3 allows you to
  248. customize the page layout using a web interface whereas Tripal v2
  249. required programatic changes to template files. If your site has
  250. considerable Tripal v2 customizations that you do not
  251. want to lose you can use the legacy templates by checking the box
  252. for the desired content types below.'),
  253. );
  254. // Get a list of enabled legacy modules with tv2 templates
  255. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  256. $info = module_invoke_all('node_info');
  257. $options = tripal_chado_get_tripal_v2_content_type_options(FALSE, TRUE);
  258. foreach ($options AS $type_name => $type_label) {
  259. $title = $type_label;
  260. if ($type_name != 'all' and !key_exists($type_name, $info)) {
  261. $title .= " <strong>(please enable the $type_name legacy module).</strong>";
  262. }
  263. $form ['step3']['legacy_template--' . $type_name] = array (
  264. '#type' => 'checkbox',
  265. '#title' => $title,
  266. '#default_value' => key_exists('legacy_template--' . $type_name, $enabled_templates) ? $enabled_templates['legacy_template--' . $type_name] : 0,
  267. );
  268. }
  269. $form['step3']['save_btn'] = array(
  270. '#type' => 'submit',
  271. '#name' => 'save_enabled_template_btn',
  272. '#value' => "Save",
  273. );
  274. // Step 4
  275. $form['step4'] = array(
  276. '#type' => 'fieldset',
  277. '#title' => 'Step4',
  278. '#description' => '<b>Complete Migration</b>',
  279. '#collapsible' => TRUE,
  280. '#collapsed' => TRUE,
  281. '#group' => 'overview_vert_tabs'
  282. );
  283. $form['step4']['instructions'] = array(
  284. '#type' => 'item',
  285. '#markup' => t('This step allows you to fully switch to Tripal v3 pages.
  286. You can move URLs and titles from Tripal v2 pages to their
  287. corresponding Tripal v3 pages. This ensures user bookmarks and external
  288. links to your site are not broken. Here you can also unpublish Tripal
  289. v2 content types or completely remove them if desired. You can
  290. perform these action in stages such as first moving titles and URLs,
  291. then unpublishing Tripal v2 pages and once the migration has been
  292. verified you can finally delete the Tripal v2 pages to free space.
  293. Deleting the Tripal v2 content will not delete the data in Chado.
  294. The page is simply removed from the site.'),
  295. );
  296. $form['step4']['warning'] = array(
  297. '#type' => 'item',
  298. '#markup' => tripal_set_message('Any of the following options cannot be
  299. undone. Also, please be sure you have migrated all
  300. desired content types in Step 2 prior to deleting the Tripal v2
  301. contents.', TRIPAL_WARNING, array('return_html' => TRUE)),
  302. );
  303. $opt_complete_migration = array (
  304. 'cp_title' => 'Copy Title over to Tripal v3 Content',
  305. 'mv_url' => 'Migrate URL Alias to Tripal v3 Content',
  306. 'unpublish' => 'Unpublish Tripal v2 Content',
  307. 'delete' => 'Delete Tripal v2 Content',
  308. );
  309. if (count($tv2_options) == 1 && key_exists('all', $tv2_options)) {
  310. $form['step4']['complete'] = array(
  311. '#markup' => 'Migration completed. All content have been migrated.'
  312. );
  313. }
  314. else {
  315. foreach ($tv2_options AS $opt_key => $opt) {
  316. $form['step4'][$opt_key . '_title'] = array(
  317. '#markup' => "<b>$opt</b>"
  318. );
  319. $form['step4']['complete_migration--' . $opt_key] = array(
  320. '#type' => 'checkboxes',
  321. '#options' => $opt_complete_migration,
  322. );
  323. }
  324. $form['step4']['submit_btn'] = array(
  325. '#type' => 'submit',
  326. '#name' => 'complete_migration_btn',
  327. '#value' => "Submit",
  328. );
  329. }
  330. return $form;
  331. }
  332. /**
  333. * Implements hook_validate()
  334. *
  335. * @param $form
  336. * @param $form_state
  337. */
  338. function tripal_chado_migrate_form_validate($form, &$form_state) {
  339. }
  340. /**
  341. * Implements hook_submit()
  342. *
  343. * By submiting the form, a Tripal job to migrate Tripal v2 content is submitted
  344. *
  345. * @param $form
  346. * @param $form_state
  347. */
  348. function tripal_chado_migrate_form_submit($form, &$form_state) {
  349. // Store the legacy template setting in a Drupal variable
  350. if ($form_state['clicked_button']['#name'] == 'save_enabled_template_btn') {
  351. $values = $form_state['values'];
  352. $enabled_templates = array();
  353. foreach ($values AS $key => $value) {
  354. if (preg_match('/^legacy_template--/', $key)) {
  355. $enabled_templates[$key] = $value;
  356. }
  357. }
  358. variable_set('tripal_chado_enabled_legacy_templates', $enabled_templates);
  359. drupal_theme_rebuild();
  360. $_SESSION['tripal_migrate_current_tab'] = 'edit-step3';
  361. }
  362. // Complete migration
  363. else if ($form_state['clicked_button']['#name'] == 'complete_migration_btn') {
  364. $values = $form_state['values'];
  365. $config = array (
  366. 'delete' => array(),
  367. 'unpublish' => array(),
  368. 'cp_title' => array(),
  369. 'mv_url' => array()
  370. );
  371. $all = array();
  372. $all_types = array();
  373. // Gather checked checkboxes and store in the $config array
  374. foreach ($values AS $key => $value) {
  375. if (preg_match('/^complete_migration--/', $key)) {
  376. $type = str_replace('complete_migration--', '', $key);
  377. if ($type != 'all') {
  378. array_push($all_types, $type);
  379. }
  380. foreach ($value AS $key_op => $op) {
  381. if ($type == 'all') {
  382. if ($op) {
  383. array_push($all, $key_op);
  384. }
  385. }
  386. else {
  387. if ($op) {
  388. array_push($config[$key_op], $type);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. foreach($all AS $a) {
  395. $config[$a] = $all_types;
  396. }
  397. // Submit jobs to complete the migration
  398. global $user;
  399. $includes = array(
  400. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  401. );
  402. if (count($config['cp_title']) > 0) {
  403. $args = array($config['cp_title']);
  404. tripal_add_job("Copy Title over to Tripal v3 Content", 'tripal_chado',
  405. 'tripal_chado_copy_title_for_selected_types', $args, $user->uid, 10, $includes);
  406. }
  407. if (count($config['mv_url']) > 0) {
  408. $args = array($config['mv_url']);
  409. tripal_add_job(" Migrate URL Alias to Tripal v3 Content ", 'tripal_chado',
  410. 'tripal_chado_migrate_url_alias_for_selected_types', $args, $user->uid, 10, $includes);
  411. }
  412. if (count($config['unpublish']) > 0) {
  413. $args = array($config['unpublish']);
  414. tripal_add_job(" Unpublish Tripal v2 Content ", 'tripal_chado',
  415. 'tripal_chado_unpublish_selected_types', $args, $user->uid, 10, $includes);
  416. }
  417. if (count($config['delete']) > 0) {
  418. $args = array($config['delete']);
  419. tripal_add_job("Delete Tripal v2 Content ", 'tripal_chado',
  420. 'tripal_chado_delete_selected_types', $args, $user->uid, 10, $includes);
  421. }
  422. $_SESSION['tripal_migrate_current_tab'] = 'edit-step4';
  423. return;
  424. }
  425. // Migrate tv2 content to tv3 content
  426. else if ($form_state['clicked_button']['#name'] == 'migrate_btn') {
  427. global $user;
  428. $values = $form_state['values'];
  429. $tv2_content_type = $form_state['values']['tv2_content_type'];
  430. $tv3_content_type = array();
  431. foreach ($values AS $key => $value) {
  432. if ($tv2_content_type != 'all') {
  433. $key = urldecode($key);
  434. if (preg_match('/^tv3_content_type--(.+)--(.+)--(.+)/', $key, $matches) &&
  435. ($value == 1 || $values['tv3_migrate_all'] == 1)) {
  436. $vocabulary = $matches[1];
  437. $accession = $matches[2];
  438. $type = $matches[3];
  439. $table = str_replace('chado_', '', $tv2_content_type);
  440. $tv3_content_type [] = array(
  441. 'vocabulary' => $vocabulary,
  442. 'accession' => $accession,
  443. 'term_name' => $type,
  444. 'storage_args' => array (
  445. 'data_table' => $table,
  446. 'type_column' => db_field_exists('chado.' . $table, 'type_id') ? 'type_id' : ''
  447. )
  448. );
  449. }
  450. }
  451. }
  452. // Submit a job to migrate content
  453. global $user;
  454. $args = array(
  455. array(
  456. 'tv2_content_type' => $tv2_content_type,
  457. 'tv3_content_type' => $tv3_content_type
  458. )
  459. );
  460. $includes = array(
  461. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  462. );
  463. if ($tv2_content_type == 'all' || count($tv3_content_type) != 0) {
  464. return tripal_add_job("Migrate $tv2_content_type Tripal v2 content.",
  465. 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
  466. }
  467. else {
  468. return drupal_set_message('Nothing to do. All data have been migrated or no data for migration.');
  469. }
  470. $_SESSION['tripal_migrate_current_tab'] = 'edit-step2';
  471. }
  472. }
  473. /**
  474. * Ajax call back that returns the entire form
  475. *
  476. * The callback is triggered by ajax elements on the form which leads to the update of
  477. * entire form according to the values set on the form
  478. *
  479. * @param $form
  480. * @param $form_state
  481. * @return $form
  482. */
  483. function tripal_chado_migrate_form_step2_ajax_callback(&$form, &$form_state) {
  484. return $form['step2']['step2_container'];
  485. }
  486. /**
  487. * Get available Tripal v2 content types
  488. *
  489. * @param boolean $all_option
  490. * Include an 'all' option in the returned array
  491. * @return string[]
  492. * Return a string array keyed by the node type
  493. */
  494. function tripal_chado_get_tripal_v2_content_type_options($all_option = FALSE, $has_template = FALSE) {
  495. // Get all available Tripal v2 chado tables
  496. $sql = "
  497. SELECT table_name
  498. FROM information_schema.tables
  499. WHERE table_schema = 'public' AND table_name LIKE 'chado_%'
  500. ";
  501. $result = db_query($sql);
  502. // Store 'chado_*' tables that has at least one node
  503. $tables = array();
  504. while ($field = $result->fetchField()) {
  505. $count = db_query("SELECT count(*) FROM $field LIMIT 1")->fetchField();
  506. if ($count != 0) {
  507. array_push($tables, $field);
  508. }
  509. }
  510. // List all available Tripal v2 content types
  511. $result = db_select('node_type', 'nt')
  512. ->fields('nt', array('type', 'name', 'description'))
  513. ->condition('type', 'chado_%', 'LIKE')
  514. ->execute();
  515. $options = array();
  516. if ($all_option) {
  517. $options['all'] = 'All';
  518. }
  519. while ($obj = $result->fetchObject()) {
  520. if ($has_template) {
  521. $mod = str_replace('chado_', 'tripal_', $obj->type);
  522. $mod_dir = drupal_get_path('module', $mod);
  523. if (file_exists($mod_dir . '/theme/templates/' . $mod . '_base.tpl.php')) {
  524. $options[$obj->type] = $obj->name;
  525. }
  526. }
  527. else {
  528. if (in_array($obj->type, $tables)) {
  529. $options[$obj->type] = $obj->name;
  530. }
  531. }
  532. }
  533. return $options;
  534. }
  535. /**
  536. * Tripal job callback to migrate Tripal v2 content into Tripal v3 content
  537. *
  538. * @param $migration
  539. * @param $job_id
  540. */
  541. function tripal_chado_migrate_records($migration, $job_id = NULL) {
  542. $tv2_content_type = $migration['tv2_content_type'];
  543. $tv3_content_type = $migration['tv3_content_type'];
  544. // If tv2_content_type is 'all', migrate all existing Tripal v2 content
  545. if ($tv2_content_type == 'all') {
  546. print "Migrating all Tripal v2 content...\n";
  547. tripal_chado_migrate_all_types();
  548. }
  549. // Otherwise, migrate only selected Tripal v2 content
  550. else {
  551. print "Migrating selected Tripal v2 content...\n";
  552. tripal_chado_migrate_selected_types($tv3_content_type);
  553. }
  554. }
  555. /**
  556. * Migrate all Tripal v2 content types
  557. *
  558. * Gather all available Tripal v2 content types and store the result in an
  559. * associated array with values of vocabulary, accession, term_name. The array
  560. * is then pass to the function tripal_chado_migrate_selected_types() that
  561. * handles the migration.
  562. */
  563. function tripal_chado_migrate_all_types() {
  564. // Get all available Tripal v2 content types
  565. $tv2_content_types = tripal_chado_get_tripal_v2_content_type_options();
  566. $types = tripal_chado_migrate_map_types($tv2_content_types);
  567. tripal_chado_migrate_selected_types($types);
  568. }
  569. /**
  570. * Map all tv2_content_type to tv3_content_type
  571. *
  572. * @param unknown $tv2_content_type
  573. * return $tv3_content_type
  574. */
  575. function tripal_chado_migrate_map_types($tv2_content_types) {
  576. $types = array();
  577. foreach($tv2_content_types AS $tv2_content_type => $value) {
  578. $table = str_replace('chado_', '', $tv2_content_type);
  579. $schema = chado_get_schema($table);
  580. $pkey = $schema['primary key'][0];
  581. $fkeys = $schema['foreign keys'];
  582. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  583. // Get all Tripal v2 node types from the chad_* linking table
  584. $sql = "
  585. SELECT V.name AS type, X.accession, db.name AS vocabulary
  586. FROM {" . $table . "} T
  587. INNER JOIN public.$tv2_content_type CT ON T.$pkey = CT.$pkey
  588. INNER JOIN {cvterm} V ON V.cvterm_id = T.type_id
  589. INNER JOIN {dbxref} X ON X.dbxref_id = V.dbxref_id
  590. INNER JOIN {db} ON db.db_id = X.db_id
  591. GROUP BY V.name, X.accession, db.name
  592. ";
  593. $tv3_content_types = chado_query($sql);
  594. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  595. array_push($types, array(
  596. 'vocabulary' => $tv3_content_type->vocabulary,
  597. 'accession' => $tv3_content_type->accession,
  598. 'term_name' => $tv3_content_type->type,
  599. 'storage_args' => array (
  600. 'data_table' => $table,
  601. 'type_column' => db_field_exists('chado.' . $table, 'type_id') ? 'type_id' : ''
  602. )
  603. ));
  604. }
  605. }
  606. else if ($table == 'organism') {
  607. array_push($types, array(
  608. 'vocabulary' => 'OBI',
  609. 'accession' => '0100026',
  610. 'term_name' => 'organism',
  611. 'storage_args' => array (
  612. 'data_table' => $table
  613. )
  614. ));
  615. }
  616. else if ($table == 'analysis') {
  617. array_push($types, array(
  618. 'vocabulary' => 'local',
  619. 'accession' => 'analysis',
  620. 'term_name' => 'analysis',
  621. 'storage_args' => array (
  622. 'data_table' => $table
  623. )
  624. ));
  625. }
  626. else if ($table == 'project') {
  627. array_push($types, array(
  628. 'vocabulary' => 'local',
  629. 'accession' => 'project',
  630. 'term_name' => 'project',
  631. 'storage_args' => array (
  632. 'data_table' => $table
  633. )
  634. ));
  635. }
  636. else if ($table == 'featuremap') {
  637. array_push($types, array(
  638. 'vocabulary' => 'data',
  639. 'accession' => '1274',
  640. 'term_name' => 'map',
  641. 'storage_args' => array (
  642. 'data_table' => $table
  643. )
  644. ));
  645. }
  646. }
  647. return $types;
  648. }
  649. /**
  650. * Migrate only selected Tripal v2 content types
  651. *
  652. * @param unknown $tv3_content_type
  653. */
  654. function tripal_chado_migrate_selected_types($tv3_content_types) {
  655. // Initialize the population of the chado_cvterm_mapping table before migration.
  656. tripal_chado_map_cvterms();
  657. foreach ($tv3_content_types AS $tv3_content_type) {
  658. // Check if the term already exists
  659. $term = tripal_load_term_entity($tv3_content_type);
  660. // If term doesn't exist, create a new bundle for this term
  661. if (!$term) {
  662. print("Creating bundle for term '" . $tv3_content_type['term_name'] . "'...\n");
  663. $success = tripal_create_bundle($tv3_content_type);
  664. $term = tripal_load_term_entity($tv3_content_type);
  665. }
  666. // Create bundle name
  667. $bundle_name = 'bio_data_' . $term->id;
  668. // Publish records for the bundle
  669. $value = array(
  670. 'sync_node' => 1,
  671. 'bundle_name' => $bundle_name
  672. );
  673. tripal_chado_publish_records($value);
  674. }
  675. }
  676. /**
  677. * Delete selected Tripal v2 content
  678. *
  679. * Delete all records from chado_* table then call the cleanup orphan nodes function
  680. *
  681. * @param unknown $tv2_content_types
  682. */
  683. function tripal_chado_delete_selected_types($tv2_content_types = array()) {
  684. foreach ($tv2_content_types AS $type) {
  685. $sql = "DELETE FROM $type";
  686. db_query($sql);
  687. chado_cleanup_orphaned_nodes(str_replace('chado_', '', $type));
  688. }
  689. }
  690. /**
  691. * Unpublish selected Tripal v2 content
  692. *
  693. * Set status = 0 (unpublished) for all nodes of selected Tripal v2 content types
  694. *
  695. * @param unknown $tv2_content_types
  696. */
  697. function tripal_chado_unpublish_selected_types($tv2_content_types = array()) {
  698. foreach ($tv2_content_types AS $type) {
  699. $sql = "UPDATE node SET status = 0 WHERE nid IN (SELECT nid FROM $type)";
  700. db_query($sql);
  701. $sql = "UPDATE node_revision SET status = 0 WHERE nid IN (SELECT nid FROM $type)";
  702. db_query($sql);
  703. }
  704. }
  705. /**
  706. * Copy titles for selected Tripal v2 content
  707. *
  708. * Copy tiltles for all nodes of selected Tripal v2 content types
  709. *
  710. * @param unknown $tv2_content_types
  711. */
  712. function tripal_chado_copy_title_for_selected_types($tv2_content_types = array()) {
  713. foreach ($tv2_content_types AS $type) {
  714. $sql = "SELECT nid, entity_id FROM chado_entity WHERE nid IN (SELECT nid FROM $type)";
  715. $result = db_query($sql);
  716. while ($entity = $result->fetchObject()) {
  717. $usql = "
  718. UPDATE tripal_entity
  719. SET title = (SELECT title FROM node WHERE nid = :nid)
  720. WHERE id = :entity_id";
  721. db_query($usql, array(
  722. ':nid' => $entity->nid,
  723. ':entity_id' => $entity->entity_id)
  724. );
  725. }
  726. }
  727. }
  728. /**
  729. * Migrate URL alias for selected Tripal v2 content
  730. *
  731. * Migrate URL alias for all nodes of selected Tripal v2 content types
  732. *
  733. * @param unknown $tv2_content_types
  734. */
  735. function tripal_chado_migrate_url_alias_for_selected_types($tv2_content_types = array()) {
  736. foreach ($tv2_content_types AS $type) {
  737. $sql = "SELECT nid, entity_id FROM chado_entity WHERE nid IN (SELECT nid FROM $type)";
  738. $result = db_query($sql);
  739. while ($entity = $result->fetchObject()) {
  740. $usql = "
  741. UPDATE url_alias
  742. SET source = 'bio_data/" . $entity->entity_id .
  743. "' WHERE source = 'node/" . $entity->nid . "'";
  744. db_query($usql);
  745. }
  746. }
  747. }