tripal_chado.migrate.inc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. If you would like to
  66. use Trial v3 web services you must migrate content types.'),
  67. );
  68. $tv2_content_type = 'all';
  69. if (array_key_exists('values', $form_state)) {
  70. $tv2_content_type = $form_state['values']['tv2_content_type'];
  71. }
  72. $tv2_options = tripal_chado_get_tripal_v2_content_type_options(TRUE);
  73. $form['step2']['step2_container']['tv2_content_type'] = array(
  74. '#type' => 'select',
  75. '#title' => 'Tripal v2 Content Type',
  76. '#description' => t('Select the Tripal v2 content type to migrate.'),
  77. '#options' => $tv2_options,
  78. '#default_value' => $tv2_content_type,
  79. '#ajax' => array(
  80. 'callback' => "tripal_chado_migrate_form_step2_ajax_callback",
  81. 'wrapper' => "tripal-chado-migrate-form-step2",
  82. 'effect' => 'fade',
  83. 'method' => 'replace'
  84. ),
  85. );
  86. // Add a review button that allows reviewing migratable content types
  87. if ($tv2_content_type != 'all') {
  88. $table = str_replace('chado_', '', $tv2_content_type);
  89. $schema = chado_get_schema($table);
  90. $pkey = $schema['primary key'][0];
  91. $fkeys = $schema['foreign keys'];
  92. $form['step2']['step2_container']['tv3_content_type'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => 'Tripal v3 Content Type',
  95. '#description' => "Click the 'Get Tripal v3 Types' button to retrieve a list of Tripal v3
  96. content types to which this Tripal v2 type can be converted. This may take a while
  97. depending on the size of your database. The number of items to be converted is
  98. shown beside the type."
  99. );
  100. $form['step2']['step2_container']['tv3_content_type']['get_v3_type_btn'] = array(
  101. '#type' => 'button',
  102. '#name' => 'get_v3_type_btn',
  103. '#value' => "Get Tripal v3 Types",
  104. '#ajax' => array(
  105. 'callback' => "tripal_chado_migrate_form_step2_ajax_callback",
  106. 'wrapper' => "tripal-chado-migrate-form-step2",
  107. 'effect' => 'fade',
  108. 'method' => 'replace'
  109. ),
  110. );
  111. $no_data = TRUE;
  112. if ($form_state['clicked_button']['#name'] == 'get_v3_type_btn') {
  113. // Migrate all
  114. $form['step2']['step2_container']['tv3_content_type']['tv3_migrate_all'] = array(
  115. '#type' => 'checkbox',
  116. '#title' => 'Migrate All'
  117. );
  118. // Migrate selection only
  119. if ($table == 'organism') {
  120. $sql =
  121. "SELECT count(*)
  122. FROM {organism} O
  123. INNER JOIN [chado_organism] CO ON O.organism_id = CO.organism_id
  124. ";
  125. $org_count = chado_query($sql)->fetchField();
  126. if ($org_count > 0) {
  127. $key = urldecode('tv3_content_type--OBI--0100026--organism');
  128. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  129. '#type' => 'checkbox',
  130. '#title' => 'Organism (' . $org_count . ')',
  131. );
  132. $no_data = FALSE;
  133. }
  134. }
  135. else if ($table == 'analysis') {
  136. $sql =
  137. "SELECT count(*)
  138. FROM {analysis} A
  139. INNER JOIN [chado_analysis] CA ON A.analysis_id = CA.analysis_id
  140. ";
  141. $ana_count = chado_query($sql)->fetchField();
  142. if ($ana_count > 0) {
  143. $key = urlencode('tv3_content_type--operation--2945--analysis');
  144. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  145. '#type' => 'checkbox',
  146. '#title' => 'Analysis (' . $ana_count . ')',
  147. );
  148. $no_data = FALSE;
  149. }
  150. }
  151. else if ($table == 'project') {
  152. $sql =
  153. "SELECT count(*)
  154. FROM {project} P
  155. INNER JOIN [chado_project] CP ON P.project_id = CP.project_id
  156. ";
  157. $proj_count = chado_query($sql)->fetchField();
  158. if ($proj_count > 0) {
  159. $key = urlencode('tv3_content_type--local--project--project');
  160. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  161. '#type' => 'checkbox',
  162. '#title' => 'Project (' . $proj_count . ')',
  163. );
  164. $no_data = FALSE;
  165. }
  166. }
  167. else if ($table == 'featuremap') {
  168. $sql =
  169. "SELECT count(*)
  170. FROM {featuremap} M
  171. INNER JOIN [chado_featuremap] CM ON M.featuremap_id = CM.featuremap_id
  172. ";
  173. $map_count = chado_query($sql)->fetchField();
  174. if ($map_count > 0) {
  175. $key = urlencode('tv3_content_type--data--1274--map');
  176. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  177. '#type' => 'checkbox',
  178. '#title' => 'Map (' . $map_count . ')',
  179. );
  180. $no_data = FALSE;
  181. }
  182. }
  183. else if ($table == 'pub') {
  184. $sql =
  185. "SELECT count(*)
  186. FROM {pub} P
  187. INNER JOIN [chado_pub] CP ON P.pub_id = CP.pub_id
  188. ";
  189. $proj_count = chado_query($sql)->fetchField();
  190. if ($proj_count > 0) {
  191. $key = urlencode('tv3_content_type--TPUB--0000002--Publication');
  192. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  193. '#type' => 'checkbox',
  194. '#title' => 'Publication (' . $proj_count . ')',
  195. );
  196. $no_data = FALSE;
  197. }
  198. }
  199. else if ($table == 'biomaterial') {
  200. $sql =
  201. "SELECT count(*)
  202. FROM {biomaterial} B
  203. INNER JOIN [chado_biomaterial] CB ON B.biomaterial_id = CB.biomaterial_id
  204. ";
  205. $bm_count = chado_query($sql)->fetchField();
  206. if ($bm_count > 0) {
  207. $key = urlencode('tv3_content_type--sep--1095--biological sample');
  208. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  209. '#type' => 'checkbox',
  210. '#title' => 'Biomaterial (' . $bm_count . ')',
  211. );
  212. $no_data = FALSE;
  213. }
  214. }
  215. else if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  216. // Get all Tripal v2 node types from the chad_* linking table
  217. $sql =
  218. "SELECT V.name AS type, X.accession, db.name AS vocabulary , count(*) AS num
  219. FROM {" . $table . "} T
  220. INNER JOIN [" . $tv2_content_type . "] CT ON T.$pkey = CT.$pkey
  221. INNER JOIN {cvterm} V ON V.cvterm_id = T.type_id
  222. INNER JOIN {dbxref} X ON X.dbxref_id = V.dbxref_id
  223. INNER JOIN {db} ON db.db_id = X.db_id
  224. GROUP BY V.name, X.accession, db.name";
  225. $tv3_content_types = chado_query($sql);
  226. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  227. // We need to store vocabulary/accession/type for each checkbox in the key becuase
  228. // the value only allows 1 or 0
  229. $key = urlencode(
  230. 'tv3_content_type--' .
  231. $tv3_content_type->vocabulary . '--' .
  232. $tv3_content_type->accession . '--' .
  233. $tv3_content_type->type);
  234. $form['step2']['step2_container']['tv3_content_type'][$key] = array(
  235. '#type' => 'checkbox',
  236. '#title' => $tv3_content_type->type . ' (' . $tv3_content_type->num . ')',
  237. );
  238. $no_data = FALSE;
  239. }
  240. }
  241. if ($no_data) {
  242. unset($form['step2']['step2_container']['tv3_content_type']['tv3_migrate_all']);
  243. drupal_set_message('No data for migration or all have been migrated.', 'warning');
  244. }
  245. }
  246. }
  247. // Migrate button
  248. if ($tv2_content_type == 'all' || key_exists('tv3_migrate_all', $form['step2']['step2_container']['tv3_content_type'])) {
  249. $form['step2']['step2_container']['migrate_btn'] = array(
  250. '#type' => 'submit',
  251. '#name' => 'migrate_btn',
  252. '#value' => "Migrate $tv2_options[$tv2_content_type]",
  253. );
  254. // Disable the migration if all have been done
  255. if (count($tv2_options) == 1 && key_exists('all', $tv2_options)) {
  256. $form['step2']['step2_container']['migrate_btn']['#disabled'] = 1;
  257. }
  258. }
  259. // Step 3
  260. $form['step3'] = array(
  261. '#type' => 'fieldset',
  262. '#title' => 'Step3',
  263. '#description' => '<b>Use Legacy Templates (optional)</b>',
  264. '#collapsible' => TRUE,
  265. '#collapsed' => TRUE,
  266. '#group' => 'overview_vert_tabs'
  267. );
  268. $form['step3']['instructions'] = array(
  269. '#type' => 'item',
  270. '#markup' => t('Once content types have been migrated you have the option
  271. to use the Tripal v2 display templates. Tripal v3 allows you to
  272. customize the page layout using a web interface whereas Tripal v2
  273. required programatic changes to template files. If your site has
  274. considerable Tripal v2 customizations that you do not
  275. want to lose you can use the legacy templates by checking the box
  276. for the desired content types below.'),
  277. );
  278. // Get a list of enabled legacy modules with tv2 templates
  279. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  280. $info = module_invoke_all('node_info');
  281. $options = tripal_chado_get_tripal_v2_content_type_options(FALSE, TRUE);
  282. foreach ($options AS $type_name => $type_label) {
  283. $title = $type_label;
  284. if ($type_name != 'all' and !key_exists($type_name, $info)) {
  285. $title .= " <strong>(please enable the $type_name legacy module).</strong>";
  286. }
  287. $form ['step3']['legacy_template--' . $type_name] = array (
  288. '#type' => 'checkbox',
  289. '#title' => $title,
  290. '#default_value' => key_exists('legacy_template--' . $type_name, $enabled_templates) ? $enabled_templates['legacy_template--' . $type_name] : 0,
  291. );
  292. }
  293. $form['step3']['save_btn'] = array(
  294. '#type' => 'submit',
  295. '#name' => 'save_enabled_template_btn',
  296. '#value' => "Save",
  297. );
  298. // Step 4
  299. $form['step4'] = array(
  300. '#type' => 'fieldset',
  301. '#title' => 'Step4',
  302. '#description' => '<b>Complete Migration</b>',
  303. '#collapsible' => TRUE,
  304. '#collapsed' => TRUE,
  305. '#group' => 'overview_vert_tabs'
  306. );
  307. $form['step4']['instructions'] = array(
  308. '#type' => 'item',
  309. '#markup' => t('This step allows you to fully switch to Tripal v3 pages.
  310. You can move URLs and titles from Tripal v2 pages to their
  311. corresponding Tripal v3 pages. This ensures user bookmarks and external
  312. links to your site are not broken. Here you can also unpublish Tripal
  313. v2 content types or completely remove them if desired. You can
  314. perform these action in stages such as first moving titles and URLs,
  315. then unpublishing Tripal v2 pages and once the migration has been
  316. verified you can finally delete the Tripal v2 pages to free space.
  317. Deleting the Tripal v2 content will not delete the data in Chado.
  318. The page is simply removed from the site.'),
  319. );
  320. $form['step4']['warning'] = array(
  321. '#type' => 'item',
  322. '#markup' => tripal_set_message('Any of the following options cannot be
  323. undone. Also, please be sure you have migrated all
  324. desired content types in Step 2 prior to deleting the Tripal v2
  325. contents.', TRIPAL_WARNING, array('return_html' => TRUE)),
  326. );
  327. $opt_complete_migration = array (
  328. 'cp_title' => 'Copy Title over to Tripal v3 Content',
  329. 'mv_url' => 'Migrate URL Alias to Tripal v3 Content',
  330. 'unpublish' => 'Unpublish Tripal v2 Content',
  331. 'delete' => 'Delete Tripal v2 Content',
  332. );
  333. if (count($tv2_options) == 1 && key_exists('all', $tv2_options)) {
  334. $form['step4']['complete'] = array(
  335. '#markup' => 'Migration completed. All content have been migrated.'
  336. );
  337. }
  338. else {
  339. foreach ($tv2_options AS $opt_key => $opt) {
  340. $form['step4'][$opt_key . '_title'] = array(
  341. '#markup' => "<b>$opt</b>"
  342. );
  343. $form['step4']['complete_migration--' . $opt_key] = array(
  344. '#type' => 'checkboxes',
  345. '#options' => $opt_complete_migration,
  346. );
  347. }
  348. $form['step4']['submit_btn'] = array(
  349. '#type' => 'submit',
  350. '#name' => 'complete_migration_btn',
  351. '#value' => "Submit",
  352. );
  353. }
  354. return $form;
  355. }
  356. /**
  357. * Implements hook_validate()
  358. *
  359. * @param $form
  360. * @param $form_state
  361. */
  362. function tripal_chado_migrate_form_validate($form, &$form_state) {
  363. }
  364. /**
  365. * Implements hook_submit()
  366. *
  367. * By submiting the form, a Tripal job to migrate Tripal v2 content is submitted
  368. *
  369. * @param $form
  370. * @param $form_state
  371. */
  372. function tripal_chado_migrate_form_submit($form, &$form_state) {
  373. // Store the legacy template setting in a Drupal variable
  374. if ($form_state['clicked_button']['#name'] == 'save_enabled_template_btn') {
  375. $values = $form_state['values'];
  376. $enabled_templates = array();
  377. foreach ($values AS $key => $value) {
  378. if (preg_match('/^legacy_template--/', $key)) {
  379. $enabled_templates[$key] = $value;
  380. }
  381. }
  382. variable_set('tripal_chado_enabled_legacy_templates', $enabled_templates);
  383. drupal_theme_rebuild();
  384. $_SESSION['tripal_migrate_current_tab'] = 'edit-step3';
  385. }
  386. // Complete migration
  387. else if ($form_state['clicked_button']['#name'] == 'complete_migration_btn') {
  388. $values = $form_state['values'];
  389. $config = array (
  390. 'delete' => array(),
  391. 'unpublish' => array(),
  392. 'cp_title' => array(),
  393. 'mv_url' => array()
  394. );
  395. $all = array();
  396. $all_types = array();
  397. // Gather checked checkboxes and store in the $config array
  398. foreach ($values AS $key => $value) {
  399. if (preg_match('/^complete_migration--/', $key)) {
  400. $type = str_replace('complete_migration--', '', $key);
  401. if ($type != 'all') {
  402. array_push($all_types, $type);
  403. }
  404. foreach ($value AS $key_op => $op) {
  405. if ($type == 'all') {
  406. if ($op) {
  407. array_push($all, $key_op);
  408. }
  409. }
  410. else {
  411. if ($op) {
  412. array_push($config[$key_op], $type);
  413. }
  414. }
  415. }
  416. }
  417. }
  418. foreach($all AS $a) {
  419. $config[$a] = $all_types;
  420. }
  421. // Submit jobs to complete the migration
  422. global $user;
  423. $includes = array(
  424. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  425. );
  426. if (count($config['cp_title']) > 0) {
  427. $args = array($config['cp_title']);
  428. tripal_add_job("Copy Title over to Tripal v3 Content", 'tripal_chado',
  429. 'tripal_chado_copy_title_for_selected_types', $args, $user->uid, 10, $includes);
  430. }
  431. if (count($config['mv_url']) > 0) {
  432. $args = array($config['mv_url']);
  433. tripal_add_job(" Migrate URL Alias to Tripal v3 Content ", 'tripal_chado',
  434. 'tripal_chado_migrate_url_alias_for_selected_types', $args, $user->uid, 10, $includes);
  435. }
  436. if (count($config['unpublish']) > 0) {
  437. $args = array($config['unpublish']);
  438. tripal_add_job(" Unpublish Tripal v2 Content ", 'tripal_chado',
  439. 'tripal_chado_unpublish_selected_types', $args, $user->uid, 10, $includes);
  440. }
  441. if (count($config['delete']) > 0) {
  442. $args = array($config['delete']);
  443. tripal_add_job("Delete Tripal v2 Content ", 'tripal_chado',
  444. 'tripal_chado_delete_selected_types', $args, $user->uid, 10, $includes);
  445. }
  446. $_SESSION['tripal_migrate_current_tab'] = 'edit-step4';
  447. return;
  448. }
  449. // Migrate tv2 content to tv3 content
  450. else if ($form_state['clicked_button']['#name'] == 'migrate_btn') {
  451. global $user;
  452. $values = $form_state['values'];
  453. $tv2_content_type = $form_state['values']['tv2_content_type'];
  454. $tv3_content_type = array();
  455. foreach ($values AS $key => $value) {
  456. if ($tv2_content_type != 'all') {
  457. $key = urldecode($key);
  458. if (preg_match('/^tv3_content_type--(.+)--(.+)--(.+)/', $key, $matches) &&
  459. ($value == 1 || $values['tv3_migrate_all'] == 1)) {
  460. $vocabulary = $matches[1];
  461. $accession = $matches[2];
  462. $type = $matches[3];
  463. $table = str_replace('chado_', '', $tv2_content_type);
  464. $tv3_content_type [] = array(
  465. 'vocabulary' => $vocabulary,
  466. 'accession' => $accession,
  467. 'term_name' => $type,
  468. 'storage_args' => array (
  469. 'data_table' => $table,
  470. 'type_column' => db_field_exists('chado.' . $table, 'type_id') ? 'type_id' : ''
  471. )
  472. );
  473. }
  474. }
  475. }
  476. // Submit a job to migrate content
  477. global $user;
  478. $args = array(
  479. array(
  480. 'tv2_content_type' => $tv2_content_type,
  481. 'tv3_content_type' => $tv3_content_type
  482. )
  483. );
  484. $includes = array(
  485. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  486. );
  487. if ($tv2_content_type == 'all' || count($tv3_content_type) != 0) {
  488. return tripal_add_job("Migrate $tv2_content_type Tripal v2 content.",
  489. 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
  490. }
  491. else {
  492. return drupal_set_message('Nothing to do. All data have been migrated or no data for migration.');
  493. }
  494. $_SESSION['tripal_migrate_current_tab'] = 'edit-step2';
  495. }
  496. }
  497. /**
  498. * Ajax call back that returns the entire form
  499. *
  500. * The callback is triggered by ajax elements on the form which leads to the update of
  501. * entire form according to the values set on the form
  502. *
  503. * @param $form
  504. * @param $form_state
  505. * @return $form
  506. */
  507. function tripal_chado_migrate_form_step2_ajax_callback(&$form, &$form_state) {
  508. return $form['step2']['step2_container'];
  509. }
  510. /**
  511. * Get available Tripal v2 content types
  512. *
  513. * @param boolean $all_option
  514. * Include an 'all' option in the returned array
  515. * @param boolean $has_template
  516. * Return TV2 content types only if it has a base template. This will exclude TV2 content
  517. * types such as Blast/Kegg/Interpro/Unigene which are all converted into the TV3
  518. * 'Analysis' entity type
  519. * @return string[]
  520. * Return a string array keyed by the node type
  521. */
  522. function tripal_chado_get_tripal_v2_content_type_options($all_option = FALSE, $has_template = FALSE) {
  523. // Get all available Tripal v2 chado tables
  524. $sql = "
  525. SELECT table_name
  526. FROM information_schema.tables
  527. WHERE table_schema = 'public' AND table_name LIKE 'chado_%'
  528. ";
  529. $result = db_query($sql);
  530. // Store 'chado_*' tables that has at least one node
  531. $tables = array();
  532. while ($field = $result->fetchField()) {
  533. $count = db_query("SELECT count(*) FROM $field LIMIT 1")->fetchField();
  534. if ($count != 0) {
  535. array_push($tables, $field);
  536. }
  537. }
  538. // List all available Tripal v2 content types
  539. $result = db_select('node_type', 'nt')
  540. ->fields('nt', array('type', 'name', 'description'))
  541. ->condition('type', 'chado_%', 'LIKE')
  542. ->execute();
  543. $options = array();
  544. if ($all_option) {
  545. $options['all'] = 'All';
  546. }
  547. while ($obj = $result->fetchObject()) {
  548. // Ignore analysis sub modules
  549. if ($has_template && !preg_match('/^chado_analysis_.+$/', $obj->type)) {
  550. $mod = str_replace('chado_', 'tripal_', $obj->type);
  551. $mod_dir = drupal_get_path('module', $mod);
  552. if (file_exists($mod_dir . '/theme/templates/' . $mod . '_base.tpl.php')) {
  553. $options[$obj->type] = $obj->name;
  554. }
  555. }
  556. else {
  557. if (in_array($obj->type, $tables)) {
  558. $options[$obj->type] = $obj->name;
  559. }
  560. }
  561. }
  562. return $options;
  563. }
  564. /**
  565. * Tripal job callback to migrate Tripal v2 content into Tripal v3 content
  566. *
  567. * @param $migration
  568. * @param $job_id
  569. */
  570. function tripal_chado_migrate_records($migration, $job_id = NULL) {
  571. $tv2_content_type = $migration['tv2_content_type'];
  572. $tv3_content_type = $migration['tv3_content_type'];
  573. // If tv2_content_type is 'all', migrate all existing Tripal v2 content
  574. if ($tv2_content_type == 'all') {
  575. print "Migrating all Tripal v2 content...\n";
  576. tripal_chado_migrate_all_types();
  577. }
  578. // Otherwise, migrate only selected Tripal v2 content
  579. else {
  580. print "Migrating selected Tripal v2 content...\n";
  581. tripal_chado_migrate_selected_types($tv3_content_type);
  582. }
  583. }
  584. /**
  585. * Migrate all Tripal v2 content types
  586. *
  587. * Gather all available Tripal v2 content types and store the result in an
  588. * associated array with values of vocabulary, accession, term_name. The array
  589. * is then pass to the function tripal_chado_migrate_selected_types() that
  590. * handles the migration.
  591. */
  592. function tripal_chado_migrate_all_types() {
  593. // Get all available Tripal v2 content types
  594. $tv2_content_types = tripal_chado_get_tripal_v2_content_type_options();
  595. $types = tripal_chado_migrate_map_types($tv2_content_types);
  596. tripal_chado_migrate_selected_types($types);
  597. }
  598. /**
  599. * Map all tv2_content_type to tv3_content_type
  600. *
  601. * @param unknown $tv2_content_type
  602. * return $tv3_content_type
  603. */
  604. function tripal_chado_migrate_map_types($tv2_content_types) {
  605. $types = array();
  606. foreach($tv2_content_types AS $tv2_content_type => $value) {
  607. $table = str_replace('chado_', '', $tv2_content_type);
  608. $schema = chado_get_schema($table);
  609. $pkey = $schema['primary key'][0];
  610. $fkeys = $schema['foreign keys'];
  611. if ($table == 'organism') {
  612. array_push($types, array(
  613. 'vocabulary' => 'OBI',
  614. 'accession' => '0100026',
  615. 'term_name' => 'organism',
  616. 'storage_args' => array (
  617. 'data_table' => $table
  618. )
  619. ));
  620. }
  621. else if ($table == 'analysis') {
  622. array_push($types, array(
  623. 'vocabulary' => 'operation',
  624. 'accession' => '2945',
  625. 'term_name' => 'Analysis',
  626. 'storage_args' => array (
  627. 'data_table' => $table
  628. )
  629. ));
  630. }
  631. else if ($table == 'project') {
  632. array_push($types, array(
  633. 'vocabulary' => 'local',
  634. 'accession' => 'project',
  635. 'term_name' => 'project',
  636. 'storage_args' => array (
  637. 'data_table' => $table
  638. )
  639. ));
  640. }
  641. else if ($table == 'featuremap') {
  642. array_push($types, array(
  643. 'vocabulary' => 'data',
  644. 'accession' => '1274',
  645. 'term_name' => 'map',
  646. 'storage_args' => array (
  647. 'data_table' => $table
  648. )
  649. ));
  650. }
  651. else if ($table == 'pub') {
  652. array_push($types, array(
  653. 'vocabulary' => 'TPUB',
  654. 'accession' => '0000002',
  655. 'term_name' => 'Publication',
  656. 'storage_args' => array (
  657. 'data_table' => $table
  658. )
  659. ));
  660. }
  661. else if ($table == 'biomaterial') {
  662. array_push($types, array(
  663. 'vocabulary' => 'sep',
  664. 'accession' => '1095',
  665. 'term_name' => 'biological sample',
  666. 'storage_args' => array (
  667. 'data_table' => $table
  668. )
  669. ));
  670. }
  671. else if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  672. // Get all Tripal v2 node types from the chad_* linking table
  673. $sql = "
  674. SELECT V.name AS type, X.accession, db.name AS vocabulary
  675. FROM {" . $table . "} T
  676. INNER JOIN [" . $tv2_content_type . "] CT ON T.$pkey = CT.$pkey
  677. INNER JOIN {cvterm} V ON V.cvterm_id = T.type_id
  678. INNER JOIN {dbxref} X ON X.dbxref_id = V.dbxref_id
  679. INNER JOIN {db} ON db.db_id = X.db_id
  680. GROUP BY V.name, X.accession, db.name
  681. ";
  682. $tv3_content_types = chado_query($sql);
  683. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  684. array_push($types, array(
  685. 'vocabulary' => $tv3_content_type->vocabulary,
  686. 'accession' => $tv3_content_type->accession,
  687. 'term_name' => $tv3_content_type->type,
  688. 'storage_args' => array (
  689. 'data_table' => $table,
  690. 'type_column' => db_field_exists('chado.' . $table, 'type_id') ? 'type_id' : ''
  691. )
  692. ));
  693. }
  694. }
  695. }
  696. return $types;
  697. }
  698. /**
  699. * Migrate only selected Tripal v2 content types
  700. *
  701. * @param unknown $tv3_content_type
  702. */
  703. function tripal_chado_migrate_selected_types($tv3_content_types) {
  704. // Initialize the population of the chado_cvterm_mapping table before migration.
  705. tripal_chado_map_cvterms();
  706. foreach ($tv3_content_types AS $tv3_content_type) {
  707. // Check if the term already exists
  708. $term = tripal_load_term_entity($tv3_content_type);
  709. // If term doesn't exist, create a new bundle for this term
  710. if (!$term) {
  711. print("Creating bundle for term '" . $tv3_content_type['term_name'] . "'...\n");
  712. $success = tripal_create_bundle($tv3_content_type);
  713. $term = tripal_load_term_entity($tv3_content_type);
  714. }
  715. // Create bundle name
  716. $bundle_name = 'bio_data_' . $term->id;
  717. // Publish records for the bundle
  718. $value = array(
  719. 'sync_node' => 1,
  720. 'bundle_name' => $bundle_name
  721. );
  722. chado_publish_records($value);
  723. // Migrate Resource Titles/Blocks or Resource Links if available
  724. tripal_chado_migrate_resource_blocks($bundle_name);
  725. tripal_chado_migrate_resource_links($bundle_name);
  726. // Migrate organism images
  727. if ($term->name == 'organism') {
  728. tripal_chado_migrate_organism_images($bundle_name);
  729. }
  730. }
  731. }
  732. /**
  733. * Delete selected Tripal v2 content
  734. *
  735. * Delete all records from chado_* table then call the cleanup orphan nodes function
  736. *
  737. * @param unknown $tv2_content_types
  738. */
  739. function tripal_chado_delete_selected_types($tv2_content_types = array(), $job_id) {
  740. foreach ($tv2_content_types AS $type) {
  741. $sql = "DELETE FROM $type";
  742. db_query($sql);
  743. chado_cleanup_orphaned_nodes(str_replace('chado_', '', $type), 25000, NULL, NULL, $job_id);
  744. }
  745. }
  746. /**
  747. * Unpublish selected Tripal v2 content
  748. *
  749. * Set status = 0 (unpublished) for all nodes of selected Tripal v2 content types
  750. *
  751. * @param unknown $tv2_content_types
  752. */
  753. function tripal_chado_unpublish_selected_types($tv2_content_types = array(), $job_id) {
  754. foreach ($tv2_content_types AS $type) {
  755. $sql = "UPDATE node SET status = 0 WHERE nid IN (SELECT nid FROM $type)";
  756. db_query($sql);
  757. $sql = "UPDATE node_revision SET status = 0 WHERE nid IN (SELECT nid FROM $type)";
  758. db_query($sql);
  759. }
  760. }
  761. /**
  762. * Copy titles for selected Tripal v2 content
  763. *
  764. * Copy tiltles for all nodes of selected Tripal v2 content types
  765. *
  766. * @param unknown $tv2_content_types
  767. */
  768. function tripal_chado_copy_title_for_selected_types($tv2_content_types = array(), $job_id) {
  769. foreach ($tv2_content_types AS $type) {
  770. $chado_table = str_replace('chado_', '', $type);
  771. $bio_data_tables = tripal_chado_migrate_get_biodata_tables($chado_table);
  772. foreach($bio_data_tables AS $bio_data_table) {
  773. $sql = "SELECT nid, entity_id FROM $bio_data_table WHERE nid IN (SELECT nid FROM $type)";
  774. $result = db_query($sql);
  775. while ($entity = $result->fetchObject()) {
  776. $usql = "
  777. UPDATE tripal_entity
  778. SET title = (SELECT title FROM node WHERE nid = :nid)
  779. WHERE id = :entity_id";
  780. db_query($usql, array(
  781. ':nid' => $entity->nid,
  782. ':entity_id' => $entity->entity_id)
  783. );
  784. }
  785. }
  786. }
  787. }
  788. /**
  789. * Migrate URL alias for selected Tripal v2 content
  790. *
  791. * Migrate URL alias for all nodes of selected Tripal v2 content types
  792. *
  793. * @param unknown $tv2_content_types
  794. */
  795. function tripal_chado_migrate_url_alias_for_selected_types($tv2_content_types = array(), $job_id) {
  796. foreach ($tv2_content_types AS $type) {
  797. $chado_table = str_replace('chado_', '', $type);
  798. $bio_data_tables = tripal_chado_migrate_get_biodata_tables($chado_table);
  799. foreach($bio_data_tables AS $bio_data_table) {
  800. $sql = "SELECT nid, entity_id FROM $bio_data_table WHERE nid IN (SELECT nid FROM $type)";
  801. $result = db_query($sql);
  802. while ($entity = $result->fetchObject()) {
  803. $usql = "
  804. UPDATE url_alias
  805. SET source = 'bio_data/" . $entity->entity_id .
  806. "' WHERE source = 'node/" . $entity->nid . "'";
  807. db_query($usql);
  808. }
  809. }
  810. }
  811. }
  812. /**
  813. * Migrate images for Tripal v2 organism content
  814. *
  815. * Migrate images for all chado_organism
  816. *
  817. */
  818. function tripal_chado_migrate_organism_images ($bundle_name) {
  819. // Get all organism entities
  820. $bio_data_table = array_pop(tripal_chado_migrate_get_biodata_tables('organism'));
  821. $results =
  822. db_select($bio_data_table, 'ce')
  823. ->fields('ce', array('entity_id', 'record_id', 'nid'))
  824. ->execute();
  825. // Iterate through each organism entity
  826. while ($organism = $results->fetchObject()) {
  827. $nid = $organism->nid;
  828. $entity_id = $organism->entity_id;
  829. // check if there is a file record for the organism node
  830. $fid = db_select('file_usage', 'fu')
  831. ->fields('fu', array('fid'))
  832. ->condition('module', 'tripal_organism')
  833. ->condition('type', 'organism_image')
  834. ->condition('id', $nid)
  835. ->execute()
  836. ->fetchField();
  837. // check if the image was added using the old interface.
  838. if (!$fid) {
  839. $sql =
  840. "SELECT genus,species,nid
  841. FROM {organism} O
  842. INNER JOIN chado_organism CO ON O.organism_id = CO.organism_id
  843. WHERE O.organism_id = :organism_id";
  844. $chado_org = chado_query($sql, array(':organism_id' => $organism->record_id))->fetchObject();
  845. if ($chado_org) {
  846. $base_path = realpath('.');
  847. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  848. $image_name = $chado_org->genus . "_" . $chado_org->species . ".jpg";
  849. $image_path = "$base_path/$image_dir/$image_name";
  850. // image files are stored as 'genus_species.jpg'
  851. $file =NULL;
  852. if (file_exists($image_path)) {
  853. $handle = fopen($image_path, 'r');
  854. $file = file_save_data($handle, "public://$image_name");
  855. fclose($handle);
  856. }
  857. // last possible case: image files are stored as 'organism_id.jpg'
  858. else {
  859. $image_name = $chado_org->nid . ".jpg";
  860. $image_path = "$base_path/$image_dir/$image_name";
  861. if (file_exists($image_path)) {
  862. $handle = fopen($image_path, 'r');
  863. $file = file_save_data($handle, "public://$image_name");
  864. fclose($handle);
  865. }
  866. }
  867. if($file){
  868. tripal_chado_migrate_organism_image_add_file($file->fid, $entity_id, $bundle_name);
  869. }
  870. }
  871. }
  872. else {
  873. // If there is an image, add it to the organism entity
  874. tripal_chado_migrate_organism_image_add_file ($fid, $entity_id, $bundle_name);
  875. }
  876. }
  877. }
  878. /**
  879. *
  880. * Add image file for the organism entity
  881. *
  882. * @param unknown $fid
  883. */
  884. function tripal_chado_migrate_organism_image_add_file ($fid, $entity_id, $bundle_name) {
  885. if ($fid && $entity_id) {
  886. $file = file_load($fid);
  887. // Add a record to the file_usage table
  888. file_usage_add($file, 'file', 'TripalEntity', $entity_id);
  889. $image_file = (array) $file;
  890. // Attached it to the entity
  891. /*
  892. $entities = entity_load('TripalEntity', array($entity_id));
  893. $entity = $entities[$entity_id];
  894. $image = array(
  895. 'und' => array(
  896. 0 =>$image_file
  897. )
  898. );
  899. $entity->data__image = $image;
  900. field_attach_update('TripalEntity', $entity);
  901. entity_get_controller('TripalEntity')->resetCache(array($entity_id));
  902. */
  903. // Add a record to the field_data_data__image table
  904. $values = array(
  905. 'entity_type' => 'TripalEntity',
  906. 'bundle' => $bundle_name,
  907. 'entity_id' => $entity_id,
  908. 'revision_id' => $entity_id,
  909. 'language' => 'und',
  910. 'delta' => 0,
  911. 'data__image_fid' => $fid,
  912. );
  913. drupal_write_record('field_data_data__image', $values);
  914. }
  915. }
  916. /**
  917. * Retrieve chado_biodata_* table name
  918. *
  919. * @param string $chado_table
  920. * the chado_table where the record is stored
  921. *
  922. * @return
  923. * Return all chado_biodata_* table names mapping to a chado_table
  924. */
  925. function tripal_chado_migrate_get_biodata_tables ($chado_table) {
  926. // To find the bundle_table, check if type_column is used for the chado_table
  927. $query = db_select('chado_bundle', 'cb');
  928. $query->join('tripal_bundle', 'tb', 'tb.id = cb.bundle_id');
  929. $query->fields('cb', array('data_table'));
  930. $query->fields('tb', array('name'));
  931. $query->condition('cb.data_table', $chado_table);
  932. $bundles = $query->execute();
  933. $tables = array();
  934. while ($bundle = $bundles->fetchObject()) {
  935. array_push($tables, 'chado_' . $bundle->name);
  936. }
  937. return $tables;
  938. }
  939. function tripal_chado_migrate_resource_blocks($bundle_name) {
  940. // Do not try to migrate resouce blocks and titles if they weren't set up
  941. if (!db_table_exists('field_revision_field_resource_titles') || !db_table_exists('field_revision_field_resource_blocks')) {
  942. return;
  943. }
  944. $entites =
  945. db_select('chado_' . $bundle_name, 'B')
  946. ->fields('B', array('nid'))
  947. ->execute();
  948. while ($nid = $entites->fetchField()) {
  949. // Only the latest revision is migrated
  950. $sql = "
  951. SELECT
  952. entity_id,
  953. max(revision_id) AS vid,
  954. delta,
  955. (SELECT field_resource_titles_value
  956. FROM field_revision_field_resource_titles
  957. WHERE entity_id = RT.entity_id
  958. AND revision_id = max(RT.revision_id)
  959. AND delta = RT.delta
  960. ),
  961. (SELECT field_resource_blocks_value
  962. FROM field_revision_field_resource_blocks
  963. WHERE entity_id = RT.entity_id
  964. AND revision_id = max(RT.revision_id)
  965. AND delta = RT.delta
  966. )
  967. FROM field_revision_field_resource_titles RT
  968. WHERE RT.entity_id = :nid
  969. GROUP BY entity_id, delta
  970. ORDER BY RT.delta
  971. ";
  972. $results = db_query($sql, array(':nid' => $nid));
  973. while ($resource = $results->fetchObject()) {
  974. $title = $resource->field_resource_titles_value;
  975. $content = $resource->field_resource_blocks_value;
  976. $delta = $resource->delta;
  977. $nid = $resource->entity_id;
  978. $entity_id =
  979. db_select('chado_' . $bundle_name, 'B')
  980. ->fields('B', array('entity_id'))
  981. ->condition('nid', $nid)
  982. ->execute()
  983. ->fetchField()
  984. ;
  985. // field name: (can not be longer than 32 chars)
  986. // bio_data_<i>_resource_<title to lower case/space replaced with _/first 10 chars>
  987. $field_name = $bundle_name . '_rsc_' . substr(preg_replace('/\s+/', '_', strtolower($title)), 0, 15);
  988. // Create a field if it does not exist
  989. if (!field_info_field($field_name)) {
  990. field_create_field(array(
  991. 'field_name' => $field_name,
  992. 'type' => 'text',
  993. 'cardinality' => 1,
  994. 'locked' => FALSE,
  995. 'storage' => array(
  996. 'type' => 'field_sql_storage',
  997. ),
  998. 'settings' => array(
  999. 'max_length' => 10485760,
  1000. 'text_processing' => 1
  1001. )
  1002. ));
  1003. }
  1004. // Create field instance for the bundle if it does not exist
  1005. if (!field_info_instance('TripalEntity', $field_name, $bundle_name)) {
  1006. field_create_instance(array(
  1007. 'field_name' => $field_name,
  1008. 'entity_type' => 'TripalEntity',
  1009. 'bundle' => $bundle_name,
  1010. 'label' => $title,
  1011. 'widget' => array(
  1012. 'type' => 'text_textarea',
  1013. ),
  1014. 'display' => array(
  1015. 'default' => array(
  1016. 'label' => 'hidden',
  1017. ),
  1018. ),
  1019. 'settings' => array(
  1020. 'text_processing' => 1,
  1021. 'format' => 'full_html',
  1022. 'term_vocabulary' => 'schema',
  1023. 'term_name' => 'comment',
  1024. 'term_accession' => 'comment',
  1025. ),
  1026. ));
  1027. }
  1028. // Migrate the field content
  1029. $ftable = 'field_data_' . $field_name;
  1030. $frtable = 'field_revision_' . $field_name;
  1031. $fvalue = $field_name . '_value';
  1032. $fformat = $field_name . '_format';
  1033. try {
  1034. $sql = "
  1035. INSERT INTO $ftable (entity_type, bundle, entity_id, revision_id, language, delta, $fvalue, $fformat)
  1036. VALUES (:entity_type, :bundle, :entity_id, :revision_id, :language, :delta, :value, :format)
  1037. ";
  1038. db_query($sql,
  1039. array (
  1040. ':entity_type' => 'TripalEntity',
  1041. ':bundle' => $bundle_name,
  1042. ':entity_id' => $entity_id,
  1043. 'revision_id' => $entity_id,
  1044. ':language' => 'und',
  1045. ':delta' => 0,
  1046. ':value' => $content,
  1047. ':format' => 'full_html'
  1048. )
  1049. );
  1050. $rsql = "
  1051. INSERT INTO $frtable (entity_type, bundle, entity_id, revision_id, language, delta, $fvalue, $fformat)
  1052. VALUES (:entity_type, :bundle, :entity_id, :revision_id, :language, :delta, :value, :format)
  1053. ";
  1054. db_query($rsql,
  1055. array (
  1056. ':entity_type' => 'TripalEntity',
  1057. ':bundle' => $bundle_name,
  1058. ':entity_id' => $entity_id,
  1059. 'revision_id' => $entity_id,
  1060. ':language' => 'und',
  1061. ':delta' => 0,
  1062. ':value' => $content,
  1063. ':format' => 'full_html'
  1064. )
  1065. );
  1066. } catch (\PDOException $e) {
  1067. $error = $e->getMessage();
  1068. watchdog_exception('tripal_chado', $e);
  1069. }
  1070. }
  1071. }
  1072. }
  1073. function tripal_chado_migrate_resource_links($bundle_name) {
  1074. // Do not try to migrate resouce blocks and titles if they weren't set up
  1075. if (!db_table_exists('field_revision_field_resource_links')) {
  1076. return;
  1077. }
  1078. $entites =
  1079. db_select('chado_' . $bundle_name, 'B')
  1080. ->fields('B', array('nid'))
  1081. ->execute();
  1082. while ($nid = $entites->fetchField()) {
  1083. // Only the latest revision is migrated
  1084. $sql = "
  1085. SELECT
  1086. entity_id,
  1087. max(revision_id) AS vid,
  1088. delta,
  1089. (SELECT field_resource_links_value
  1090. FROM field_revision_field_resource_links
  1091. WHERE entity_id = RT.entity_id
  1092. AND revision_id = max(RT.revision_id)
  1093. AND delta = RT.delta
  1094. )
  1095. FROM field_revision_field_resource_links RT
  1096. WHERE RT.entity_id = :nid
  1097. GROUP BY entity_id, delta
  1098. ORDER BY RT.delta
  1099. ";
  1100. $results = db_query($sql, array(':nid' => $nid));
  1101. while ($resource = $results->fetchObject()) {
  1102. $values = explode('|', $resource->field_resource_links_value);
  1103. $title = $values[0];
  1104. $link = $values[1];
  1105. $delta = $resource->delta;
  1106. $nid = $resource->entity_id;
  1107. $entity_id =
  1108. db_select('chado_' . $bundle_name, 'B')
  1109. ->fields('B', array('entity_id'))
  1110. ->condition('nid', $nid)
  1111. ->execute()
  1112. ->fetchField();
  1113. // field name: (can not be longer than 32 chars)
  1114. // bio_data_<i>_resource_<title to lower case/space replaced with _/first 10 chars>
  1115. $field_name = $bundle_name . '_resource_links';
  1116. // Create a field if it does not exist
  1117. if (!field_info_field($field_name)) {
  1118. field_create_field(array(
  1119. 'field_name' => $field_name,
  1120. 'type' => 'link_field',
  1121. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1122. 'locked' => FALSE,
  1123. 'storage' => array(
  1124. 'type' => 'field_sql_storage',
  1125. ),
  1126. 'settings' => array(
  1127. )
  1128. ));
  1129. }
  1130. // Create field instance for the bundle if it does not exist.
  1131. if (!field_info_instance('TripalEntity', $field_name, $bundle_name)) {
  1132. field_create_instance(array(
  1133. 'field_name' => $field_name,
  1134. 'entity_type' => 'TripalEntity',
  1135. 'bundle' => $bundle_name,
  1136. 'label' => 'Links',
  1137. 'widget' => array(
  1138. 'type' => 'link_field',
  1139. ),
  1140. 'display' => array(
  1141. 'default' => array(
  1142. 'label' => 'hidden',
  1143. ),
  1144. ),
  1145. 'settings' => array(
  1146. 'term_vocabulary' => 'schema',
  1147. 'term_name' => 'url',
  1148. 'term_accession' => 'url',
  1149. 'absolute_url' => 0,
  1150. 'validate_url' => 0
  1151. ),
  1152. ));
  1153. }
  1154. // Migrate the field content
  1155. $ftable = 'field_data_' . $field_name;
  1156. $frtable = 'field_revision_' . $field_name;
  1157. $furl = $field_name . '_url';
  1158. $ftitle = $field_name . '_title';
  1159. try {
  1160. $sql = "
  1161. INSERT INTO $ftable (entity_type, bundle, entity_id, revision_id, language, delta, $furl, $ftitle)
  1162. VALUES (:entity_type, :bundle, :entity_id, :revision_id, :language, :delta, :url, :title)
  1163. ";
  1164. db_query($sql,
  1165. array (
  1166. ':entity_type' => 'TripalEntity',
  1167. ':bundle' => $bundle_name,
  1168. ':entity_id' => $entity_id,
  1169. 'revision_id' => $entity_id,
  1170. ':language' => 'und',
  1171. ':delta' => $delta,
  1172. ':url' => $link,
  1173. ':title' => $title
  1174. )
  1175. );
  1176. $rsql = "
  1177. INSERT INTO $frtable (entity_type, bundle, entity_id, revision_id, language, delta, $furl, $ftitle)
  1178. VALUES (:entity_type, :bundle, :entity_id, :revision_id, :language, :delta, :url, :title)
  1179. ";
  1180. db_query($rsql,
  1181. array (
  1182. ':entity_type' => 'TripalEntity',
  1183. ':bundle' => $bundle_name,
  1184. ':entity_id' => $entity_id,
  1185. 'revision_id' => $entity_id,
  1186. ':language' => 'und',
  1187. ':delta' => $delta,
  1188. ':url' => $link,
  1189. ':title' => $title
  1190. )
  1191. );
  1192. } catch (\PDOException $e) {
  1193. $error = $e->getMessage();
  1194. watchdog_exception('tripal_chado', $e);
  1195. }
  1196. }
  1197. }
  1198. }