tripal_bulk_loader.module 17 KB

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