tripal_bulk_loader.module 17 KB

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