tripal_bulk_loader.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. /**
  3. * @file
  4. * The functions for the Tripal bulk loader.
  5. *
  6. * @defgroup tripal_bulk_loader Tripal Bulk Loader Module
  7. * @ingroup tripal_modules
  8. */
  9. include('includes/tripal_bulk_loader.loader.inc');
  10. include('includes/tripal_bulk_loader.constants.inc');
  11. include('includes/tripal_bulk_loader.admin.inc');
  12. include('includes/tripal_bulk_loader.admin.templates.inc');
  13. // API
  14. include('api/tripal_bulk_loader.api.templates.inc');
  15. /**
  16. * Implements hook_init
  17. * Used to add stylesheets and javascript files to the header
  18. *
  19. * @ingroup tripal_bulk_loader
  20. */
  21. function tripal_bulk_loader_init() {
  22. // Add javascript and style sheet
  23. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_bulk_loader.css');
  24. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_bulk_loader.js');
  25. }
  26. /**
  27. * Implements hook_menu
  28. *
  29. * @ingroup tripal_bulk_loader
  30. */
  31. function tripal_bulk_loader_menu() {
  32. $items = array();
  33. // Bulk Loading Job Node
  34. $items['node/%node/constants/%/edit'] = array(
  35. 'title' => 'Edit Constant Set',
  36. 'description' => 'Edit a group of constants associated with the current bulk loader',
  37. 'page callback' => 'drupal_get_form',
  38. 'page arguments' => array('tripal_bulk_loader_edit_constant_set_form', 1, 3),
  39. 'access arguments' => array('administer tripal_bulk_loader'),
  40. 'type' => MENU_CALLBACK,
  41. );
  42. $items['node/%node/constants/%/delete'] = array(
  43. 'title' => 'Delete Constant Set',
  44. 'description' => 'Delete a group of constants associated with the current bulk loader',
  45. 'page callback' => 'drupal_get_form',
  46. 'page arguments' => array('tripal_bulk_loader_delete_constant_set_form', 1, 3),
  47. 'access arguments' => array('administer tripal_bulk_loader'),
  48. 'type' => MENU_CALLBACK,
  49. );
  50. // Admin pages -----------------
  51. $items['admin/tripal/tripal_bulk_loader_template'] = array(
  52. 'title' => 'Bulk Loader',
  53. 'description' => 'Templates for loading tab-delimited data',
  54. 'page callback' => 'theme',
  55. 'page arguments' => array('tripal_bulk_loader_admin'),
  56. 'access arguments' => array('administer tripal_bulk_loader'),
  57. 'type' => MENU_NORMAL_ITEM,
  58. );
  59. $items['admin/tripal/tripal_bulk_loader_template/configure'] = array(
  60. 'title' => 'Configure',
  61. 'description' => 'Configuration of global options related to bulk loading jobs',
  62. 'page callback' => 'drupal_get_form',
  63. 'page arguments' => array('tripal_bulk_loader_configuration_form'),
  64. 'access arguments' => array('administer tripal_bulk_loader'),
  65. 'type' => MENU_NORMAL_ITEM,
  66. );
  67. $items['admin/tripal/tripal_bulk_loader_template/manage_templates'] = array(
  68. 'title' => 'Manage Templates',
  69. 'description' => 'Create/Update/Delete/Import/Export Templates',
  70. 'page callback' => 'tripal_bulk_loader_admin_manage_templates',
  71. 'access arguments' => array('administer tripal_bulk_loader'),
  72. 'type' => MENU_NORMAL_ITEM,
  73. );
  74. $items['admin/tripal/tripal_bulk_loader_template/jobs'] = array(
  75. 'title' => 'Bulk Loader Jobs',
  76. 'description' => 'Listing of Bulk Loading Jobs',
  77. 'page callback' => 'tripal_bulk_loader_admin_jobs',
  78. 'access arguments' => array('administer tripal_bulk_loader'),
  79. 'type' => MENU_NORMAL_ITEM,
  80. );
  81. // Create/Edit Template --------
  82. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/create'] = array(
  83. 'title' => 'Create Template',
  84. 'description' => 'Create loader template for loading tab-delimited data',
  85. 'page callback' => 'drupal_get_form',
  86. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'create'),
  87. 'access arguments' => array('administer tripal_bulk_loader'),
  88. 'type' => MENU_NORMAL_ITEM,
  89. );
  90. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/edit'] = array(
  91. 'title' => 'Edit Template',
  92. 'description' => 'Edit loader template for loading tab-delimited data',
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit'),
  95. 'access arguments' => array('administer tripal_bulk_loader'),
  96. 'type' => MENU_NORMAL_ITEM,
  97. );
  98. $items['admin/tripal/tripal_bulk_loader_template/edit_record'] = array(
  99. 'title' => 'Edit Template Record',
  100. 'description' => 'Edit a record in an existing tripal bulk loader template.',
  101. 'page callback' => 'drupal_get_form',
  102. 'page arguments' => array('tripal_bulk_loader_edit_template_record_form'),
  103. 'access arguments' => array('administer tripal_bulk_loader'),
  104. 'type' => MENU_CALLBACK,
  105. );
  106. $items['admin/tripal/tripal_bulk_loader_template/add_field'] = array(
  107. 'title' => 'Add Template Field',
  108. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  109. 'page callback' => 'drupal_get_form',
  110. 'page arguments' => array('tripal_bulk_loader_add_template_field_form'),
  111. 'access arguments' => array('administer tripal_bulk_loader'),
  112. 'type' => MENU_CALLBACK,
  113. );
  114. $items['admin/tripal/tripal_bulk_loader_template/edit_field'] = array(
  115. 'title' => 'Edit Template Field',
  116. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  117. 'page callback' => 'drupal_get_form',
  118. 'page arguments' => array('tripal_bulk_loader_edit_template_field_form'),
  119. 'access arguments' => array('administer tripal_bulk_loader'),
  120. 'type' => MENU_CALLBACK,
  121. );
  122. // Delete Template -----
  123. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/delete'] = array(
  124. 'title' => 'Delete Template',
  125. 'description' => 'Delete bulk loader template',
  126. 'page callback' => 'drupal_get_form',
  127. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  128. 'access arguments' => array('administer tripal_bulk_loader'),
  129. 'type' => MENU_NORMAL_ITEM,
  130. );
  131. // Import/Export ---------
  132. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/import'] = array(
  133. 'title' => 'Import Template',
  134. 'description' => 'Import Loaders',
  135. 'page callback' => 'drupal_get_form',
  136. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'import'),
  137. 'access arguments' => array('administer tripal_bulk_loader'),
  138. 'type' => MENU_NORMAL_ITEM,
  139. );
  140. $items['admin/tripal/tripal_bulk_loader_template/manage_templates/export'] = array(
  141. 'title' => 'Export Template',
  142. 'description' => 'Export Loaders',
  143. 'page callback' => 'drupal_get_form',
  144. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'export'),
  145. 'access arguments' => array('administer tripal_bulk_loader'),
  146. 'type' => MENU_NORMAL_ITEM,
  147. );
  148. // AHAH ---------
  149. $items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
  150. 'page callback' => 'tripal_bulk_loader_add_field_ahah',
  151. 'access arguments' => array('administer tripal_bulk_loader'),
  152. 'type' => MENU_CALLBACK,
  153. );
  154. $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
  155. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  156. 'access arguments' => array('administer tripal_bulk_loader'),
  157. 'type' => MENU_CALLBACK,
  158. );
  159. return $items;
  160. }
  161. /**
  162. * Implements hook_theme
  163. *
  164. * @ingroup tripal_bulk_loader
  165. */
  166. function tripal_bulk_loader_theme() {
  167. return array(
  168. 'node' => array(
  169. 'variables' => array('node' => NULL),
  170. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  171. ),
  172. 'tripal_bulk_loader_set_constants_form' => array(
  173. 'function' => 'theme_tripal_bulk_loader_set_constants_form',
  174. 'variables' => array('form' => NULL),
  175. ),
  176. 'tripal_bulk_loader_template' => array(
  177. 'variables' => array('template_id' => NULL),
  178. 'template' => 'tripal_bulk_loader_template',
  179. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  180. ),
  181. 'tripal_bulk_loader_modify_template_base_form' => array(
  182. 'variables' => array('form' => NULL),
  183. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  184. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  185. ),
  186. 'tripal_bulk_loader_edit_template_field_form' => array(
  187. 'variables' => array('form' => NULL),
  188. 'template' => 'tripal_bulk_loader_edit_template_field_form',
  189. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  190. ),
  191. 'tripal_bulk_loader_add_template_field_form' => array(
  192. 'variables' => array('form' => NULL),
  193. 'template' => 'tripal_bulk_loader_add_template_field_form',
  194. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  195. ),
  196. 'tripal_bulk_loader_admin' => array(
  197. 'template' => 'tripal_bulk_loader_admin',
  198. 'variables' => array(NULL),
  199. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  200. ),
  201. );
  202. }
  203. /**
  204. * Implement hook_access().
  205. *
  206. * This hook allows node modules to limit access to the node types they define.
  207. *
  208. * @param $op
  209. * The operation to be performed
  210. *
  211. * @param $node
  212. * The node on which the operation is to be performed, or, if it does not yet exist, the
  213. * type of node to be created
  214. *
  215. * @param $account
  216. * A user object representing the user for whom the operation is to be performed
  217. *
  218. * @return
  219. * If the permission for the specified operation is not set then return FALSE. If the
  220. * permission is set then return NULL as this allows other modules to disable
  221. * access. The only exception is when the $op == 'create'. We will always
  222. * return TRUE if the permission is set.
  223. * @ingroup tripal_bulk_loader
  224. */
  225. function tripal_bulk_loader_node_access($node, $op, $account) {
  226. if ($op == 'create') {
  227. if (!user_access('create tripal_bulk_loader', $account)) {
  228. return FALSE;
  229. }
  230. return TRUE;
  231. }
  232. if ($op == 'update') {
  233. if (!user_access('edit tripal_bulk_loader', $account)) {
  234. return FALSE;
  235. }
  236. }
  237. if ($op == 'delete') {
  238. if (!user_access('delete tripal_bulk_loader', $account)) {
  239. return FALSE;
  240. }
  241. }
  242. if ($op == 'view') {
  243. if (!user_access('access tripal_bulk_loader', $account)) {
  244. return FALSE;
  245. }
  246. }
  247. return NULL;
  248. }
  249. /**
  250. * Implements hook_perm
  251. *
  252. * @ingroup tripal_bulk_loader
  253. */
  254. function tripal_bulk_loader_permission() {
  255. return array(
  256. 'access tripal_bulk_loader',
  257. 'create tripal_bulk_loader',
  258. 'delete tripal_bulk_loader',
  259. 'edit tripal_bulk_loader',
  260. 'administer tripal_bulk_loader',
  261. );
  262. }
  263. //////////////////////////////////////////////////////////////////////////////////////////////
  264. // Node Functions
  265. //////////////////////////////////////////////////////////////////////////////////////////////
  266. /**
  267. * Implements hook_node_info
  268. *
  269. * @ingroup tripal_bulk_loader
  270. */
  271. function tripal_bulk_loader_node_info() {
  272. $nodes = array();
  273. $nodes['tripal_bulk_loader'] = array(
  274. 'name' => t('Bulk Loading Job'),
  275. 'base' => 'tripal_bulk_loader',
  276. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  277. 'has_title' => TRUE,
  278. 'has_body' => FALSE,
  279. 'locked' => TRUE
  280. );
  281. return $nodes;
  282. }
  283. /**
  284. * Implements node_form
  285. * Used to gather the extra details stored with a Bulk Loading Job Node
  286. *
  287. * @ingroup tripal_bulk_loader
  288. */
  289. function tripal_bulk_loader_form($node, $form_state) {
  290. $form = array();
  291. if (isset($form_state['values'])) {
  292. $node = $form_state['values'] + (array)$node;
  293. $node = (object) $node;
  294. }
  295. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  296. $results = db_query($sql)->execute();
  297. $templates = array();
  298. foreach ($results as $template) {
  299. $templates [$template->template_id] = $template->name;
  300. }
  301. if (!$templates) {
  302. $form['label'] = array(
  303. '#type' => 'item',
  304. '#description' => t("Loader template needs to be created before any bulk loader can be added. Go to 'Tripal Management > Bulk Loader Template' to create the template."),
  305. '#weight' => -10,
  306. );
  307. return $form;
  308. }
  309. $form['loader'] = array(
  310. '#type' => 'fieldset',
  311. '#title' => t('Basic Details'),
  312. );
  313. $form['loader']['loader_name'] = array(
  314. '#type' => 'textfield',
  315. '#title' => t('Loading Job Name'),
  316. '#weight' => -10,
  317. '#required' => TRUE,
  318. '#default_value' => $node->loader_name
  319. );
  320. $form['loader']['template_id'] = array(
  321. '#type' => 'select',
  322. '#title' => t('Template'),
  323. '#description' => t('Please specify a template for this loader'),
  324. '#options' => $templates,
  325. '#weight' => -9,
  326. '#required' => TRUE,
  327. '#default_value' => $node->template_id,
  328. );
  329. $form['loader']['file']= array(
  330. '#type' => 'textfield',
  331. '#title' => t('Data File'),
  332. '#description' => t('Please specify the data file to be loaded. This must be a tab-delimited text file with UNIX line endings.'),
  333. '#weight' => -8,
  334. '#default_value' => $node->file,
  335. '#maxlength' => 1024,
  336. );
  337. $form['loader']['has_header'] = array(
  338. '#type' => 'radios',
  339. '#title' => t('File has a Header'),
  340. '#options' => array( 1 => 'Yes', 2 => 'No'),
  341. '#weight' => -7,
  342. '#default_value' => $node->file_has_header,
  343. );
  344. $form['loader']['keep_track_inserted'] = array(
  345. '#type' => 'radios',
  346. '#title' => t('Keep track of inserted record IDs'),
  347. '#description' => t('This enables the ability to revert an entire loading job even if '
  348. .'it completed successfully. Furthermore, it displays the number of records '
  349. .'successfully inserted into each table.'),
  350. '#options' => array( 1 => 'Yes', 0 => 'No'),
  351. '#weight' => -7,
  352. '#default_value' => (isset($node->keep_track_inserted)) ? $node->keep_track_inserted : variable_get('tripal_bulk_loader_keep_track_inserted', FALSE),
  353. );
  354. return $form;
  355. }
  356. /**
  357. * Implements node_load
  358. *
  359. * @ingroup tripal_bulk_loader
  360. */
  361. function tripal_bulk_loader_load($node) {
  362. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = :nid";
  363. $node = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  364. $node->title = 'Bulk Loading Job: ' . $node->loader_name;
  365. // Add job details
  366. $progress = tripal_bulk_loader_progess_file_get_progress($node->job_id);
  367. $sql = "SELECT * FROM {tripal_jobs} WHERE job_id=:job_id";
  368. $node->job = db_query($sql, array(':job_id' => $node->job_id))->fetchObject();
  369. // Add the loader template
  370. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  371. $results = db_query($sql, array(':template' => $node->template_id))->fetchObject();
  372. $template = unserialize($results->template_array);
  373. $node->template = $results;
  374. $node->template->template_array = $template;
  375. // Add inserted records
  376. $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid';
  377. $resource = db_query($sql, array(':nid' => $node->nid))->execute();
  378. foreach ($resource as $r) {
  379. $r->num_inserted = sizeof(preg_split('/,/', $r->ids_inserted));
  380. $node->inserted_records->{$r->table_inserted_into} = $r;
  381. }
  382. // Add exposed field list
  383. $node->exposed_fields = array();
  384. if ($template) {
  385. foreach ($template as $record_id => $record) {
  386. foreach ($record['fields'] as $field_id => $field) {
  387. if ($field['exposed']) {
  388. $node->exposed_fields[] = array(
  389. 'record_id' => $record_id,
  390. 'field_id' => $field_id,
  391. 'title' => $field['title'],
  392. );
  393. }
  394. }
  395. }
  396. if (empty($node->exposed_fields)) {
  397. $node->exposed_fields[] = array();
  398. }
  399. }
  400. // Add constants
  401. $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=:nid ORDER BY group_id, record_id, field_id';
  402. $resource = db_query($sql, array(':nid' => $node->nid))->execute();
  403. foreach ($resource as $r) {
  404. $node->constants[$r->group_id][$r->record_id][$r->field_id] = array(
  405. 'constant_id' => $r->constant_id,
  406. 'group_id' => $r->group_id,
  407. 'chado_table' => $r->chado_table,
  408. 'chado_field' => $r->chado_field,
  409. 'record_id' => $r->record_id,
  410. 'field_id' => $r->field_id,
  411. 'value' => $r->value
  412. );
  413. }
  414. if (!$node->constants) {
  415. $node->constants[] = array();
  416. }
  417. return $node;
  418. }
  419. /**
  420. * Implements node_insert
  421. * Insert the data from the node form on Create content
  422. *
  423. * @ingroup tripal_bulk_loader
  424. */
  425. function tripal_bulk_loader_insert($node) {
  426. // Insert into tripal_bulk_loader
  427. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status, keep_track_inserted) VALUES (:nid, :loader, :template, :file, :header, :status, :ids)";
  428. db_query($sql,
  429. array(':nid' => $node->nid,
  430. ':loader' => $node->loader_name,
  431. ':template' => $node->template_id,
  432. ':file' => $node->file,
  433. ':header' => $node->has_header,
  434. ':status' => 'Initialized',
  435. ':ids' => $node->keep_track_inserted
  436. )
  437. )->execute();
  438. // Update title
  439. $node->title =$node->loader_name;
  440. drupal_write_record('node', $node, 'nid');
  441. drupal_write_record('node_revision', $node, 'nid');
  442. drupal_set_message(t('After reviewing the details, please Submit this Job (by clicking the "Submit Job" button below). No data will be loaded until the submitted job is reached in the queue.'));
  443. }
  444. /**
  445. * Implements node_delete
  446. * Deletes the data when the delete button on the node form is clicked
  447. *
  448. * @ingroup tripal_bulk_loader
  449. */
  450. function tripal_bulk_loader_delete($node) {
  451. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = :nid";
  452. db_query($sql, array(':nid' => $node->nid))->execute();
  453. }
  454. /**
  455. * Implements node_update
  456. * Updates the data submitted by the node form on edit
  457. *
  458. * @ingroup tripal_bulk_loader
  459. */
  460. function tripal_bulk_loader_update($node) {
  461. // Update tripal_bulk_loader
  462. $sql = "UPDATE {tripal_bulk_loader} SET nid = :nid, loader_name = :loader, template_id = :template, file = :file, file_has_header = :header, keep_track_inserted = :ids WHERE nid = :wherenid";
  463. db_query($sql,
  464. array(':nid' => $node->nid,
  465. ':loader' => $node->loader_name,
  466. ':template' => $node->template_id,
  467. ':file' => $node->file,
  468. ':header' => $node->has_header,
  469. ':ids' => $node->keep_track_inserted,
  470. ':wherenid' => $node->nid
  471. )
  472. )->execute();
  473. // Add a job if the user want to load the data
  474. global $user;
  475. if ($node->job) {
  476. $job_args[0] =$node->loader_name;
  477. $job_args[1] = $node->template_id;
  478. $job_args[2] = $node->file;
  479. if (is_readable($node->file)) {
  480. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  481. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  482. }
  483. else {
  484. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  485. }
  486. }
  487. }
  488. ///////////////////////////////////////////////////////////
  489. /**
  490. * Preprocessor function for the tripal_bulk_loader template
  491. *
  492. * @ingroup tripal_bulk_loader
  493. */
  494. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template(&$variables) {
  495. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  496. $template = db_query($sql, array(':template' => $variables['template_id']))->fetchObject();
  497. $template->template_array = unserialize($template->template_array);
  498. $variables['template'] = $template;
  499. }
  500. /**
  501. * Get the progress of the current constant set from the progress file
  502. *
  503. * When transactions are used, database updates to drupal cannot be made. Thus a separate
  504. * method to keep track of progress was implemented: save a period to the file for each
  505. * record successfully inserted; each line in the file represents a processed line.
  506. *
  507. * @param $job_id
  508. * The id of the tripal job to check the progress of
  509. * @param $node
  510. * The tripal_bulk_loader node associated with the job
  511. *
  512. * @return
  513. * An array with the following keys:
  514. * num_lines = the number of lines in the file processed so far
  515. * total_lines = the total number of lines in the input file
  516. * percent_file = the percent the input file has been loaded
  517. * num_records = the number of records successfully inserted
  518. *
  519. * @ingroup tripal_bulk_loader
  520. */
  521. function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress = TRUE) {
  522. $filename = '/tmp/tripal_bulk_loader_progress-' . $job_id . '.out';
  523. if (!file_exists($filename)) {
  524. return (object) array();
  525. }
  526. $num_lines = trim(`wc --lines < $filename`);
  527. $num_records = trim(`grep -o "." $filename | wc --lines`);
  528. $job = db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
  529. FROM {tripal_jobs} j
  530. LEFT JOIN {tripal_bulk_loader} b ON b.job_id=j.job_id
  531. LEFT JOIN (
  532. SELECT nid, count(distinct(group_id)) as num
  533. FROM {tripal_bulk_loader_constants}
  534. GROUP BY nid
  535. ) c ON c.nid=b.nid
  536. WHERE j.job_id=:job", array(':job' =>$job_id))->execute();
  537. if ($job->num_constant_sets) {
  538. $num_constant_sets_loaded = round($job->progress / (100 / $job->num_constant_sets), 4);
  539. // If the next constant set has started loading
  540. if ($job->num_constant_sets != $num_constant_sets_loaded) {
  541. // total lines in input file
  542. $total_lines = trim(`wc --lines < $job->file`);
  543. if ($job->file_has_header) {
  544. $total_lines--;
  545. }
  546. // percent of the current constant set loaded
  547. $percent = round($num_lines/$total_lines * 100, 2);
  548. // percent of the total job = (<# fully loaded constant sets> * 100 )
  549. // + <percent of current constant set>
  550. // / <total number of constant sets>
  551. $total_percent = (($num_constant_sets_loaded * 100) + $percent) / $job->num_constant_sets;
  552. // update the progress of the job
  553. if ($update_progress AND ($percent != 0 OR $percent != 100)) {
  554. tripal_job_set_progress($job_id, round($total_percent, 0));
  555. }
  556. }
  557. }
  558. return (object) array(
  559. 'num_lines' => $num_lines,
  560. 'total_lines' => $total_lines,
  561. 'percent_file' => $percent,
  562. 'num_constant_sets_loaded' => $num_constant_sets_loaded,
  563. 'total_percent' => $total_percent,
  564. 'num_records' => $num_records
  565. );
  566. }
  567. /**
  568. * Implements hook_job_describe_args()
  569. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  570. *
  571. * @params $callback
  572. * The callback passed into tripal_add_job()
  573. * @param $args
  574. * The arguements passed into tripal_add_job()
  575. * @return
  576. * An array where keys are the human readable headers describing each arguement
  577. * and the value is the aguement passed in after formatting
  578. *
  579. * @ingroup tripal_bulk_loader
  580. */
  581. function tripal_bulk_loader_job_describe_args($callback, $args) {
  582. $new_args = array();
  583. if ($callback == 'tripal_bulk_loader_load_data') {
  584. //1st arg is the nid for a bulk loader node
  585. $node = node_load($args[0]);
  586. $new_args['Bulk Loading Job'] = l($node->title, 'node/' . $args[0]);
  587. return $new_args;
  588. }
  589. }
  590. /**
  591. * Implements hook_coder_ignore().
  592. * Defines the path to the file (tripal_bulk_loader.coder_ignores.txt) where ignore rules for coder are stored
  593. *
  594. * @ingroup tripal_bulk_loader
  595. */
  596. function tripal_bulk_loader_coder_ignore() {
  597. return array(
  598. 'path' => drupal_get_path('module', 'tripal_bulk_loader'),
  599. 'line prefix' => drupal_get_path('module', 'tripal_bulk_loader'),
  600. );
  601. }