|
@@ -55,7 +55,7 @@
|
|
*
|
|
*
|
|
* @ingroup tripal_pub_api
|
|
* @ingroup tripal_pub_api
|
|
*/
|
|
*/
|
|
-function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to_retrieve, $page = 0) {
|
|
|
|
|
|
+function pub_search_remote($remote_db, $search_array, $num_to_retrieve, $page = 0) {
|
|
|
|
|
|
// now call the callback function to get the results
|
|
// now call the callback function to get the results
|
|
$callback = "tripal_pub_remote_search_$remote_db";
|
|
$callback = "tripal_pub_remote_search_$remote_db";
|
|
@@ -70,6 +70,154 @@ function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to
|
|
return $pubs;
|
|
return $pubs;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Builds the SQL statement need to search Chado for the publications
|
|
|
|
+ * that match the user supplied criteria. Tpyically, this function is
|
|
|
|
+ * called by the search form generated by the tripal_pub_search_form() function
|
|
|
|
+ * but this function is included in the API for calling by anyone.
|
|
|
|
+ *
|
|
|
|
+ * @param $search_array
|
|
|
|
+ * An array of search criteria provided by the user. The search array is
|
|
|
|
+ * an associative array with the following keys:
|
|
|
|
+ * 'num_criteria': an integer indicating the number of search criteria supplied
|
|
|
|
+ * 'from_year': filters records by a start year
|
|
|
|
+ * 'to_year': filters records by an end year
|
|
|
|
+ * 'criteria': an array of criteria. Each criteria is an associative
|
|
|
|
+ * array with the following keys:
|
|
|
|
+ * 'search_terms': The text used for searching
|
|
|
|
+ * 'scope': The cvterm_id of the property used for filtering
|
|
|
|
+ * 'mode': The operation (e.g. AND, OR or NOT)
|
|
|
|
+ * @param $offset
|
|
|
|
+ * The offset for paging records. The first record returned will be
|
|
|
|
+ * at the offset indicated here, and the next $limit number of records
|
|
|
|
+ * will be returned.
|
|
|
|
+ *
|
|
|
|
+ * @param $limit
|
|
|
|
+ * The number of records to retrieve
|
|
|
|
+ *
|
|
|
|
+ * @param total_records
|
|
|
|
+ * A value passed by reference. This value will get set to the total
|
|
|
|
+ * number of matching records
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ * a PDO database object of the query results.
|
|
|
|
+ *
|
|
|
|
+ * @ingroup tripal_pub
|
|
|
|
+ */
|
|
|
|
+function pub_search($search_array, $offset, $limit, &$total_records) {
|
|
|
|
+
|
|
|
|
+ // build the SQL based on the criteria provided by the user
|
|
|
|
+ $select = "SELECT DISTINCT P.*, CP.nid ";
|
|
|
|
+ $from = "FROM {pub} P
|
|
|
|
+ LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
|
|
|
|
+ INNER JOIN {cvterm} CVT on CVT.cvterm_id = P.type_id
|
|
|
|
+ ";
|
|
|
|
+ $where = "WHERE (NOT P.title = 'null') "; // always exclude the dummy pub
|
|
|
|
+ $order = "ORDER BY P.pyear DESC, P.title ASC";
|
|
|
|
+ $args = array(); // arguments for where clause
|
|
|
|
+ $join = 0;
|
|
|
|
+
|
|
|
|
+ $num_criteria = $search_array['num_criteria'];
|
|
|
|
+ $from_year = $search_array['from_year'];
|
|
|
|
+ $to_year = $search_array['to_year'];
|
|
|
|
+
|
|
|
|
+ for ($i = 1; $i <= $num_criteria; $i++) {
|
|
|
|
+ $value = $search_array['criteria'][$i]['search_terms'];
|
|
|
|
+ $type_id = $search_array['criteria'][$i]['scope'];
|
|
|
|
+ $mode = $search_array['criteria'][$i]['mode'];
|
|
|
|
+ $op = $search_array['criteria'][$i]['operation'];
|
|
|
|
+
|
|
|
|
+ // skip criteria with no values
|
|
|
|
+ if(!$value) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // to prevent SQL injection make sure our operator is
|
|
|
|
+ // what we expect
|
|
|
|
+ if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
|
|
|
|
+ $op = 'AND';
|
|
|
|
+ }
|
|
|
|
+ if ($op == 'NOT') {
|
|
|
|
+ $op = 'AND NOT';
|
|
|
|
+ }
|
|
|
|
+ if (!$op) {
|
|
|
|
+ $op = 'AND';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // get the scope type
|
|
|
|
+ $values = array('cvterm_id' => $type_id);
|
|
|
|
+ $cvterm = chado_select_record('cvterm', array('name'), $values);
|
|
|
|
+ $type_name = '';
|
|
|
|
+ if (count($cvterm) > 0) {
|
|
|
|
+ $type_name = $cvterm[0]->name;
|
|
|
|
+ }
|
|
|
|
+ if ($type_name == 'Title') {
|
|
|
|
+ $where .= " $op (lower(P.title) LIKE lower(:crit$i)) ";
|
|
|
|
+ $args[":crit$i"] = '%' . $value . '%';
|
|
|
|
+ }
|
|
|
|
+ elseif ($type_name == 'Year') {
|
|
|
|
+ $where .= " $op (lower(P.pyear) = lower(:crit$i)) ";
|
|
|
|
+ $args[":crit$i"] = '%' . $value . '%';
|
|
|
|
+ }
|
|
|
|
+ elseif ($type_name == 'Volume') {
|
|
|
|
+ $where .= " $op (lower(P.volume) = lower(:crit$i)) ";
|
|
|
|
+ $args[":crit$i"] = '%' . $value . '%';
|
|
|
|
+ }
|
|
|
|
+ elseif ($type_name == 'Issue') {
|
|
|
|
+ $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) = 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) = 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) = 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) 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 = :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) {
|
|
|
|
+ $where .= " AND (P.pyear ~ '....' AND to_number(P.pyear,'9999') >= :from$i AND to_number(P.pyear,'9999') <= :to$i) ";
|
|
|
|
+ $args[":from$i"] = $from_year;
|
|
|
|
+ $args[":to$i"] = $to_year;
|
|
|
|
+ }
|
|
|
|
+ $sql = "$select $from $where $order LIMIT " . (int) $limit . ' OFFSET ' . (int) $offset;
|
|
|
|
+ $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
|
|
|
|
+
|
|
|
|
+ // first get the total number of matches
|
|
|
|
+ $total_records = chado_query($count, $args)->fetchField();
|
|
|
|
+ $results = chado_query($sql, $args);
|
|
|
|
+
|
|
|
|
+ return $results;
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* This function is used to perfom a query using one of the supported databases
|
|
* This function is used to perfom a query using one of the supported databases
|
|
* and return the raw query results. This may be XML or some other format
|
|
* and return the raw query results. This may be XML or some other format
|
|
@@ -82,11 +230,13 @@ function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to
|
|
* for the record in the database.
|
|
* for the record in the database.
|
|
*
|
|
*
|
|
* @return
|
|
* @return
|
|
- * Returns the raw output wrapped in an HTML textarea element
|
|
|
|
|
|
+ * Returns the raw output wrapped in an HTML textarea element or an
|
|
|
|
+ * error message indicating if the database type is unsupported or the
|
|
|
|
+ * dbxref is invalid
|
|
*
|
|
*
|
|
* @ingroup tripal_pub_api
|
|
* @ingroup tripal_pub_api
|
|
*/
|
|
*/
|
|
-function tripal_pub_get_raw_data($dbxref) {
|
|
|
|
|
|
+function tripal_get_remote_pub_record($dbxref) {
|
|
|
|
|
|
if(preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
|
|
if(preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
|
|
$remote_db = $matches[1];
|
|
$remote_db = $matches[1];
|
|
@@ -110,9 +260,9 @@ function tripal_pub_get_raw_data($dbxref) {
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
);
|
|
- $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
|
|
|
|
|
|
+ $pubs = pub_search_remote($remote_db, $search, 1, 0);
|
|
|
|
|
|
- return '<textarea cols=80 rows=20>' . $pubs[0]['raw'] . '</textarea>';
|
|
|
|
|
|
+ return $pubs[0]['raw'];
|
|
}
|
|
}
|
|
return 'Invalid DB xref';
|
|
return 'Invalid DB xref';
|
|
}
|
|
}
|
|
@@ -196,7 +346,7 @@ function tripal_pub_update_publications($do_contact = FALSE, $dbxref = NULL, $db
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
);
|
|
- $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
|
|
|
|
|
|
+ $pubs = pub_search_remote($remote_db, $search, 1, 0);
|
|
tripal_pub_add_publications($pubs, $do_contact, TRUE);
|
|
tripal_pub_add_publications($pubs, $do_contact, TRUE);
|
|
|
|
|
|
$i++;
|
|
$i++;
|
|
@@ -257,7 +407,7 @@ function tripal_pub_import_publications_by_import_id($import_id, $job_id = NULL)
|
|
$total_pubs = 0;
|
|
$total_pubs = 0;
|
|
do {
|
|
do {
|
|
// retrieve the pubs for this page. We'll retreive 100 at a time
|
|
// 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);
|
|
|
|
|
|
+ $results = pub_search_remote($remote_db, $criteria, $num_to_retrieve, $page);
|
|
$pubs = $results['pubs'];
|
|
$pubs = $results['pubs'];
|
|
$num_pubs = $rseults['total_records'];
|
|
$num_pubs = $rseults['total_records'];
|
|
$total_pubs += $num_pubs;
|
|
$total_pubs += $num_pubs;
|
|
@@ -332,7 +482,7 @@ function tripal_pub_import_publications($report_email = FALSE, $do_update = FALS
|
|
$remote_db = $criteria['remote_db'];
|
|
$remote_db = $criteria['remote_db'];
|
|
do {
|
|
do {
|
|
// retrieve the pubs for this page. We'll retreive 100 at a time
|
|
// 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);
|
|
|
|
|
|
+ $results = pub_search_remote($remote_db, $criteria, $num_to_retrieve, $page);
|
|
$pubs = $results['pubs'];
|
|
$pubs = $results['pubs'];
|
|
$reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
|
|
$reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
|
|
$page++;
|
|
$page++;
|
|
@@ -435,7 +585,7 @@ function tripal_pub_import_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_updat
|
|
),
|
|
),
|
|
);
|
|
);
|
|
$remote_db = $criteria['remote_db'];
|
|
$remote_db = $criteria['remote_db'];
|
|
- $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
|
|
|
|
|
|
+ $results = pub_search_remote($remote_db, $criteria, $num_to_retrieve, $page);
|
|
$pubs = $results['pubs'];
|
|
$pubs = $results['pubs'];
|
|
$search_str = $results['search_str'];
|
|
$search_str = $results['search_str'];
|
|
$total_records = $results['total_records'];
|
|
$total_records = $results['total_records'];
|
|
@@ -527,80 +677,6 @@ function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
|
|
return $report;
|
|
return $report;
|
|
}
|
|
}
|
|
|
|
|
|
-/**
|
|
|
|
- * Adds a database cross-reference to a publication
|
|
|
|
- *
|
|
|
|
- * @param $pub_id
|
|
|
|
- * The ID of the publication
|
|
|
|
- * @param $pub_dbxref
|
|
|
|
- * The cross reference. This value must be of the format DB_NAME:ACCESSION
|
|
|
|
- * where DB_NAME is the name of the database and the
|
|
|
|
- * ACCESSION is the unique identifier for the record in the database.
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- *
|
|
|
|
- * @ingroup tripal_pub_api
|
|
|
|
- */
|
|
|
|
-function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
|
|
|
|
-
|
|
|
|
- // break apart the dbxref
|
|
|
|
- $dbname = '';
|
|
|
|
- $accession = '';
|
|
|
|
- if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
|
|
|
|
- $dbname = $matches[1];
|
|
|
|
- $accession = $matches[2];
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- return FALSE;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // check to see if the pub_dbxref record already exist
|
|
|
|
- $values = array(
|
|
|
|
- 'dbxref_id' => array(
|
|
|
|
- 'accession' => $accession,
|
|
|
|
- 'db_id' => array(
|
|
|
|
- 'name' => $dbname,
|
|
|
|
- ),
|
|
|
|
- ),
|
|
|
|
- 'pub_id' => $pub_id,
|
|
|
|
- );
|
|
|
|
- $options = array('statement_name' => 'sel_pubdbxref_dbpu');
|
|
|
|
- $results = chado_select_record('pub_dbxref', array('*'), $values, $options);
|
|
|
|
-
|
|
|
|
- // if the pub_dbxref record exist then we don't need to re-add it.
|
|
|
|
- if(count($results) > 0) {
|
|
|
|
- return $results[0];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // make sure our database already exists
|
|
|
|
- $db = tripal_db_add_db($dbname);
|
|
|
|
-
|
|
|
|
- // get the database cross-reference
|
|
|
|
- $dbxvalues = array(
|
|
|
|
- 'accession' => $accession,
|
|
|
|
- 'db_id' => $db->db_id,
|
|
|
|
- );
|
|
|
|
- $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
|
|
|
|
- $results = chado_select_record('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
|
|
|
|
- // if the accession doesn't exist then add it
|
|
|
|
- if(count($results) == 0){
|
|
|
|
- $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- $dbxref = $results[0];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // now add the record
|
|
|
|
- $options = array('statement_name' => 'ins_pubdbxref_dbpu');
|
|
|
|
- $results = chado_insert_record('pub_dbxref', $values, $options);
|
|
|
|
- if (!$results) {
|
|
|
|
- tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot add publication dbxref: %db:%accession.",
|
|
|
|
- array('%db' => $dbname, '%accession' => $accession));
|
|
|
|
- return FALSE;
|
|
|
|
- }
|
|
|
|
- return $results;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Returns the list of publications that are assigned the database
|
|
* Returns the list of publications that are assigned the database
|
|
* cross-reference provided
|
|
* cross-reference provided
|