tripal_bulk_loader.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. // 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+$/', $arg)) {
  216. return $arg;
  217. }
  218. }
  219. /**
  220. * Implements hook_views_api()
  221. *
  222. * Purpose: Essentially this hook tells drupal that there is views support for
  223. * for this module which then includes tripal_views.views.inc where all the
  224. * views integration code is
  225. *
  226. * @ingroup tripal_views
  227. */
  228. function tripal_bulk_loader_views_api() {
  229. return array(
  230. 'api' => 3.0,
  231. );
  232. }
  233. /**
  234. * Implements hook_theme
  235. *
  236. * @ingroup tripal_bulk_loader
  237. */
  238. function tripal_bulk_loader_theme() {
  239. return array(
  240. 'node__tripal_bulk_loader' => array(
  241. 'template' => 'node--tripal-bulk-loader',
  242. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  243. 'render element' => 'node',
  244. 'base hook' => 'node',
  245. ),
  246. 'tripal_bulk_loader_template' => array(
  247. 'variables' => array('template_id' => NULL),
  248. 'template' => 'tripal_bulk_loader_template',
  249. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  250. ),
  251. 'tripal_bulk_loader_modify_template_base_form' => array(
  252. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  253. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  254. 'render element' => 'form'
  255. ),
  256. 'tripal_bulk_loader_field_regex_fieldset' => array(
  257. 'file' => 'theme/tripal_bulk_loader.theme.inc',
  258. 'function' => 'tripal_bulk_loader_field_regex_fieldset',
  259. 'render element' => 'element'
  260. ),
  261. 'tripal_bulk_loader_admin' => array(
  262. 'template' => 'tripal_bulk_loader_admin',
  263. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  264. ),
  265. );
  266. }
  267. /**
  268. * Implements hook_perm
  269. *
  270. * @ingroup tripal_bulk_loader
  271. */
  272. function tripal_bulk_loader_permission() {
  273. return array(
  274. 'access tripal_bulk_loader' => array(
  275. 'title' => t('View Tripal Bulk Loading Jobs'),
  276. 'description' => t('Permission to view Tripal Bulk Loader Nodes')
  277. ),
  278. 'create tripal_bulk_loader' => array(
  279. 'title' => t('Create Tripal Bulk Loading Jobs'),
  280. 'description' => t('Permission to create Tripal Bulk Loader Nodes')
  281. ),
  282. 'edit tripal_bulk_loader' => array(
  283. 'title' => t('Edit Tripal Bulk Loading Jobs'),
  284. 'description' => t('Permission to edit Tripal Bulk Loader Nodes')
  285. ),
  286. 'delete tripal_bulk_loader' => array(
  287. 'title' => t('Delete Tripal Bulk Loading Jobs'),
  288. 'description' => t('Permission to delete Tripal Bulk Loader Nodes')
  289. ),
  290. 'administer tripal_bulk_loader' => array(
  291. 'title' => t('Administrate Tripal Bulk Loader'),
  292. 'description' => t('Permission to administrate the Tripal Bulk Loader including template management.')
  293. ),
  294. );
  295. }
  296. /**
  297. * Get the progress of the current constant set from the progress file
  298. *
  299. * When transactions are used, database updates to drupal cannot be made. Thus a separate
  300. * method to keep track of progress was implemented: save a period to the file for each
  301. * record successfully inserted; each line in the file represents a processed line.
  302. *
  303. * @param $job_id
  304. * The id of the tripal job to check the progress of
  305. * @param $node
  306. * The tripal_bulk_loader node associated with the job
  307. *
  308. * @return
  309. * An array with the following keys:
  310. * num_lines = the number of lines in the file processed so far
  311. * total_lines = the total number of lines in the input file
  312. * percent_file = the percent the input file has been loaded
  313. * num_records = the number of records successfully inserted
  314. *
  315. * @ingroup tripal_bulk_loader
  316. */
  317. function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress = TRUE) {
  318. $filename = '/tmp/tripal_bulk_loader_progress-' . $job_id . '.out';
  319. if (!file_exists($filename)) {
  320. return (object) array();
  321. }
  322. $num_lines = trim(`wc --lines < $filename`);
  323. $num_records = trim(`grep -o "." $filename | wc --lines`);
  324. $job = db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
  325. FROM {tripal_jobs} j
  326. LEFT JOIN {tripal_bulk_loader} b ON b.job_id=j.job_id
  327. LEFT JOIN (
  328. SELECT nid, count(distinct(group_id)) as num
  329. FROM {tripal_bulk_loader_constants}
  330. GROUP BY nid
  331. ) c ON c.nid=b.nid
  332. WHERE j.job_id=:job", array(':job' =>$job_id))->execute();
  333. if ($job->num_constant_sets) {
  334. $num_constant_sets_loaded = round($job->progress / (100 / $job->num_constant_sets), 4);
  335. // If the next constant set has started loading
  336. if ($job->num_constant_sets != $num_constant_sets_loaded) {
  337. // total lines in input file
  338. $total_lines = trim(`wc --lines < $job->file`);
  339. if ($job->file_has_header) {
  340. $total_lines--;
  341. }
  342. // percent of the current constant set loaded
  343. $percent = round($num_lines/$total_lines * 100, 2);
  344. // percent of the total job = (<# fully loaded constant sets> * 100 )
  345. // + <percent of current constant set>
  346. // / <total number of constant sets>
  347. $total_percent = (($num_constant_sets_loaded * 100) + $percent) / $job->num_constant_sets;
  348. // update the progress of the job
  349. if ($update_progress AND ($percent != 0 OR $percent != 100)) {
  350. tripal_job_set_progress($job_id, round($total_percent, 0));
  351. }
  352. }
  353. }
  354. return (object) array(
  355. 'num_lines' => $num_lines,
  356. 'total_lines' => $total_lines,
  357. 'percent_file' => $percent,
  358. 'num_constant_sets_loaded' => $num_constant_sets_loaded,
  359. 'total_percent' => $total_percent,
  360. 'num_records' => $num_records
  361. );
  362. }
  363. /**
  364. * Implements hook_job_describe_args()
  365. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  366. *
  367. * @params $callback
  368. * The callback passed into tripal_add_job()
  369. * @param $args
  370. * The arguements passed into tripal_add_job()
  371. * @return
  372. * An array where keys are the human readable headers describing each arguement
  373. * and the value is the aguement passed in after formatting
  374. *
  375. * @ingroup tripal_bulk_loader
  376. */
  377. function tripal_bulk_loader_job_describe_args($callback, $args) {
  378. $new_args = array();
  379. if ($callback == 'tripal_bulk_loader_load_data') {
  380. //1st arg is the nid for a bulk loader node
  381. $node = node_load($args[0]);
  382. $new_args['Bulk Loading Job'] = l($node->title, 'node/' . $args[0]);
  383. return $new_args;
  384. }
  385. }
  386. /**
  387. * Implements hook_coder_ignore().
  388. * Defines the path to the file (tripal_bulk_loader.coder_ignores.txt) where ignore rules for coder are stored
  389. *
  390. * @ingroup tripal_bulk_loader
  391. */
  392. function tripal_bulk_loader_coder_ignore() {
  393. return array(
  394. 'path' => drupal_get_path('module', 'tripal_bulk_loader'),
  395. 'line prefix' => drupal_get_path('module', 'tripal_bulk_loader'),
  396. );
  397. }