ソースを参照

Pub module importing is done. Added new functionality to submit an importer for a single job execution

Stephen Ficklin 11 年 前
コミット
1f580a445e

+ 2 - 2
tripal_core/api/tripal_core_chado.api.inc

@@ -2045,7 +2045,7 @@ function tripal_core_exclude_field_from_feature_by_default() {
 function chado_pager_query($query, $args, $limit, $element, $count_query = '') {
   
   // get the page and offset for the pager
-  $page = pager_find_page($element);
+  $page = isset($_GET['page']) ? $_GET['page'] : '0';
   $offset = $limit * $page;
 
   // Construct a count query if none was given.
@@ -2109,7 +2109,7 @@ function chado_query($sql, $args = array()) {
   if ($is_local) {
     $sql = preg_replace('/\n/', '', $sql);  // remove carriage returns
     $sql = preg_replace('/\{(.*?)\}/', 'chado.$1', $sql);
-    
+
     // the featureloc table has some indexes that use function that call other functions
     // and those calls do not reference a schema, therefore, any tables with featureloc
     // must automaticaly have the chado schema set as active to find 

+ 240 - 178
tripal_pub/api/tripal_pub.api.inc

@@ -99,92 +99,148 @@ function tripal_pub_get_raw_data($dbxref) {
  */
 function tripal_pub_update_publications($do_contact = FALSE, $dbxref = NULL, $db = NULL) {
 
-  // get a persistent connection
-  $connection = tripal_db_persistent_chado();
-  if (!$connection) {
-    print "A persistant connection was not obtained. Loading will be slow\n";
-  }
-
-  // if we cannot get a connection then let the user know the loading will be slow
-  tripal_db_start_transaction();
-  if ($connection) {
-    print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
-           "If the load fails or is terminated prematurely then the entire set of \n" .
-           "insertions/updates is rolled back and will not be found in the database\n\n";
-  }
+  print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
+      "If the load fails or is terminated prematurely then the entire set of \n" .
+      "insertions/updates is rolled back and will not be found in the database\n\n";
+  $transaction = db_transaction();
+  try {
 
-  // get a list of all publications by their Dbxrefs that have supported databases
-  $sql = "
-    SELECT DB.name as db_name, DBX.accession
-    FROM pub P
-      INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
-      INNER JOIN dbxref DBX      ON DBX.dbxref_id = PDBX.dbxref_id
-      INNER JOIN db DB           ON DB.db_id = DBX.db_id
-  "; 
-  $args = array();
-  if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
-    $dbname = $matches[1];
-    $accession = $matches[2];
-    $sql .= "WHERE DBX.accession = :accession and DB.name = :dbname ";
-    $args[':accession'] = $accession;
-    $args[':dbname'] = $dbname;
+    // get a list of all publications by their Dbxrefs that have supported databases
+    $sql = "
+      SELECT DB.name as db_name, DBX.accession
+      FROM pub P
+        INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
+        INNER JOIN dbxref DBX      ON DBX.dbxref_id = PDBX.dbxref_id
+        INNER JOIN db DB           ON DB.db_id = DBX.db_id
+    "; 
+    $args = array();
+    if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
+      $dbname = $matches[1];
+      $accession = $matches[2];
+      $sql .= "WHERE DBX.accession = :accession and DB.name = :dbname ";
+      $args[':accession'] = $accession;
+      $args[':dbname'] = $dbname;
+    }
+    elseif ($db) {
+      $sql .= " WHERE DB.name = :dbname ";
+      $args[':dbname'] = $db;
+    }
+    $sql .= "ORDER BY DB.name, P.pub_id";
+    $results = chado_query($sql, $args);
+  
+    $num_to_retrieve = 100;
+    $i = 0;                 // count the number of IDs. When we hit $num_to_retrieve we'll do the query
+    $curr_db = '';          // keeps track of the current current database
+    $ids = array();         // the list of IDs for the database
+    $search = array();      // the search array passed to the search function
+  
+    // iterate through the pub IDs
+    while ($pub = $results->fetchObject()) {
+      $accession = $pub->accession;
+      $remote_db = $pub->db_name;
+  
+      // here we need to only update publications for databases we support
+      $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
+      if(!in_array($remote_db, $supported_dbs)) {
+        continue;
+      }
+      $search = array(
+        'num_criteria' => 1,
+        'remote_db' => $remote_db,
+        'criteria' => array(
+          '1' => array(
+            'search_terms' => "$remote_db:$accession",
+            'scope' => 'id',
+            'operation' => '',
+            'is_phrase' => 0,
+          ),
+        ),
+      );
+      $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
+      tripal_pub_add_publications($pubs, $do_contact, TRUE);
+  
+      $i++;
+    }
+  
+    // sync the newly added publications with Drupal
+    print "Syncing publications with Drupal...\n";
+    tripal_pub_sync_pubs();
+  
+    // if the caller wants to create contacts then we should sync them
+    if ($do_contact) {
+      print "Syncing contacts with Drupal...\n";
+      tripal_contact_sync_contacts();
+    }
   }
-  elseif ($db) {
-    $sql .= " WHERE DB.name = :dbname ";
-    $args[':dbname'] = $db;
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    watchdog_exception('T_pub_import', $e);
+    $transaction->rollback();
+    print "FAILED: Rolling back database changes...\n";
+    return;
   }
-  $sql .= "ORDER BY DB.name, P.pub_id";
-  $results = chado_query($sql, $args);
-
-  $num_to_retrieve = 100;
-  $i = 0;                 // count the number of IDs. When we hit $num_to_retrieve we'll do the query
-  $curr_db = '';          // keeps track of the current current database
-  $ids = array();         // the list of IDs for the database
-  $search = array();      // the search array passed to the search function
-
-  // iterate through the pub IDs
-  while ($pub = $results->fetchObject()) {
-    $accession = $pub->accession;
-    $remote_db = $pub->db_name;
-
-    // here we need to only update publications for databases we support
-    $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
-    if(!in_array($remote_db, $supported_dbs)) {
-      continue;
-    }
-    $search = array(
-      'num_criteria' => 1,
-      'remote_db' => $remote_db,
-      'criteria' => array(
-        '1' => array(
-          'search_terms' => "$remote_db:$accession",
-          'scope' => 'id',
-          'operation' => '',
-          'is_phrase' => 0,
-        ),
-      ),
-    );
-    $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
-    tripal_pub_add_publications($pubs, $do_contact, TRUE);
+  print "Done.\n";
+}
+/**
+ *
+ * @param $pub_importer_id
+ */
+function tripal_pub_import_publications_by_import_id($import_id, $job_id = NULL) {
+  print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
+        "If the load fails or is terminated prematurely then the entire set of \n" .
+        "insertions/updates is rolled back and will not be found in the database\n\n";
 
-    $i++;
-  }
+  // start the transaction
+  $transaction = db_transaction();
 
-  // transaction is complete
-  tripal_db_commit_transaction();
+  try {
+    $page = 0;
+    $do_contact = FALSE;
+    $num_to_retrieve = 100;
 
-  print "Transaction Complete\n";
+    // get all of the loaders
+    $args = array(':import_id' => $import_id);
+    $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :import_id ";
+    $import = db_query($sql, $args)->fetchObject();
 
-  // sync the newly added publications with Drupal
-  print "Syncing publications with Drupal...\n";
-  tripal_pub_sync_pubs();
+    print "Importing: " . $import->name . "\n";
 
-  // if the caller wants to create contacts then we should sync them
-  if ($do_contact) {
-    print "Syncing contacts with Drupal...\n";
-    tripal_contact_sync_contacts();
+    $criteria = unserialize($import->criteria);
+    $remote_db = $criteria['remote_db'];
+    $total_pubs = 0;
+    do {
+      // retrieve the pubs for this page. We'll retreive 100 at a time
+      $results  = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
+      $pubs     = $results['pubs'];
+      $num_pubs = $rseults['total_records'];
+      $total_pubs += $num_pubs;
+      tripal_pub_add_publications($pubs, $import->do_contact);
+      $page++;
+    }
+    // continue looping until we have a $pubs array that does not have
+    // our requested numer of records.  This means we've hit the end
+    while (count($pubs) == $num_to_retrieve);
+
+    // sync the newly added publications with Drupal. If the user
+    // requested a report then we don't want to print any syncing information
+    // so pass 'FALSE' to the sync call
+    print "Syncing publications with Drupal...\n";
+    tripal_pub_sync_pubs();
+
+    // if any of the importers wanted to create contacts from the authors then sync them
+    if($import->do_contact) {
+      print "Syncing contacts with Drupal...\n";
+      tripal_contact_sync_contacts();
+    }
+    tripal_job_set_progress($job_id, '100');
+  }
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    watchdog_exception('T_pub_import', $e);
+    $transaction->rollback();
+    print "FAILED: Rolling back database changes...\n";
+    return;
   }
-
   print "Done.\n";
 }
 /*
@@ -194,6 +250,10 @@ function tripal_pub_import_publications($report_email = FALSE, $do_update = FALS
   $num_to_retrieve = 100;
   $page = 0;
   
+  print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
+      "If the load fails or is terminated prematurely then the entire set of \n" .
+      "insertions/updates is rolled back and will not be found in the database\n\n";
+  
   // start the transaction
   $transaction = db_transaction();
   
@@ -224,53 +284,51 @@ function tripal_pub_import_publications($report_email = FALSE, $do_update = FALS
       // our requested numer of records.  This means we've hit the end
       while (count($pubs) == $num_to_retrieve);
     }
+    
+    // sync the newly added publications with Drupal. If the user
+    // requested a report then we don't want to print any syncing information
+    // so pass 'FALSE' to the sync call
+    print "Syncing publications with Drupal...\n";
+    tripal_pub_sync_pubs();
+  
+    // iterate through each of the reports and generate a final report with HTML links
+    $HTML_report = '';
+    if ($report_email) {
+      $HTML_report .= "<html>";
+      global $base_url;
+      foreach ($reports as $importer => $report) {
+        $total = count($report['inserted']);
+        $HTML_report .= "<b>$total new publications from importer: $importer</b><br><ol>\n";
+        foreach ($report['inserted'] as $pub) {
+          $item = $pub['Title'];
+          if (array_key_exists('pub_id', $pub)) {
+            $item = l($pub['Title'], "$base_url/pub/" . $pub['pub_id']);
+          }
+          $HTML_report .= "<li>$item</li>\n";
+        }
+        $HTML_report .= "</ol>\n";
+      }
+      $HTML_report .= "</html>";
+      $site_email = variable_get('site_mail', '');
+      $params = array(
+        'message' => $HTML_report
+      );
+      drupal_mail('tripal_pub', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
+    }
+  
+    // if any of the importers wanted to create contacts from the authors then sync them
+    if($do_contact) {
+      print "Syncing contacts with Drupal...\n";
+      tripal_contact_sync_contacts();
+    }
   }
   catch (Exception $e) {
     print "\n"; // make sure we start errors on new line
     watchdog_exception('T_pub_import', $e);
-    $transaction->rollback();
-    print "FAILED: Rolling back database changes...\n";
+      $transaction->rollback();
+      print "FAILED: Rolling back database changes...\n";
     return;
   }
-  print "Transaction Complete\n";
-
-  // sync the newly added publications with Drupal. If the user
-  // requested a report then we don't want to print any syncing information
-  // so pass 'FALSE' to the sync call
-  print "Syncing publications with Drupal...\n";
-  tripal_pub_sync_pubs();
-
-  // iterate through each of the reports and generate a final report with HTML links
-  $HTML_report = '';
-  if ($report_email) {
-    $HTML_report .= "<html>";
-    global $base_url;
-    foreach ($reports as $importer => $report) {
-      $total = count($report['inserted']);
-      $HTML_report .= "<b>$total new publications from importer: $importer</b><br><ol>\n";
-      foreach ($report['inserted'] as $pub) {
-        $item = $pub['Title'];
-        if (array_key_exists('pub_id', $pub)) {
-          $item = l($pub['Title'], "$base_url/pub/" . $pub['pub_id']);
-        }
-        $HTML_report .= "<li>$item</li>\n";
-      }
-      $HTML_report .= "</ol>\n";
-    }
-    $HTML_report .= "</html>";
-    $site_email = variable_get('site_mail', '');
-    $params = array(
-      'message' => $HTML_report
-    );
-    drupal_mail('tripal_pub', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
-  }
-
-  // if any of the importers wanted to create contacts from the authors then sync them
-  if($do_contact) {
-    print "Syncing contacts with Drupal...\n";
-    tripal_contact_sync_contacts();
-  }
-   
   print "Done.\n";
 }
 /*
@@ -282,61 +340,61 @@ function tripal_pub_import_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_updat
   $page = 0;
   $num_pubs = 0;
 
-  // get a persistent connection
-  $connection = tripal_db_persistent_chado();
-  if (!$connection) {
-    print "A persistant connection was not obtained. Loading will be slow\n";
-  }
-
-  // if we cannot get a connection then let the user know the loading will be slow
-  tripal_db_start_transaction();
-  if ($connection) {
-    print "\nNOTE: Loading of the publication is performed using a database transaction. \n" .
-           "If the load fails or is terminated prematurely then the entire set of \n" .
-           "insertions/updates is rolled back and will not be found in the database\n\n";
-  }
-
-  if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
-    $dbname = $matches[1];
-    $accession = $matches[2];
-
-    $criteria = array(
-      'num_criteria' => 1,
-      'remote_db' => $dbname,
-      'criteria' => array(
-        '1' => array(
-          'search_terms' => "$dbname:$accession",
-          'scope' => 'id',
-          'operation' => '',
-          'is_phrase' => 0,
+  print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
+      "If the load fails or is terminated prematurely then the entire set of \n" .
+      "insertions/updates is rolled back and will not be found in the database\n\n";
+  
+  $transaction = db_transaction();
+  try {
+    if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
+      $dbname = $matches[1];
+      $accession = $matches[2];
+  
+      $criteria = array(
+        'num_criteria' => 1,
+        'remote_db' => $dbname,
+        'criteria' => array(
+          '1' => array(
+            'search_terms' => "$dbname:$accession",
+            'scope' => 'id',
+            'operation' => '',
+            'is_phrase' => 0,
+          ),
         ),
-      ),
-    );
-    $remote_db = $criteria['remote_db'];
-    $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
-    $pub_id = tripal_pub_add_publications($pubs, $do_contact, $do_update);
+      );
+      $remote_db = $criteria['remote_db'];
+      $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
+      $pubs          = $results['pubs'];
+      $search_str    = $results['search_str'];
+      $total_records = $results['total_records'];
+      $pub_id = tripal_pub_add_publications($pubs, $do_contact, $do_update);
+    }
 
+    // sync the newly added publications with Drupal
+    print "Syncing publications with Drupal...\n";
+    tripal_pub_sync_pubs();
+  
+    // if any of the importers wanted to create contacts from the authors then sync them
+    if($do_contact) {
+      print "Syncing contacts with Drupal...\n";
+      tripal_contact_sync_contacts();
+    }
   }
-
-  // transaction is complete
-  tripal_db_commit_transaction();
-
-  print "Transaction Complete\n";
-
-  // sync the newly added publications with Drupal
-  print "Syncing publications with Drupal...\n";
-  tripal_pub_sync_pubs();
-
-  // if any of the importers wanted to create contacts from the authors then sync them
-  if($do_contact) {
-    print "Syncing contacts with Drupal...\n";
-    tripal_contact_sync_contacts();
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    watchdog_exception('T_pub_import', $e);
+        $transaction->rollback();
+        print "FAILED: Rolling back database changes...\n";
+      return;
   }
    
   print "Done.\n";
 }
-/*
- *
+/**
+ * 
+ * @param $pubs
+ * @param $do_contact
+ * @param $update
  */
 function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
   $report = array();
@@ -344,7 +402,7 @@ function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
   $report['inserted'] = array();
   $report['skipped'] = array();
   $total_pubs = count($pubs);
-
+  
   // iterate through the publications and add each one
   $i = 1;
   foreach ($pubs as $pub) {
@@ -614,18 +672,21 @@ function tripal_pub_get_pub_by_uniquename($name) {
  *
  */
 function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE, $update_if_exists = FALSE) {
-
   $pub_id = 0;
+  
+  if (!is_array($pub_details)) {
+    return FALSE;
+  }
 
   // first try to find the publication using the accession number. It will have
   // one if the pub has already been loaded for the publication database
-  if ($pub_details['Publication Dbxref']) {
+  if (array_key_exists('Publication Dbxref', $pub_details)) {
     $results = tripal_pub_get_pubs_by_dbxref($pub_details['Publication Dbxref']);
     if(count($results) == 1) {
       $pub_id = $results[0];
       if ($pub_id and !$update_if_exists) {
-        watchdog('tripal_pub', "A publication with this Dbxref already exists... Skipping: %dbxref",
-        array('%dbxref' => $pub_details['Publication Dbxref']), WATCHDOG_WARNING);
+        //watchdog('tripal_pub', "A publication with this Dbxref already exists... Skipping: %dbxref",
+        //array('%dbxref' => $pub_details['Publication Dbxref']), WATCHDOG_WARNING);
         $action = 'skipped';
         return $pub_id;
       }
@@ -641,7 +702,7 @@ function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE,
   // if we couldn't find a publication by the accession (which means it doesn't
   // yet exist or it has been added using a different publication database) then
   // try to find it using the title and publication year.
-  if (!$pub_id and $pub_details['Title']) {
+  if (!$pub_id and array_key_exists('Title', $pub_details)) {
 
     $results = tripal_pub_get_pubs_by_title_type_pyear_series($pub_details['Title'], NULL, $pub_details['Year']);
     if (count($results) == 1) {
@@ -662,13 +723,14 @@ function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE,
       return FALSE;
     }
   }
-
   // get the publication type (use the first publication type, any others will get stored as properties)
-  if (is_array($pub_details['Publication Type'])) {
-    $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
-  }
-  elseif ($pub_details['Publication Type']) {
-    $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'], NULL, 'tripal_pub');
+  if (array_key_exists('Publication Type', $pub_details)) {
+    if(is_array($pub_details['Publication Type'])) {
+      $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
+    }
+    else {
+      $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'], NULL, 'tripal_pub');
+    }
   }
   else {
     watchdog('tripal_pub', "The Publication Type is a required property but is missing", array(), WATCHDOG_ERROR);

+ 41 - 8
tripal_pub/includes/pub_importers.inc

@@ -25,7 +25,11 @@ function tripal_pub_importers_list() {
     }
 
     $rows[] = array(
-      l(t('Edit/Test'), "admin/tripal/chado/tripal_pub/import/edit/$importer->pub_import_id"),
+      array(
+        'data' => l(t('Edit/Test'), "admin/tripal/chado/tripal_pub/import/edit/$importer->pub_import_id") . '<br>' .
+                  l(t('Import Pubs'), "admin/tripal/chado/tripal_pub/import/submit/$importer->pub_import_id"),
+        'nowrap' => 'nowrap'
+      ),
       $importer->name,
       $criteria['remote_db'],
       $criteria_str,
@@ -45,9 +49,15 @@ function tripal_pub_importers_list() {
      to query a remote database, find publications that match the specified criteria 
      and then import those publications into the Chado database. An example use case would
      be to peridocially add new publications to this Tripal site that have appeared in PubMed
-     in the last 30 days.  See the " . 
+     in the last 30 days.  You can import publications in one of two ways:
+     <ol>
+      <li>Create a new importer by clicking the 'New Importer' link above, and after saving it should appear in the list below.  Click the
+          link labeled 'Import Pubs' to schedule a job to import the publications</li>
+      <li>The first method only performs the import once.  However, you can schedule the 
+          importer to run peridically by adding a cron job. See the " . 
      l("Pub Module help instructions", "admin/tripal/chado/tripal_pub/help") . " to learn how to 
-     set the importers to run automatically.") . '</p>';
+     set the importers to run automatically.") . '</li>
+     </ol><br>';
 
   $page .= theme('table', array('header' => $header, 'rows' => $rows));
   
@@ -232,6 +242,12 @@ function tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_
     $do_contact   = $_SESSION['tripal_pub_import']['do_contact'];
     $num_criteria = $_SESSION['tripal_pub_import']['num_criteria'];
     $loader_name  = $_SESSION['tripal_pub_import']['loader_name'];
+    
+    // check if the pub_import_id in the session variable is not the same as the one we've been provided
+    // if so, then clear the session variable
+    if ($pub_import_id and $pub_import_id != $_SESSION['tripal_pub_import']['pub_import_id']) {
+      unset($_SESSION['tripal_pub_import']);
+    }
   }
   
   // if we are re constructing the form from a failed validation or ajax callback
@@ -267,11 +283,7 @@ function tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_
     $num_criteria--;
   }
   
-  // check if the pub_import_id in the session variable is not the same as the one we've been provided
-  // if so, then clear the session variable
-  if ($pub_import_id and $pub_import_id != $_SESSION['tripal_pub_import']['pub_import_id']) {
-    unset($_SESSION['tripal_pub_import']);
-  }
+
   
   // set the values we need for later but that should not be shown on the form
   $form['num_criteria']= array(
@@ -732,3 +744,24 @@ function theme_tripal_pub_importer_setup_form_elements($variables) {
   
   return drupal_render($form);
 }
+
+/**
+ * Add a job to import publications
+ * 
+ * @param $pub_importer_id
+ */
+function tripal_pub_importer_submit_job($import_id) {
+  global $user;
+  
+  // get all of the loaders
+  $args = array(':import_id' => $import_id);
+  $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :import_id ";
+  $import = db_query($sql, $args)->fetchObject();
+  
+  $args = array($import_id);
+  tripal_add_job("Import publications $import->name", 'tripal_pub',
+    'tripal_pub_import_publications_by_import_id', $args, $user->uid);
+  
+  drupal_goto('admin/tripal/chado/tripal_pub/import_list');
+}
+

+ 71 - 57
tripal_pub/includes/pub_search.inc

@@ -3,9 +3,7 @@
  *
  */
 function tripal_pub_search_page() {
-  global $pager_total, $pager_total_items;
 
-  $pager_id = 0;
   $limit = 25;
 
   // generate the search form
@@ -31,22 +29,10 @@ function tripal_pub_search_page() {
     }
 
     // get the list of publications from the remote database using the search criteria.
-    $pubs = tripal_pub_get_search_results($search_array, $limit, $pager_id);
-
-    // generate the pager
+    $pubs = tripal_pub_get_search_results($search_array, $limit);
     $page = isset($_GET['page']) ? $_GET['page'] : '0';
     $total_records = $_SESSION['chado_pager'][0]['total_records'];
     $total_pages = (int) ($total_records / $limit) + 1;
-        
-    // generate the pager
-    pager_default_initialize($total_records, $limit);
-    $pager = array(
-      'tags' => array(),
-      'element' => 0,
-      'parameters' => array(),
-      'quantity' => $limit,
-    );
-    $pager = theme_pager($pager);
 
     // iterate through the results and construct the table displaying the publications
     $rows = array();
@@ -57,7 +43,7 @@ function tripal_pub_search_page() {
         'pub_id' => $pub->pub_id, 
         'type_id' => array(
           'name' => 'Citation',
-      ),
+        ),
       );
       $citation_rec = tripal_core_generate_chado_var('pubprop', $values);
       $citation_rec = tripal_core_expand_chado_vars($citation_rec, 'field', 'pubprop.value');
@@ -79,15 +65,42 @@ function tripal_pub_search_page() {
       );
       $i++;
     }
+    
+    if (count($rows) == 0) {
+      $rows[] = array(
+        array(
+          'data' => 'No results found',
+          'colspan' => 3
+        )
+      );
+    }
 
     $headers = array('', 'Year', 'Publication');
-    $table = theme('table', $headers, $rows);
+    $table = array(
+      'header' => $headers,
+      'rows' => $rows,
+      'attributes' => array('id' => 'tripal-pub-search-results-table', 'border' => '0'),
+      'sticky' => TRUE,
+      'caption' => '',
+      'colgroups' => array(),
+      'empty' => '',
+    );
+    $results = theme_table($table);
     
+    // generate the pager
+    pager_default_initialize($total_records, $limit);
+    $pager = array(
+      'tags' => array(),
+      'element' => 0,
+      'parameters' => array(),
+      'quantity' => $limit,
+    );
+    $pager = theme_pager($pager);
     
     // join all to form the results
     $output .= "<br><p><b>Found " . number_format($total_records) .
       ". Page " . ($page + 1) . " of $total_pages. " .
-      " Results</b></br>" . $table . '</p>' . $pager;    
+      " Results</b></br>" . $results . $pager;    
   }
   return $output;
 }
@@ -145,6 +158,17 @@ function tripal_pub_search_form($form, &$form_state) {
     '#type'          => 'hidden',
     '#default_value' => $num_criteria,
   );
+  
+  if (user_access('access administration pages')) {
+    $form['admin-instructions'] = array(
+      '#prefix' => '<div class="tripal-no-results">',
+      '#markup'  =>  t('Administrators, you can select the fields with which a user can use to search,
+          by checking the desired fields on the ' .
+          l('Publication Module Settings Page', 'admin/tripal/chado/tripal_pub/configuration'). ' in
+         the section titled "Search Options".  The selected fields will appear in the dropdowns below.'),
+      '#suffix' => '</div>'
+    );
+  }
   $form['instructions'] = array(
     '#markup'  =>  t('To search for publications enter keywords in the text boxes below.  
         You can limit your search by selecting the field in the dropdown box. Click the 
@@ -433,10 +457,10 @@ function tripal_pub_search_page_update_criteria($action, $i) {
   )
   );
 }
-/*
+/**
  *
  */
-function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
+function tripal_pub_get_search_results($search_array, $limit) {
 
   // build the SQL based on the criteria provided by the user
   $select = "SELECT DISTINCT P.*, CP.nid ";
@@ -476,17 +500,6 @@ function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
       $op = 'AND';
     }
 
-    $action = "= lower('%s')";
-    if($mode == 'Contains') {
-      $action = 'LIKE lower(\'%%' . ":crit$i" . '%%\')';
-    }
-    if($mode == 'Starts With') {
-      $action = '= lower(\'%%' . ":crit$i" . '\')';
-    }
-    if($mode == 'Ends With') {
-      $action = '= lower(\'' . ":crit$i" . '%%\')';
-    }
-     
     // get the scope type
     $values = array('cvterm_id' => $type_id);
     $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
@@ -495,54 +508,55 @@ function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
       $type_name = $cvterm[0]->name;
     }
     if ($type_name == 'Title') {
-      $where .= " $op (lower(P.title)  $action) ";
-      $args[":crit$i"] = $value;
+      $where .= " $op (lower(P.title) LIKE lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
     }
     elseif ($type_name == 'Year') {
-      $where .= " $op (lower(P.pyear)  $action) ";
-      $args[":crit$i"] = $value;
+      $where .= " $op (lower(P.pyear) = lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
     }
     elseif ($type_name == 'Volume') {
-      $where .= " $op (lower(P.volume)  $action) ";
-      $args[":crit$i"] = $value;
+      $where .= " $op (lower(P.volume) = lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
     }
     elseif ($type_name == 'Issue') {
-      $where .= " $op (lower(P.issue)  $action)";
-      $args[":crit$i"] = $value;
+      $where .= " $op (lower(P.issue) = lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
     }
     elseif ($type_name == 'Journal Name') {
       $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
-      $where .= " $op ((lower(P.series_name) $action and CVT.name = 'Journal Article') OR
-                       (lower(PP$i.value) $action)) ";
+      $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Journal Article') OR
+                       (lower(PP$i.value) = lower(:crit$i))) ";
       $args[":crit$i"] = $type_id;
     }
     elseif ($type_name == 'Conference Name') {
       $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
-      $where .= " $op ((lower(P.series_name) $action and CVT.name = 'Conference Proceedings') OR
-                       (lower(PP$i.value) $action)) ";
+      $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Conference Proceedings') OR
+                       (lower(PP$i.value) = lower(:crit$i))) ";
       $args[":crit$i"] = $type_id;
     }
     elseif ($type_name == 'Publication Type') {
-      $where .= " $op (lower(CVT.name) $action)";
+      $where .= " $op (lower(CVT.name) = lower(:crit$i))";
       $args[":crit$i"] = $value;
     }
     elseif ($type_id == 0) { //'Any Field'
       $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
-      $where .= " $op (lower(PP$i.value)  $action OR
-                       lower(P.title) $action OR 
-                       lower(P.volumetitle) $action OR
-                       lower(P.publisher) $action OR
-                       lower(P.uniquename) $action OR
-                       lower(P.pubplace) $action OR
-                       lower(P.miniref) $action OR
-                       lower(P.series_name) $action) ";
-      $args[":crit$i"] = $value;
+      $where .= " $op (lower(PP$i.value)  LIKE lower(:crit$i) OR
+                       lower(P.title) LIKE lower(:crit$i) OR 
+                       lower(P.volumetitle) LIKE lower(:crit$i) OR
+                       lower(P.publisher) LIKE lower(:crit$i) OR
+                       lower(P.uniquename) LIKE lower(:crit$i) OR
+                       lower(P.pubplace) LIKE lower(:crit$i) OR
+                       lower(P.miniref) LIKE lower(:crit$i) OR
+                       lower(P.series_name) LIKE lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
     }
     // for all other properties
     else {
-      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
-      $where .= " $op (lower(PP$i.value) $action) ";
-      $args[":crit$i"] = $type_id;
+      $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :type_id$i ";
+      $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i)) ";
+      $args[":crit$i"] = '%' . $value . '%';
+      $args[":type_id$i"] = $type_id;
     }
   }
   if($from_year and $to_year) {
@@ -552,7 +566,7 @@ function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
   }
   $sql = "$select $from $where $order";
   $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
-  return chado_pager_query($sql, $args, $limit, $pager_id, $count);
+  return chado_pager_query($sql, $args, $limit, 0, $count);
 }
 
 /**

+ 1 - 1
tripal_pub/tripal_pub.drush.inc

@@ -78,7 +78,7 @@ function drush_tripal_pub_tripal_pubs_import() {
   }
 
   if ($dbxref) {
-    tripal_pub_import_by_dbxref($dbxref, $create_contacts);
+    tripal_pub_import_by_dbxref($dbxref, $create_contacts, $update);
   }
   else {
     tripal_pub_import_publications($do_report, $update);

+ 26 - 0
tripal_pub/tripal_pub.module

@@ -166,6 +166,13 @@ function tripal_pub_menu() {
     'access arguments' => array('administer tripal pubs'),
     'type ' => MENU_CALLBACK,
   );
+  
+  $items['admin/tripal/chado/tripal_pub/import/submit/%'] = array(
+    'page callback' => 'tripal_pub_importer_submit_job',
+    'page arguments' => array(6),
+    'access arguments' => array('administer tripal pubs'),
+    'type ' => MENU_CALLBACK,
+  );
 
   $items['admin/tripal/chado/tripal_pub/import/delete/%'] = array(
     'page callback' => 'tripal_pub_importer_delete',
@@ -1059,4 +1066,23 @@ function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
   }
   if ($form_id == "chado_pub_node_form") {
   }
+}
+
+/**
+ * 
+ * @param $callback
+ * @param $args
+ */
+function tripal_pub_job_describe_args($callback, $args) {
+
+  $new_args = array();
+  if ($callback == 'tripal_pub_import_publications_by_import_id') {
+    // get all of the loaders
+    $qargs = array(':import_id' => $args[0]);
+    $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :import_id ";
+    $import = db_query($sql, $qargs)->fetchObject();
+    
+    $new_args['Importer'] = $import->name;
+  }
+  return $new_args;
 }