tripal.importer.api.inc

Provides an application programming interface (API) for working with data file importers using the TripalImporter class.

File

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