12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367 |
- <?php
- function tripal_pub_get_remote_search_results($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_pub_get_raw_data($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 "Unsupported database: $dbxref";
- }
- $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);
- return '<textarea cols=80 rows=20>' . $pubs[0]['raw'] . '</textarea>';
- }
- return 'Invalid DB xref';
- }
- function tripal_pub_update_publications($do_contact = FALSE, $dbxref = NULL, $db = NULL) {
-
- $connection = tripal_db_persistent_chado();
- if (!$connection) {
- print "A persistant connection was not obtained. Loading will be slow\n";
- }
-
- 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";
- }
-
- $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_pub_get_remote_search_results($remote_db, $search, 1, 0);
- tripal_pub_add_publications($pubs, $do_contact, TRUE);
- $i++;
- }
-
- tripal_db_commit_transaction();
- print "Transaction Complete\n";
-
- print "Syncing publications with Drupal...\n";
- tripal_pub_sync_pubs();
-
- if ($do_contact) {
- print "Syncing contacts with Drupal...\n";
- tripal_contact_sync_contacts();
- }
- print "Done.\n";
- }
- function tripal_pub_import_publications($report_email = FALSE, $do_update = FALSE) {
- $num_to_retrieve = 100;
- $pager_id = 0;
- $page = 0;
- $num_pubs = 0;
-
- $connection = tripal_db_persistent_chado();
- if (!$connection) {
- print "A persistant connection was not obtained. Loading will be slow\n";
- }
-
- 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";
- }
-
- $args = array();
- $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
- $results = db_query($sql, $args);
- $do_contact = FALSE;
- $reports = array();
- while ($import = $results->fetchObject()) {
- print "Importing: " . $import->name . "\n";
-
- if ($import->do_contact == 1) {
- $do_contact = TRUE;
- }
- $criteria = unserialize($import->criteria);
- $remote_db = $criteria['remote_db'];
- do {
-
- $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
- $reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
- $page++;
- }
-
-
- while (count($pubs) == $num_to_retrieve);
- }
-
- tripal_db_commit_transaction();
- print "Transaction Complete\n";
-
-
-
- print "Syncing publications with Drupal...\n";
- tripal_pub_sync_pubs();
-
- $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";
- tripal_contact_sync_contacts();
- }
-
- print "Done.\n";
- }
- function tripal_pub_import_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_update) {
- $num_to_retrieve = 1;
- $pager_id = 0;
- $page = 0;
- $num_pubs = 0;
-
- $connection = tripal_db_persistent_chado();
- if (!$connection) {
- print "A persistant connection was not obtained. Loading will be slow\n";
- }
-
- 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,
- ),
- ),
- );
- $remote_db = $criteria['remote_db'];
- $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
- $pub_id = tripal_pub_add_publications($pubs, $do_contact, $do_update);
- }
-
- tripal_db_commit_transaction();
- print "Transaction Complete\n";
-
- print "Syncing publications with Drupal...\n";
- tripal_pub_sync_pubs();
-
- if($do_contact) {
- print "Syncing contacts with Drupal...\n";
- tripal_contact_sync_contacts();
- }
-
- print "Done.\n";
- }
- function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
- $report = array();
- $report['error'] = 0;
- $report['inserted'] = array();
- $report['skipped'] = array();
- $total_pubs = count($pubs);
-
- $i = 1;
- foreach ($pubs as $pub) {
- $memory = number_format(memory_get_usage()) . " bytes";
- print "Processing $i of $total_pubs. Memory usage: $memory.\r";
-
-
- $action = '';
- $pub_id = tripal_pub_add_publication($pub, $action, $do_contact, $update);
- if ($pub_id){
-
- if ($pub_id and $pub['Publication Dbxref']) {
- $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub['Publication Dbxref']);
- }
- $pub['pub_id'] = $pub_id;
- }
- switch ($action) {
- case 'error':
- $report['error']++;
- break;
- case 'inserted':
- $report['inserted'][] = $pub;
- break;
- case 'updated':
- $report['updated'][] = $pub;
- break;
- case 'skipped':
- $report['skipped'][] = $pub;
- break;
- }
- $i++;
- }
- print "\n";
- return $report;
- }
- function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
-
- $dbname = '';
- $accession = '';
- if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
- $dbname = $matches[1];
- $accession = $matches[2];
- }
- else {
- return FALSE;
- }
-
- $values = array(
- 'dbxref_id' => array(
- 'accession' => $accession,
- 'db_id' => array(
- 'name' => $dbname,
- ),
- ),
- 'pub_id' => $pub_id,
- );
- $options = array('statement_name' => 'sel_pubdbxref_dbpu');
- $results = tripal_core_chado_select('pub_dbxref', array('*'), $values, $options);
-
- if(count($results) > 0) {
- return $results[0];
- }
-
- $db = tripal_db_add_db($dbname);
-
-
- $dbxvalues = array(
- 'accession' => $accession,
- 'db_id' => $db->db_id,
- );
- $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
- $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
-
- if(count($results) == 0){
- $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
- }
- else {
- $dbxref = $results[0];
- }
-
- $options = array('statement_name' => 'ins_pubdbxref_dbpu');
- $results = tripal_core_chado_insert('pub_dbxref', $values, $options);
- if (!$results) {
- watchdog('tripal_pub', "Cannot add publication dbxref: %db:%accession.",
- array('%db' => $dbname, '%accession' => $accession). WATCHDOG_ERROR);
- return FALSE;
- }
- return $results;
- }
- function tripal_pub_get_pubs_by_dbxref($pub_dbxref) {
- $return = array();
- if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
- $dbname = $matches[1];
- $accession = $matches[2];
- $values = array(
- 'dbxref_id' => array (
- 'accession' => $accession,
- 'db_id' => array(
- 'name' => $dbname
- ),
- ),
- );
- $options = array('statement_name' => 'sel_pubdbxref_db');
- $results = tripal_core_chado_select('pub_dbxref', array('pub_id'), $values, $options);
- foreach ($results as $index => $pub) {
- $return[] = $pub->pub_id;
- }
- }
- return $return;
- }
- function tripal_pub_get_pubs_by_title_type_pyear_series($title, $type = NULL, $pyear = NULL, $series_name = NULL) {
- $return = array();
-
- $values = array(
- 'title' => $title,
- );
- $stmnt_suffix = 'ti';
- if ($type) {
- $values['type_id'] = array(
- 'name' => $type,
- 'cv_id' => array(
- 'name' => 'tripal_pub'
- )
- );
- $stmnt_suffix .= 'ty';
- }
- if ($pyear) {
- $values['pyear'] = $pyear;
- $stmnt_suffix .= 'py';
- }
- if ($series_name) {
- $values['series_name'] = strtolower($series_name);
- $stmnt_suffix .= 'se';
- }
- $options = array(
- 'statement_name' => 'sel_pub_' . $stmnt_suffix,
- 'case_insensitive_columns' => array('title', 'series_name'),
- );
- $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
-
- foreach ($results as $index => $pub) {
- $return[] = $pub->pub_id;
- }
- return $return;
- }
- function tripal_pub_get_pub_by_uniquename($name) {
- $return = array();
-
- $values = array(
- 'uniquename' => $name,
- );
- $options = array(
- 'statement_name' => 'sel_pub_un',
- 'case_insensitive_columns' => array('uniquename'),
- );
- $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
-
- foreach ($results as $index => $pub) {
- $return[] = $pub->pub_id;
- }
- return $return;
- }
- function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE, $update_if_exists = FALSE) {
- $pub_id = 0;
-
-
- if ($pub_details['Publication Dbxref']) {
- $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);
- $action = 'skipped';
- return $pub_id;
- }
- }
- elseif (count($results) > 1) {
- watchdog('tripal_pub', "There are two publications with this accession: %db:%accession. Cannot determine which to update.",
- array('%db' => $dbname, '%accession' => $accession), WATCHDOG_ERROR);
- $action = 'error';
- return FALSE;
- }
- }
-
-
-
- if (!$pub_id and $pub_details['Title']) {
- $results = tripal_pub_get_pubs_by_title_type_pyear_series($pub_details['Title'], NULL, $pub_details['Year']);
- if (count($results) == 1) {
- $pub_id = $results[0];
- if ($pub_id and !$update_if_exists) {
- watchdog('tripal_pub', "The publication with the same title, type and year already exists. Skipping. ".
- " Title: '%title'. Type: '%type'. Year: '%year'",
- array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_WARNING);
- $action = 'skipped';
- return $pub_id;
- }
- }
- elseif (count($results) > 1) {
- watchdog('tripal_pub', "The publication with the same title, type and year is present multiple times. Cannot ".
- "determine which to use. Title: '%title'. Type: '%type'. Year: '%year'",
- array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_ERROR);
- $action = 'error';
- return FALSE;
- }
- }
-
- 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');
- }
- else {
- watchdog('tripal_pub', "The Publication Type is a required property but is missing", array(), WATCHDOG_ERROR);
- $action = 'error';
- return FALSE;
- }
- if (!$pub_type) {
- watchdog('tripal_pub', "Cannot find publication type: '%type'",
- array('%type' => $pub_details['Publication Type'][0]), WATCHDOG_ERROR);
- $action = 'error';
- return FALSE;
- }
-
- $values = array(
- 'title' => $pub_details['Title'],
- 'volume' => $pub_details['Volume'],
- 'series_name' => substr($pub_details['Journal Name'], 0, 255),
- 'issue' => $pub_details['Issue'],
- 'pyear' => $pub_details['Year'],
- 'pages' => $pub_details['Pages'],
- 'uniquename' => $pub_details['Citation'],
- 'type_id' => $pub_type->cvterm_id,
- );
-
- if (!$pub_id) {
- $options = array('statement_name' => 'ins_pub_tivoseispypaunty');
- $pub = tripal_core_chado_insert('pub', $values, $options);
- if (!$pub) {
- watchdog('tripal_pub', "Cannot insert the publication with title: %title",
- array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
- $action = 'error';
- return FALSE;
- }
- $pub_id = $pub['pub_id'];
- $action = 'inserted';
- }
-
- if ($pub_id and $update_if_exists) {
- $match = array('pub_id' => $pub_id);
- $options = array('statement_name' => 'up_pub_tivoseispypaunty');
- $success = tripal_core_chado_update('pub', $match, $values, $options);
- if (!$success) {
- watchdog('tripal_pub', "Cannot update the publication with title: %title",
- array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
- $action = 'error';
- return FALSE;
- }
- $action = 'updated';
- }
-
-
- if ($update_if_exists) {
- $sql = "
- DELETE FROM {pubprop}
- WHERE
- pub_id = :pub_id AND
- NOT type_id in (
- SELECT cvterm_id
- FROM {cvterm}
- WHERE name = 'Publication Dbxref'
- )
- ";
- chado_query($sql, array(':pub_id' => $pub_id));
- }
-
- foreach ($pub_details as $key => $value) {
-
- if($key == 'raw') {
- continue;
- }
-
- $cvterm = tripal_cv_get_cvterm_by_name($key, NULL, 'tripal_pub');
- if (!$cvterm) {
- $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
- }
- if (!$cvterm) {
- watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", array('%prop' => $key), WATCHDOG_ERROR);
- continue;
- }
-
- if ($key == 'Author List') {
- tripal_pub_add_authors($pub_id, $value, $do_contact);
- continue;
- }
- if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
- $key == 'Year' or $key == 'Pages') {
- continue;
- }
-
- $success = 0;
- if (is_array($value)) {
- foreach ($value as $subkey => $subvalue) {
-
-
- if(is_int($subkey)) {
- $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $subvalue, FALSE);
- }
- else {
- $success = tripal_core_insert_property('pub', $pub_id, $subkey, 'tripal_pub', $subvalue, FALSE);
- }
- }
- }
- else {
- $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
- }
- if (!$success) {
- watchdog('tripal_pub', "Cannot add property '%prop' to publication. Skipping.",
- array('%prop' => $key), WATCHDOG_ERROR);
- continue;
- }
- }
- return $pub_id;
- }
- function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
- $rank = 0;
-
- $sql = "DELETE FROM {pubauthor} WHERE pub_id = :pub_id";
- chado_query($sql, array(':pub_id' => $pub_id));
-
-
- foreach ($authors as $author) {
-
- if ($author['valid'] == 'N') {
- continue;
- }
-
- unset($author['valid']);
-
- $name = '';
- $type = 'Person';
- if ($author['Given Name']) {
- $name .= $author['Given Name'];
- }
- if ($author['Surname']) {
- $name .= ' ' . $author['Surname'];
- }
- if ($author['Suffix']) {
- $name .= ' ' . $author['Suffix'];
- }
- if ($author['Collective']) {
- $name = $author['Collective'];
- $type = 'Collective';
- }
- $name = trim($name);
-
-
- $values = array(
- 'pub_id' => $pub_id,
- 'rank' => $rank,
- 'surname' => $author['Surname'] ? substr($author['Surname'], 0, 100) : substr($author['Collective'], 0, 100),
- 'givennames' => $author['Given Name'],
- 'suffix' => $author['Suffix'],
- );
- $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
- $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
-
- if ($do_contact) {
-
- $contact = tripal_contact_add_contact($name, '', $type, $author);
-
-
- if ($contact and $pubauthor) {
-
- $values = array(
- 'pubauthor_id' => $pubauthor['pubauthor_id'],
- 'contact_id' => $contact['contact_id'],
- );
- $options = array('statement_name' => 'ins_pubauthorcontact_puco');
- $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
- if (!$pubauthor_contact) {
- watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
- }
- }
- }
- $rank++;
- }
- }
- function tripal_pub_get_property($pub_id, $property) {
- return tripal_core_get_property('pub', $pub_id, $property, 'tripal_pub');
- }
- function tripal_pub_insert_property($pub_id, $property, $value, $update_if_present = 0) {
- return tripal_core_insert_property('pub', $pub_id, $property, 'tripal_pub', $value, $update_if_present);
- }
- function tripal_pub_update_property($pub_id, $property, $value, $insert_if_missing = 0) {
- return tripal_core_update_property('pub', $pub_id, $property, 'tripal_pub', $value, $insert_if_missing);
- }
- function tripal_pub_delete_property($pub_id, $property) {
- return tripal_core_delete_property('pub', $pub_id, $property, 'tripal_pub');
- }
- function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
- $options = array('return_array' => 1);
-
-
-
- $values = array('pub_id' => $pub_id);
- $pub = tripal_core_generate_chado_var('pub', $values);
-
- $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
- $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
- $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
- $pub_array = array();
- if (trim($pub->title)) {
- $pub_array['Title'] = $pub->title;
- }
- if (trim($pub->volumetitle)) {
- $pub_array['Volume Title'] = $pub->volumetitle;
- }
- if (trim($pub->volume)) {
- $pub_array['Volume'] = $pub->volume;
- }
- if (trim($pub->series_name)) {
- $pub_array['Series Name'] = $pub->series_name;
- }
- if (trim($pub->issue)) {
- $pub_array['Issue'] = $pub->issue;
- }
- if (trim($pub->pyear)) {
- $pub_array['Year'] = $pub->pyear;
- }
- if (trim($pub->pages)) {
- $pub_array['Pages'] = $pub->pages;
- }
- if (trim($pub->miniref)) {
- $pub_array['Mini Ref'] = $pub->miniref;
- }
- if (trim($pub->uniquename)) {
- $pub_array['Uniquename'] = $pub->uniquename;
- }
- $pub_array['Publication Type'][] = $pub->type_id->name;
-
-
-
- $values = array(
- 'pub_id' => $pub->pub_id,
- 'type_id' => array(
- 'name' => 'Citation',
- ),
- );
- $citation = tripal_core_generate_chado_var('pubprop', $values);
- if ($citation) {
- $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value', $options);
- if (count($citation) > 1) {
- watchdog('tripal_pub', "Publication has multiple citations already: %pub_id",
- array('%pub_id' => $pubid), WATCHDOG_ERROR);
- return FALSE;
- }
- elseif (count($citation) == 1 and $skip_existing == TRUE) {
-
- return FALSE;
- }
- }
-
-
-
- $values = array(
- 'pub_id' => $pub->pub_id,
- 'type_id' => array(
- 'name' => 'Publication Type',
- ),
- );
- $ptypes = tripal_core_generate_chado_var('pubprop', $values, $options);
- if ($ptypes) {
- $ptypes = tripal_core_expand_chado_vars($ptypes, 'field', 'pubprop.value', $options);
- foreach ($ptypes as $ptype) {
- $pub_array['Publication Type'][] = $ptype->value;
- }
- }
-
-
-
- $values = array(
- 'pub_id' => $pub->pub_id,
- 'type_id' => array(
- 'name' => 'Authors',
- ),
- );
- $authors = tripal_core_generate_chado_var('pubprop', $values);
- $authors = tripal_core_expand_chado_vars($authors, 'field', 'pubprop.value', $options);
- if (count($authors) > 1) {
- watchdog('tripal_pub', "Publication has multiple author lists. It should have only one list: %pub_id",
- array('%pub_id' => $pubid), WATCHDOG_ERROR);
- return FALSE;
- }
- else if (trim($authors->value)) {
- $pub_array['Authors'] = $authors->value;
- }
-
- else {
- $sql = "
- SELECT string_agg(surname || ' ' || givennames, ', ')
- FROM {pubauthor}
- WHERE pub_id = :pub_id
- GROUP BY pub_id
- ";
- $au = chado_query($sql, array(':pub_id' => $pub_id))->fetchField();
- if ($au) {
- $pub_array['Authors'] = $au;
- }
- }
-
- $props = array(
- 'Journal Abbreviation',
- 'Elocation',
- 'Media Code',
- 'Conference Name',
- 'Keywords',
- 'Series Name',
- 'pISSN',
- 'Publication Date',
- 'Journal Code',
- 'Journal Alias',
- 'Journal Country',
- 'Published Location',
- 'Publication Model',
- 'Language Abbr',
- 'Alias',
- 'Publication Dbxref',
- 'Copyright',
- 'Abstract',
- 'Notes',
- 'Citation',
- 'Language',
- 'URL',
- 'eISSN',
- 'DOI',
- 'ISSN',
- 'Publication Code',
- 'Comments',
- 'Publisher',
- 'Media Alias',
- 'Original Title');
- foreach ($props AS $prop) {
- $sql =
- "SELECT value FROM {pubprop}
- WHERE type_id =
- (SELECT cvterm_id
- FROM {cvterm}
- WHERE name = :cvtname AND cv_id =
- (SELECT cv_id
- FROM {cv}
- WHERE name = 'tripal_pub'
- )
- )
- AND pub_id = :pub_id
- ";
- $val = trim(chado_query($sql, array(':cvtname' => $prop, ':pub_id' => $pub->pub_id))->fetchField());
- if ($val) {
- $pub_array[$prop] =$val;
- }
- }
- return $pub_array;
- }
- function tripal_pub_create_citation($pub) {
- $citation = '';
- $pub_type = '';
-
-
-
-
-
-
- if(is_array($pub['Publication Type'])) {
- foreach ($pub['Publication Type'] as $ptype) {
- if ($ptype == 'Journal Article' ) {
- $pub_type = $ptype;
- break;
- }
- else if ($ptype == 'Conference Proceedings'){
- $pub_type = $ptype;
- break;
- }
- else if ($ptype == 'Book') {
- $pub_type = $ptype;
- break;
- }
- else if ($ptype == 'Letter') {
- $pub_type = $ptype;
- break;
- }
- else if ($ptype == 'Book Chapter') {
- $pub_type = $ptype;
- break;
- }
- else if ($ptype == "Research Support, Non-U.S. Gov't") {
- $pub_type = $ptype;
-
-
- }
- }
- if (!$pub_type) {
- watchdog('tripal_pub', "Cannot generate citation for publication type: %types",
- array('%types' => print_r($pub['Publication Type'], TRUE)), WATCHDOG_ERROR);
- return FALSE;
- }
- }
- else {
- $pub_type = $pub['Publication Type'];
- }
-
-
-
- if ($pub_type == 'Journal Article') {
- $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
-
- if (array_key_exists('Journal Name', $pub)) {
- $citation .= $pub['Journal Name'] . '. ';
- }
- elseif (array_key_exists('Journal Abbreviation', $pub)) {
- $citation .= $pub['Journal Abbreviation'] . '. ';
- }
- elseif (array_key_exists('Series Name', $pub)) {
- $citation .= $pub['Series Name'] . '. ';
- }
- elseif (array_key_exists('Series Abbreviation', $pub)) {
- $citation .= $pub['Series Abbreviation'] . '. ';
- }
- if (array_key_exists('Publication Date', $pub)) {
- $citation .= $pub['Publication Date'];
- }
- elseif (array_key_exists('Year', $pub)) {
- $citation .= $pub['Year'];
- }
- if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
- $citation .= '; ';
- }
- if (array_key_exists('Volume', $pub)) {
- $citation .= $pub['Volume'];
- }
- if (array_key_exists('Issue', $pub)) {
- $citation .= '(' . $pub['Issue'] . ')';
- }
- if (array_key_exists('Pages', $pub)) {
- if (array_key_exists('Volume', $pub)) {
- $citation .= ':';
- }
- $citation .= $pub['Pages'];
- }
- $citation .= '.';
- }
-
-
-
- elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
- $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
-
- if (array_key_exists('Journal Name', $pub)) {
- $citation .= $pub['Journal Name'] . '. ';
- }
- if (array_key_exists('Publication Date', $pub)) {
- $citation .= $pub['Publication Date'];
- }
- elseif (array_key_exists('Year', $pub)) {
- $citation .= $pub['Year'];
- }
- $citation .= '.';
- }
-
-
-
- elseif ($pub_type == 'Letter') {
- $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
-
- if (array_key_exists('Journal Name', $pub)) {
- $citation .= $pub['Journal Name'] . '. ';
- }
- elseif (array_key_exists('Journal Abbreviation', $pub)) {
- $citation .= $pub['Journal Abbreviation'] . '. ';
- }
- elseif (array_key_exists('Series Name', $pub)) {
- $citation .= $pub['Series Name'] . '. ';
- }
- elseif (array_key_exists('Series Abbreviation', $pub)) {
- $citation .= $pub['Series Abbreviation'] . '. ';
- }
- if (array_key_exists('Publication Date', $pub)) {
- $citation .= $pub['Publication Date'];
- }
- elseif (array_key_exists('Year', $pub)) {
- $citation .= $pub['Year'];
- }
- if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
- $citation .= '; ';
- }
- if (array_key_exists('Volume', $pub)) {
- $citation .= $pub['Volume'];
- }
- if (array_key_exists('Issue', $pub)) {
- $citation .= '(' . $pub['Issue'] . ')';
- }
- if (array_key_exists('Pages', $pub)) {
- if (array_key_exists('Volume', $pub)) {
- $citation .= ':';
- }
- $citation .= $pub['Pages'];
- }
- $citation .= '.';
- }
-
-
-
- elseif ($pub_type == 'Book') {
-
- }
-
-
-
- elseif ($pub_type == 'Book Chapter') {
-
- }
-
-
-
- elseif ($pub_type == 'Conference Proceedings') {
- $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
-
- if (array_key_exists('Conference Name', $pub)) {
- $citation .= $pub['Conference Name'] . '. ';
- }
- elseif (array_key_exists('Series Name', $pub)) {
- $citation .= $pub['Series Name'] . '. ';
- }
- elseif (array_key_exists('Series Abbreviation', $pub)) {
- $citation .= $pub['Series Abbreviation'] . '. ';
- }
- if (array_key_exists('Publication Date', $pub)) {
- $citation .= $pub['Publication Date'];
- }
- elseif (array_key_exists('Year', $pub)) {
- $citation .= $pub['Year'];
- }
- if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
- $citation .= '; ';
- }
- if (array_key_exists('Volume', $pub)) {
- $citation .= $pub['Volume'];
- }
- if (array_key_exists('Issue', $pub)) {
- $citation .= '(' . $pub['Issue'] . ')';
- }
- if (array_key_exists('Pages', $pub)) {
- if (array_key_exists('Volume', $pub)) {
- $citation .= ':';
- }
- $citation .= $pub['Pages'];
- }
- $citation .= '.';
- }
- return $citation;
- }
|