12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247 |
- <?php
- function tripal_bulk_loader_add_loader_job_form($form, &$form_state, $node) {
- $form = [];
-
- if ($node->job_status == 'Loading...') {
- drupal_set_message(t("The Loading Summary only updates at the end of each constant set.
- Although records may have already been inserted, they won't be available until the
- current constant set is full loaded and no errors are encountered.", []), 'warning');
- }
- $form['nid'] = [
- '#type' => 'hidden',
- '#value' => $node->nid,
- ];
- $form['file'] = [
- '#type' => 'hidden',
- '#value' => $node->file,
- ];
- $form['job_id'] = [
- '#type' => 'hidden',
- '#value' => $node->job_id,
- ];
- $form['submit'] = [
- '#type' => 'submit',
- '#value' => ($node->job_id) ? 'Re-Submit Job' : 'Submit Job',
- ];
- $form['submit-cancel'] = [
- '#type' => ($node->job_id) ? 'submit' : 'hidden',
- '#value' => 'Cancel Job',
- ];
- if ($node->keep_track_inserted) {
- $form['submit-revert'] = [
- '#type' => ($node->job_id) ? 'submit' : 'hidden',
- '#value' => 'Revert',
- ];
- }
- return $form;
- }
- function tripal_bulk_loader_add_loader_job_form_submit($form, $form_state) {
- global $user;
- if (preg_match('/Submit Job/', $form_state['values']['op'])) {
-
- $job_args[1] = $form_state['values']['nid'];
- if (is_readable($form_state['values']['file'])) {
- $fname = basename($form_state['values']['file']);
- $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
-
- $success = db_update('tripal_bulk_loader')->fields([
- 'job_id' => $job_id,
- 'job_status' => 'Submitted to Queue',
- ])->condition('nid', $form_state['values']['nid'])->execute();
- }
- else {
- drupal_set_message(t("Can not open %file. Job not scheduled.", ['%file' => $form_state['values']['file']]));
- }
- }
- elseif (preg_match('/Re-Submit Job/', $form_state['values']['op'])) {
- tripal_rerun_job($form_state['values']['job_id']);
- $success = db_update('tripal_bulk_loader')->fields([
- 'job_status' => 'Submitted to Queue',
- ])->condition('nid', $form_state['values']['nid'])->execute();
- }
- elseif (preg_match('/Cancel Job/', $form_state['values']['op'])) {
- $success = db_update('tripal_bulk_loader')->fields([
- 'job_status' => 'Job Cancelled',
- ])->condition('nid', $form_state['values']['nid'])->execute();
- tripal_cancel_job($form_state['values']['job_id']);
- }
- elseif (preg_match('/Revert/', $form_state['values']['op'])) {
-
- $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid ORDER BY tripal_bulk_loader_inserted_id DESC', [':nid' => $form_state['values']['nid']]);
- while ($r = $resource->fetchObject()) {
- $ids = preg_split('/,/', $r->ids_inserted);
- db_query('DELETE FROM {' . $r->table_inserted_into . '} WHERE ' . $r->table_primary_key . ' IN (' . $r->ids_inserted . ')')->execute();
- $result = db_query('SELECT true as present FROM {' . $r->table_inserted_into . '} WHERE ' . $r->table_primary_key . ' IN (' . $r->ids_inserted . ')')->fetchObject();
- if (!$result->present) {
- drupal_set_message(t('Successfully Removed data Inserted into the %tableto table.', ['%tableto' => $r->table_inserted_into]));
- db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=:id', [':id' => $r->tripal_bulk_loader_inserted_id])->execute();
- }
- else {
- drupal_set_message(t('Unable to remove data Inserted into the %tableto table!', ['%tableto' => $r->table_inserted_into]), 'error');
- }
- }
-
- $success = db_update('tripal_bulk_loader')->fields([
- 'job_status' => 'Reverted -Data Deleted',
- ])->condition('nid', $form_state['values']['nid'])->execute();
- }
- }
- function tripal_bulk_loader_load_data($nid, $job_id) {
-
- drupal_set_time_limit(0);
-
- db_update('tripal_bulk_loader')
- ->fields(['job_status' => 'Loading...'])
- ->condition('nid', $nid)
- ->execute();
- $node = node_load($nid);
- print "Template: " . $node->template->name . " (" . $node->template_id . ")\n";
-
- $total_lines = 0;
- $handle = fopen($node->file, "r");
- while (!feof($handle)) {
- $line = fgets($handle);
- $line = trim($line);
- if (empty($line)) {
- continue;
- }
- $total_lines++;
- }
- fclose($handle);
-
- if (($total_lines) and (preg_match('/(t|true|1)/', $node->file_has_header))) {
- $total_lines--;
- }
-
- $total_lines = ($total_lines == 0) ? 1 : $total_lines;
- print "File: " . $node->file . " (" . $total_lines . " lines with data)\n";
-
-
-
- print "\nPreparing to load...\n";
- $loaded_without_errors = TRUE;
-
- $default_data = [];
- $field2column = [];
- $record2priority = [];
- $tables = [];
- $template_array = $node->template->template_array;
-
- foreach ($template_array as $priority => $record_array) {
- $record2priority[$record_array['record_id']] = $priority;
- }
-
- foreach ($template_array as $priority => $record_array) {
- if (!is_array($record_array)) {
- continue;
- }
-
-
- if (preg_match('/insert/', $record_array['mode'])) {
- $tables[$record_array['table']] = $record_array['table'];
- }
-
-
- foreach ($record_array['fields'] as $field_index => $field_array) {
- $default_data[$priority]['table'] = $record_array['table'];
- $default_data[$priority]['mode'] = ($record_array['mode']) ? $record_array['mode'] : 'insert';
- $default_data[$priority]['select_if_duplicate'] = ($record_array['select_if_duplicate']) ? $record_array['select_if_duplicate'] : 0;
- $default_data[$priority]['update_if_duplicate'] = ($record_array['update_if_duplicate']) ? $record_array['update_if_duplicate'] : 0;
- $default_data[$priority]['disabled'] = ($record_array['disable']) ? $record_array['disable'] : 0;
- $default_data[$priority]['optional'] = ($record_array['optional']) ? $record_array['optional'] : 0;
- $default_data[$priority]['select_optional'] = ($record_array['select_optional']) ? $record_array['select_optional'] : 0;
- $default_data[$priority]['record_id'] = $record_array['record_id'];
- $default_data[$priority]['required'][$field_array['field']] = $field_array['required'];
- $one = $default_data[$priority];
- if (isset($field_array['regex'])) {
- $default_data[$priority]['regex_transform'][$field_array['field']] = $field_array['regex'];
- }
- $two = $default_data[$priority];
- if (preg_match('/table field/', $field_array['type'])) {
- $default_data[$priority]['values_array'][$field_array['field']] = '';
- $default_data[$priority]['need_further_processing'] = TRUE;
- $field2column[$priority][$field_array['field']] = $field_array['spreadsheet column'];
- }
- elseif (preg_match('/constant/', $field_array['type'])) {
- $default_data[$priority]['values_array'][$field_array['field']] = $field_array['constant value'];
- }
- elseif (preg_match('/foreign key/', $field_array['type'])) {
- $default_data[$priority]['values_array'][$field_array['field']] = [];
- $default_data[$priority]['need_further_processing'] = TRUE;
- $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['record'] = $field_array['foreign key'];
-
- $fk_priority = $record2priority[$field_array['foreign key']];
- $fk_table = $template_array[$fk_priority]['table'];
- $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['table'] = $fk_table;
-
-
-
-
- if (!array_key_exists('foreign field', $field_array)) {
- $tbl_description = chado_get_schema($record_array['table']);
- foreach ($tbl_description['foreign keys'] as $key_table => $key_array) {
- if ($key_table == $fk_table) {
- foreach ($key_array['columns'] as $left_field => $right_field) {
- if ($left_field == $field_array['field']) {
- $field_array['foreign field'] = $right_field;
- }
- }
- }
- }
- }
- $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['field'] = $field_array['foreign field'];
- }
- else {
- print 'WARNING: Unsupported type: ' . $field_array['type'] . ' for ' . $table . '.' . $field_array['field'] . "!\n";
- }
- $three = $default_data[$priority];
- }
- }
-
-
-
- print "Loading...\n";
- $original_default_data = $default_data;
- $group_index = 0;
- $total_num_groups = sizeof($node->constants);
-
-
- if ($total_num_groups == 0 && empty($node->exposed_fields)) {
- $node->constants = [
- 0 => [],
- ];
- $total_num_groups = 1;
- }
- foreach ($node->constants as $group_id => $set) {
-
- $default_data = $original_default_data;
- $group_index++;
-
- if (!empty($set)) {
- print "Constants:\n";
- foreach ($set as $priority => $record) {
- foreach ($record as $field_id => $field) {
- print "\t- " . $field['chado_table'] . '.' . $field['chado_field'] . ' = ' . $field['value'] . "\n";
- if ($default_data[$priority]['table'] == $field['chado_table']) {
- if (isset($default_data[$priority]['values_array'][$field['chado_field']])) {
- if (isset($field2column[$priority][$field['chado_field']])) {
- $field2column[$priority][$field['chado_field']] = $field['value'];
- }
- else {
- $default_data[$priority]['values_array'][$field['chado_field']] = $field['value'];
- }
- }
- else {
- print "ERROR: Template has changed after constants were assigned!\n";
- tripal_bulk_loader_throw_error('Template has changed after constants were assigned', [], TRIPAL_NOTICE);
- exit(1);
- }
- }
- else {
- print "ERROR: Template has changed after constants were assigned!\n";
- tripal_bulk_loader_throw_error('Template has changed after constants were assigned', [], TRIPAL_NOTICE);
- exit(1);
- }
- }
- }
- }
-
- print "\tPreparing to load the current constant set...\n";
- print "\t\tOpen File...\n";
- try {
- $file = new SplFileObject($node->file, 'r');
- } catch (Exception $e) {
- tripal_bulk_loader_throw_error('Could not open file %file',
- ['%file' => $node->file], TRIPAL_ERROR);
- return;
- }
-
- $header = '';
- if (preg_match('/(t|true|1)/', $node->file_has_header)) {
- $file->next();
- $header = $file->current();
- }
- $num_records = 0;
- $num_lines = 0;
- $num_errors = 0;
- $interval = intval($total_lines * 0.0001);
- if ($interval == 0) {
- $interval = 1;
- }
-
- $savepoint = '';
- switch (variable_get('tripal_bulk_loader_transactions', 'row')) {
- case "none":
- break;
- case "all":
- print "\t\tStart Transaction...\n";
- $TRANSACTION = db_transaction();
- $transactions = TRUE;
- break;
- case "row":
- print "\t\tStart Transaction...\n";
- $TRANSACTION = db_transaction();
- $transactions = TRUE;
- $new_transaction_per_row = TRUE;
- break;
- }
-
- $triggers_disabled = FALSE;
- if ($transactions AND variable_get('tripal_bulk_loader_disable_triggers', TRUE)) {
- print "\t\tDefer Constraints...\n";
- $triggers_disabled = TRUE;
- chado_query("SET CONSTRAINTS ALL DEFERRED");
- }
-
- if ($transactions) {
- print "\t\tAcquiring Table Locks...\n";
- $lockmode = variable_get('tripal_bulk_loader_lock', 'ROW EXCLUSIVE');
- foreach ($tables as $table) {
- print "\t\t\t$lockmode for $table\n";
- chado_query("LOCK TABLE {" . $table . "} IN " . $lockmode . " MODE");
- }
- }
- print "\tLoading the current constant set...\n";
- tripal_bulk_loader_progress_bar(0, $total_lines);
- while (!$file->eof()) {
- $file->next();
- $raw_line = $file->current();
- $raw_line = trim($raw_line);
- if (empty($raw_line)) {
- continue;
- }
- $line = explode("\t", $raw_line);
- $num_lines++;
-
- if ($node->job_id and $num_lines % $interval == 0) {
-
- $group_progress = round(($num_lines / $total_lines) * 100);
-
- if ($num_lines < $total_lines) {
- tripal_bulk_loader_progress_bar($num_lines, $total_lines);
- }
-
-
-
-
-
-
-
- $job_progress = round(((($group_index - 1) * 100) + $group_progress) / $total_num_groups);
- tripal_set_job_progress($node->job_id, $job_progress);
- }
- $data = $default_data;
-
- $data_keys = array_keys($data);
- foreach ($data_keys as $priority) {
- $options = [
- 'field2column' => $field2column,
- 'record2priority' => $record2priority,
- 'line' => $line,
- 'line_num' => $num_lines,
- 'group_index' => $group_index,
- 'node' => $node,
- 'nid' => $node->nid,
- ];
-
- $no_errors = FALSE;
- if (array_key_exists($priority, $data) and
- array_key_exists('disabled', $data[$priority]) and
- $data[$priority]['disabled'] == 0) {
- $no_errors = process_data_array_for_line($priority, $data, $default_data, $options);
- }
- else {
-
- $no_errors = TRUE;
- }
- tripal_bulk_loader_progress_file_track_job($job_id, $no_errors);
- $failed = FALSE;
- if ($no_errors == FALSE) {
-
- if ($transactions) {
- $TRANSACTION->rollback();
- }
- tripal_bulk_loader_finish_loading($node->nid, FALSE);
- break;
- }
- }
- tripal_bulk_loader_progress_file_track_job($job_id, FALSE, TRUE);
- if ($failed) {
- $TRANSACTION->rollback();
- tripal_bulk_loader_finish_loading($node->nid, FALSE);
- break;
- }
- else {
-
- if ($transactions && $new_transaction_per_row) {
-
- unset($TRANSACTION);
- $TRANSACTION = db_transaction();
- }
- }
- }
-
- if ($transactions) {
- unset($TRANSACTION);
- }
- if ($failed) {
- $loaded_without_errors = FALSE;
- break;
- }
- tripal_bulk_loader_progress_bar($num_lines, $total_lines);
- tripal_bulk_loader_progress_file_track_job($job_id, FALSE, FALSE, TRUE);
- }
- tripal_bulk_loader_finish_loading($node->nid, $loaded_without_errors);
- }
- function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
-
- $table_data = $data[$priority];
- $addt = (object) $addt;
- $no_errors = TRUE;
- $table = $table_data['table'];
- $values = $table_data['values_array'];
-
-
- if (array_key_exists('need_further_processing', $table_data) and $table_data['need_further_processing']) {
- if (array_key_exists($priority, $addt->field2column)) {
- $values = tripal_bulk_loader_add_spreadsheetdata_to_values($values, $addt->line, $addt->field2column[$priority]);
- }
- $values = tripal_bulk_loader_add_foreignkey_to_values($table_data, $values, $data, $addt->record2priority, $addt->nid, $priority, $default_data);
- }
- $values = tripal_bulk_loader_regex_tranform_values($values, $table_data, $addt->line);
- if (!$values) {
-
- }
-
- $table_desc = chado_get_schema($table);
- if (!$table_desc) {
- $msg = "Tripal does not know about the table named '%table'. If this is a custom table,
- please define it first";
- tripal_bulk_loader_throw_error($msg, ['%table' => $table], TRIPAL_ERROR);
- $data[$priority]['error'] = TRUE;
- return;
- }
-
-
-
- $skip_optional = 0;
- foreach ($table_data['required'] as $field => $required) {
- if ($required) {
-
- if (!isset($values[$field]) or
- (is_array($values[$field]) and count($values[$field]) == 0)) {
-
-
- if ($table_data['optional'] or preg_match('/optional/', $table_data['mode']) or
- $table_data['select_optional']) {
- $skip_optional = 1;
-
-
-
- $values = [];
- }
- else {
- $msg = "Line " . $addt->line_num . ' "' . $table_data['record_id'] .
- '" (' . $table_data['mode'] . ') Missing template required value: ' . $table . '.' . $field;
- tripal_bulk_loader_throw_error($msg, [], TRIPAL_WARNING);
- $data[$priority]['error'] = TRUE;
- $no_errors = FALSE;
- }
- }
- }
- }
-
-
-
- if (!$skip_optional and (preg_match('/insert/', $table_data['mode']) or
- preg_match('/optional/', $table_data['mode']))) {
-
- $fields = $table_desc['fields'];
- foreach ($fields as $field => $def) {
-
-
- if (array_key_exists('not null', $def) and $def['not null'] == 1 and
- !array_key_exists($field, $values) and
- !array_key_exists('default', $def) and
- strcmp($def['type'], 'serial') != 0) {
- $msg = "Line " . $addt->line_num . ' ' . $table_data['record_id'] .
- ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
- tripal_bulk_loader_throw_error($msg, [], TRIPAL_ERROR);
- $data[$priority]['error'] = TRUE;
- $no_errors = FALSE;
- }
- }
- }
-
- $data[$priority]['values_array'] = $values;
-
- if (array_key_exists('error', $data[$priority]) and $data[$priority]['error']) {
- tripal_bulk_loader_throw_error('Skipping processing of %table due to previous errors', ['%table' => $table], TRIPAL_NOTICE);
- return $no_errors;
- }
-
- if ($skip_optional) {
-
-
-
-
- return $no_errors;
- }
-
- if (array_key_exists('inserted', $table_data) and $table_data['inserted']) {
-
-
-
-
- return $no_errors;
- }
-
-
- if (array_key_exists('selected', $table_data) and $table_data['selected']) {
- $data[$priority]['values_array'] = $default_data[$priority]['values_array'];
-
-
-
-
- return $no_errors;
- }
-
- if (!array_key_exists('select_if_duplicate', $table_data)) {
- $table_data['select_if_duplicate'] = 0;
- }
- if (!array_key_exists('update_if_duplicate', $table_data)) {
- $table_data['update_if_duplicate'] = 0;
- }
-
-
-
- $data[$priority]['is_duplicate'] = 0;
- if (preg_match('/insert_unique/', $table_data['mode']) or
- $table_data['select_if_duplicate'] == 1 or
- $table_data['update_if_duplicate'] == 1) {
- $options = ['is_duplicate' => TRUE, 'print_errors' => TRUE];
- $duplicate = chado_select_record($table, array_keys($table_desc['fields']), $values, $options);
-
-
- if ($duplicate and is_array($duplicate) and count($duplicate) == 1) {
- $dup_record = $duplicate[0];
-
-
- $data[$priority]['is_duplicate'] = (array) $dup_record;
-
-
-
-
- if (count((array) $dup_record) == 1) {
- foreach ($dup_record as $key => $value) {
- $data[$priority]['values_array'] = $value;
- }
- }
-
-
- else {
-
- $new_values = [];
- foreach ($dup_record as $key => $value) {
- $new_values[$key] = $value;
- }
- $data[$priority]['values_array'] = $new_values;
- }
-
- if ($table_data['select_if_duplicate'] == 1) {
-
-
-
-
- return $no_errors;
- }
- }
- }
- else {
-
-
- }
- if (!preg_match('/select/', $table_data['mode'])) {
-
- if (variable_get('tripal_bulk_loader_prepare', TRUE)) {
- $options = ['statement_name' => 'record_' . $addt->nid . '_' . $priority];
- if (($addt->line_num > 1 && $addt->group_index == 1) OR $addt->group_index > 1) {
-
- }
- }
- else {
- $options = [];
- }
-
- if (variable_get('tripal_bulk_loader_skip_validation', FALSE)) {
- $options['skip_validation'] = TRUE;
- }
- if ($table_data['update_if_duplicate'] == 1) {
- if (array_key_exists('statement_name', $options)) {
- $options['statement_name'] = 'upd_' . $options['statement_name'];
- }
-
- $match = $data[$priority]['is_duplicate'];
-
-
-
- if (empty($match)) {
- $match = [];
-
- foreach ($table_desc['primary key'] as $k_field) {
- if (!empty($values[$k_field])) {
- $match[$k_field] = $values[$k_field];
- }
- }
-
- if (empty($match)) {
- foreach ($table_desc['unique keys'] as $u_keys) {
- foreach ($u_keys as $u_field) {
- if (!empty($values[$u_field])) {
- $match[$u_field] = $values[$u_field];
- }
- }
- }
- }
- }
- if (!empty($match)) {
-
- $results = chado_select_record($table, array_keys($table_desc['fields']), $match, ['print_errors' => TRUE]);
-
- if (empty($results)) {
- $options['statement_name'] = 'ins_' . $options['statement_name'];
- $options['print_errors'] = TRUE;
- $record = chado_insert_record($table, $values, $options);
- }
- else {
- $options['return_record'] = TRUE;
- $options['print_errors'] = TRUE;
- $record = chado_update_record($table, $match, $values, $options);
- }
- }
- else {
- $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
- $table_data['mode'] . ') Unable to update record since none of the unique key or primary key fields were available ' .
- ' where values:' . print_r($values, TRUE);
- tripal_bulk_loader_throw_error($msg, [], TRIPAL_ERROR);
- $data[$priority]['error'] = TRUE;
- $no_errors = FALSE;
- }
- }
- else {
- $options['print_errors'] = TRUE;
- $record = chado_insert_record($table, $values, $options);
- }
-
- if (!$record) {
- $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
- $table_data['mode'] . ') Unable to insert record into ' . $table .
- ' where values:' . print_r($values, TRUE);
- tripal_bulk_loader_throw_error($msg, [], TRIPAL_ERROR);
- $data[$priority]['error'] = TRUE;
- $no_errors = FALSE;
- }
-
- else {
-
- if (preg_match('/insert_once/', $table_data['mode'])) {
- $default_data[$priority]['inserted'] = TRUE;
- }
-
- if ($addt->node->keep_track_inserted) {
- $insert_record = db_query(
- "SELECT * FROM {tripal_bulk_loader_inserted} WHERE table_inserted_into=:table AND nid=:nid",
- [
- ':table' => $table,
- 'nid' => $addt->nid,
- ])->fetchObject();
- if ($insert_record) {
- $insert_record->ids_inserted .= ',' . $record[$table_desc['primary key'][0]];
- drupal_write_record('tripal_bulk_loader_inserted', $insert_record, 'tripal_bulk_loader_inserted_id');
-
-
- }
- else {
- $insert_record = [
- 'nid' => $addt->nid,
- 'table_inserted_into' => $table,
- 'table_primary_key' => $table_desc['primary key'][0],
- 'ids_inserted' => $record[$table_desc['primary key'][0]],
- ];
-
- $success = drupal_write_record('tripal_bulk_loader_inserted', $insert_record);
-
- }
- }
-
-
- if (array_key_exists('primary key', $table_desc)) {
- if (count($table_desc['primary key']) == 1) {
- $pkey_field = $table_desc['primary key'][0];
- $data[$priority]['values_array'] = $record[$pkey_field];
- }
- }
- else {
-
- $data[$priority]['values_array'] = $record;
- $values = $record;
- }
- }
- }
-
- else {
-
- $matches = [];
- if (is_array($values) and count($values) > 0) {
- $matches = chado_select_record($table, array_keys($table_desc['fields']), $values, ['print_errors' => TRUE]);
- }
-
- if (count($matches) == 0) {
-
- if ($table_data['select_optional'] != 1) {
- $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') No Matching record in ' . $table . ' where values:' . print_r($values, TRUE);
- tripal_bulk_loader_throw_error($msg, [], TRIPAL_ERROR);
- $data[$priority]['error'] = TRUE;
- $no_errors = FALSE;
- }
-
-
- else {
- $data[$priority]['values_array'] = NULL;
- }
- }
-
- if (count($matches) > 1) {
- if ($table_data['select_optional'] != 1) {
- $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') Too many matching records in ' . $table . ' where values:' . print_r($values, TRUE);
- tripal_bulk_loader_throw_error($msg, [], TRIPAL_WARNING);
- $data[$priority]['error'] = TRUE;
- $no_errors = FALSE;
- }
-
-
- else {
- $data[$priority]['values_array'] = NULL;
- }
- }
-
- if (preg_match('/select_once/', $table_data['mode'])) {
- $default_data[$priority]['selected'] = TRUE;
-
- if (array_key_exists('primary key', $table_desc)) {
- $new_values = [];
- foreach ($matches[0] as $key => $value) {
- $new_values[$key] = $value;
- }
- $default_data[$priority]['values_default'] = $new_values;
- }
- }
- }
- return $no_errors;
- }
- function tripal_bulk_loader_add_spreadsheetdata_to_values($values, $line, $field2column) {
- foreach ($values as $field => $value) {
- if (is_array($value)) {
- continue;
- }
- $column = $field2column[$field] - 1;
- if ($column < 0) {
- continue;
- }
- if (preg_match('/\S+/', $line[$column])) {
- $values[$field] = $line[$column];
- }
- else {
- unset($values[$field]);
- }
- }
- return $values;
- }
- function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority, $default_data) {
-
-
- foreach ($values as $field => $value) {
-
- if (is_array($value)) {
-
- $foreign_record = $value['foreign record']['record'];
- $foreign_priority = $record2priority[$foreign_record];
- $foreign_table = $value['foreign record']['table'];
- $foreign_field = $value['foreign record']['field'];
-
- $foreign_values = $data[$foreign_priority]['values_array'];
-
-
-
- if (array_key_exists($foreign_priority, $default_data) and
- array_key_exists('values_default', $default_data[$foreign_priority]) and
- array_key_exists($foreign_field, $default_data[$foreign_priority]['values_default'])) {
- $values[$field] = $default_data[$foreign_priority]['values_default'][$foreign_field];
- continue;
- }
-
-
- $tbl_description = chado_get_schema($table_array['table']);
- if ($tbl_description and
- array_key_exists('foreign keys', $tbl_description) and
- array_key_exists($foreign_table, $tbl_description['foreign keys']) and
- array_key_exists($field, $tbl_description['foreign keys'][$foreign_table]['columns']) and
- $foreign_field == $tbl_description['foreign keys'][$foreign_table]['columns'][$field]) {
- $values[$field] = $foreign_values;
- }
-
-
-
- else {
-
-
- $fk_description = chado_get_schema($foreign_table);
- if (!is_array($foreign_values)) {
-
- if ($foreign_values) {
- $fvalues = [$fk_description['primary key'][0] => $foreign_values];
- $columns = [$foreign_field];
- $options = ['statement_name' => 'pk_' . $foreign_table];
- $options['print_errors'] = TRUE;
- $record = chado_select_record($foreign_table, $columns, $fvalues, $options);
- if ($record) {
- $values[$field] = $record[0]->$foreign_field;
- }
- else {
- unset($values[$field]);
- }
- }
-
-
- else {
- unset($values[$field]);
- }
- }
-
-
- else {
- $fvalues = $foreign_values;
- $columns = [$foreign_field];
- $options = ['statement_name' => 'blk_' . $nid . $priority . $foreign_table];
- $options['print_errors'] = TRUE;
- $record = chado_select_record($foreign_table, $columns, $fvalues, $options);
- if ($record) {
- $values[$field] = $record[0]->$foreign_field;
- }
- else {
- unset($values[$field]);
- }
- }
- }
- }
- }
-
- return $values;
- }
- function tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) {
- if (!array_key_exists('regex_transform', $table_data) or
- empty($table_data['regex_transform']) or
- !array_key_exists('regex_transform', $table_data) or
- !is_array($table_data['regex_transform'])) {
- return $values;
- }
-
- foreach ($table_data['regex_transform'] as $field => $regex_array) {
- if (!array_key_exists('replace', $regex_array) or
- !array_key_exists('pattern', $regex_array) or
- !is_array($regex_array['replace'])) {
- continue;
- }
-
-
- foreach ($regex_array['replace'] as $key => $replace) {
- if (preg_match_all('/<#column:(\d+)#>/', $replace, $matches)) {
- foreach ($matches[1] as $k => $column_num) {
- $replace = preg_replace('/' . $matches[0][$k] . '/', $line[$column_num - 1], $replace);
- }
- $regex_array['replace'][$key] = $replace;
- }
- }
-
- $old_value = $values[$field];
- $new_value = preg_replace($regex_array['pattern'], $regex_array['replace'], $old_value);
- $values[$field] = $new_value;
- if ($values[$field] === '') {
- unset($values[$field]);
- }
-
- }
- return $values;
- }
- function tripal_bulk_loader_flatten_array($values) {
- $flattened_values = [];
- foreach ($values as $k => $v) {
- if (is_array($v)) {
- $vstr = [];
- foreach ($v as $vk => $vv) {
- if (drupal_strlen($vv) > 20) {
- $vstr[] = $vk . '=>' . drupal_substr($vv, 0, 20) . '...';
- }
- else {
- $vstr[] = $vk . '=>' . $vv;
- }
- }
- $v = '{' . implode(',', $vstr) . '}';
- }
- elseif (drupal_strlen($v) > 20) {
- $v = drupal_substr($v, 0, 20) . '...';
- }
- $flattened_values[] = $k . '=>' . $v;
- }
- return implode(', ', $flattened_values);
- }
- function tripal_bulk_loader_progress_bar($current = 0, $total = 100, $size = 50) {
- $new_bar = FALSE;
- $mem = memory_get_usage();
-
- if ($current == 0) {
- $new_bar = TRUE;
- fputs(STDOUT, "Progress:\n");
- }
-
- $percent = sprintf("%.02f", round(($current / $total) * 100, 2));
-
- for ($i = strlen($percent); $i <= 4; $i++) {
- $percent = ' ' . $percent;
- }
- $total_size = $size + $i + 3 + 2;
- $place = 0;
-
- if (!$new_bar) {
- for ($place = $total_size; $place > 0; $place--) {
-
-
- }
- }
-
-
- echo '[';
- for ($place = 0; $place <= $size; $place++) {
-
- if ($place <= ($current / $total * $size)) {
- echo '|';
- }
- else {
-
- echo '-';
- }
- }
-
- echo ']';
-
- echo " $percent%. ($current of $total) Memory: $mem\r";
-
- if ($current == $total) {
- echo "\n";
- }
- }
- function tripal_bulk_loader_progress_file_track_job($job_id, $record_added, $line_complete = FALSE, $close = FALSE) {
-
- $file_handle = variable_get('tripal_bulk_loader_progress_file_handle', NULL);
-
- if (!$file_handle) {
- $file_handle = fopen('/tmp/tripal_bulk_loader_progress-' . $job_id . '.out', 'w');
- variable_set('tripal_bulk_loader_progress_file_handle', $file_handle);
- }
- if ($record_added) {
- fwrite($file_handle, '.');
- }
- if ($line_complete) {
- fwrite($file_handle, "\n");
- }
-
- if ($close) {
- fclose($file_handle);
- variable_set('tripal_bulk_loader_progress_file_handle', NULL);
- }
- }
- function tripal_bulk_loader_throw_error($msg, $args, $severity) {
- $options = ['print' => TRUE];
- if ($severity == TRIPAL_NOTICE OR $severity == TRIPAL_INFO OR $severity == TRIPAL_DEBUG) {
- unset($options['print']);
- }
- tripal_report_error(
- 'tripal_bulk',
- $severity,
- $msg,
- $args,
- $options
- );
- }
- function tripal_bulk_loader_finish_loading($nid, $loaded_without_errors) {
-
- if ($loaded_without_errors) {
- $status = 'Loading Completed Successfully';
- }
- else {
- $status = 'Errors Encountered';
- }
- db_update('tripal_bulk_loader')
- ->fields(['job_status' => $status])
- ->condition('nid', $nid)
- ->execute();
- if (!$loaded_without_errors) {
- drush_set_error('BULK_LOAD_FAILED', 'Execution aborted due to errors.');
- exit();
- }
- }
|