tripal_bulk_loader.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * @file
  4. * Provides general functions for the tripal bulk loader.
  5. *
  6. * @ingroup tripal_bulk_loader
  7. */
  8. /**
  9. * @defgroup tripal_bulk_loader Tripal Bulk Loader Module
  10. * @ingroup tripal_modules
  11. * @{
  12. * Functions implementing the Tripal Generic tab-delimited chado data loader
  13. * @}
  14. */
  15. // Loader
  16. include('includes/tripal_bulk_loader.loader.inc');
  17. // Node
  18. include('includes/tripal_bulk_loader.chado_node.inc');
  19. include('includes/tripal_bulk_loader.constants.inc');
  20. // Administration
  21. include('includes/tripal_bulk_loader.admin.inc');
  22. include('includes/tripal_bulk_loader.admin.templates.inc');
  23. // API
  24. include('api/tripal_bulk_loader.api.templates.inc');
  25. /**
  26. * Implements hook_init().
  27. * Used to add stylesheets and javascript files to the header
  28. *
  29. * @ingroup tripal_bulk_loader
  30. */
  31. function tripal_bulk_loader_init() {
  32. // Add javascript and style sheet
  33. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_bulk_loader.css');
  34. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_bulk_loader.js');
  35. }
  36. /**
  37. * Implements hook_menu().
  38. *
  39. * @ingroup tripal_bulk_loader
  40. */
  41. function tripal_bulk_loader_menu() {
  42. $items = array();
  43. // Bulk Loading Job Node
  44. $items['node/%node/constants/%/edit'] = array(
  45. 'title' => 'Edit Constant Set',
  46. 'description' => 'Edit a group of constants associated with the current bulk loader',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('tripal_bulk_loader_edit_constant_set_form', 1, 3),
  49. 'access arguments' => array('administer tripal_bulk_loader'),
  50. 'type' => MENU_CALLBACK,
  51. );
  52. $items['node/%node/constants/%/delete'] = array(
  53. 'title' => 'Delete Constant Set',
  54. 'description' => 'Delete a group of constants associated with the current bulk loader',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('tripal_bulk_loader_delete_constant_set_form', 1, 3),
  57. 'access arguments' => array('administer tripal_bulk_loader'),
  58. 'type' => MENU_CALLBACK,
  59. );
  60. // Admin pages -----------------
  61. $items['admin/tripal/loaders/bulk'] = array(
  62. 'title' => 'Bulk Loader',
  63. 'description' => 'Templates for loading tab-delimited data',
  64. 'page callback' => 'tripal_bulk_loader_admin_jobs_listing',
  65. 'access arguments' => array('administer tripal_bulk_loader'),
  66. 'type' => MENU_NORMAL_ITEM,
  67. );
  68. $items['admin/tripal/loaders/bulk/configure'] = array(
  69. 'title' => 'Configure',
  70. 'description' => 'Configuration of global options related to bulk loading jobs',
  71. 'page callback' => 'drupal_get_form',
  72. 'page arguments' => array('tripal_bulk_loader_configuration_form'),
  73. 'access arguments' => array('administer tripal_bulk_loader'),
  74. 'weight' => 8,
  75. 'type' => MENU_LOCAL_TASK,
  76. );
  77. $items['admin/tripal/loaders/bulk/help'] = array(
  78. 'title' => 'Help',
  79. 'description' => "A description of the Tripal Bulk Loader module including a short description of it's usage.",
  80. 'page callback' => 'tripal_bulk_loader_admin_manage_templates',
  81. 'access arguments' => array('administer tripal_bulk_loader'),
  82. 'weight' => 10,
  83. 'type' => MENU_LOCAL_TASK,
  84. );
  85. // Enable View Callbacks
  86. $items['admin/tripal/loaders/bulk/views/jobs/enable'] = array(
  87. 'title' => 'Enable Jobs Administrative View',
  88. 'description' => 'Enable Jobs Administrative View',
  89. 'page callback' => 'tripal_views_admin_enable_view',
  90. 'page arguments' => array('tripal_bulk_loading_jobs','admin/tripal/loaders/bulk'),
  91. 'access arguments' => array('administer tripal_bulk_loader'),
  92. 'type' => MENU_CALLBACK,
  93. );
  94. $items['admin/tripal/loaders/bulk/views/templates/enable'] = array(
  95. 'title' => 'Enable Templates Administrative View',
  96. 'description' => 'Enable Templates Administrative View',
  97. 'page callback' => 'tripal_views_admin_enable_view',
  98. 'page arguments' => array('tripal_bulk_loader_templates', 'admin/tripal/loaders/bulk'),
  99. 'access arguments' => array('administer tripal_bulk_loader'),
  100. 'type' => MENU_CALLBACK,
  101. );
  102. // Create/Edit Template --------
  103. $items['admin/tripal/loaders/bulk/template/create'] = array(
  104. 'title' => 'Create Template',
  105. 'description' => 'Create loader template for loading tab-delimited data',
  106. 'page callback' => 'drupal_get_form',
  107. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'create'),
  108. 'access arguments' => array('administer tripal_bulk_loader'),
  109. 'weight' => -8,
  110. 'type' => MENU_CALLBACK,
  111. );
  112. $items['admin/tripal/loaders/bulk/template/%tblid/edit'] = array(
  113. 'title' => 'Edit Template',
  114. 'description' => 'Edit loader template for loading tab-delimited data',
  115. 'page callback' => 'drupal_get_form',
  116. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit',5),
  117. 'access arguments' => array('administer tripal_bulk_loader'),
  118. 'type' => MENU_CALLBACK,
  119. );
  120. /**
  121. $items['admin/tripal/loaders/bulk/template/edit'] = array(
  122. 'title' => 'Edit Template',
  123. 'description' => 'Edit loader template for loading tab-delimited data',
  124. 'page callback' => 'drupal_get_form',
  125. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit',FALSE),
  126. 'access arguments' => array('administer tripal_bulk_loader'),
  127. 'weight' => -6,
  128. 'type' => MENU_CALLBACK,
  129. );
  130. */
  131. // Add/Edit Record -----
  132. $items['admin/tripal/loaders/bulk/template/%tblid/add_record'] = array(
  133. 'title' => 'Add Template Field',
  134. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  135. 'page callback' => 'drupal_get_form',
  136. 'page arguments' => array('tripal_bulk_loader_template_field_form','create_record',5,FALSE),
  137. 'access arguments' => array('administer tripal_bulk_loader'),
  138. 'type' => MENU_CALLBACK,
  139. );
  140. $items['admin/tripal/loaders/bulk/template/%tblid/edit_record/%tblid'] = array(
  141. 'title' => 'Edit Template Record',
  142. 'description' => 'Edit a record in an existing tripal bulk loader template.',
  143. 'page callback' => 'drupal_get_form',
  144. 'page arguments' => array('tripal_bulk_loader_edit_template_record_form',5,7),
  145. 'access arguments' => array('administer tripal_bulk_loader'),
  146. 'type' => MENU_CALLBACK,
  147. );
  148. $items['admin/tripal/loaders/bulk/template/%tblid/delete_record/%tblid'] = array(
  149. 'title' => 'Delete Template Record',
  150. 'description' => 'Delete a record in an existing tripal bulk loader template.',
  151. 'page callback' => 'drupal_get_form',
  152. 'page arguments' => array('tripal_bulk_loader_delete_template_record_form',5,7),
  153. 'access arguments' => array('administer tripal_bulk_loader'),
  154. 'type' => MENU_CALLBACK,
  155. );
  156. $items['admin/tripal/loaders/bulk/template/%tblid/duplicate_record/%tblid'] = array(
  157. 'title' => 'Duplicate Template Record',
  158. 'description' => 'Duplicate a record in an existing tripal bulk loader template.',
  159. 'page callback' => 'drupal_get_form',
  160. 'page arguments' => array('tripal_bulk_loader_duplicate_template_record_form',5,7),
  161. 'access arguments' => array('administer tripal_bulk_loader'),
  162. 'type' => MENU_CALLBACK,
  163. );
  164. // Add/Edit Field ------
  165. $items['admin/tripal/loaders/bulk/template/%tblid/add_field/%tblid'] = array(
  166. 'title' => 'Add Template Field',
  167. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  168. 'page callback' => 'drupal_get_form',
  169. 'page arguments' => array('tripal_bulk_loader_template_field_form','create',5,7),
  170. 'access arguments' => array('administer tripal_bulk_loader'),
  171. 'type' => MENU_CALLBACK,
  172. );
  173. $items['admin/tripal/loaders/bulk/template/%tblid/edit_field/%tblid/%tblid'] = array(
  174. 'title' => 'Edit Template Field',
  175. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  176. 'page callback' => 'drupal_get_form',
  177. 'page arguments' => array('tripal_bulk_loader_template_field_form','edit',5,7,8),
  178. 'access arguments' => array('administer tripal_bulk_loader'),
  179. 'type' => MENU_CALLBACK,
  180. );
  181. $items['admin/tripal/loaders/bulk/template/%tblid/delete_field/%tblid/%tblid'] = array(
  182. 'title' => 'Delete Template Field',
  183. 'description' => 'Delete an existing field from a tripal bulk loader template.',
  184. 'page callback' => 'drupal_get_form',
  185. 'page arguments' => array('tripal_bulk_loader_delete_template_field_form',5,7,8),
  186. 'access arguments' => array('administer tripal_bulk_loader'),
  187. 'type' => MENU_CALLBACK,
  188. );
  189. // Delete Template -----
  190. $items['admin/tripal/loaders/bulk/template/%tblid/delete'] = array(
  191. 'title' => 'Delete Template',
  192. 'description' => 'Delete bulk loader template',
  193. 'page callback' => 'drupal_get_form',
  194. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form',5),
  195. 'access arguments' => array('administer tripal_bulk_loader'),
  196. 'weight' => -4,
  197. 'type' => MENU_CALLBACK,
  198. );
  199. // Import/Export ---------
  200. $items['admin/tripal/loaders/bulk/templates/import'] = array(
  201. 'title' => 'Import Template',
  202. 'description' => 'Import Loaders',
  203. 'page callback' => 'drupal_get_form',
  204. 'page arguments' => array('tripal_bulk_loader_import_template_form'),
  205. 'access arguments' => array('administer tripal_bulk_loader'),
  206. 'weight' => 2,
  207. 'type' => MENU_CALLBACK,
  208. );
  209. $items['admin/tripal/loaders/bulk/template/%tblid/export'] = array(
  210. 'title' => 'Export Template',
  211. 'description' => 'Export Loaders',
  212. 'page callback' => 'drupal_get_form',
  213. 'page arguments' => array('tripal_bulk_loader_export_template_form', 5),
  214. 'access arguments' => array('administer tripal_bulk_loader'),
  215. 'weight' => 4,
  216. 'type' => MENU_CALLBACK,
  217. );
  218. return $items;
  219. }
  220. /**
  221. * Implements hook_to_arg().
  222. * Ensures the arguement for the bulk loader templates path is correct
  223. *
  224. * @ingroup tripal_bulk_loader
  225. */
  226. function tblid_to_arg($arg, $map, $index) {
  227. if (preg_match('/^(\d+|O)$/', $arg)) {
  228. return $arg;
  229. }
  230. }
  231. /**
  232. * Implements hook_load() for the menu system.
  233. *
  234. * Ensures that a number is passed to the form. We use the letter o in our path instead
  235. * of the number 0 because the drupal menu system has a bug that doesn't allow 0 as
  236. * the only character in the path.
  237. *
  238. * @ingroup tripal_bulk_loader
  239. */
  240. function tblid_load($tblid_id) {
  241. if (preg_match('/O/',$tblid_id)) {
  242. // This ensures that the number 0 is sent to the form as the correct arg
  243. return 0;
  244. }
  245. else {
  246. return $tblid_id;
  247. }
  248. }
  249. /**
  250. * Implements hook_views_api().
  251. *
  252. * Essentially this hook tells drupal that there is views support for
  253. * for this module which then includes tripal_views.views.inc where all the
  254. * views integration code is.
  255. *
  256. * @ingroup tripal_bulk_loader
  257. */
  258. function tripal_bulk_loader_views_api() {
  259. return array(
  260. 'api' => 3.0,
  261. );
  262. }
  263. /**
  264. * Implements hook_theme().
  265. *
  266. * @ingroup tripal_bulk_loader
  267. */
  268. function tripal_bulk_loader_theme() {
  269. return array(
  270. 'node__tripal_bulk_loader' => array(
  271. 'template' => 'node--tripal-bulk-loader',
  272. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  273. 'render element' => 'node',
  274. 'base hook' => 'node',
  275. ),
  276. 'tripal_bulk_loader_template' => array(
  277. 'variables' => array('template_id' => NULL),
  278. 'template' => 'tripal_bulk_loader_template',
  279. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  280. ),
  281. 'tripal_bulk_loader_modify_template_base_form' => array(
  282. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  283. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  284. 'render element' => 'form'
  285. ),
  286. 'tripal_bulk_loader_field_regex_fieldset' => array(
  287. 'file' => 'theme/tripal_bulk_loader.theme.inc',
  288. 'function' => 'tripal_bulk_loader_field_regex_fieldset',
  289. 'render element' => 'element'
  290. ),
  291. 'tripal_bulk_loader_admin' => array(
  292. 'template' => 'tripal_bulk_loader_admin',
  293. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  294. ),
  295. );
  296. }
  297. /**
  298. * Implements hook_permission().
  299. *
  300. * @ingroup tripal_bulk_loader
  301. */
  302. function tripal_bulk_loader_permission() {
  303. return array(
  304. 'access tripal_bulk_loader' => array(
  305. 'title' => t('View Tripal Bulk Loading Jobs'),
  306. 'description' => t('Permission to view Tripal Bulk Loader Nodes')
  307. ),
  308. 'create tripal_bulk_loader' => array(
  309. 'title' => t('Create Tripal Bulk Loading Jobs'),
  310. 'description' => t('Permission to create Tripal Bulk Loader Nodes')
  311. ),
  312. 'edit tripal_bulk_loader' => array(
  313. 'title' => t('Edit Tripal Bulk Loading Jobs'),
  314. 'description' => t('Permission to edit Tripal Bulk Loader Nodes')
  315. ),
  316. 'delete tripal_bulk_loader' => array(
  317. 'title' => t('Delete Tripal Bulk Loading Jobs'),
  318. 'description' => t('Permission to delete Tripal Bulk Loader Nodes')
  319. ),
  320. 'administer tripal_bulk_loader' => array(
  321. 'title' => t('Administrate Tripal Bulk Loader'),
  322. 'description' => t('Permission to administrate the Tripal Bulk Loader including template management.')
  323. ),
  324. );
  325. }
  326. /**
  327. * Get the progress of the current constant set from the progress file
  328. *
  329. * When transactions are used, database updates to drupal cannot be made. Thus a separate
  330. * method to keep track of progress was implemented: save a period to the file for each
  331. * record successfully inserted; each line in the file represents a processed line.
  332. *
  333. * @param $job_id
  334. * The id of the tripal job to check the progress of
  335. * @param $node
  336. * The tripal_bulk_loader node associated with the job
  337. *
  338. * @return
  339. * An array with the following keys:
  340. * num_lines = the number of lines in the file processed so far
  341. * total_lines = the total number of lines in the input file
  342. * percent_file = the percent the input file has been loaded
  343. * num_records = the number of records successfully inserted
  344. *
  345. * @ingroup tripal_bulk_loader
  346. */
  347. function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress = TRUE) {
  348. $filename = '/tmp/tripal_bulk_loader_progress-' . $job_id . '.out';
  349. if (!file_exists($filename)) {
  350. return (object) array();
  351. }
  352. $num_lines = trim(`wc --lines < $filename`);
  353. $num_records = trim(`grep -o "." $filename | wc --lines`);
  354. $job = db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
  355. FROM {tripal_jobs} j
  356. LEFT JOIN {tripal_bulk_loader} b ON b.job_id=j.job_id
  357. LEFT JOIN (
  358. SELECT nid, count(distinct(group_id)) as num
  359. FROM {tripal_bulk_loader_constants}
  360. GROUP BY nid
  361. ) c ON c.nid=b.nid
  362. WHERE j.job_id=:job", array(':job' =>$job_id))->execute();
  363. if ($job->num_constant_sets) {
  364. $num_constant_sets_loaded = round($job->progress / (100 / $job->num_constant_sets), 4);
  365. // If the next constant set has started loading
  366. if ($job->num_constant_sets != $num_constant_sets_loaded) {
  367. // total lines in input file
  368. $total_lines = trim(`wc --lines < $job->file`);
  369. if ($job->file_has_header) {
  370. $total_lines--;
  371. }
  372. // percent of the current constant set loaded
  373. $percent = round($num_lines/$total_lines * 100, 2);
  374. // percent of the total job = (<# fully loaded constant sets> * 100 )
  375. // + <percent of current constant set>
  376. // / <total number of constant sets>
  377. $total_percent = (($num_constant_sets_loaded * 100) + $percent) / $job->num_constant_sets;
  378. // update the progress of the job
  379. if ($update_progress AND ($percent != 0 OR $percent != 100)) {
  380. tripal_job_set_progress($job_id, round($total_percent, 0));
  381. }
  382. }
  383. }
  384. return (object) array(
  385. 'num_lines' => $num_lines,
  386. 'total_lines' => $total_lines,
  387. 'percent_file' => $percent,
  388. 'num_constant_sets_loaded' => $num_constant_sets_loaded,
  389. 'total_percent' => $total_percent,
  390. 'num_records' => $num_records
  391. );
  392. }
  393. /**
  394. * Implements hook_job_describe_args()
  395. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  396. *
  397. * @params $callback
  398. * The callback passed into tripal_add_job()
  399. * @param $args
  400. * The arguements passed into tripal_add_job()
  401. * @return
  402. * An array where keys are the human readable headers describing each arguement
  403. * and the value is the aguement passed in after formatting
  404. *
  405. * @ingroup tripal_bulk_loader
  406. */
  407. function tripal_bulk_loader_job_describe_args($callback, $args) {
  408. $new_args = array();
  409. if ($callback == 'tripal_bulk_loader_load_data') {
  410. //1st arg is the nid for a bulk loader node
  411. $node = node_load($args[0]);
  412. $new_args['Bulk Loading Job'] = l($node->title, 'node/' . $args[0]);
  413. return $new_args;
  414. }
  415. }
  416. /**
  417. * Implements hook_coder_ignore().
  418. * Defines the path to the file (tripal_bulk_loader.coder_ignores.txt) where ignore rules for coder are stored
  419. *
  420. * @ingroup tripal_bulk_loader
  421. */
  422. function tripal_bulk_loader_coder_ignore() {
  423. return array(
  424. 'path' => drupal_get_path('module', 'tripal_bulk_loader'),
  425. 'line prefix' => drupal_get_path('module', 'tripal_bulk_loader'),
  426. );
  427. }