Эх сурвалжийг харах

Fixed imporater to add a new 'files' argument to support multiple files

Stephen Ficklin 7 жил өмнө
parent
commit
ec5129476f

+ 1 - 1
tripal/api/tripal.importer.api.inc

@@ -100,7 +100,7 @@ function tripal_run_importer($import_id, TripalJob $job = NULL) {
 
     $loader = TripalImporter::byID($import_id);
     $loader->setJob($job);
-    $loader->prepareFile();
+    $loader->prepareFiles();
     $loader->run();
     $loader->cleanFile();
 

+ 94 - 70
tripal/includes/TripalImporter.inc

@@ -267,32 +267,54 @@ class TripalImporter {
       // and the file.
       $arguments = array(
         'run_args' => $run_args,
-        'file' => array(
-          'file_path' => '',
-          'file_local' => '',
-          'file_remote' => '',
-          'fid' => NULL,
-        ),
+        'files' => array(),
       );
 
       // Get the file argument.
       $has_file = 0;
       if (array_key_exists('file_local', $file_details)) {
-        $arguments['file']['file_local'] = $file_details['file_local'];
-        $arguments['file']['file_path'] = $file_details['file_local'];
+        $arguments['files'][] = array(
+          'file_local' => $file_details['file_local'],
+          'file_path' => $file_details['file_local']
+        );
         $has_file++;
       }
       if (array_key_exists('file_remote', $file_details)) {
-        $arguments['file']['file_remote'] = $file_details['file_remote'];
+        $arguments['files'][] = array(
+          'file_remote' => $file_details['file_remote']
+        );
         $has_file++;
       }
       if (array_key_exists('fid', $file_details)) {
-        $fid = $file_details['fid'];
-        $file = file_load($fid);
-        $arguments['file']['file_path'] =  base_path() . drupal_realpath($file->uri);
-        $arguments['file']['fid'] = $fid;
-        $values['fid'] = $fid;
-        $has_file++;
+        $values['fid'] = $file_details['fid'];
+        // Handle multiple file uploads.
+        if (preg_match('/\|/', $file_details['fid'])) {
+          $fids = explode('|', $file_details['fid']);
+          foreach ($fids as $fid) {
+            $file = file_load($fid);
+            $arguments['files'][] = array(
+              'file_path' => base_path() . drupal_realpath($file->uri),
+              'fid' => $fid
+            );
+            $has_file++;
+          }
+        }
+        // Handle a single file.
+        else {
+          $fid = $file_details['fid'];
+          $file = file_load($fid);
+          $arguments['files'][] = array(
+            'file_path' => base_path() . drupal_realpath($file->uri),
+            'fid' => $fid
+          );
+          $has_file++;
+
+          // For backwards compatibility add the old 'file' element.
+          $arguments['file'] = array(
+            'file_path' => base_path() . drupal_realpath($file->uri),
+            'fid' => $fid
+          );
+        }
       }
 
       // Validate the $file_details argument.
@@ -383,7 +405,7 @@ class TripalImporter {
    * This function must be run prior to the run() function to ensure that
    * the import file is ready to go.
    */
-  public function prepareFile() {
+  public function prepareFiles() {
     $class = get_called_class();
 
     // If no file is required then just indicate that all is good to go.
@@ -393,63 +415,65 @@ class TripalImporter {
     }
 
     try {
-      if (!empty($this->arguments['file']['file_remote'])) {
-        $file_remote = $this->arguments['file']['file_remote'];
-        $this->logMessage('Download file: !file_remote...', array('!file_remote' => $file_remote));
-
-        // If this file is compressed then keepthe .gz extension so we can
-        // uncompress it.
-        $ext = '';
-        if (preg_match('/^(.*?)\.gz$/', $this->arguments['file']['file_remote'])) {
-          $ext = '.gz';
-        }
-        // Create a temporary file.
-        $temp = tempnam("temporary://", 'import_') . $ext;
-        $this->logMessage("Saving as: !file", array('!file' => $temp));
-
-        $url_fh = fopen($file_remote, "r");
-        $tmp_fh = fopen($temp, "w");
-        if (!$url_fh) {
-          throw new Exception(t("Unable to download the remote file at %url. Could a firewall be blocking outgoing connections?",
-              array('%url', $file_remote)));
-        }
-
-        // Write the contents of the remote file to the temp file.
-        while (!feof($url_fh)) {
-          fwrite($tmp_fh, fread($url_fh, 255), 255);
-        }
-        // Set the path to the file for the importer to use.
-        $this->arguments['file']['file_path'] = $temp;
-        $this->is_prepared = TRUE;
-      }
-
-      // Is this file compressed?  If so, then uncompress it
-      $matches = array();
-      if (preg_match('/^(.*?)\.gz$/', $this->arguments['file']['file_path'], $matches)) {
-        $this->logMessage("Uncompressing: !file", array('!file' => $this->arguments['file']['file_path']));
-        $buffer_size = 4096;
-        $new_file_path = $matches[1];
-        $gzfile = gzopen($this->arguments['file']['file_path'], 'rb');
-        $out_file = fopen($new_file_path, 'wb');
-        if (!$out_file) {
-          throw new Exception("Cannot uncompress file: new temporary file, '$new_file_path', cannot be created.");
+      for($i = 0; $i < count($this->arguments['file']); $i++) {
+        if (!empty($this->arguments['file'][$i]['file_remote'])) {
+          $file_remote = $this->arguments['file'][$i]['file_remote'];
+          $this->logMessage('Download file: !file_remote...', array('!file_remote' => $file_remote));
+
+          // If this file is compressed then keepthe .gz extension so we can
+          // uncompress it.
+          $ext = '';
+          if (preg_match('/^(.*?)\.gz$/', $this->arguments['file']['file_remote'])) {
+            $ext = '.gz';
+          }
+          // Create a temporary file.
+          $temp = tempnam("temporary://", 'import_') . $ext;
+          $this->logMessage("Saving as: !file", array('!file' => $temp));
+
+          $url_fh = fopen($file_remote, "r");
+          $tmp_fh = fopen($temp, "w");
+          if (!$url_fh) {
+            throw new Exception(t("Unable to download the remote file at %url. Could a firewall be blocking outgoing connections?",
+                array('%url', $file_remote)));
+          }
+
+          // Write the contents of the remote file to the temp file.
+          while (!feof($url_fh)) {
+            fwrite($tmp_fh, fread($url_fh, 255), 255);
+          }
+          // Set the path to the file for the importer to use.
+          $this->arguments['file']['file_path'] = $temp;
+          $this->is_prepared = TRUE;
         }
 
-        // Keep repeating until the end of the input file
-        while (!gzeof($gzfile)) {
-          // Read buffer-size bytes
-          // Both fwrite and gzread and binary-safe
-          fwrite($out_file, gzread($gzfile, $buffer_size));
+        // Is this file compressed?  If so, then uncompress it
+        $matches = array();
+        if (preg_match('/^(.*?)\.gz$/', $this->arguments['file']['file_path'], $matches)) {
+          $this->logMessage("Uncompressing: !file", array('!file' => $this->arguments['file']['file_path']));
+          $buffer_size = 4096;
+          $new_file_path = $matches[1];
+          $gzfile = gzopen($this->arguments['file']['file_path'], 'rb');
+          $out_file = fopen($new_file_path, 'wb');
+          if (!$out_file) {
+            throw new Exception("Cannot uncompress file: new temporary file, '$new_file_path', cannot be created.");
+          }
+
+          // Keep repeating until the end of the input file
+          while (!gzeof($gzfile)) {
+            // Read buffer-size bytes
+            // Both fwrite and gzread and binary-safe
+            fwrite($out_file, gzread($gzfile, $buffer_size));
+          }
+
+          // Files are done, close files
+          fclose($out_file);
+          gzclose($gzfile);
+
+          // Now remove the .gz file and reset the file_path to the new
+          // uncompressed version.
+          unlink($this->arguments['file'][$i]['file_path']);
+          $this->arguments['file'][$i]['file_path'] = $new_file_path;
         }
-
-        // Files are done, close files
-        fclose($out_file);
-        gzclose($gzfile);
-
-        // Now remove the .gz file and reset the file_path to the new
-        // uncompressed version.
-        unlink($this->arguments['file']['file_path']);
-        $this->arguments['file']['file_path'] = $new_file_path;
       }
     }
     catch (Exception $e){

+ 21 - 3
tripal/tripal.install

@@ -331,10 +331,9 @@ function tripal_tripal_import_schema() {
         'not null' => TRUE,
       ),
       'fid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
+        'type' => 'text',
         'not null' => FALSE,
-        'description' => 'The file ID of the file to import. This only applies if the file was uploaded (i.e. not already on the server) and is mangaged by Drupal.'
+        'description' => 'The file IDs of the to import. This only applies if the file was uploaded (i.e. not already on the server) and is mangaged by Drupal. Multiple fids are separated using a | character.'
       ),
       'arguments' => array(
         'type' => 'text',
@@ -917,4 +916,23 @@ function tripal_update_7304() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Could not perform update: '. $error);
   }
+}
+
+/**
+ * Adjusts the tripal_import table to support multiple file uploads.
+ */
+function tripal_update_7305() {
+  $transaction = db_transaction();
+  try {
+    db_change_field('tripal_import', 'fid', 'fid', array(
+      'type' => 'text',
+      'not null' => FALSE,
+      'description' => 'The file IDs of the to import. This only applies if the file was uploaded (i.e. not already on the server) and is mangaged by Drupal. Multiple fids are separated using a | character.'
+    ));
+  }
+  catch (\PDOException $e) {
+    $transaction->rollback();
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: '. $error);
+  }
 }

+ 1 - 1
tripal_chado/includes/TripalImporter/FASTAImporter.inc

@@ -324,7 +324,7 @@ class FASTAImporter extends TripalImporter {
   public function run() {
 
     $arguments = $this->arguments['run_args'];
-    $file_path = $this->arguments['file']['file_path'];
+    $file_path = $this->arguments['files'][0]['file_path'];
 
     $organism_id = $arguments['organism_id'];
     $type = $arguments['seqtype'];

+ 3 - 1
tripal_chado/includes/TripalImporter/GFF3Importer.inc

@@ -43,6 +43,8 @@ class GFF3Importer extends TripalImporter {
    */
   public static $button_text = 'Import GFF3 file';
 
+  public static $cardinality = 0;
+
   /**
    * @see TripalImporter::form()
    */
@@ -297,7 +299,7 @@ class GFF3Importer extends TripalImporter {
   public function run() {
 
     $arguments = $this->arguments['run_args'];
-    $file_path = $this->arguments['file']['file_path'];
+    $file_path = $this->arguments['files'][0]['file_path'];
 
     $organism_id = $arguments['organism_id'];
     $analysis_id = $arguments['analysis_id'];