tripal_bulk_loader.loader.inc 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Add Loader Job Form
  8. *
  9. * This form is meant to be included on the node page to allow users to submit/re-submit
  10. * loading jobs
  11. *
  12. * @ingroup tripal_bulk_loader
  13. */
  14. function tripal_bulk_loader_add_loader_job_form($form, $form_state, $node) {
  15. $form = array();
  16. // --notify--
  17. if ($node->job_status == 'Loading...') {
  18. drupal_set_message(t("The Loading Summary only updates at the end of each constant set.
  19. Although records may have already been inserted, they won't be available until the
  20. current constant set is full loaded and no errors are encountered.", array()), 'warning');
  21. }
  22. $form['nid'] = array(
  23. '#type' => 'hidden',
  24. '#value' => $node->nid,
  25. );
  26. $form['file'] = array(
  27. '#type' => 'hidden',
  28. '#value' => $node->file
  29. );
  30. $form['job_id'] = array(
  31. '#type' => 'hidden',
  32. '#value' => $node->job_id,
  33. );
  34. $form['submit'] = array(
  35. '#type' => 'submit',
  36. '#value' => ($node->job_id) ? 'Re-Submit Job' : 'Submit Job',
  37. );
  38. $form['submit-cancel'] = array(
  39. '#type' => ($node->job_id)? 'submit' : 'hidden',
  40. '#value' => 'Cancel Job',
  41. );
  42. if ($node->keep_track_inserted) {
  43. $form['submit-revert'] = array(
  44. '#type' => ($node->job_id) ? 'submit' : 'hidden',
  45. '#value' => 'Revert',
  46. );
  47. }
  48. return $form;
  49. }
  50. /**
  51. * Add Loader Job Form (Submit)
  52. *
  53. * @ingroup tripal_bulk_loader
  54. */
  55. function tripal_bulk_loader_add_loader_job_form_submit($form, $form_state) {
  56. global $user;
  57. if (preg_match('/Submit Job/', $form_state['values']['op'])) {
  58. //Submit Tripal Job
  59. $job_args[1] = $form_state['values']['nid'];
  60. if (is_readable($form_state['values']['file'])) {
  61. $fname = basename($form_state['values']['file']);
  62. $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  63. // add job_id to bulk_loader node
  64. $success = db_update('tripal_bulk_loader')->fields(array(
  65. 'job_id' => $job_id,
  66. 'job_status' => 'Submitted to Queue'
  67. ))->condition('nid',$form_state['values']['nid'])->execute();
  68. }
  69. else {
  70. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $form_state['values']['file'])));
  71. }
  72. }
  73. elseif (preg_match('/Re-Submit Job/', $form_state['values']['op'])) {
  74. tripal_jobs_rerun($form_state['values']['job_id']);
  75. $success = db_update('tripal_bulk_loader')->fields(array(
  76. 'job_status' => 'Submitted to Queue'
  77. ))->condition('nid',$form_state['values']['nid'])->execute();
  78. }
  79. elseif (preg_match('/Cancel Job/', $form_state['values']['op'])) {
  80. $success = db_update('tripal_bulk_loader')->fields(array(
  81. 'job_status' => 'Job Cancelled'
  82. ))->condition('nid',$form_state['values']['nid'])->execute();
  83. tripal_jobs_cancel($form_state['values']['job_id']);
  84. }
  85. elseif (preg_match('/Revert/', $form_state['values']['op'])) {
  86. // Remove the records from the database that were already inserted
  87. $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid ORDER BY tripal_bulk_loader_inserted_id DESC', array(':nid' => $form_state['values']['nid']));
  88. while ($r = $resource->fetchObject()) {
  89. $ids = preg_split('/,/', $r->ids_inserted);
  90. db_query('DELETE FROM {'.$r->table_inserted_into.'} WHERE '.$r->table_primary_key.' IN ('.$r->ids_inserted.')')->execute();
  91. $result = db_query('SELECT true as present FROM {'.$r->table_inserted_into.'} WHERE '.$r->table_primary_key.' IN ('.$r->ids_inserted.')')->fetchObject();
  92. if (!$result->present) {
  93. drupal_set_message(t('Successfully Removed data Inserted into the %tableto table.', array('%tableto' => $r->table_inserted_into)));
  94. db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=:id', array(':id' => $r->tripal_bulk_loader_inserted_id))->execute();
  95. }
  96. else {
  97. drupal_set_message(t('Unable to remove data Inserted into the %tableto table!', array('%tableto' => $r->table_inserted_into)), 'error');
  98. }
  99. }
  100. // reset status
  101. $success = db_update('tripal_bulk_loader')->fields(array(
  102. 'job_status' => 'Reverted -Data Deleted'
  103. ))->condition('nid',$form_state['values']['nid'])->execute();
  104. }
  105. }
  106. /**
  107. * Tripal Bulk Loader
  108. *
  109. * This is the function that's run by tripal_launch_jobs to bulk load chado data.
  110. *
  111. * @param $nid
  112. * The Node ID of the bulk loading job node to be loaded. All other needed data is expected to be
  113. * in the node (ie: template ID and file)
  114. *
  115. * Note: Instead of returning a value this function updates the tripal_bulk_loader.status.
  116. * Errors are thrown through watchdog and can be viewed at admin/reports/dblog.
  117. *
  118. * @ingroup tripal_bulk_loader
  119. */
  120. function tripal_bulk_loader_load_data($nid, $job_id) {
  121. // ensure no timeout
  122. drupal_set_time_limit(0);
  123. // set the status of the job (in the node not the tripal jobs)
  124. db_update('tripal_bulk_loader')
  125. ->fields(array('job_status' => 'Loading...'))
  126. ->condition('nid', $nid)
  127. ->execute();
  128. $node = node_load($nid);
  129. print "Template: " . $node->template->name . " (" . $node->template_id . ")\n";
  130. $total_lines = trim(`wc --lines < $node->file`);
  131. print "File: " . $node->file . " (" . $total_lines . " lines)\n";
  132. //print "\nClearing all prepared statements from previous runs of this loader...\n";
  133. //tripal_core_chado_clear_prepared('_'.$node->nid.'_');
  134. // Prep Work ==================================================================================
  135. print "\nPreparing to load...\n";
  136. $loaded_without_errors = TRUE;
  137. // Generate default values array
  138. $default_data = array();
  139. $field2column = array();
  140. $record2priority = array();
  141. $tables = array();
  142. $template_array = $node->template->template_array;
  143. // first build the record2priority array
  144. foreach ($template_array as $priority => $record_array) {
  145. $record2priority[$record_array['record_id']] = $priority;
  146. }
  147. //
  148. foreach ($template_array as $priority => $record_array) {
  149. if (!is_array($record_array)) {
  150. continue;
  151. }
  152. // Add tables being inserted into to a list to be treated differently
  153. // this is used to acquire locks on these tables
  154. if (preg_match('/insert/', $record_array['mode'])) {
  155. $tables[$record_array['table']] = $record_array['table'];
  156. }
  157. // iterate through each of the fiels for the current record and
  158. // set the default_data array
  159. foreach ($record_array['fields'] as $field_index => $field_array) {
  160. $default_data[$priority]['table'] = $record_array['table'];
  161. $default_data[$priority]['mode'] = ($record_array['mode']) ? $record_array['mode'] : 'insert';
  162. $default_data[$priority]['select_if_duplicate'] = ($record_array['select_if_duplicate']) ? $record_array['select_if_duplicate'] : 0;
  163. $default_data[$priority]['update_if_duplicate'] = ($record_array['update_if_duplicate']) ? $record_array['update_if_duplicate'] : 0;
  164. $default_data[$priority]['disabled'] = ($record_array['disable']) ? $record_array['disable'] : 0;
  165. $default_data[$priority]['optional'] = ($record_array['optional']) ? $record_array['optional'] : 0;
  166. $default_data[$priority]['select_optional'] = ($record_array['select_optional']) ? $record_array['select_optional'] : 0;
  167. $default_data[$priority]['record_id'] = $record_array['record_id'];
  168. $default_data[$priority]['required'][$field_array['field']] = $field_array['required'];
  169. $one = $default_data[$priority];
  170. if (isset($field_array['regex'])) {
  171. $default_data[$priority]['regex_transform'][$field_array['field']] = $field_array['regex'];
  172. }
  173. $two = $default_data[$priority];
  174. if (preg_match('/table field/', $field_array['type'])) {
  175. $default_data[$priority]['values_array'][$field_array['field']] = '';
  176. $default_data[$priority]['need_further_processing'] = TRUE;
  177. $field2column[$priority][$field_array['field']] = $field_array['spreadsheet column'];
  178. }
  179. elseif (preg_match('/constant/', $field_array['type'])) {
  180. $default_data[$priority]['values_array'][$field_array['field']] = $field_array['constant value'];
  181. }
  182. elseif (preg_match('/foreign key/', $field_array['type'])) {
  183. $default_data[$priority]['values_array'][$field_array['field']] = array();
  184. $default_data[$priority]['need_further_processing'] = TRUE;
  185. $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['record'] = $field_array['foreign key'];
  186. // Add in the FK / Referral table
  187. $fk_priority = $record2priority[$field_array['foreign key']];
  188. $fk_table = $template_array[$fk_priority]['table'];
  189. $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['table'] = $fk_table;
  190. // Add in the FK / Referral field
  191. // for backwards compatibility we need to get the FK relationship to find
  192. // out what field we're joining on. For templates created using a
  193. // previous version it was assumed that the FK field was always the field to join
  194. if (!array_key_exists('foreign field', $field_array)) {
  195. $tbl_description = tripal_core_get_chado_table_schema($record_array['table']);
  196. foreach ($tbl_description['foreign keys'] as $key_table => $key_array) {
  197. if ($key_table == $fk_table) {
  198. foreach ($key_array['columns'] as $left_field => $right_field) {
  199. if ($left_field == $field_array['field']) {
  200. $field_array['foreign field'] = $right_field;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['field'] = $field_array['foreign field'];
  207. }
  208. else {
  209. print 'WARNING: Unsupported type: ' . $field_array['type'] . ' for ' . $table . '.' . $field_array['field'] . "!\n";
  210. }
  211. $three = $default_data[$priority];
  212. } // end of foreach field
  213. } //end of foreach record
  214. ///////////////////////////////////////////////
  215. // For each set of constants
  216. ///////////////////////////////////////////////
  217. print "Loading...\n";
  218. $original_default_data = $default_data;
  219. $group_index = 0;
  220. $total_num_groups = sizeof($node->constants);
  221. // If there are no constant sets and no exposed fields
  222. // then create an empty constant set so loader runs
  223. if ($total_num_groups == 0 && empty($node->exposed_fields)) {
  224. $node->constants = array(
  225. 0 => array()
  226. );
  227. $total_num_groups = 1;
  228. }
  229. foreach ($node->constants as $group_id => $set) {
  230. // revert default data array for next set of constants
  231. $default_data = $original_default_data;
  232. $group_index++;
  233. // Add constants
  234. if (!empty($set)) {
  235. print "Constants:\n";
  236. foreach ($set as $priority => $record) {
  237. foreach ($record as $field_id => $field) {
  238. print "\t- " . $field['chado_table'] . '.' . $field['chado_field'] . ' = ' . $field['value'] . "\n";
  239. if ($default_data[$priority]['table'] == $field['chado_table']) {
  240. if (isset($default_data[$priority]['values_array'][$field['chado_field']])) {
  241. if (isset($field2column[$priority][$field['chado_field']])) {
  242. $field2column[$priority][$field['chado_field']] = $field['value'];
  243. }
  244. else {
  245. $default_data[$priority]['values_array'][$field['chado_field']] = $field['value'];
  246. }
  247. }
  248. else {
  249. print "ERROR: Template has changed after constants were assigned!\n";
  250. tripal_bulk_loader_throw_error('Template has changed after constants were assigned', array(), TRIPAL_NOTICE);
  251. exit(1);
  252. }
  253. }
  254. else {
  255. print "ERROR: Template has changed after constants were assigned!\n";
  256. tripal_bulk_loader_throw_error('Template has changed after constants were assigned', array(), TRIPAL_NOTICE);
  257. exit(1);
  258. }
  259. }
  260. }
  261. }
  262. // Open File
  263. print "\tPreparing to load the current constant set...\n";
  264. print "\t\tOpen File...\n";
  265. try {
  266. $file = new SplFileObject($node->file, 'r');
  267. }
  268. catch (Exception $e) {
  269. tripal_bulk_loader_throw_error('Could not open file %file',
  270. array('%file' => $node->file), TRIPAL_ERROR);
  271. return;
  272. }
  273. // Set defaults
  274. $header = '';
  275. if (preg_match('/(t|true|1)/', $node->file_has_header)) {
  276. $file->next();
  277. $header = $file->current();
  278. }
  279. $num_records = 0;
  280. $num_lines = 0;
  281. $num_errors = 0;
  282. $interval = intval($total_lines * 0.0001);
  283. if ($interval == 0) {
  284. $interval = 1;
  285. }
  286. // Start Transaction
  287. $savepoint = '';
  288. switch (variable_get('tripal_bulk_loader_transactions', 'row')) {
  289. case "none":
  290. break;
  291. case "all":
  292. print "\t\tStart Transaction...\n";
  293. $TRANSACTION = db_transaction();
  294. $transactions = TRUE;
  295. break;
  296. case "row":
  297. print "\t\tStart Transaction...\n";
  298. $TRANSACTION = db_transaction();
  299. $transactions = TRUE;
  300. $new_transaction_per_row = TRUE;
  301. break;
  302. }
  303. // Disable triggers
  304. $triggers_disabled = FALSE;
  305. if ($transactions AND variable_get('tripal_bulk_loader_disable_triggers', TRUE)) {
  306. print "\t\tDefer Constraints...\n";
  307. $triggers_disabled = TRUE;
  308. chado_query("SET CONSTRAINTS ALL DEFERRED");
  309. }
  310. // Acquire Locks
  311. if ($transactions) {
  312. print "\t\tAcquiring Table Locks...\n";
  313. $lockmode = variable_get('tripal_bulk_loader_lock', 'ROW EXCLUSIVE');
  314. foreach ($tables as $table) {
  315. print "\t\t\t$lockmode for $table\n";
  316. chado_query("LOCK TABLE ".$table." IN ".$lockmode." MODE");
  317. }
  318. }
  319. print "\tLoading the current constant set...\n";
  320. tripal_bulk_loader_progress_bar(0, $total_lines);
  321. while (!$file->eof()) {
  322. $file->next();
  323. $raw_line = $file->current();
  324. $raw_line = trim($raw_line);
  325. if (empty($raw_line)) {
  326. continue;
  327. } // skips blank lines
  328. $line = explode("\t", $raw_line);
  329. $num_lines++;
  330. // update the job status every 1% of lines processed for the current group
  331. if ($node->job_id and $num_lines % $interval == 0) {
  332. // percentage of lines processed for the current group
  333. $group_progress = round(($num_lines / $total_lines) * 100);
  334. tripal_bulk_loader_progress_bar($num_lines, $total_lines);
  335. // percentage of lines processed for all groups
  336. // <previous group index> * 100 + <current group progress>
  337. // --------------------------------------------------------
  338. // <total number of groups>
  339. // For example, if you were in the third group of 3 constant sets
  340. // and had a group percentage of 50% then the job progress would be
  341. // (2*100 + 50%) / 3 = 250%/3 = 83%
  342. $job_progress = round(((($group_index - 1) * 100) + $group_progress) / $total_num_groups);
  343. tripal_job_set_progress($node->job_id, $job_progress);
  344. }
  345. $data = $default_data;
  346. // iterate through each record and process the line
  347. $data_keys = array_keys($data);
  348. foreach ($data_keys as $priority) {
  349. $options = array(
  350. 'field2column' => $field2column,
  351. 'record2priority' => $record2priority,
  352. 'line' => $line,
  353. 'line_num' => $num_lines,
  354. 'group_index' => $group_index,
  355. 'node' => $node,
  356. 'nid' => $node->nid,
  357. );
  358. // execute all records that are not disabled
  359. $no_errors = FALSE;
  360. if (array_key_exists($priority, $data) and
  361. array_key_exists('disabled', $data[$priority]) and
  362. $data[$priority]['disabled'] == 0) {
  363. $no_errors = process_data_array_for_line($priority, $data, $default_data, $options);
  364. }
  365. else {
  366. // set status to true for skipped records
  367. $no_errors = TRUE;
  368. }
  369. tripal_bulk_loader_progress_file_track_job($job_id, $no_errors);
  370. $failed = FALSE;
  371. if ($no_errors == FALSE) {
  372. // Encountered an error
  373. if ($transactions) {
  374. $TRANSACTION->rollback();
  375. }
  376. tripal_bulk_loader_finish_loading($node->nid, FALSE);
  377. break;
  378. }
  379. } // end of foreach table in default data array
  380. tripal_bulk_loader_progress_file_track_job($job_id, FALSE, TRUE);
  381. if ($failed) {
  382. $TRANSACTION->rollback();
  383. tripal_bulk_loader_finish_loading($node->nid, FALSE);
  384. break;
  385. }
  386. else {
  387. // Row inserted successfully
  388. if ($transactions && $new_transaction_per_row) {
  389. // commit current transaction and start a new one
  390. unset($TRANSACTION);
  391. $TRANSACTION = db_transaction();
  392. }
  393. }
  394. } //end of foreach line of file
  395. // END Transaction
  396. if ($transactions) {
  397. unset($TRANSACTION);
  398. }
  399. if ($failed) {
  400. $loaded_without_errors = FALSE;
  401. break;
  402. }
  403. tripal_bulk_loader_progress_bar($total_lines, $total_lines);
  404. tripal_bulk_loader_progress_file_track_job($job_id, FALSE, FALSE, TRUE);
  405. } //end of foreach constant set
  406. tripal_bulk_loader_finish_loading($node->nid, $loaded_without_errors);
  407. }
  408. /**
  409. * Process the data array for a given line
  410. *
  411. * @param $addt
  412. * Requires: field2column', 'record2priority', 'line', 'line_num',
  413. * 'group_index', 'node', 'nid'
  414. *
  415. * @ingroup tripal_bulk_loader
  416. */
  417. function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
  418. //$time_start = microtime(true);
  419. $table_data = $data[$priority];
  420. $addt = (object) $addt;
  421. $no_errors = TRUE;
  422. $table = $table_data['table'];
  423. $values = $table_data['values_array'];
  424. // populate the values array with real value either from the input data file line
  425. // or from the foreign key / referral record
  426. if (array_key_exists('need_further_processing', $table_data) and $table_data['need_further_processing']) {
  427. if (array_key_exists($priority, $addt->field2column)) {
  428. $values = tripal_bulk_loader_add_spreadsheetdata_to_values($values, $addt->line, $addt->field2column[$priority]);
  429. }
  430. $values = tripal_bulk_loader_add_foreignkey_to_values($table_data, $values, $data, $addt->record2priority, $addt->nid, $priority, $default_data);
  431. }
  432. $values = tripal_bulk_loader_regex_tranform_values($values, $table_data, $addt->line);
  433. if (!$values) {
  434. //tripal_bulk_loader_throw_error('Line ' . $addt->line_num . ' Regex:<pre>' . print_r($values, TRUE) . print_r($table_data, TRUE) . '</pre>' . '</pre>', array(), TRIPAL_NOTICE);
  435. }
  436. // get the table description
  437. $table_desc = tripal_core_get_chado_table_schema($table);
  438. if (!$table_desc) {
  439. $msg = "Tripal does not know about the table named '%table'. If this is a custom table,
  440. please define it first";
  441. tripal_bulk_loader_throw_error($msg, array('%table' => $table), TRIPAL_ERROR);
  442. $data[$priority]['error'] = TRUE;
  443. return;
  444. }
  445. // Check that template required fields are present. if a required field is
  446. // missing and this
  447. // is an optional record then just return. otherwise raise an error
  448. $skip_optional = 0;
  449. foreach ($table_data['required'] as $field => $required) {
  450. if ($required) {
  451. // check if the field has no value (or array is empty)
  452. if (!isset($values[$field]) or
  453. (is_array($values[$field]) and count($values[$field]) == 0)) {
  454. // check if the record is optional. For backwards compatiblity we need to
  455. // check if the 'mode' is set to 'optional'
  456. if ($table_data['optional'] or preg_match('/optional/', $table_data['mode']) or
  457. $table_data['select_optional']) {
  458. $skip_optional = 1;
  459. // set the values array to be empty since we all required fields are
  460. // optional and we can't do a select/insert so we don't want to keep
  461. // the values if this record is used in a later FK relationship.
  462. $values = array();
  463. }
  464. else {
  465. $msg = "Line " . $addt->line_num . ' "' . $table_data['record_id'] .
  466. '" (' . $table_data['mode'] . ') Missing template required value: ' . $table . '.' . $field;
  467. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_WARNING);
  468. $data[$priority]['error'] = TRUE;
  469. $no_errors = FALSE;
  470. }
  471. }
  472. }
  473. }
  474. // for an insert, check that all database required fields are present in the values array
  475. // we check for 'optional' in the mode for backwards compatibility. The 'optional'
  476. // mode used to be a type of insert
  477. if (!$skip_optional and (preg_match('/insert/', $table_data['mode']) or
  478. preg_match('/optional/', $table_data['mode']))) {
  479. // Check all database table required fields are set
  480. $fields = $table_desc['fields'];
  481. foreach ($fields as $field => $def) {
  482. // a field is considered missing if it cannot be null and there is no default
  483. // value for it or it is not of type 'serial'
  484. if (array_key_exists('not null', $def) and $def['not null'] == 1 and // field must have a value
  485. !array_key_exists($field, $values) and // there is not a value for it
  486. !array_key_exists('default', $def) and // there is no default for it
  487. strcmp($def['type'], 'serial') != 0) { // it is not a 'serial' type column
  488. $msg = "Line " . $addt->line_num . ' ' . $table_data['record_id'] .
  489. ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
  490. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  491. $data[$priority]['error'] = TRUE;
  492. $no_errors = FALSE;
  493. }
  494. }
  495. }
  496. // add updated values array into the data array
  497. $data[$priority]['values_array'] = $values;
  498. // if there was an error already -> don't insert
  499. if (array_key_exists('error', $data[$priority]) and $data[$priority]['error']) {
  500. tripal_bulk_loader_throw_error('Skipping processing of %table due to previous errors',array('%table'=>$table),TRIPAL_NOTICE);
  501. return $no_errors;
  502. }
  503. // skip optional fields
  504. if ($skip_optional) {
  505. tripal_bulk_loader_throw_error('Skipping an optional record (%record)',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  506. return $no_errors;
  507. }
  508. // check if it is already inserted
  509. if (array_key_exists('inserted', $table_data) and $table_data['inserted']) {
  510. tripal_bulk_loader_throw_error('Skipping %record since it is already inserted',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  511. return $no_errors;
  512. }
  513. // check if it is already selected, if so, just get the value stored in
  514. // the default_data array
  515. if (array_key_exists('selected', $table_data) and $table_data['selected']) {
  516. $data[$priority]['values_array'] = $default_data[$priority]['values_array'];
  517. tripal_bulk_loader_throw_error('%record was already selected thus we are just returning the values previously selected.',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  518. return $no_errors;
  519. }
  520. // make sure we have some value in the select_if_duplicate and update_if_duplicate options
  521. if (!array_key_exists('select_if_duplicate', $table_data)) {
  522. $table_data['select_if_duplicate'] = 0;
  523. }
  524. if (!array_key_exists('update_if_duplicate', $table_data)) {
  525. $table_data['update_if_duplicate'] = 0;
  526. }
  527. // if "select if duplicate" is enabled then check to ensure unique constraint is not violated.
  528. // If it is violated then simply return, the record already exists in the database.
  529. // We check for "insert_unique" for backwards compatibilty but that mode no longer exists
  530. $data[$priority]['is_duplicate'] = 0;
  531. if (preg_match('/insert_unique/', $table_data['mode']) or
  532. $table_data['select_if_duplicate'] == 1 or
  533. $table_data['update_if_duplicate'] == 1) {
  534. $options = array('is_duplicate' => TRUE, 'print_errors' => TRUE);
  535. $duplicate = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, $options);
  536. // if this is a duplicate then substitute the values in the table_data array so
  537. // that for future records that may depend on this one, they can get the values needed
  538. if ($duplicate and is_array($duplicate) and count($duplicate) == 1) {
  539. $dup_record = $duplicate[0];
  540. // save the duplicate record for later. If this is an update_if_duplicate
  541. // then we'll need this record as the match
  542. $data[$priority]['is_duplicate'] = (array) $dup_record;
  543. // if all we have is one field then we will just use the value returned
  544. // rather than create an array of values. This way it will prevent
  545. // the tripal_core_chado_(select|insert|update) from recursing on
  546. // foreign keys and make the loader go faster.
  547. if (count((array) $dup_record) == 1) {
  548. foreach ($dup_record as $key => $value) {
  549. $data[$priority]['values_array'] = $value;
  550. }
  551. }
  552. // if we have multiple fields returned then we need to set the values
  553. // the new array.
  554. else {
  555. // convert object to array
  556. $new_values = array();
  557. foreach ($dup_record as $key => $value) {
  558. $new_values[$key] = $value;
  559. }
  560. $data[$priority]['values_array'] = $new_values;
  561. }
  562. // return if this is a select_if_duplicate
  563. if ($table_data['select_if_duplicate'] == 1) {
  564. tripal_bulk_loader_throw_error('Simply returning values for %record since it was already inserted',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  565. return $no_errors;
  566. }
  567. }
  568. }
  569. else {
  570. # TODO: what to do if there are more than one value returned when
  571. # checking for a duplicate?
  572. }
  573. if (!preg_match('/select/', $table_data['mode'])) {
  574. // Use prepared statement?
  575. if (variable_get('tripal_bulk_loader_prepare', TRUE)) {
  576. $options = array('statement_name' => 'record_' . $addt->nid . '_' . $priority);
  577. if (($addt->line_num > 1 && $addt->group_index == 1) OR $addt->group_index > 1) {
  578. //$options['is_prepared'] = TRUE;
  579. }
  580. }
  581. else {
  582. $options = array();
  583. }
  584. // Skip tripal_core_chado_insert() built-in validation?
  585. if (variable_get('tripal_bulk_loader_skip_validation', FALSE)) {
  586. $options['skip_validation'] = TRUE;
  587. }
  588. if ($table_data['update_if_duplicate'] == 1) {
  589. if (array_key_exists('statement_name', $options)) {
  590. $options['statement_name'] = 'upd_' . $options['statement_name'];
  591. }
  592. // This should have been set on the first round of inserts for this record
  593. $match = $data[$priority]['is_duplicate'];
  594. // However, sometimes there is a pre-existing record before the loader starts
  595. // Thus check that this value is set and if not, then generate a match array
  596. // based on the unique keys for this record.
  597. if (empty($match)) {
  598. $match = array();
  599. // First check to see if we have fields for the primary key
  600. foreach ($table_desc['primary key'] as $k_field) {
  601. if (!empty($values[$k_field])) {
  602. $match[$k_field] = $values[$k_field];
  603. }
  604. }
  605. // Otherwise check the fields that are part of the unique key
  606. if (empty($match)) {
  607. foreach ($table_desc['unique keys'] as $u_keys) {
  608. foreach ($u_keys as $u_field) {
  609. if (!empty($values[$u_field])) {
  610. $match[$u_field] = $values[$u_field];
  611. }
  612. }
  613. }
  614. }
  615. }
  616. if (!empty($match)) {
  617. // Now we need to check if it already exists via a select
  618. $results = tripal_core_chado_select($table, array_keys($table_desc['fields']), $match, array('print_errors' => TRUE));
  619. // If not then insert
  620. if (empty($results)) {
  621. $options['statement_name'] = 'ins_'.$options['statement_name'];
  622. $options['print_errors'] = TRUE;
  623. $record = tripal_core_chado_insert($table, $values, $options);
  624. }
  625. else {
  626. $options['return_record'] = TRUE;
  627. $options['print_errors'] = TRUE;
  628. $record = tripal_core_chado_update($table, $match, $values, $options);
  629. }
  630. }
  631. else {
  632. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
  633. $table_data['mode'] . ') Unable to update record since none of the unique key or primary key fields were available ' .
  634. ' where values:' . print_r($values, TRUE);
  635. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  636. $data[$priority]['error'] = TRUE;
  637. $no_errors = FALSE;
  638. }
  639. }
  640. else {
  641. $options['print_errors'] = TRUE;
  642. $record = tripal_core_chado_insert($table, $values, $options);
  643. }
  644. // if the insert was not successful
  645. if (!$record) {
  646. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
  647. $table_data['mode'] . ') Unable to insert record into ' . $table .
  648. ' where values:' . print_r($values, TRUE);
  649. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  650. $data[$priority]['error'] = TRUE;
  651. $no_errors = FALSE;
  652. }
  653. // if the insert was succesful
  654. else {
  655. // if mode=insert_once then ensure we only insert it once
  656. if (preg_match('/insert_once/', $table_data['mode'])) {
  657. $default_data[$priority]['inserted'] = TRUE;
  658. }
  659. // add to tripal_bulk_loader_inserted
  660. if ($addt->node->keep_track_inserted) {
  661. $insert_record = db_query(
  662. "SELECT * FROM {tripal_bulk_loader_inserted} WHERE table_inserted_into=:table AND nid=:nid",
  663. array(
  664. ':table' => $table,
  665. 'nid' => $addt->nid
  666. ))->fetchObject();
  667. if ($insert_record) {
  668. $insert_record->ids_inserted .= ',' . $record[$table_desc['primary key'][0] ];
  669. drupal_write_record('tripal_bulk_loader_inserted', $insert_record, 'tripal_bulk_loader_inserted_id');
  670. //print 'Update: '.print_r($insert_record,TRUE)."\n";
  671. //return $no_errors;
  672. }
  673. else {
  674. $insert_record = array(
  675. 'nid' => $addt->nid,
  676. 'table_inserted_into' => $table,
  677. 'table_primary_key' => $table_desc['primary key'][0],
  678. 'ids_inserted' => $record[ $table_desc['primary key'][0] ],
  679. );
  680. //print 'New: '.print_r($insert_record,TRUE)."\n";
  681. $success = drupal_write_record('tripal_bulk_loader_inserted', $insert_record);
  682. //return $no_errors;
  683. }//end of if insert record
  684. }// end of if keeping track of records inserted
  685. // substitute the values array for the primary key if it exists
  686. // and is a single field
  687. if (array_key_exists('primary key', $table_desc)) {
  688. if (count($table_desc['primary key']) == 1) {
  689. $pkey_field = $table_desc['primary key'][0];
  690. $data[$priority]['values_array'] = $record[$pkey_field];
  691. }
  692. }
  693. else {
  694. //add changes back to values array
  695. $data[$priority]['values_array'] = $record;
  696. $values = $record;
  697. }
  698. } //end of if insert was successful
  699. }
  700. // perform a select
  701. else {
  702. // get the matches for this select
  703. $matches = array();
  704. if (is_array($values) and count($values) > 0) {
  705. $matches = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, array('print_errors' => TRUE));
  706. }
  707. // if the record doesn't exist and it's not optional then generate an error
  708. if (count($matches) == 0) {
  709. // No record on select
  710. if ($table_data['select_optional'] != 1) {
  711. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') No Matching record in ' . $table . ' where values:' . print_r($values, TRUE);
  712. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  713. $data[$priority]['error'] = TRUE;
  714. $no_errors = FALSE;
  715. }
  716. // there is no match and select optional is turned on, so we want to set
  717. // the values to empty for any records with an FK relationship on this one
  718. else {
  719. $data[$priority]['values_array'] = NULL;
  720. }
  721. }
  722. // if we have more than one record matching and this select isn't optional then fail
  723. if (count($matches) > 1) {
  724. if ($table_data['select_optional'] != 1) {
  725. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') Too many matching records in ' . $table . ' where values:' . print_r($values, TRUE);
  726. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_WARNING);
  727. $data[$priority]['error'] = TRUE;
  728. $no_errors = FALSE;
  729. }
  730. // there are too many matches and this is an optional select so set
  731. // the values to empty for any records with an FK relationship on this one
  732. else {
  733. $data[$priority]['values_array'] = NULL;
  734. }
  735. }
  736. // if mode=select_once then ensure we only select it once
  737. if (preg_match('/select_once/', $table_data['mode'])) {
  738. $default_data[$priority]['selected'] = TRUE;
  739. // save the pkey
  740. if (array_key_exists('primary key', $table_desc)) {
  741. $new_values = array();
  742. foreach ($matches[0] as $key => $value) {
  743. $new_values[$key] = $value;
  744. }
  745. $default_data[$priority]['values_default'] = $new_values;
  746. }
  747. }
  748. }
  749. return $no_errors;
  750. }
  751. /**
  752. * This function adds the file data to the values array
  753. *
  754. * @param $values
  755. * The default values array -contains all constants
  756. * @param $line
  757. * An array of values for the current line
  758. * @param $field2column
  759. * An array mapping values fields to line columns
  760. * @return
  761. * Supplemented values array
  762. *
  763. * @ingroup tripal_bulk_loader
  764. */
  765. function tripal_bulk_loader_add_spreadsheetdata_to_values($values, $line, $field2column) {
  766. foreach ($values as $field => $value) {
  767. if (is_array($value)) {
  768. continue;
  769. }
  770. $column = $field2column[$field] - 1;
  771. if ($column < 0) {
  772. continue;
  773. }
  774. if (preg_match('/\S+/', $line[$column])) {
  775. $values[$field] = $line[$column];
  776. }
  777. else {
  778. unset($values[$field]);
  779. }
  780. }
  781. return $values;
  782. }
  783. /**
  784. * Handles foreign keys in the values array.
  785. *
  786. * Specifically, if the value for a field is an array then it is assumed that the array contains
  787. * the name of the record whose values array should be substituted here. Thus the foreign
  788. * record is looked up and the values array is substituted in.
  789. *
  790. * @ingroup tripal_bulk_loader
  791. */
  792. function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority, $default_data) {
  793. // iterate through each field in the $values arrray and
  794. // substitute any values for FK / referring fields
  795. foreach ($values as $field => $value) {
  796. // if the field value is an array then it is an FK
  797. if (is_array($value)) {
  798. // get the name and priority of the foreign record
  799. $foreign_record = $value['foreign record']['record'];
  800. $foreign_priority = $record2priority[$foreign_record];
  801. $foreign_table = $value['foreign record']['table'];
  802. $foreign_field = $value['foreign record']['field'];
  803. // get the values of the foreign record and substitute those for the values
  804. $foreign_values = $data[$foreign_priority]['values_array'];
  805. // check to see if we have any default values in the $default_data array
  806. // these were populated from select statements that only need to run once
  807. // so we can reuse the values from those previous selects.
  808. if (array_key_exists($foreign_priority, $default_data) and
  809. array_key_exists('values_default', $default_data[$foreign_priority]) and
  810. array_key_exists($foreign_field, $default_data[$foreign_priority]['values_default'])) {
  811. $values[$field] = $default_data[$foreign_priority]['values_default'][$foreign_field];
  812. continue;
  813. }
  814. // if the field in the Referral records is in a FK relationship with
  815. // this field then we can simply keep the value we have
  816. $tbl_description = tripal_core_get_chado_table_schema($table_array['table']);
  817. if ($tbl_description and
  818. array_key_exists('foreign keys', $tbl_description) and
  819. array_key_exists($foreign_table, $tbl_description['foreign keys']) and
  820. array_key_exists($field, $tbl_description['foreign keys'][$foreign_table]['columns']) and
  821. $foreign_field == $tbl_description['foreign keys'][$foreign_table]['columns'][$field]) {
  822. $values[$field] = $foreign_values;
  823. }
  824. // if the field in the Referral records is not in an FK relationship
  825. // with this field then we we have to get the requested value, we must
  826. // return only a single value
  827. else {
  828. // if the current value of the referral records is a non-array then this
  829. // is the primary key, we can use it to select the value we need.
  830. $fk_description = tripal_core_get_chado_table_schema($foreign_table);
  831. if (!is_array($foreign_values)) {
  832. // if we have a value then use it to get the field we need
  833. if ($foreign_values) {
  834. $fvalues = array($fk_description['primary key'][0] => $foreign_values);
  835. $columns = array($foreign_field);
  836. $options = array('statement_name' => 'pk_' . $foreign_table);
  837. $options['print_errors'] = TRUE;
  838. $record = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
  839. if ($record) {
  840. $values[$field] = $record[0]->$foreign_field;
  841. }
  842. else {
  843. unset($values[$field]);
  844. }
  845. }
  846. // if we don't have a value then there's nothing we can do so
  847. // set this value to nothing as well
  848. else {
  849. unset($values[$field]);
  850. }
  851. }
  852. // if the current value is an array and our field is not in it, then
  853. // we need to select a value for our field.
  854. else {
  855. $fvalues = $foreign_values;
  856. $columns = array($foreign_field);
  857. $options = array('statement_name' => 'blk_' . $nid . $priority . $foreign_table);
  858. $options['print_errors'] = TRUE;
  859. $record = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
  860. if ($record) {
  861. $values[$field] = $record[0]->$foreign_field;
  862. }
  863. else {
  864. unset($values[$field]);
  865. }
  866. } // end else from: if (!is_array($foreign_values) ...
  867. } // end else from: if ($tbl_description ...
  868. } // end if(is_array($value)) ...
  869. } // end foreach ($values ...
  870. // return the updated field values
  871. return $values;
  872. }
  873. /**
  874. * Uses a supplied regex to transform spreadsheet values
  875. *
  876. * @param $values
  877. * The select/insert values array for the given table
  878. * @param $table_data
  879. * The data array for the given table
  880. *
  881. * @ingroup tripal_bulk_loader
  882. */
  883. function tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) {
  884. if (!array_key_exists('regex_transform', $table_data) or
  885. empty($table_data['regex_transform']) or
  886. !array_key_exists('regex_transform', $table_data) or
  887. !is_array($table_data['regex_transform'])) {
  888. return $values;
  889. }
  890. //watchdog('T_bulk_loader','Regex Transformation:<pre>'.print_r($table_data['regex_transform'], TRUE).'</pre>', array(), TRIPAL_NOTICE);
  891. foreach ($table_data['regex_transform'] as $field => $regex_array) {
  892. if (!array_key_exists('replace', $regex_array) or
  893. !array_key_exists('pattern', $regex_array) or
  894. !is_array($regex_array['replace'])) {
  895. continue;
  896. }
  897. // Check for <#column:\d+#> notation
  898. // if present replace with that column in the current line
  899. foreach ($regex_array['replace'] as $key => $replace) {
  900. if (preg_match_all('/<#column:(\d+)#>/', $replace, $matches)) {
  901. foreach ($matches[1] as $k => $column_num) {
  902. $replace = preg_replace('/' . $matches[0][$k] .'/', $line[$column_num-1], $replace);
  903. }
  904. $regex_array['replace'][$key] = $replace;
  905. }
  906. }
  907. // do the full replacement
  908. $old_value = $values[$field];
  909. $new_value = preg_replace($regex_array['pattern'], $regex_array['replace'], $old_value);
  910. $values[$field] = $new_value;
  911. if ($values[$field] === '') {
  912. unset($values[$field]);
  913. }
  914. //print 'Now:'.$values[$field]."\n";
  915. }
  916. return $values;
  917. }
  918. /**
  919. * Flattens an array up to two levels
  920. * Used for printing of arrays without taking up much space
  921. *
  922. * @ingroup tripal_bulk_loader
  923. */
  924. function tripal_bulk_loader_flatten_array($values) {
  925. $flattened_values = array();
  926. foreach ($values as $k => $v) {
  927. if (is_array($v)) {
  928. $vstr = array();
  929. foreach ($v as $vk => $vv) {
  930. if (drupal_strlen($vv) > 20) {
  931. $vstr[] = $vk . '=>' . drupal_substr($vv, 0, 20) . '...';
  932. }
  933. else {
  934. $vstr[] = $vk . '=>' . $vv;
  935. }
  936. }
  937. $v = '{' . implode(',', $vstr) . '}';
  938. }
  939. elseif (drupal_strlen($v) > 20) {
  940. $v = drupal_substr($v, 0, 20) . '...';
  941. }
  942. $flattened_values[] = $k . '=>' . $v;
  943. }
  944. return implode(', ', $flattened_values);
  945. }
  946. /**
  947. * Used to display loader progress to the user
  948. *
  949. * @ingroup tripal_bulk_loader
  950. */
  951. function tripal_bulk_loader_progress_bar($current=0, $total=100, $size=50) {
  952. $new_bar = FALSE;
  953. $mem = memory_get_usage();
  954. // First iteration
  955. if ($current == 0) {
  956. $new_bar = TRUE;
  957. fputs(STDOUT, "Progress:\n");
  958. }
  959. // Percentage round off for a more clean, consistent look
  960. $percent = sprintf("%.02f", round(($current/$total) * 100, 2));
  961. // percent indicator must be four characters, if shorter, add some spaces
  962. for ($i = strlen($percent); $i <= 4; $i++) {
  963. $percent = ' ' . $percent;
  964. }
  965. $total_size = $size + $i + 3 + 2;
  966. $place = 0;
  967. // if it's not first go, remove the previous bar
  968. if (!$new_bar) {
  969. for ($place = $total_size; $place > 0; $place--) {
  970. // echo a backspace (hex:08) to remove the previous character
  971. //echo "\x08";
  972. }
  973. }
  974. // output the progess bar as it should be
  975. // Start with a border
  976. echo '[';
  977. for ($place = 0; $place <= $size; $place++) {
  978. // output "full" spaces if this portion is completed
  979. if ($place <= ($current / $total * $size)) {
  980. echo '|';
  981. }
  982. else {
  983. // Otherwise empty space
  984. echo '-';
  985. }
  986. }
  987. // End with a border
  988. echo ']';
  989. // end a bar with a percent indicator
  990. echo " $percent%. ($current of $total) Memory: $mem\r";
  991. // if it's the end, add a new line
  992. if ($current == $total) {
  993. echo "\n";
  994. }
  995. }
  996. /**
  997. * Keep track of progress in file rather then database
  998. *
  999. * This provides an alternative method to keep track of progress that doesn't require the
  1000. * database. It was needed because you can't switch databases within a transaction...
  1001. * Waiting until the end of a constant set is much too long to wait for any indication
  1002. * that things are working.
  1003. *
  1004. * Each line represents a line processed in the loading file. Each period (.) represents
  1005. * a successfully inserted record.
  1006. *
  1007. * @param $job_id
  1008. * The ID of the current tripal job
  1009. * @param $record_added
  1010. * A boolean indicated whether a record was added successfully
  1011. * @param $line_complete
  1012. * A boolean indicating whether the current line is finished
  1013. * @param $close
  1014. * A boolean indicating that the file should be closed
  1015. *
  1016. * @ingroup tripal_bulk_loader
  1017. */
  1018. function tripal_bulk_loader_progress_file_track_job($job_id, $record_added, $line_complete = FALSE, $close = FALSE) {
  1019. // retrieve the file handle
  1020. $file_handle = variable_get('tripal_bulk_loader_progress_file_handle', NULL);
  1021. // open file for reading if not already
  1022. if (!$file_handle) {
  1023. $file_handle = fopen('/tmp/tripal_bulk_loader_progress-'. $job_id . '.out', 'w');
  1024. variable_set('tripal_bulk_loader_progress_file_handle', $file_handle);
  1025. }
  1026. if ($record_added) {
  1027. fwrite($file_handle, '.');
  1028. }
  1029. if ($line_complete) {
  1030. fwrite($file_handle, "\n");
  1031. }
  1032. // close the file if finished
  1033. if ($close) {
  1034. fclose($file_handle);
  1035. variable_set('tripal_bulk_loader_progress_file_handle', NULL);
  1036. }
  1037. }
  1038. function tripal_bulk_loader_throw_error($msg, $args, $severity) {
  1039. $options = array('print' => TRUE);
  1040. if ($severity == TRIPAL_NOTICE OR $severity == TRIPAL_INFO OR $severity == TRIPAL_DEBUG) {
  1041. unset($options['print']);
  1042. }
  1043. tripal_core_report_error(
  1044. 'tripal_bulk',
  1045. $severity,
  1046. $msg,
  1047. $args,
  1048. $options
  1049. );
  1050. }
  1051. function tripal_bulk_loader_finish_loading($nid, $loaded_without_errors) {
  1052. // set the status of the job (in the node not the tripal jobs)
  1053. if ($loaded_without_errors) {
  1054. $status = 'Loading Completed Successfully';
  1055. }
  1056. else {
  1057. $status = 'Errors Encountered';
  1058. }
  1059. db_update('tripal_bulk_loader')
  1060. ->fields(array('job_status' => $status))
  1061. ->condition('nid', $nid)
  1062. ->execute();
  1063. if(!$loaded_without_errors) {
  1064. drush_set_error('BULK_LOAD_FAILED', 'Execution aborted due to errors.');
  1065. exit();
  1066. }
  1067. }