tripal_bulk_loader.module 17 KB

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