123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867 |
- <?php
- function tripal_get_remote_pubs($remote_db, $search_array, $num_to_retrieve, $page = 0) {
-
- $callback = "tripal_pub_remote_search_$remote_db";
- $pubs = array(
- 'total_records' => 0,
- 'search_str' => '',
- 'pubs' => array(),
- );
- if (function_exists($callback)) {
- $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $page);
- }
- return $pubs;
- }
- function tripal_get_remote_pub($dbxref) {
- if(preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
- $remote_db = $matches[1];
- $accession = $matches[2];
-
- $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
- if(!in_array($remote_db, $supported_dbs)) {
- return FALSE;
- }
- $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_get_remote_pubs($remote_db, $search, 1, 0);
- return $pubs['pubs'][0];
- }
- return FALSE;
- }
- function tripal_search_publications($search_array, $offset, $limit, &$total_records) {
-
- $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') ";
- $order = "ORDER BY P.pyear DESC, P.title ASC";
- $args = array();
- $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'];
-
- if(!$value) {
- continue;
- }
-
-
- if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
- $op = 'AND';
- }
- if ($op == 'NOT') {
- $op = 'AND NOT';
- }
- if (!$op) {
- $op = 'AND';
- }
-
- $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) {
- $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 . '%';
- }
-
- 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";
-
- $total_records = chado_query($count, $args)->fetchField();
- $results = chado_query($sql, $args);
- return $results;
- }
- function tripal_get_publication($identifiers, $options = array()) {
-
- if (!is_array($identifiers)) {
- tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
- "chado_get_publication: The identifier passed in is expected to be an array with the key
- matching a column name in the pub table (ie: pub_id or name). You passed in %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
- elseif (empty($identifiers)) {
- tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
- "chado_get_publication: You did not pass in anything to identify the publication you want. The identifier
- is expected to be an array with the key matching a column name in the pub table
- (ie: pub_id or name). You passed in %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
-
- if (array_key_exists('property', $identifiers)) {
- $property = $identifiers['property'];
- unset($identifiers['property']);
- $pub = chado_get_record_with_property('pub', $property, $identifiers, $options);
- }
- elseif (array_key_exists('dbxref', $identifiers)) {
- if(preg_match('/^(.*?):(.*?)$/', $identifiers['dbxref'], $matches)) {
- $dbname = $matches[1];
- $accession = $matches[2];
- $values = array(
- 'dbxref_id' => array (
- 'accession' => $accession,
- 'db_id' => array(
- 'name' => $dbname
- ),
- ),
- );
- $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), $values);
- if (count($pub_dbxref) > 0) {
- $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
- }
- else {
- return FALSE;
- }
- }
- else {
- tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
- "chado_get_publication: The dbxref identifier is not correctly formatted.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
- }
- elseif (array_key_exists('dbxref_id', $identifiers)) {
-
- $values = array('dbxref_id' => $identifiers['dbxref_id']);
- $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), $values);
-
- if (count($pub_dbxref) > 0) {
- $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
- }
- else {
- return FALSE;
- }
- }
-
- else {
-
- $pub = chado_generate_var('pub', $identifiers, $options);
- }
-
- if (is_array($pub)) {
- tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
- "chado_get_publication: The identifiers did not find a single unique record. Identifiers passed: %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
-
- elseif ($pub === FALSE) {
- tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
- "chado_get_publication: Could not find a publication using the identifiers
- provided. Check that the identifiers are correct. Identifiers passed: %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
-
- else {
- return $pub;
- }
- }
- function tripal_publication_exists($pub_details) {
-
- if (array_key_exists('Publication Dbxref', $pub_details)) {
- $pub = chado_get_publication(array('dbxref' => $pub_details['Publication Dbxref']));
- if($pub) {
- return array($pub->pub_id);
- }
- }
-
- if (array_key_exists('Citation', $pub_details)) {
- $pub = chado_get_publication(array('uniquename' => $pub_details['Citation']));
- if($pub) {
- return array($pub->pub_id);
- }
- }
-
- if (array_key_exists('Publication Type', $pub_details)) {
- $type_name = '';
- if(is_array($pub_details['Publication Type'])) {
- $type_name = $pub_details['Publication Type'][0];
- }
- else {
- $type_name = $pub_details['Publication Type'];
- }
- $identifiers = array(
- 'name' => $type_name,
- 'cv_id' => array(
- 'name' => 'tripal_pub',
- ),
- );
- $pub_type = tripal_get_cvterm($identifiers);
- }
- else {
- tripal_report_error('tripal_pub', TRIPAL_ERROR,
- "chado_does_pub_exist(): The Publication Type is a " .
- "required property but is missing", array());
- return array();
- }
- if (!$pub_type) {
- tripal_report_error('tripal_pub', TRIPAL_ERROR,
- "chado_does_pub_exist(): Cannot find publication type: '%type'",
- array('%type' => $pub_details['Publication Type'][0]));
- return array();
- }
-
- $series_name = '';
- if (array_key_exists('Series Name', $pub_details)) {
- $series_name = substr($pub_details['Series Name'], 0, 255);
- }
- if (array_key_exists('Journal Name', $pub_details)) {
- $series_name = substr($pub_details['Journal Name'], 0, 255);
- }
- if (array_key_exists('Conference Name', $pub_details)) {
- $series_name = substr($pub_details['Conference Name'], 0, 255);
- }
-
-
- $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
- $pubs = array();
- switch ($import_dups_check) {
- case 'title_year':
- $identifiers = array(
- 'title' => $pub_details['Title'],
- 'pyear' => $pub_details['Year']
- );
- $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
- break;
- case 'title_year_type':
- $identifiers = array(
- 'title' => $pub_details['Title'],
- 'pyear' => $pub_details['Year'],
- 'type_id' => $pub_type->cvterm_id,
- );
- $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
- break;
- case 'title_year_media':
- $identifiers = array(
- 'title' => $pub_details['Title'],
- 'pyear' => $pub_details['Year'],
- 'series_name' => $series_name,
- );
- $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
- break;
- }
- $return = array();
- foreach ($pubs as $pub) {
- $return[] = $pub->pub_id;
- }
- return $return;
- }
- function tripal_reimport_publications($do_contact = FALSE, $dbxref = NULL, $db = 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";
- $transaction = db_transaction();
- try {
-
- $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;
- $curr_db = '';
- $ids = array();
- $search = array();
-
- while ($pub = $results->fetchObject()) {
- $accession = $pub->accession;
- $remote_db = $pub->db_name;
-
- $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_get_remote_pubs($remote_db, $search, 1, 0);
- tripal_pub_add_publications($pubs, $do_contact, TRUE);
- $i++;
- }
-
- print "Syncing publications with Drupal...\n";
- chado_node_sync_records('pub');
-
- if ($do_contact) {
- print "Syncing contacts with Drupal...\n";
- chado_node_sync_records('contact');
- }
- }
- catch (Exception $e) {
- $transaction->rollback();
- print "\n";
- watchdog_exception('T_pub_import', $e);
- print "FAILED: Rolling back database changes...\n";
- return;
- }
- print "Done.\n";
- }
- function tripal_execute_pub_importer($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";
-
- $transaction = db_transaction();
- try {
- $page = 0;
- $do_contact = FALSE;
- $num_to_retrieve = 100;
-
- $args = array(':import_id' => $import_id);
- $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :import_id ";
- $import = db_query($sql, $args)->fetchObject();
- print "Executing Importer: '" . $import->name . "'\n";
- $criteria = unserialize($import->criteria);
- $remote_db = $criteria['remote_db'];
- $total_pubs = 0;
- do {
-
- $results = tripal_get_remote_pubs($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++;
- }
-
-
- while (count($pubs) == $num_to_retrieve);
-
-
-
- print "Syncing publications with Drupal...\n";
- chado_node_sync_records('pub');
-
- if($import->do_contact) {
- print "Syncing contacts with Drupal...\n";
- chado_node_sync_records('contact');
- }
- tripal_set_job_progress($job_id, '100');
- }
- catch (Exception $e) {
- $transaction->rollback();
- print "\n";
- watchdog_exception('T_pub_import', $e);
- print "FAILED: Rolling back database changes...\n";
- return;
- }
- print "Done.\n";
- }
- function tripal_execute_active_pub_importers($report_email = FALSE, $do_update = FALSE) {
- $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";
-
- $transaction = db_transaction();
- try {
-
- $args = array();
- $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
- $results = db_query($sql, $args);
- $do_contact = FALSE;
- $reports = array();
- foreach ($results as $import) {
- $page = 0;
- print "Executing importer: '" . $import->name . "'\n";
-
- if ($import->do_contact == 1) {
- $do_contact = TRUE;
- }
- $criteria = unserialize($import->criteria);
- $remote_db = $criteria['remote_db'];
- do {
-
- $results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
- $pubs = $results['pubs'];
- $reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
- $page++;
- }
-
-
- while (count($pubs) == $num_to_retrieve);
- }
-
-
-
- print "Syncing publications with Drupal...\n";
- chado_node_sync_records('pub');
-
- $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($do_contact) {
- print "Syncing contacts with Drupal...\n";
- chado_node_sync_records('contact');
- }
- }
- catch (Exception $e) {
- $transaction->rollback();
- print "\n";
- watchdog_exception('T_pub_import', $e);
- print "FAILED: Rolling back database changes...\n";
- return;
- }
- print "Done.\n";
- }
- function tripal_import_pub_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_update) {
- $num_to_retrieve = 1;
- $pager_id = 0;
- $page = 0;
- $num_pubs = 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'];
- $results = tripal_get_remote_pubs($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);
- }
-
- print "Syncing publications with Drupal...\n";
- chado_node_sync_records('pub');
-
- if($do_contact) {
- print "Syncing contacts with Drupal...\n";
- chado_node_sync_records('contact');
- }
- }
- catch (Exception $e) {
- $transaction->rollback();
- print "\n";
- watchdog_exception('T_pub_import', $e);
- print "FAILED: Rolling back database changes...\n";
- return;
- }
- print "Done.\n";
- }
|