tripal.importer.api.inc 5.4 KB

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