tripal.importer.api.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for working with
  5. * data file importers using the TripalImporter class.
  6. *
  7. */
  8. /**
  9. * @defgroup tripal_importer_api Data Importing
  10. * @ingroup tripal_api
  11. * @{
  12. * Provides an application programming interface (API) for working with
  13. * data file importers using the TripalImporter class into a chado database.
  14. * @}
  15. *
  16. */
  17. /**
  18. * Implements hook_handle_uploaded_file().
  19. *
  20. * This is a Tripal hook that allows the module to set the proper
  21. * parameters for a file uploaded via the Tripal HTML5 uploader.
  22. *
  23. * @param $file
  24. * The Drupal file object of the newly uploaded file.
  25. * @param $type
  26. * The category or type of file.
  27. *
  28. * @return
  29. * A Drupal managed file ID.
  30. *
  31. * @ingroup tripal_importer_api
  32. */
  33. function hook_handle_uploaded_file($file, $type) {
  34. }
  35. /**
  36. * Implements hook_importer_finish().
  37. *
  38. * This hook is executed before a TripalImporter has started. This allows
  39. * modules to implement specific actions prior to execution.
  40. *
  41. * @param $importer
  42. * The instance of the TripalImporter class that just completed its run.
  43. * @param $job_id
  44. * The job_id
  45. */
  46. function hook_importer_start($importer) {
  47. }
  48. /**
  49. * Implements hook_importer_finish().
  50. *
  51. * This hook is executed once a TripalImporter has completed but it's run
  52. * and post run activities. This allows modules to implement specific actions
  53. * once loaders are completed.
  54. *
  55. * @param $importer
  56. * The instance of the TripalImporter class that just completed its run.
  57. * @param $job_id
  58. * The job_id
  59. */
  60. function hook_importer_finish($importer) {
  61. }
  62. /**
  63. * Retrieves a list of TripalImporter Importers.
  64. *
  65. * The TripalImporter classes can be added by a site developer that wishes
  66. * to create a new data loader. The class file should
  67. * be placed in the [module]/includes/TripalImporter directory. Tripal will
  68. * support any loader as long as it is in this directory and extends the
  69. * TripalImporter class.
  70. *
  71. * @return
  72. * A list of TripalImporter names.
  73. *
  74. * @ingroup tripal_importer_api
  75. */
  76. function tripal_get_importers() {
  77. $importers = array();
  78. $modules = module_list(TRUE);
  79. foreach ($modules as $module) {
  80. // Find all of the files in the tripal_chado/includes/fields directory.
  81. $loader_path = drupal_get_path('module', $module) . '/includes/TripalImporter';
  82. $loader_files = file_scan_directory($loader_path, '/.inc$/');
  83. // Iterate through the fields, include the file and run the info function.
  84. foreach ($loader_files as $file) {
  85. $class = $file->name;
  86. module_load_include('inc', $module, 'includes/TripalImporter/' . $class);
  87. if (class_exists($class) and is_subclass_of($class, 'TripalImporter')) {
  88. $importers[] = $class;
  89. }
  90. }
  91. }
  92. return $importers;
  93. }
  94. /**
  95. * Loads the TripalImporter class file into scope.
  96. *
  97. * @param $class
  98. * The TripalImporter class to include.
  99. *
  100. * @return
  101. * TRUE if the field type class file was found, FALSE otherwise.
  102. *
  103. * @ingroup tripal_importer_api
  104. */
  105. function tripal_load_include_importer_class($class) {
  106. $modules = module_list(TRUE);
  107. foreach ($modules as $module) {
  108. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalImporter/' . $class . '.inc';
  109. if (file_exists($file_path)) {
  110. module_load_include('inc', $module, 'includes/TripalImporter/' . $class);
  111. if (class_exists($class)) {
  112. return TRUE;
  113. }
  114. }
  115. }
  116. return FALSE;
  117. }
  118. /**
  119. * Imports data into the database.
  120. *
  121. * Tripal provides the TripalImporter class to allow site developers to
  122. * create their own data loaders. Site users can then use any data loader
  123. * implemented for the site by submitting the form that comes with the
  124. * TripalImporter impelmentation. This function runs the importer using the
  125. * arguments provided by the user.
  126. *
  127. * @param $import_id
  128. * The ID of the import record.
  129. * @throws Exception
  130. *
  131. * @ingroup tripal_importer_api
  132. */
  133. function tripal_run_importer($import_id, TripalJob $job = NULL) {
  134. $loader = NULL;
  135. $loader = TripalImporter::byID($import_id);
  136. $loader->setJob($job);
  137. $loader->prepareFiles();
  138. print "\nRunning '".$loader::$name."' importer";
  139. print "\nNOTE: Loading of file is performed using a database transaction. \n" .
  140. "If it fails or is terminated prematurely then all insertions and \n" .
  141. "updates are rolled back and will not be found in the database\n\n";
  142. try {
  143. // Call the hook_importer_start functions.
  144. $modules = module_implements('importer_start');
  145. foreach ($modules as $module) {
  146. $function = $module . '_importer_start';
  147. $function($loader);
  148. }
  149. // Run the loader
  150. tripal_run_importer_run($loader, $job);
  151. // Handle the post run.
  152. tripal_run_importer_post_run($loader, $job);
  153. // Call the hook_importer_finish functions.
  154. $modules = module_implements('importer_finish');
  155. foreach ($modules as $module) {
  156. $function = $module . '_importer_finish';
  157. $function($loader);
  158. }
  159. // Check for tables with new cvterms
  160. print "Remapping Chado Controlled vocabularies to Tripal Terms...";
  161. tripal_chado_map_cvterms();
  162. // Check for new fields and notify the user.
  163. tripal_tripal_cron_notification();
  164. // Clear the Drupal cache
  165. //cache_clear_all();
  166. }
  167. catch (Exception $e) {
  168. if ($job) {
  169. $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
  170. }
  171. if ($loader) {
  172. $loader->cleanFile();
  173. }
  174. }
  175. }
  176. /**
  177. * First step of the tripal_run_importer.
  178. *
  179. * @param $loader
  180. * The TripalImporter object.
  181. * @param $job
  182. * The TripalJob object.$this
  183. * @throws Exception
  184. *
  185. * @ingroup tripal_importer_api
  186. */
  187. function tripal_run_importer_run($loader, $job) {
  188. // begin the transaction
  189. $transaction = db_transaction();
  190. try {
  191. $loader->run();
  192. if ($job) {
  193. $job->logMessage("\nDone.\n");
  194. }
  195. // Remove the temp file
  196. if (!empty($details->arguments['file_url'])) {
  197. $loader->logMessage('Removing downloaded file...');
  198. unlink($temp);
  199. }
  200. }
  201. catch (Exception $e) {
  202. // Rollback and re-throw the error.
  203. $transaction->rollback();
  204. throw $e;
  205. }
  206. }
  207. /**
  208. * Second step of the tripal_run_importer.
  209. *
  210. * @param $loader
  211. * The TripalImporter object.
  212. * @param $job
  213. * The TripalJob object.
  214. * @throws Exception
  215. *
  216. * @ingroup tripal_importer_api
  217. */
  218. function tripal_run_importer_post_run($loader, $job) {
  219. // the transaction
  220. $transaction = db_transaction();
  221. try {
  222. $loader->postRun();
  223. }
  224. catch (Exception $e) {
  225. // Rollback and re-throw the error.
  226. $transaction->rollback();
  227. throw $e;
  228. }
  229. }